forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzen_mode_button.ts
More file actions
67 lines (58 loc) · 1.67 KB
/
Copy pathzen_mode_button.ts
File metadata and controls
67 lines (58 loc) · 1.67 KB
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
import {controller, target} from '@github/catalyst'
@controller('zen-mode-button')
class ZenModeButtonElement extends HTMLElement {
@target button: HTMLElement
inZenMode = false
// eslint-disable-next-line custom-elements/no-constructor
constructor() {
super()
document.addEventListener('fullscreenchange', this.fullscreenChangeEventHandler.bind(this))
}
disconnectedCallback() {
document.removeEventListener('fullscreenchange', this.fullscreenChangeEventHandler.bind(this))
}
fullscreenChangeEventHandler() {
this.changeButtonState(!this.inZenMode)
this.dispatchZenModeStatus()
}
dispatchZenModeStatus() {
// Create a new custom event
const event = new CustomEvent('zenModeToggled', {
detail: {
active: this.inZenMode,
},
})
// Dispatch the custom event
window.dispatchEvent(event)
}
private deactivateZenMode() {
if (document.exitFullscreen) {
void document.exitFullscreen()
}
}
private activateZenMode() {
if (document.documentElement.requestFullscreen) {
void document.documentElement.requestFullscreen()
}
}
public changeButtonState(inZenMode: boolean) {
this.inZenMode = inZenMode
this.button.setAttribute('aria-pressed', inZenMode.toString())
}
public performAction() {
if (this.inZenMode) {
this.deactivateZenMode()
} else {
this.activateZenMode()
}
}
}
declare global {
interface Window {
ZenModeButtonElement: typeof ZenModeButtonElement
}
}
if (!window.customElements.get('zen-mode-button')) {
window.ZenModeButtonElement = ZenModeButtonElement
window.customElements.define('zen-mode-button', ZenModeButtonElement)
}