-
Notifications
You must be signed in to change notification settings - Fork 5
/
Snackbar.js
160 lines (136 loc) · 3.95 KB
/
Snackbar.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// https://w3c.github.io/aria/#status
import CustomElement from '../core/CustomElement.js';
import { EVENT_HANDLER_TYPE } from '../core/customTypes.js';
import AriaReflectorMixin from '../mixins/AriaReflectorMixin.js';
import DensityMixin from '../mixins/DensityMixin.js';
import ElevationMixin from '../mixins/ElevationMixin.js';
import ShapeMixin from '../mixins/ShapeMixin.js';
import ThemableMixin from '../mixins/ThemableMixin.js';
import './Button.js';
import './IconButton.js';
export default CustomElement
.extend()
.mixin(ThemableMixin)
.mixin(ShapeMixin)
.mixin(DensityMixin)
.mixin(AriaReflectorMixin)
.mixin(ElevationMixin)
.set({
_ariaRole: 'status',
elevated: true,
})
.observe({
open: 'boolean',
persistent: 'boolean',
action: 'string',
actionInk: { empty: 'inverse-primary' },
actionTypeStyle: { empty: 'label-large' },
closeButton: 'boolean',
closeIcon: { empty: 'close' },
closeInk: { empty: 'inherit' },
onaction: EVENT_HANDLER_TYPE,
})
.methods({
async close() {
if (!this.dispatchEvent(new Event('close', { cancelable: true }))) return;
if (!this.open) return;
this.open = false;
if (window.getComputedStyle(this).transitionDuration === '0s') return;
await new Promise((resolve) => {
this.addEventListener('transitionend', resolve, { once: true });
});
},
show() {
this.open = true;
},
/** @param {string} text */
update(text) {
this.textContent = text;
},
})
.html`
<div id=content><slot id=slot></div>
<mdw-button mdw-if={action} id=action class=button ink={actionInk} type-style={actionTypeStyle}>{action}</mdw-button>
<mdw-icon-button mdw-if={closeButton} id=close class=button icon={closeIcon} ink={closeInk}>Close</mdw-button>
`
.childEvents({
action: {
'~click'() {
if (!this.dispatchEvent(new Event('action', { cancelable: true }))) return;
this.close();
},
},
close: {
'~click'() {
this.close();
},
},
})
.css`
/* https://m3.material.io/components/snackbar/specs */
:host {
--mdw-shape__size: var(--mdw-shape__small);
--mdw-bg: var(--mdw-color__inverse-surface);
--mdw-ink: var(--mdw-color__inverse-on-surface);
--mdw-type__line-height: var(--mdw-typescale__body-medium__line-height);
display: flex;
align-items: center;
grid-area: snackbar;
padding-inline: 16px;
pointer-events: auto;
filter: var(--mdw-elevation__drop-shadow__3);
opacity: 0;
transform: translateY(25%) scaleY(0.25);
transform-origin: bottom center;
visibility: hidden; /* Remove from tab order */
z-index: 22;
background-color: rgb(var(--mdw-bg));
color: rgb(var(--mdw-ink));
font: var(--mdw-typescale__body-medium__font);
letter-spacing: var(--mdw-typescale__body-medium__letter-spacing);
transition: transform 200ms, opacity 200ms, visibility 200ms;
}
:host([action]) {
gap: 8px;
padding-inline-end: 8px;
}
:host([close-button]) {
gap: 4px;
padding-inline-end: 4px;
}
:host([open]) {
opacity: 1;
transform: scale(1);
visibility: inherit;
}
#content {
display: flex;
align-items: center;
flex: 1;
padding-block: max(2px, calc(14px + (var(--mdw-density) * 2px)));
}
#slot {
display: block;
overflow-x: hidden;
overflow-y: hidden;
max-block-size: calc(var(--mdw-type__line-height) * 2);
text-align: start;
text-overflow: ellipsis;
text-transform: none;
white-space: normal;
word-break: break-word;
}
@supports(width: 1lh) {
#slot {
max-block-size: 2lh;
}
}
@supports(-webkit-line-clamp:1) {
#slot {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
}
`
.autoRegister('mdw-snackbar');