-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathdrawer.ts
128 lines (117 loc) · 3.2 KB
/
drawer.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
import { attr, FASTElement, observable, Updates } from '@microsoft/fast-element';
import { DrawerPosition, DrawerSize, DrawerType } from './drawer.options.js';
/**
* A Drawer component that allows content to be displayed in a side panel. It can be rendered as modal or non-modal.
* @extends FASTElement
*
* @tag fluent-drawer
*
* @csspart dialog - The dialog element of the drawer.
*
* @slot - Default slot for the content of the drawer.
*
* @fires toggle - Event emitted after the dialog's open state changes.
* @fires beforetoggle - Event emitted before the dialog's open state changes.
*
* @summary A component that provides a drawer for displaying content in a side panel.
*/
export class Drawer extends FASTElement {
/**
* @public
* Determines whether the drawer should be displayed as modal or non-modal
* When rendered as a modal, an overlay is applied over the rest of the view.
*/
@attr
public type: DrawerType = DrawerType.modal;
/**
* @public
* The ID of the element that labels the drawer.
*/
@attr({ attribute: 'aria-labelledby' })
public ariaLabelledby?: string;
/**
* @public
* The ID of the element that describes the drawer.
*/
@attr({ attribute: 'aria-describedby' })
public ariaDescribedby?: string;
/**""
* @public
* @defaultValue start
* Sets the position of the drawer (start/end).
*/
@attr
public position: DrawerPosition = DrawerPosition.start;
/**
* @public
* @defaultValue medium
* Sets the size of the drawer (small/medium/large).
*/
@attr({ attribute: 'size' })
public size: DrawerSize = DrawerSize.medium;
/**
* @public
* The dialog element.
*/
@observable
public dialog!: HTMLDialogElement;
/**
* @public
* Method to emit an event after the dialog's open state changes
* HTML spec proposal: https://github.com/whatwg/html/issues/9733
*/
public emitToggle = (): void => {
this.$emit('toggle', {
oldState: this.dialog.open ? 'closed' : 'open',
newState: this.dialog.open ? 'open' : 'closed',
});
};
/**
* @public
* Method to emit an event before the dialog's open state changes
* HTML spec proposal: https://github.com/whatwg/html/issues/9733
*/
public emitBeforeToggle = (): void => {
this.$emit('beforetoggle', {
oldState: this.dialog.open ? 'open' : 'closed',
newState: this.dialog.open ? 'closed' : 'open',
});
};
/**
* @public
* Method to show the drawer
*/
public show(): void {
Updates.enqueue(() => {
this.emitBeforeToggle();
if (this.type === DrawerType.inline || this.type === DrawerType.nonModal) {
this.dialog.show();
} else {
this.dialog.showModal();
}
this.emitToggle();
});
}
/**
* @public
* Method to hide the drawer
*/
public hide(): void {
this.emitBeforeToggle();
this.dialog.close();
this.emitToggle();
}
/**
* @public
* @param event - The click event
* @returns boolean - Always returns true
* Handles click events on the drawer.
*/
public clickHandler(event: Event): boolean {
event.preventDefault();
if (this.dialog.open && event.target === this.dialog) {
this.hide();
}
return true;
}
}