-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathopensearch-button.html
418 lines (395 loc) · 15.8 KB
/
opensearch-button.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<template id="opensearch_button_element_tpl">
<style>
:host {
font-size: 16px;
cursor: pointer;
background-color: inherit;
display: inline-flex;
clear: none;
flex-wrap: nowrap;
margin: auto;
box-sizing: border-box;
border-radius: 6px;
border-style: solid;
border-width: 1px;
width: inherit;
height: auto;
white-space: nowrap;
}
:host([circular]) {
border-radius: 50%;
}
:host([hidden]) {
display: none;
}
:host([disabled]),
:host([disabled]) > button {
cursor: not-allowed;
opacity: 0.5;
}
:host > button {
display: inline-flex;
justify-content: center;
align-items: center;
appearance: none;
cursor: pointer;
font-weight: inherit;
padding: 0;
margin: 0;
box-sizing: border-box;
border: none;
width: inherit;
height: inherit;
border-radius: 6px;
}
:host([text-only]) > button {
display: block;
text-align: center;
}
:host([icon-only]) > button {
padding-top: 12.5%;
}
:host([icon-position="left"]) > button {
flex-direction: row;
}
:host([icon-position="right"]) > button {
flex-direction: row-reverse;
}
:host([icon-position="top"]) > button {
flex-direction: column;
}
:host([icon-position="bottom"]) > button {
flex-direction: column-reverse;
}
:host([icon-position="bottom"][icon-align="left"]) > button,
:host([icon-position="top"][icon-align="left"]) > button {
align-items: start;
}
:host([icon-position="bottom"][icon-align="right"]) > button,
:host([icon-position="top"][icon-align="right"]) > button {
align-items: end;
}
.icon,
.text {
line-height: 2;
padding: 0.2em;
font-size: 1em;
font-weight: inherit;
}
:host([icon-position="left"]) .icon {
padding-right: 0;
}
:host([icon-position="left"]) .text {
padding-right: 8px;
padding-left: 2px;
}
:host([icon-position="right"]) .icon {
padding-left: 0;
}
:host([icon-position="right"]) .text {
padding-left: 8px;
padding-right: 2px;
}
:host([text-only]) .icon {
display: none;
}
:host([size="large"]) .icon,
:host([size="large"]) .text {
font-size: 18px;
}
:host([size="xlarge"]) .icon,
:host([size="xlarge"]) .text {
font-size: 2em;
}
:host([size="small"]) .icon,
:host([size="small"]) .text {
font-size: 1em;
line-height: 1;
}
.icon {
display: inline-flex;
flex-basis: 10%;
flex-grow: 1;
flex-shrink: 1;
}
.text {
display: inline-flex;
flex-basis: 90%;
flex-grow: 1;
flex-shrink: 1;
}
:host([icon-only]) .text {
display: none;
}
:host([icon-only]) .icon {
flex-basis: 100%;
}
:host([text-only]) .icon {
display: none;
}
:host([text-only]) .text {
flex-basis: 100%;
}
:host([theme-color="primary"]) > button {
background-color: #003B5C;
}
:host([theme-color="secondary"]) > button {
background-color: #DCEBF4;
}
</style>
<button>
<span class="icon">
<slot name="icon-slot"></slot>
</span>
<span class="text">
<slot name="text-slot"></slot>
</span>
</button>
</template>
<script type="module">
class OpenSearchButtonElement extends HTMLElement {
static get observedAttributes() {
return [
'icon-position',
'icon-align',
'text-align',
'size',
'label-text',
'icon-text',
'icon-only',
'text-only',
'disabled',
'fill',
'no-outline',
'href',
'circular',
'theme-color'
];
}
static get DEFAULT_ICON_POSITION() {
return 'left';
}
static get DEFUULT_ICON_ALIGN() {
return 'center';
}
static get DEFAULT_TEXT_ALIGN() {
return 'center';
}
static get DEFAULT_SIZE() {
return 'normal';
}
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open', delegatesFocus: true});
const template = document.getElementById('opensearch_button_element_tpl');
const templateContent = template.content.cloneNode(true);
shadowRoot.appendChild(templateContent);
this.onKeyDownHandler = this.onKeyDownHandler.bind(this);
this.onFocusHandler = this.onFocusHandler.bind(this);
this.onBlurHandler = this.onBlurHandler.bind(this);
}
connectedCallback() {
//
// State expectations:
// If there is an icon then ensure that the icon attributes are valid.
// Also, if the icon is specified as a text value in the attribute then
// assign the attribute value to the span that contains the icon slot.
// If there is text then ensure that the text attributes are valid.
// Also, if the text is specified as a text value in the attribute then
// assign the attribute value to the span that contains the text slot.
// If the button is configured to be an icon-only button then ensure that
// the text slot is not displayed, and the text attributes are removed.
// If the button is configured to be a text-only button then ensure that
// the icon slot is not displayed, and the icon attributes are removed.
// If there is a fill attribute specified then assign it to the background color
// of the button. No validation is applied to the fill attribute. Whereas it
// may be trivial to pattern match against HEX, rgb and rgba values, it is
// not trivial to pattern match against CSS color names since there are so many.
// If the no-outline attribute is specfied then the button border is removed.
//
const isIconOnly = this.hasAttribute('icon-only');
const isTextOnly = this.hasAttribute('text-only');
if (isIconOnly && isTextOnly) {
console.warn('[opensearch-button] Cannot have both icon-only and text-only attributes as their semantics is mutually exclusive.');
return;
}
if (isTextOnly) {
// Remove all icon specific attributes
this.removeAttribute('icon-position');
this.removeAttribute('icon-align');
this.removeAttribute('icon-text');
}
// State assertion: for text only buttons all icon specific attributes are removed.
if (isIconOnly) {
// Remove all text specific attributes
this.removeAttribute('text-align');
this.removeAttribute('label-text');
}
// State assertion: for a icon only button all text specific attributes are removed.
if (!isTextOnly) {
// Validate the configuration attributes setting defaults as necessary.
if (
!this.hasAttribute('icon-position') ||
!this.isValidIconPosition(this.getAttribute('icon-position'))
) {
// Ensure a valid icon position.
this.setAttribute('icon-position', OpenSearchButtonElement.DEFAULT_ICON_POSITION);
}
if (
!this.hasAttribute('icon-align') ||
!this.isValidAlignment(this.getAttribute('icon-align'))
) {
// Ensure a valid icon align.
this.setAttribute('icon-align', OpenSearchButtonElement.DEFUULT_ICON_ALIGN);
}
// Ensure there is a valid icon. Either as an element or as text.
const hasIcon = (
this.hasAttribute('icon-text') && this.getAttribute('icon-text').trim() !== ''
) || this.shadowRoot.querySelector('slot[name="icon-slot"]').assignedNodes().length > 0;
if (!hasIcon) {
console.warn('No icon specified for button.');
return;
}
const iconText = this.getAttribute('icon-text') || '';
if (iconText !== '') {
this.shadowRoot.querySelector('.icon').innerHTML = iconText;
}
// State assertion: for a non text-only icon there is a properly configured icon.
}
if (!isIconOnly) {
// Ensure a valid text align.
if (
!this.hasAttribute('text-align') ||
!this.isValidAlignment(this.getAttribute('text-align'))
) {
this.setAttribute('text-align', OpenSearchButtonElement.DEFAULT_TEXT_ALIGN);
}
// Button label can be defined via the label-text attribute or via the text slot.
// Ensure that one or the other is defined.
const slotText = Array.from(
this.shadowRoot.querySelector('slot[name="text-slot"]')?.assignedNodes?.() ?? []
).reduce((carry, current) => carry + current.textContent, '').trim();
if (slotText === '') {
const labelText = this.getAttribute('label-text') || '';
if (labelText === '') {
console.warn('No text specified for button.');
return;
}
// Accesibility.
this.setAttribute('aria-label', labelText);
// Add the text from the attribute to the text span.
this.shadowRoot.querySelector('.text').textContent = labelText;
} else {
// Accesibility.
this.setAttribute('aria-label', slotText);
}
// State assertion: for a non icon-only button there is a properly configured text.
}
const fill = this.getAttribute('fill');
if (fill) {
this.shadowRoot.querySelector('button').style.backgroundColor = fill;
}
// State assertion: if a fill has been specified then it has been applied to the button.
const noOutline = this.hasAttribute('no-outline');
if (noOutline) {
this.style.border = 'none';
}
// State assertion: if the no-outline attribute is specified then the button border is removed.
// Accessibility
this.setAttribute('role', 'button');
if (!this.hasAttribute('tabindex')) {
// only assign a tabindex if one is not already set.
// This should make it focusable by tabbing through elements in the page.
this.setAttribute('tabindex', 0);
}
this.addEventListener('keydown', this.onKeyDownHandler);
// this.addEventListener('focus', this.onFocusHandler);
// this.addEventListener('blur', this.onBlurHandler);
// State assertion: the button is accessible.
}
onKeyDownHandler(event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.shadowRoot.querySelector('button')?.click?.();
}
}
onFocusHandler(event) {
this.shadowRoot.querySelector('button')?.focus?.();
}
onBlurHandler(event) {
this.shadowRoot.querySelector('button')?.blur?.();
}
disconnectCallback() {
this.removeEventListener('keydown', this.onKeyDownHandler);
// this.removeEventListener('focus', this.onFocusHandler);
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'icon-position') {
if (!this.isValidIconPosition(newValue)) {
this.setAttribute('icon-position', oldValue || OpenSearchButtonElement.DEFAULT_ICON_POSITION);
}
} else if (name === 'icon-align') {
if (!this.isValidAlignment(newValue)) {
this.setAttribute('icon-align', oldValue || OpenSearchButtonElement.DEFUULT_ICON_ALIGN);
}
} else if (name === 'text-align') {
if (!this.isValidAlignment(newValue)) {
this.setAttribute('text-align', oldValue || OpenSearchButtonElement.DEFAULT_TEXT_ALIGN);
}
} else if (name === 'size') {
if (!this.isValidSize(newValue)) {
this.setAttribute('size', oldValue || OpenSearchButtonElement.DEFAULT_SIZE);
}
} else if (name === 'label-text') {
if (newValue === '') {
this.removeAttribute('label-text');
} else {
this.shadowRoot.querySelector('.text').textContent = newValue;
}
} else if (name === 'icon-text') {
if (newValue === '') {
this.removeAttribute('icon-text');
} else {
this.shadowRoot.querySelector('.icon').innerHTML = newValue;
}
} else if (name === 'fill') {
if (newValue === '') {
this.removeAttribute('fill');
} else {
this.shadowRoot.querySelector('button').style.backgroundColor = newValue;
}
} else if (name === 'no-outline') {
if (newValue === '') {
this.removeAttribute('no-outline');
} else {
this.style.border = 'none';
}
} else if (name === 'icon-only') {
if (newValue === '') {
this.removeAttribute('icon-only');
} else {
this.shadowRoot.querySelector('.text').style.display = 'none';
}
} else if (name === 'text-only') {
if (newValue === '') {
this.removeAttribute('text-only');
} else {
this.shadowRoot.querySelector('.icon').style.display = 'none';
}
}
}
isValidIconPosition(value) {
const validIconPositionValues = ['left', 'right', 'top', 'bottom'];
return validIconPositionValues.includes(value);
}
isValidAlignment(value) {
const validAlignmentValues = ['left', 'right', 'center'];
return validAlignmentValues.includes(value);
}
isValidSize(value) {
const validSizeValues = ['small', 'normal', 'large', 'xlarge'];
return validSizeValues.includes(value);
}
}
customElements.define('opensearch-button', OpenSearchButtonElement);
</script>