Skip to content

Commit

Permalink
fix: make sp-tab-list work in Edge
Browse files Browse the repository at this point in the history
- Fix an error with awaiting font loading
- Fix event handling on slotted content
  • Loading branch information
cuberoot committed Apr 17, 2020
1 parent 34fc0d6 commit 948559a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"@typescript-eslint/eslint-plugin": "^2.24.0",
"@typescript-eslint/parser": "^2.24.0",
"@vaadin/router": "^1.2.0",
"@webcomponents/webcomponentsjs": "^2.2.0",
"@webcomponents/webcomponentsjs": "^2.4.3",
"babel-loader": "^8.0.5",
"babel-plugin-bundled-import-meta": "^0.3.2",
"babel-plugin-transform-node-env-inline": "^0.4.3",
Expand Down
28 changes: 21 additions & 7 deletions packages/tab-list/src/tab-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const availableArrowsByDirection = {
horizontal: ['ArrowLeft', 'ArrowRight'],
};

declare global {
interface Document {
fonts?: {
ready: Promise<void>,
}
}
}

/**
* @slot - Child tab elements
* @attr {Boolean} quiet - The tab-list border is a lot smaller
Expand Down Expand Up @@ -75,6 +83,15 @@ export class TabList extends Focusable {
this.querySelector('sp-tab')) as Tab;
}

constructor() {
super()

// These can be added as @click and @keydown handlers on the
// slot once we no longer need web component polyfills
this.addEventListener('click', this.onClick);
this.addEventListener('keydown', this.onKeyDown);
}

protected manageAutoFocus(): void {
const tabs = [...this.children] as Tab[];
const tabUpdateCompletes = tabs.map((tab) => {
Expand All @@ -87,8 +104,6 @@ export class TabList extends Focusable {
protected render(): TemplateResult {
return html`
<slot
@click=${this.onClick}
@keydown=${this.onKeyDown}
@slotchange=${this.onSlotChange}
></slot>
<div
Expand Down Expand Up @@ -161,7 +176,7 @@ export class TabList extends Focusable {
].focus();
}

private onClick(event: Event): void {
private onClick = (event: Event): void => {
const target = event.target as HTMLElement;
this.selectTarget(target);
if (this.shouldApplyFocusVisible) {
Expand All @@ -175,7 +190,7 @@ export class TabList extends Focusable {
}
}

private onKeyDown(event: KeyboardEvent): void {
private onKeyDown = (event: KeyboardEvent): void => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
const target = event.target as HTMLElement;
Expand Down Expand Up @@ -240,9 +255,8 @@ export class TabList extends Focusable {
return;
}
await Promise.all([
await selectedElement.updateComplete,
await ((document as unknown) as { fonts: { ready: Promise<void> } })
.fonts.ready,
selectedElement.updateComplete,
document.fonts ? document.fonts.ready : Promise.resolve(),
]);
const tabBoundingClientRect = selectedElement.getBoundingClientRect();
const parentBoundingClientRect = this.getBoundingClientRect();
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3804,7 +3804,7 @@
resolved "https://registry.yarnpkg.com/@webcomponents/webcomponents-platform/-/webcomponents-platform-1.0.1.tgz#4e7ae8b40bcade06f5fdfa73fdb0211d90908866"
integrity sha512-7Ua2c3FvqAuiC2++gIoStG9r0B5sxleq8B7o0XKuIM052amWPTaCka0UMvnQRRJEsdGgRGIUv6sLkOyfhcr1dw==

"@webcomponents/webcomponentsjs@^2.2.0", "@webcomponents/webcomponentsjs@^2.4.0":
"@webcomponents/webcomponentsjs@^2.4.0", "@webcomponents/webcomponentsjs@^2.4.3":
version "2.4.3"
resolved "https://registry.yarnpkg.com/@webcomponents/webcomponentsjs/-/webcomponentsjs-2.4.3.tgz#384f4f6d54563ba465fb4df21fe89e78a76fc530"
integrity sha512-cV4+sAmshf8ysU2USutrSRYQkJzEYKHsRCGa0CkMElGpG5747VHtkfsW3NdVIBV/m2MDKXTDydT4lkrysH7IFA==
Expand Down

0 comments on commit 948559a

Please sign in to comment.