2023-10-28 17:50:17 +02:00
|
|
|
import Modifier from 'ember-modifier';
|
|
|
|
|
2023-10-29 19:16:33 +01:00
|
|
|
type Named = {
|
2023-11-04 14:54:30 +01:00
|
|
|
enabled?: boolean;
|
2023-10-29 19:16:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
interface AutofocusModifierSignature {
|
2023-11-04 14:54:30 +01:00
|
|
|
Element: HTMLInputElement | HTMLSelectElement;
|
2023-10-29 19:16:33 +01:00
|
|
|
Args: {
|
|
|
|
Named: Named;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AutofocusModifier extends Modifier<AutofocusModifierSignature> {
|
2023-10-28 17:50:17 +02:00
|
|
|
isInstalled = false;
|
|
|
|
|
2023-10-29 19:16:33 +01:00
|
|
|
modify(element: HTMLInputElement, _: [], { enabled = true }: Named) {
|
2023-10-28 17:50:17 +02:00
|
|
|
// element should be only autofocused on initial render
|
|
|
|
// not when `enabled` option is invalidated
|
|
|
|
if (this.isInstalled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isInstalled = true;
|
2020-10-27 22:34:08 +01:00
|
|
|
|
2023-10-28 17:50:17 +02:00
|
|
|
if (!enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
}
|
2023-11-04 14:54:30 +01:00
|
|
|
|
|
|
|
declare module '@glint/environment-ember-loose/registry' {
|
|
|
|
export default interface Registry {
|
|
|
|
autofocus: typeof AutofocusModifier;
|
|
|
|
}
|
|
|
|
}
|