Skip to content

Commit

Permalink
Dropdown: items not shown when used within kirby-page (#3392)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobe authored Feb 26, 2024
1 parent 52b4b9f commit 754b577
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
40 changes: 40 additions & 0 deletions libs/designsystem/dropdown/src/dropdown.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createHostFactory, Spectator, SpectatorHost } from '@ngneat/spectator';
import { MockComponents } from 'ng-mocks';

import { DesignTokenHelper } from '@kirbydesign/designsystem/helpers';
import { TestHelper } from '@kirbydesign/designsystem/testing';
import { CardComponent } from '@kirbydesign/designsystem/card';
import { IconComponent } from '@kirbydesign/designsystem/icon';
import { ItemComponent, ItemModule } from '@kirbydesign/designsystem/item';
Expand Down Expand Up @@ -1143,4 +1144,43 @@ describe('DropdownComponent', () => {
});
});
});

describe('when configured with expand=block and usePopover=true', () => {
const createHost = createHostFactory({
component: DropdownComponent,
imports: [ItemModule],
declarations: [
ItemComponent,
MockComponents(ButtonComponent, CardComponent, IconComponent, IonItem, PopoverComponent),
],
});

let spectator: SpectatorHost<DropdownComponent>;

beforeEach(() => {
spectator = createHost(`<kirby-dropdown></kirby-dropdown>`, {
props: {
items: items,
expand: 'block',
usePopover: true,
},
});
});

it('should update popover card size when resized', async () => {
const initWidth = spectator.element.clientWidth;
const popoverCard = spectator.element.querySelector<HTMLElement>('kirby-card');
const initCardWidth = popoverCard.style.getPropertyValue('--kirby-card-width');
expect(initCardWidth).toEqual(`${initWidth}px`);

const newWidth = '200px';
(spectator.hostElement as HTMLElement).style.width = newWidth;
await TestHelper.waitForResizeObserver();
// Resize observe callback can be flaky in test, so ensure width has changed before asserting:
await TestHelper.whenTrue(() => spectator.element.clientWidth !== initWidth);

const cardWidth = popoverCard.style.getPropertyValue('--kirby-card-width');
expect(cardWidth).toEqual(newWidth);
});
});
});
19 changes: 15 additions & 4 deletions libs/designsystem/dropdown/src/dropdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import { ItemComponent } from '@kirbydesign/designsystem/item';
import { ListItemTemplateDirective } from '@kirbydesign/designsystem/list';
import { HorizontalDirection, PopoverComponent } from '@kirbydesign/designsystem/popover';
import { ButtonComponent } from '@kirbydesign/designsystem/button';

import { EventListenerDisposeFn } from '@kirbydesign/designsystem/types';
import { ResizeObserverService } from '@kirbydesign/designsystem/shared';

import { OpenState, VerticalDirection } from './dropdown.types';
import { KeyboardHandlerService } from './keyboard-handler.service';

Expand Down Expand Up @@ -245,7 +246,8 @@ export class DropdownComponent implements AfterViewInit, OnDestroy, ControlValue
private renderer: Renderer2,
private elementRef: ElementRef<HTMLElement>,
private changeDetectorRef: ChangeDetectorRef,
private keyboardHandlerService: KeyboardHandlerService
private keyboardHandlerService: KeyboardHandlerService,
private resizeObserverService: ResizeObserverService
) {}

onToggle(event: MouseEvent) {
Expand Down Expand Up @@ -286,10 +288,18 @@ export class DropdownComponent implements AfterViewInit, OnDestroy, ControlValue

ngAfterViewInit() {
if (this.usePopover && this.expand === 'block') {
const { width } = this.elementRef.nativeElement.getBoundingClientRect();
this.setPopoverCardStyle('--kirby-card-width', `${width}px`);
const { width: initialWidth } = this.elementRef.nativeElement.getBoundingClientRect();
this.setPopoverCardStyle('max-width', 'initial');
this.setPopoverCardStyle('min-width', 'initial');
// Ensure initial width is set even if the resize observer callback also fires initially:
this.setPopoverCardStyle('--kirby-card-width', `${initialWidth}px`);

this.resizeObserverService.observe(this.elementRef, (entry) => {
const newWidth = entry.contentRect.width;
if (newWidth > 0) {
this.setPopoverCardStyle('--kirby-card-width', `${newWidth}px`);
}
});
}
this.initializeAlignment();
}
Expand Down Expand Up @@ -629,6 +639,7 @@ export class DropdownComponent implements AfterViewInit, OnDestroy, ControlValue

ngOnDestroy(): void {
this.unlistenAllSlottedItems();
this.resizeObserverService.unobserve(this.elementRef);
if (this.intersectionObserverRef) {
this.intersectionObserverRef.disconnect();
}
Expand Down

0 comments on commit 754b577

Please sign in to comment.