Skip to content

Commit

Permalink
perf(shared): focus-visible, lazily instantiate the focus-visible pol…
Browse files Browse the repository at this point in the history
…yfilling
  • Loading branch information
Westbrook committed Nov 24, 2020
1 parent 0283b7e commit 1ffcedb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
22 changes: 14 additions & 8 deletions packages/shared/src/focus-visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,26 @@ export const FocusVisiblePolyfillMixin = <
// document:
connectedCallback(): void {
super.connectedCallback && super.connectedCallback();
if (this[$endPolyfillCoordination] == null) {
this[$endPolyfillCoordination] = coordinateWithPolyfill(this);
}
requestAnimationFrame(() => {
if (this[$endPolyfillCoordination] == null) {
this[$endPolyfillCoordination] = coordinateWithPolyfill(
this
);
}
});
}

disconnectedCallback(): void {
super.disconnectedCallback && super.disconnectedCallback();
// It's important to remove the polyfill event listener when we
// disconnect, otherwise we will leak the whole element via window:
if (this[$endPolyfillCoordination] != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this[$endPolyfillCoordination]!();
this[$endPolyfillCoordination] = null;
}
requestAnimationFrame(() => {
if (this[$endPolyfillCoordination] != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this[$endPolyfillCoordination]!();
this[$endPolyfillCoordination] = null;
}
});
}
}

Expand Down
14 changes: 8 additions & 6 deletions packages/shared/src/focusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ export class Focusable extends FocusVisiblePolyfillMixin(SpectrumElement) {
protected manageAutoFocus(): void {
if (this.autofocus) {
/* Trick :focus-visible polyfill into thinking keyboard based focus */
this.dispatchEvent(
new KeyboardEvent('keydown', {
code: 'Tab',
})
);
this.focusElement.focus();
requestAnimationFrame(() => {
this.dispatchEvent(
new KeyboardEvent('keydown', {
code: 'Tab',
})
);
this.focusElement.focus();
});
}
}

Expand Down

0 comments on commit 1ffcedb

Please sign in to comment.