Skip to content

Commit

Permalink
chore: updated the strategy to include the dependency controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TarunAdobe committed May 28, 2024
1 parent 757b90e commit 814916d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
21 changes: 19 additions & 2 deletions packages/picker/src/InteractionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ export class InteractionController implements ReactiveController {
* Set `open`
*/
public set open(open: boolean) {
if (this._open === open) return;
this._open = open;
if (this.overlay) {
// If there already is an Overlay, apply the value of `open` directly.
this.overlay.open = open;
if (this.host.dependencyManager.loaded) {
this.overlay.open = open;
}
this.host.open = open;
return;
}
Expand All @@ -68,7 +71,10 @@ export class InteractionController implements ReactiveController {
'@spectrum-web-components/overlay/src/Overlay.js'
);
this.overlay = new Overlay();
this.overlay.open = true;
this.host.requestUpdate();
if (this.host.dependencyManager.loaded) {
this.overlay.open = open;
}
this.host.open = true;
});
import('@spectrum-web-components/overlay/sp-overlay.js');
Expand All @@ -95,6 +101,7 @@ export class InteractionController implements ReactiveController {
) {
this.target = target;
this.host = host;
this.host.addController(this);
this.init();
}

Expand Down Expand Up @@ -175,4 +182,14 @@ export class InteractionController implements ReactiveController {
hostDisconnected(): void {
this.abortController?.abort();
}

public hostUpdated(): void {
if (
this.overlay &&
this.host.dependencyManager.loaded &&
this.host.open
) {
this.overlay.open = true;
}
}
}
28 changes: 23 additions & 5 deletions packages/picker/src/Picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export const DESCRIPTION_ID = 'option-picker';
export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
public isMobile = new MatchMediaController(this, IS_MOBILE);

public strategy?: DesktopController | MobileController;
public strategy!: DesktopController | MobileController;

@state()
appliedLabel?: string;

@query('#button')
public button!: HTMLButtonElement;

private dependencyManager = new DependencyManagerController(this);
public dependencyManager = new DependencyManagerController(this);

private deprecatedMenu: Menu | null = null;

Expand Down Expand Up @@ -217,6 +217,7 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
} else {
// Non-cancelable "change" events announce a selection with no value
// change that should close the Picker element.
this.open = false;
if (this.strategy) {
this.strategy.open = false;
}
Expand Down Expand Up @@ -249,6 +250,7 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
item: MenuItem,
menuChangeEvent?: Event
): Promise<void> {
this.open = false;
// should always close when "setting" a value
if (this.strategy) {
this.strategy.open = false;
Expand Down Expand Up @@ -278,6 +280,7 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
}
this.selectedItem = oldSelectedItem;
this.value = oldValue;
this.open = true;
if (this.strategy) {
this.strategy.open = true;
}
Expand All @@ -304,9 +307,9 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
if (this.readonly || this.pending) {
return;
}
this.open = typeof target !== 'undefined' ? target : !this.open;
if (this.strategy) {
this.strategy.open =
typeof target !== 'undefined' ? target : !this.open;
this.strategy.open = this.open;
}
}

Expand All @@ -315,6 +318,7 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
return;
}
if (this.strategy) {
this.open = false;
this.strategy.open = false;
}
}
Expand Down Expand Up @@ -456,6 +460,9 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
};

protected renderOverlay(menu: TemplateResult): TemplateResult {
if (this.strategy?.overlay === undefined) {
return menu;
}
const container = this.renderContainer(menu);
render(container, this.strategy?.overlay as unknown as HTMLElement, {
host: this,
Expand Down Expand Up @@ -517,11 +524,13 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
}
if (changes.has('disabled') && this.disabled) {
if (this.strategy) {
this.open = false;
this.strategy.open = false;
}
}
if (changes.has('pending') && this.pending) {
if (this.strategy) {
this.open = false;
this.strategy.open = false;
}
}
Expand Down Expand Up @@ -574,6 +583,13 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
this.button.addEventListener('keydown', this.handleKeydown);
}

protected override updated(changes: PropertyValues<this>): void {
super.updated(changes);
if (changes.has('open')) {
this.strategy.open = this.open;
}
}

protected override firstUpdated(changes: PropertyValues<this>): void {
super.firstUpdated(changes);
this.bindButtonKeydownListener();
Expand Down Expand Up @@ -652,6 +668,9 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {
this.open ||
!!this.deprecatedMenu;
if (this.hasRenderedOverlay) {
if (this.dependencyManager.loaded) {
this.dependencyManager.add('sp-overlay');
}
return this.renderOverlay(menu);
}
return menu;
Expand Down Expand Up @@ -759,7 +778,6 @@ export class PickerBase extends SizedMixin(Focusable, { noDefaultSize: true }) {

protected bindEvents(): void {
this.strategy?.abort();
this.strategy = undefined;
if (this.isMobile.matches) {
this.strategy = new strategies['mobile'](this.button, this);
} else {
Expand Down
3 changes: 3 additions & 0 deletions packages/picker/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ export function runPickerTests(): void {
option2.innerHTML = 'Invert Selection';
await itemUpdated;
await elementUpdated(el);
await aTimeout(150);
expect(el.value).to.equal('option-2');
expect((el.button.textContent || '').trim()).to.include(
'Invert Selection'
Expand Down Expand Up @@ -619,6 +620,8 @@ export function runPickerTests(): void {
expect(el.value).to.equal(thirdItem.value);
});
it('opens/closes multiple times', async () => {
await nextFrame();
await nextFrame();
expect(el.open).to.be.false;
const boundingRect = el.button.getBoundingClientRect();
let opened = oneEvent(el, 'sp-opened');
Expand Down

0 comments on commit 814916d

Please sign in to comment.