-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullscreenButton.ts
87 lines (76 loc) · 2.95 KB
/
FullscreenButton.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
import * as shadyCss from '@webcomponents/shadycss';
import { Button, buttonTemplate } from './Button';
import fullscreenButtonCss from './FullscreenButton.css';
import enterIcon from '../icons/fullscreen-enter.svg';
import exitIcon from '../icons/fullscreen-exit.svg';
import { StateReceiverMixin } from './StateReceiverMixin';
import { createCustomEvent } from '../util/EventUtils';
import { ENTER_FULLSCREEN_EVENT, type EnterFullscreenEvent } from '../events/EnterFullscreenEvent';
import { EXIT_FULLSCREEN_EVENT, type ExitFullscreenEvent } from '../events/ExitFullscreenEvent';
import { Attribute } from '../util/Attribute';
import { toggleAttribute } from '../util/CommonUtils';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate(
'theoplayer-fullscreen-button',
buttonTemplate(
`<span part="enter-icon"><slot name="enter-icon">${enterIcon}</slot></span>` +
`<span part="exit-icon"><slot name="exit-icon">${exitIcon}</slot></span>`,
fullscreenButtonCss
)
);
/**
* `<theoplayer-fullscreen-button>` - A button that toggles fullscreen.
*
* @group Components
*/
export class FullscreenButton extends StateReceiverMixin(Button, ['fullscreen']) {
static get observedAttributes() {
return [...Button.observedAttributes, Attribute.FULLSCREEN];
}
constructor() {
super({ template: template() });
this._upgradeProperty('fullscreen');
}
override connectedCallback() {
super.connectedCallback();
this._updateAriaLabel();
}
get fullscreen(): boolean {
return this.hasAttribute(Attribute.FULLSCREEN);
}
set fullscreen(fullscreen: boolean) {
toggleAttribute(this, Attribute.FULLSCREEN, fullscreen);
}
protected override handleClick(): void {
if (!this.fullscreen) {
const event: EnterFullscreenEvent = createCustomEvent(ENTER_FULLSCREEN_EVENT, {
bubbles: true,
composed: true
});
this.dispatchEvent(event);
} else {
const event: ExitFullscreenEvent = createCustomEvent(EXIT_FULLSCREEN_EVENT, {
bubbles: true,
composed: true
});
this.dispatchEvent(event);
}
}
override attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
super.attributeChangedCallback(attrName, oldValue, newValue);
if (FullscreenButton.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
this._updateAriaLabel();
}
}
private _updateAriaLabel(): void {
const label = this.fullscreen ? 'exit fullscreen' : 'enter fullscreen';
this.setAttribute(Attribute.ARIA_LABEL, label);
}
}
customElements.define('theoplayer-fullscreen-button', FullscreenButton);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-fullscreen-button': FullscreenButton;
}
}