decide.nolog.cz/app/modifiers/autofocus.ts
2023-11-04 14:54:30 +01:00

37 lines
785 B
TypeScript

import Modifier from 'ember-modifier';
type Named = {
enabled?: boolean;
};
interface AutofocusModifierSignature {
Element: HTMLInputElement | HTMLSelectElement;
Args: {
Named: Named;
};
}
export default class AutofocusModifier extends Modifier<AutofocusModifierSignature> {
isInstalled = false;
modify(element: HTMLInputElement, _: [], { enabled = true }: Named) {
// element should be only autofocused on initial render
// not when `enabled` option is invalidated
if (this.isInstalled) {
return;
}
this.isInstalled = true;
if (!enabled) {
return;
}
element.focus();
}
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
autofocus: typeof AutofocusModifier;
}
}