Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/app/templates/public-pages/docs/migrate-8-0-to-9-0.gts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ import { LinkTo } from '@ember/routing';
a regular HTML attribute instead.
</p>
</li>
<li>
<p>
If you are using the public API without the trigger modifier and change
<code>@disabled</code>
from
<code>false</code>
to
<code>true</code>, please follow the guidance
<a href="https://github.com/cibernox/ember-basic-dropdown/pull/1064">in
this PR</a>
</p>
</li>
<li>
<p>
If you are using
Expand Down
2 changes: 1 addition & 1 deletion src/components/basic-dropdown-content.gts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default class BasicDropdownContent<
if (!triggerElement) {
triggerElement = document.querySelector(selector) as HTMLElement;
}
this.handleRootMouseDown = (e: MouseEvent | TouchEvent) => {
this.handleRootMouseDown = (e: MouseEvent | TouchEvent): void => {
const target = (e.composedPath?.()[0] || e.target) as Element;
if (target === null) return;
if (
Expand Down
48 changes: 23 additions & 25 deletions src/components/basic-dropdown.gts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import calculatePosition from '../utils/calculate-position.ts';
import { schedule } from '@ember/runloop';
import { hash } from '@ember/helper';
import BasicDropdownTrigger from './basic-dropdown-trigger.gts';
import BasicDropdownContent from './basic-dropdown-content.gts';
Expand All @@ -27,7 +26,6 @@ import type {
TRootEventType,
} from '../types.ts';

const UNINITIALIZED = {};
const IGNORED_STYLES = ['top', 'left', 'right', 'width', 'height'];

export interface BasicDropdownDefaultBlock<
Expand Down Expand Up @@ -122,35 +120,44 @@ export default class BasicDropdown<
@tracked width: string | undefined;
@tracked height: string | undefined;
@tracked otherStyles: Record<string, string | number | undefined> = {};
@tracked isOpen = this.args.initiallyOpened || false;
@tracked renderInPlace =
this.args.renderInPlace !== undefined ? this.args.renderInPlace : false;
@tracked private _isOpen = this.args.initiallyOpened || false;

private previousVerticalPosition?: VerticalPosition | undefined;
private previousHorizontalPosition?: HorizontalPosition | undefined;
private triggerElement: HTMLElement | null = null;
private dropdownElement: HTMLElement | null = null;

private _uid = guidFor(this);
private _dropdownId: string = `ember-basic-dropdown-content-${this._uid}`;
private _previousDisabled = UNINITIALIZED;
private _actions: DropdownActions = {
open: this.open.bind(this),
close: this.close.bind(this),
toggle: this.toggle.bind(this),
reposition: this.reposition.bind(this),
updatePublicApi: this.updatePublicApi.bind(this),
registerTriggerElement: this.registerTriggerElement.bind(this),
registerDropdownElement: this.registerDropdownElement.bind(this),
getTriggerElement: () => this.triggerElement,
};

private get horizontalPosition() {
private get horizontalPosition(): HorizontalPosition {
return this.args.horizontalPosition || 'auto'; // auto-right | right | center | left
}

private get verticalPosition() {
private get verticalPosition(): VerticalPosition {
return this.args.verticalPosition || 'auto'; // above | below
}

get isOpen(): boolean {
return !this.disabled && this._isOpen;
}

set isOpen(v: boolean) {
this._isOpen = v;
}

get destination(): string {
return this.args.destination || this._getDestinationId();
}
Expand Down Expand Up @@ -179,25 +186,7 @@ export default class BasicDropdown<
}

get disabled(): boolean {
const newVal = this.args.disabled || false;
if (
this._previousDisabled !== UNINITIALIZED &&
this._previousDisabled !== newVal
) {
// eslint-disable-next-line ember/no-runloop
schedule('actions', () => {
if (newVal && this.publicAPI.isOpen) {
// eslint-disable-next-line ember/no-side-effects
this.isOpen = false;
}
if (this.args.registerAPI) {
this.args.registerAPI(this.publicAPI);
}
});
}
// eslint-disable-next-line ember/no-side-effects
this._previousDisabled = newVal;
return newVal;
return this.args.disabled || false;
}

get publicAPI(): Dropdown {
Expand Down Expand Up @@ -337,6 +326,15 @@ export default class BasicDropdown<
return this.applyReposition(triggerElement, dropdownElement, positionData);
}

@action
updatePublicApi() {
if (!this.args.registerAPI) {
return;
}

this.args.registerAPI(this.publicAPI);
}

@action
registerTriggerElement(element: HTMLElement): void {
this.triggerElement = element;
Expand Down
13 changes: 13 additions & 0 deletions src/modifiers/basic-dropdown-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default class DropdownTriggerModifier extends Modifier<Signature> {
desiredEventType!: string;
stopPropagation: boolean | undefined;

private _previousDisabled: boolean | undefined = undefined;

constructor(owner: Owner, args: ArgsFor<Signature>) {
super(owner, args);
registerDestructor(this, cleanup);
Expand All @@ -51,6 +53,17 @@ export default class DropdownTriggerModifier extends Modifier<Signature> {
this.setup(element);
this.didSetup = true;
}

if (this._previousDisabled === undefined) {
this._previousDisabled = this.dropdown.disabled;
}

if (this.dropdown.disabled !== this._previousDisabled) {
this._previousDisabled = this.dropdown.disabled;

this.dropdown.actions.updatePublicApi();
}

this.update(element, positional, named);
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface DropdownActions {
close: (e?: Event, skipFocus?: boolean) => void;
open: (e?: Event) => void;
reposition: () => undefined | RepositionChanges;
updatePublicApi: () => void;
registerTriggerElement: (e: HTMLElement) => void;
registerDropdownElement: (e: HTMLElement) => void;
getTriggerElement: () => HTMLElement | null;
Expand Down
Loading