Wire up garden controls
This commit is contained in:
parent
018f8c9d4d
commit
839747304e
15 changed files with 1442 additions and 378 deletions
|
|
@ -1,44 +1,90 @@
|
|||
export class CollapsiblePanelAnimator {
|
||||
private _isOpen = false;
|
||||
|
||||
public onOpen: () => unknown = () => {};
|
||||
public onClose: () => unknown = () => {};
|
||||
private focusBeforeOpen: HTMLElement | null = null;
|
||||
private readonly abortController = new AbortController();
|
||||
public onOpen?: () => void;
|
||||
|
||||
public constructor(
|
||||
private readonly toggleButton: HTMLButtonElement,
|
||||
private readonly collapsibleContent: HTMLElement,
|
||||
ignoreForCloseOnClick: HTMLElement
|
||||
) {
|
||||
toggleButton.addEventListener('click', this.toggle.bind(this));
|
||||
const { signal } = this.abortController;
|
||||
toggleButton.addEventListener('click', this.toggle, { signal });
|
||||
window.addEventListener(
|
||||
'click',
|
||||
(event) => !ignoreForCloseOnClick.contains(event.target as Node) && this.close()
|
||||
(event) => !ignoreForCloseOnClick.contains(event.target as Node) && this.close(),
|
||||
{ signal }
|
||||
);
|
||||
window.addEventListener(
|
||||
'keydown',
|
||||
(event) => {
|
||||
if (this._isOpen && event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
this.close();
|
||||
}
|
||||
},
|
||||
{ signal }
|
||||
);
|
||||
this.syncAccessibility();
|
||||
}
|
||||
|
||||
public open() {
|
||||
if (this._isOpen) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.focusBeforeOpen =
|
||||
document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
this._isOpen = true;
|
||||
this.collapsibleContent.classList.remove('hidden');
|
||||
this.toggleButton.classList.add('active');
|
||||
this.onOpen();
|
||||
this.onOpen?.();
|
||||
this.syncAccessibility();
|
||||
this.focusPanel();
|
||||
}
|
||||
|
||||
public close() {
|
||||
if (!this._isOpen) {
|
||||
return;
|
||||
}
|
||||
|
||||
const focusWasInside = this.collapsibleContent.contains(document.activeElement);
|
||||
this._isOpen = false;
|
||||
this.collapsibleContent.classList.add('hidden');
|
||||
this.toggleButton.classList.remove('active');
|
||||
this.onClose();
|
||||
this.syncAccessibility();
|
||||
|
||||
if (focusWasInside) {
|
||||
(this.focusBeforeOpen ?? this.toggleButton).focus({ preventScroll: true });
|
||||
}
|
||||
}
|
||||
|
||||
public toggle() {
|
||||
public readonly toggle = () => {
|
||||
if (this._isOpen) {
|
||||
this.close();
|
||||
} else {
|
||||
this.open();
|
||||
}
|
||||
};
|
||||
|
||||
public destroy(): void {
|
||||
this.abortController.abort();
|
||||
}
|
||||
|
||||
public get isOpen() {
|
||||
return this._isOpen;
|
||||
}
|
||||
|
||||
private syncAccessibility() {
|
||||
this.collapsibleContent.classList.toggle('hidden', !this._isOpen);
|
||||
this.toggleButton.classList.toggle('active', this._isOpen);
|
||||
this.toggleButton.setAttribute('aria-expanded', String(this._isOpen));
|
||||
this.collapsibleContent.setAttribute('aria-hidden', String(!this._isOpen));
|
||||
this.collapsibleContent.inert = !this._isOpen;
|
||||
}
|
||||
|
||||
private focusPanel() {
|
||||
requestAnimationFrame(() => {
|
||||
if (this._isOpen) {
|
||||
this.collapsibleContent.focus({ preventScroll: true });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue