-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkButton.ts
143 lines (120 loc) · 4.76 KB
/
LinkButton.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as shadyCss from '@webcomponents/shadycss';
import linkButtonCss from './LinkButton.css';
import { Attribute } from '../util/Attribute';
import type { ButtonOptions } from './Button';
import { Button, buttonTemplate } from './Button';
import { KeyCode } from '../util/KeyCode';
import { toggleAttribute } from '../util/CommonUtils';
import { createTemplate } from '../util/TemplateUtils';
export function linkButtonTemplate(button: string, extraCss: string = ''): string {
return buttonTemplate(`<a>${button}</a>`, `${linkButtonCss}\n${extraCss}`);
}
const defaultTemplate = createTemplate('theoplayer-link-button', linkButtonTemplate('<slot></slot>'));
/**
* `<theoplayer-link-button>` - A {@link Button | button} that opens a hyperlink.
*
* @attribute `disabled` - Whether the button is disabled. When disabled, the button cannot be clicked.
* @group Components
*/
export class LinkButton extends HTMLElement {
private readonly _linkEl: HTMLAnchorElement;
static get observedAttributes() {
return [Attribute.DISABLED];
}
constructor(options?: ButtonOptions) {
super();
const template = options?.template ?? defaultTemplate();
const shadowRoot = this.attachShadow({ mode: 'open', delegatesFocus: true });
shadowRoot.appendChild(template.content.cloneNode(true));
this._linkEl = shadowRoot.querySelector('a')!;
this._upgradeProperty('disabled');
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
connectedCallback(): void {
shadyCss.styleElement(this);
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'button');
}
if (!this.hasAttribute('tabindex')) {
this.setAttribute('tabindex', '0');
}
if (!this.hasAttribute(Attribute.ARIA_LIVE)) {
// Let the screen reader user know that the text of the button may change
this.setAttribute(Attribute.ARIA_LIVE, 'polite');
}
this._linkEl.addEventListener('keydown', this._onKeyDown);
this._linkEl.addEventListener('click', this._onClick);
}
disconnectedCallback(): void {
this._linkEl.removeEventListener('keydown', this._onKeyDown);
this._linkEl.removeEventListener('click', this._onClick);
}
/**
* Whether the button is disabled.
*
* When disabled, the button cannot be clicked.
*/
get disabled() {
return this.hasAttribute(Attribute.DISABLED);
}
set disabled(disabled: boolean) {
toggleAttribute(this, Attribute.DISABLED, disabled);
}
protected setLink(href: string, target: string): void {
this._linkEl.href = href;
this._linkEl.target = target;
}
attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
if (attrName === Attribute.DISABLED && newValue !== oldValue) {
const hasValue = newValue != null;
this.setAttribute('aria-disabled', hasValue ? 'true' : 'false');
// The `tabindex` attribute does not provide a way to fully remove focusability from an element.
// Elements with `tabindex=-1` can still be focused with a mouse or by calling `focus()`.
// To make sure an element is disabled and not focusable, remove the `tabindex` attribute.
if (hasValue) {
this.removeAttribute('tabindex');
// If the focus is currently on this element, unfocus it by calling the `HTMLElement.blur()` method.
this.blur();
} else {
this.setAttribute('tabindex', '0');
}
}
if (Button.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
private readonly _onKeyDown = (event: KeyboardEvent) => {
// Don't handle modifier shortcuts typically used by assistive technology.
if (event.altKey) return;
switch (event.keyCode) {
// Enter is already handled by the browser.
// case KeyCode.ENTER:
case KeyCode.SPACE:
event.preventDefault();
this._linkEl.click();
break;
// Any other key press is ignored and passed back to the browser.
default:
return;
}
};
private readonly _onClick = () => {
if (this.disabled) {
return;
}
this.handleClick();
};
protected handleClick(): void {}
}
customElements.define('theoplayer-link-button', LinkButton);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-link-button': LinkButton;
}
}