@graupl/core 1.0.0-beta.18 → 1.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/dist/css/component/badge.css +2 -0
  2. package/dist/css/component/badge.css.map +1 -0
  3. package/dist/css/component/disclosure.css +2 -0
  4. package/dist/css/component/disclosure.css.map +1 -0
  5. package/dist/css/component.css +1 -1
  6. package/dist/css/component.css.map +1 -1
  7. package/dist/css/graupl.css +1 -1
  8. package/dist/css/graupl.css.map +1 -1
  9. package/dist/css/layout.css +1 -1
  10. package/dist/css/layout.css.map +1 -1
  11. package/dist/css/utilities/border.css +1 -1
  12. package/dist/css/utilities/border.css.map +1 -1
  13. package/dist/css/utilities.css +1 -1
  14. package/dist/css/utilities.css.map +1 -1
  15. package/dist/js/accordion.js +2 -2
  16. package/dist/js/accordion.js.map +1 -1
  17. package/dist/js/alert.js.map +1 -1
  18. package/dist/js/carousel.js +2 -2
  19. package/dist/js/carousel.js.map +1 -1
  20. package/dist/js/component/accordion.cjs.js +2 -2
  21. package/dist/js/component/accordion.cjs.js.map +1 -1
  22. package/dist/js/component/accordion.es.js +2 -2
  23. package/dist/js/component/accordion.es.js.map +1 -1
  24. package/dist/js/component/accordion.iife.js +2 -2
  25. package/dist/js/component/accordion.iife.js.map +1 -1
  26. package/dist/js/component/alert.cjs.js.map +1 -1
  27. package/dist/js/component/alert.es.js.map +1 -1
  28. package/dist/js/component/alert.iife.js +2 -2
  29. package/dist/js/component/alert.iife.js.map +1 -1
  30. package/dist/js/component/carousel.cjs.js +2 -2
  31. package/dist/js/component/carousel.cjs.js.map +1 -1
  32. package/dist/js/component/carousel.es.js +2 -2
  33. package/dist/js/component/carousel.es.js.map +1 -1
  34. package/dist/js/component/carousel.iife.js +2 -2
  35. package/dist/js/component/carousel.iife.js.map +1 -1
  36. package/dist/js/component/disclosure.cjs.js +5 -0
  37. package/dist/js/component/disclosure.cjs.js.map +1 -0
  38. package/dist/js/component/disclosure.es.js +5 -0
  39. package/dist/js/component/disclosure.es.js.map +1 -0
  40. package/dist/js/component/disclosure.iife.js +5 -0
  41. package/dist/js/component/disclosure.iife.js.map +1 -0
  42. package/dist/js/generator/accordion.cjs.js +2 -2
  43. package/dist/js/generator/accordion.cjs.js.map +1 -1
  44. package/dist/js/generator/accordion.es.js +2 -2
  45. package/dist/js/generator/accordion.es.js.map +1 -1
  46. package/dist/js/generator/accordion.iife.js +2 -2
  47. package/dist/js/generator/accordion.iife.js.map +1 -1
  48. package/dist/js/generator/alert.cjs.js.map +1 -1
  49. package/dist/js/generator/alert.es.js.map +1 -1
  50. package/dist/js/generator/alert.iife.js +2 -2
  51. package/dist/js/generator/alert.iife.js.map +1 -1
  52. package/dist/js/generator/carousel.cjs.js +2 -2
  53. package/dist/js/generator/carousel.cjs.js.map +1 -1
  54. package/dist/js/generator/carousel.es.js +2 -2
  55. package/dist/js/generator/carousel.es.js.map +1 -1
  56. package/dist/js/generator/carousel.iife.js +2 -2
  57. package/dist/js/generator/carousel.iife.js.map +1 -1
  58. package/dist/js/generator/disclosure.cjs.js +5 -0
  59. package/dist/js/generator/disclosure.cjs.js.map +1 -0
  60. package/dist/js/generator/disclosure.es.js +5 -0
  61. package/dist/js/generator/disclosure.es.js.map +1 -0
  62. package/dist/js/generator/disclosure.iife.js +5 -0
  63. package/dist/js/generator/disclosure.iife.js.map +1 -0
  64. package/dist/js/generator/navigation.cjs.js.map +1 -1
  65. package/dist/js/generator/navigation.es.js.map +1 -1
  66. package/dist/js/generator/navigation.iife.js +1 -1
  67. package/dist/js/generator/navigation.iife.js.map +1 -1
  68. package/dist/js/graupl.js +6 -4
  69. package/dist/js/graupl.js.map +1 -1
  70. package/dist/js/navigation.js.map +1 -1
  71. package/package.json +1 -1
  72. package/scss/component/badge.scss +3 -0
  73. package/scss/component/disclosure.scss +3 -0
  74. package/src/js/TransactionalValue.js +140 -0
  75. package/src/js/accordion/Accordion.js +4 -4
  76. package/src/js/carousel/Carousel.js +1 -1
  77. package/src/js/disclosure/Disclosure.js +1299 -0
  78. package/src/js/disclosure/generator.js +47 -0
  79. package/src/js/disclosure/index.js +5 -0
  80. package/src/js/main.js +2 -0
  81. package/src/js/validate.js +7 -7
  82. package/src/scss/_index.scss +8 -8
  83. package/src/scss/base/_index.scss +4 -4
  84. package/src/scss/component/_index.scss +10 -8
  85. package/src/scss/component/badge/_defaults.scss +47 -0
  86. package/src/scss/component/badge/_index.scss +201 -0
  87. package/src/scss/component/badge/_variables.scss +112 -0
  88. package/src/scss/component/disclosure/_defaults.scss +45 -0
  89. package/src/scss/component/disclosure/_index.scss +214 -0
  90. package/src/scss/component/disclosure/_variables.scss +205 -0
  91. package/src/scss/layout/_index.scss +3 -3
  92. package/src/scss/theme/_index.scss +2 -2
  93. package/src/scss/utilities/_index.scss +21 -21
  94. package/src/scss/utilities/border/_index.scss +91 -0
@@ -0,0 +1,1299 @@
1
+ /**
2
+ * @file
3
+ * The Disclosure class.
4
+ */
5
+
6
+ import {
7
+ isValidInstance,
8
+ isValidClassList,
9
+ isValidType,
10
+ isValidState,
11
+ isValidEvent,
12
+ isTag,
13
+ isQuerySelector,
14
+ } from "../validate.js";
15
+ import { addClass, removeClass } from "../domHelpers.js";
16
+ import { keyPress, preventEvent } from "../eventHandlers.js";
17
+ import storage from "../storage.js";
18
+ import { TransactionalValue } from "../TransactionalValue.js";
19
+
20
+ class Disclosure {
21
+ /**
22
+ * The DOM elements within the disclosure.
23
+ *
24
+ * @protected
25
+ *
26
+ * @type {Object<HTMLElement>}
27
+ *
28
+ * @property {HTMLElement} controller - The disclosure toggle element.
29
+ * @property {HTMLElement} disclosure - The disclosure element.
30
+ * @property {HTMLElement} content - The disclosure content element.
31
+ */
32
+ _dom = {
33
+ controller: null,
34
+ disclosure: null,
35
+ content: null,
36
+ };
37
+
38
+ /**
39
+ * The DOM elements within the disclosure that cannot be reset or generated by the disclosure itself.
40
+ *
41
+ * @protected
42
+ *
43
+ * @type {string[]}
44
+ */
45
+ _domLock = ["controller", "disclosure"];
46
+
47
+ /**
48
+ * The query selectors used by the disclosure.
49
+ *
50
+ * @protected
51
+ *
52
+ * @type {Object<string>}
53
+ *
54
+ * @property {string} content - The query selector for the disclosure content.
55
+ */
56
+ _selectors = {
57
+ content: "",
58
+ };
59
+
60
+ /**
61
+ * The classes to apply when the disclosure is in various states.
62
+ *
63
+ * @protected
64
+ *
65
+ * @type {Object<string, string[]>}
66
+ *
67
+ * @property {string|string[]} open - The class(es) to apply when the disclosure is open.
68
+ * @property {string|string[]} close - The class(es) to apply when the disclousre is closed.
69
+ * @property {string|string[]} tranistion - The class(es) to apply when the disclosure is transitioning between states.
70
+ * @property {string|string[]} initialize - The class(es) to apply when the disclosure is initializing.
71
+ */
72
+ _classes = {
73
+ open: "show",
74
+ close: "hide",
75
+ transition: "transitioning",
76
+ initialize: "initializing",
77
+ };
78
+
79
+ /**
80
+ * The duration times (in milliseconds) for various things throughout the disclosure.
81
+ *
82
+ * @protected
83
+ *
84
+ * @type {Object<number>}
85
+ *
86
+ * @property {number} transition - The duration time (in milliseconds) for the transition between open and closed states.
87
+ * @property {number} open - The duration time (in milliseconds) for the transition from closed to open states.
88
+ * @property {number} close - The duration time (in milliseconds) for the transition from open to closed states.
89
+ */
90
+ _durations = {
91
+ transition: 5000,
92
+ open: -1,
93
+ close: -1,
94
+ };
95
+
96
+ /**
97
+ * The current state of the disclosure's focus.
98
+ *
99
+ * @protected
100
+ *
101
+ * @type {string}
102
+ */
103
+ _focusState = "none";
104
+
105
+ /**
106
+ * This last event triggered on the disclosure.
107
+ *
108
+ * @protected
109
+ *
110
+ * @type {string}
111
+ */
112
+ _currentEvent = "none";
113
+
114
+ /**
115
+ * The open state of the disclosure.
116
+ *
117
+ * @protected
118
+ *
119
+ * @type {TransactionalValue<boolean>}
120
+ */
121
+ _open = new TransactionalValue(false);
122
+
123
+ /**
124
+ * Whether or not to close the disclosure when it loses focus in the DOM.
125
+ *
126
+ * @protected
127
+ *
128
+ * @type {boolean}
129
+ */
130
+ _closeOnBlur = false;
131
+
132
+ /**
133
+ * The width of the screen (in pixels) that the disclosure will automatically open/close itself.
134
+ *
135
+ * @protected
136
+ *
137
+ * @type {number}
138
+ */
139
+ _breakpointWidth = -1;
140
+
141
+ /**
142
+ * This ResizeObserver for the disclosure.
143
+ *
144
+ * @protected
145
+ *
146
+ * @type {ResizeObserver|null}
147
+ */
148
+ _observer = null;
149
+
150
+ /**
151
+ * The event that is triggered when the disclosure expands.
152
+ *
153
+ * @protected
154
+ *
155
+ * @event grauplDisclosureExpand
156
+ *
157
+ * @type {CustomEvent}
158
+ *
159
+ * @property {boolean} bubbles - A flag to bubble the event.
160
+ * @property {Object<Disclosure>} details - The details object containing the Disclosure itself.
161
+ */
162
+ _expandEvent = new CustomEvent("grauplDisclosureExpand", {
163
+ bubbles: true,
164
+ detail: { disclosure: this },
165
+ });
166
+
167
+ /**
168
+ * The event that is triggered when the disclosure collapses.
169
+ *
170
+ * @protected
171
+ *
172
+ * @event grauplDisclosureCollapse
173
+ *
174
+ * @type {CustomEvent}
175
+ *
176
+ * @property {boolean} bubbles - A flag to bubble the event.
177
+ * @property {Object<Disclosure>} details - The details object containing the Disclosure itself.
178
+ */
179
+ _collapseEvent = new CustomEvent("grauplDisclosureCollapse", {
180
+ bubbles: true,
181
+ detail: { disclosure: this },
182
+ });
183
+
184
+ /**
185
+ * The prefix to use for CSS custom properties.
186
+ *
187
+ * @protected
188
+ *
189
+ * @type {string}
190
+ */
191
+ _prefix = "graupl-";
192
+
193
+ /**
194
+ * An array of error messages generated by the disclosure.
195
+ *
196
+ * @protected
197
+ *
198
+ * @type {string[]}
199
+ */
200
+ _errors = [];
201
+
202
+ /**
203
+ * Constructs a new `Disclosure`.
204
+ *
205
+ * @param {object} options - The options for generating the disclosure.
206
+ * @param {HTMLElement} options.disclosureElement - The disclosure element in the DOM.
207
+ * @param {HTMLElement} options.controllerElement - The disclosure toggle element in the DOM.
208
+ * @param {string} [options.disclosureContentSelector = .disclosure-content] - The query selector string for the disclosure content.
209
+ * @param {?(string|string[])} [options.openClass = show] - The class to apply when a disclosure is "open".
210
+ * @param {?(string|string[])} [options.closeClass = hide] - The class to apply when a disclosure is "closed".
211
+ * @param {?(string|string[])} [options.transitionClass = transitioning] - The class to apply when a disclosure is transitioning between "open" and "closed" states.
212
+ * @param {number} [options.transitionDuration = 250] - The duration of the transition between "open" and "closed" states (in milliseconds).
213
+ * @param {boolean} [options.openDuration = -1] - The duration of the transition from "closed" to "open" states (in milliseconds).
214
+ * @param {boolean} [options.closeDuration = -1] - The duration of the transition from "open" to "closed" states (in milliseconds).
215
+ * @param {boolean} [options.closeOnBlur = false] - Whether to close the disclosure when it loses focus in the dom.
216
+ * @param {boolean} [options.minWidth = -1] - The width of the screen (in pixels) that the disclosure will automatically open/close itself.
217
+ * @param {?string} [options.prefix = graupl-] - The prefix to use for CSS custom properties.
218
+ * @param {?(string|string[])} [options.initializeClass = initializing] - The class to apply when a disclosure is initialzing.
219
+ * @param {boolean} [options.initialize = false] - Whether to initialize the disclosure upon construction.
220
+ */
221
+ constructor({
222
+ disclosureElement,
223
+ controllerElement,
224
+ disclosureContentSelector = ".disclosure-content",
225
+ openClass = "show",
226
+ closeClass = "hide",
227
+ transitionClass = "transitioning",
228
+ transitionDuration = 250,
229
+ openDuration = -1,
230
+ closeDuration = -1,
231
+ closeOnBlur = false,
232
+ minWidth = -1,
233
+ prefix = "graupl-",
234
+ initializeClass = "initializing",
235
+ initialize = false,
236
+ }) {
237
+ // Set the DOM elements.
238
+ this._dom.disclosure = disclosureElement;
239
+ this._dom.controller = controllerElement;
240
+
241
+ // Set the DOM selectors.
242
+ this._selectors.content = disclosureContentSelector;
243
+
244
+ // Set the classes.
245
+ this._classes.open = openClass || "";
246
+ this._classes.close = closeClass || "";
247
+ this._classes.transition = transitionClass || "";
248
+ this._classes.initialize = initializeClass || "";
249
+
250
+ // Set the transition durations.
251
+ this._durations.transition = transitionDuration;
252
+ this._durations.open = openDuration;
253
+ this._durations.close = closeDuration;
254
+
255
+ // Set close on blur.
256
+ this._closeOnBlur = closeOnBlur;
257
+
258
+ // Set collapse width.
259
+ this._breakpointWidth = minWidth;
260
+
261
+ // Set the prefix.
262
+ this._prefix = prefix;
263
+
264
+ if (initialize) {
265
+ this.initialize();
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Initializes the disclosure.
271
+ */
272
+ initialize() {
273
+ try {
274
+ if (!this._validate()) {
275
+ throw new Error(
276
+ `Graupl Disclosure: cannot initialize disclosure. The following errors have been found:\n - ${this.errors.join(
277
+ "\n - "
278
+ )}`
279
+ );
280
+ }
281
+
282
+ addClass(this._classes.initialize, this.dom.disclosure);
283
+
284
+ // Set up the DOM.
285
+ this._generateKey();
286
+ this._setDOMElements();
287
+ this._setIds();
288
+ this._setAriaAttributes();
289
+
290
+ // Handle events.
291
+ this._handleFocus();
292
+ this._handleClick();
293
+ this._handleKeydown();
294
+ this._handleKeyup();
295
+ this._handleResize();
296
+
297
+ // Set the custom props.
298
+ this._setTransitionDurations();
299
+
300
+ // Set up the storage.
301
+ storage.initializeStorage("disclosures");
302
+ storage.pushToStorage("disclosures", this.dom.disclosure.id, this);
303
+
304
+ if (this.dom.controller.getAttribute("aria-expanded") === "true") {
305
+ this._expand(false, false);
306
+ } else {
307
+ this._collapse(false, false);
308
+ }
309
+
310
+ requestAnimationFrame(() => {
311
+ removeClass(this._classes.initialize, this.dom.disclosure);
312
+ });
313
+ } catch (error) {
314
+ console.error(error);
315
+ }
316
+ }
317
+
318
+ /**
319
+ * The DOM elements within the disclosure.
320
+ *
321
+ * @readonly
322
+ *
323
+ * @type {Object<HTMLElement>}
324
+ *
325
+ * @see _dom
326
+ */
327
+ get dom() {
328
+ return this._dom;
329
+ }
330
+
331
+ /**
332
+ * The query selectors used by the disclosure.
333
+ *
334
+ * @readonly
335
+ *
336
+ * @type {Object<string>}
337
+ *
338
+ * @see _selectors
339
+ */
340
+ get selectors() {
341
+ return this._selectors;
342
+ }
343
+
344
+ /**
345
+ * The class(es) to apply when the disclosure is open.
346
+ *
347
+ * @type {string|string[]}
348
+ *
349
+ * @see _classes.open
350
+ */
351
+ get openClass() {
352
+ return this._classes.open;
353
+ }
354
+
355
+ set openClass(value) {
356
+ isValidClassList({ openClass: value });
357
+
358
+ if (this._classes.open !== value) {
359
+ this._classes.open = value;
360
+ }
361
+ }
362
+
363
+ /**
364
+ * The class(es) to apply when the disclosure is closed.
365
+ *
366
+ * @type {string|string[]}
367
+ *
368
+ * @see _classes.close
369
+ */
370
+ get closeClass() {
371
+ return this._classes.close;
372
+ }
373
+
374
+ set closeClass(value) {
375
+ isValidClassList({ closeClass: value });
376
+
377
+ if (this._classes.close !== value) {
378
+ this._classes.close = value;
379
+ }
380
+ }
381
+
382
+ /**
383
+ * The class(es) to apply when the disclosure is transitioning between open and closed.
384
+ *
385
+ * @type {string|string[]}
386
+ *
387
+ * @see _classes.transition
388
+ */
389
+ get transitionClass() {
390
+ return this._classes.transition;
391
+ }
392
+
393
+ set transitionClass(value) {
394
+ isValidClassList({ transitionClass: value });
395
+
396
+ if (this._classes.transition !== value) {
397
+ this._classes.transition = value;
398
+ }
399
+ }
400
+
401
+ /**
402
+ * The class(es) to apply to the shelf when the disclosure is initializing.
403
+ *
404
+ * @type {string|string[]}
405
+ *
406
+ * @see _classes
407
+ */
408
+ get initializeClass() {
409
+ return this._classes.initialize;
410
+ }
411
+
412
+ set initializeClass(value) {
413
+ isValidClassList({ initializeClass: value });
414
+
415
+ if (this._classes.initialize !== value) {
416
+ this._classes.initialize = value;
417
+ }
418
+ }
419
+
420
+ /**
421
+ * The duration time (in milliseconds) for the transition between open and closed states.
422
+ *
423
+ * Setting this value will also set the --graupl-transition-duration CSS custom property on the disclosure.
424
+ *
425
+ * @type {number}
426
+ *
427
+ * @see _durations.transition
428
+ */
429
+ get transitionDuration() {
430
+ return this._durations.transition;
431
+ }
432
+
433
+ set transitionDuration(value) {
434
+ isValidType("number", { value });
435
+
436
+ if (this._durations.transition !== value) {
437
+ this._durations.transition = value;
438
+ this._setTransitionDurations();
439
+ }
440
+ }
441
+
442
+ /**
443
+ * The duration time (in milliseconds) for the transition from closed to open states.
444
+ *
445
+ * If openDuration is set to -1, the transitionDuration value will be used instead.
446
+ *
447
+ * Setting this value will also set the --graupl-open-transition-duration CSS custom property on the disclosure.
448
+ *
449
+ * @type {number}
450
+ *
451
+ * @see _durations.open
452
+ */
453
+ get openDuration() {
454
+ if (this._durations.open === -1) return this.transitionDuration;
455
+
456
+ return this._durations.open;
457
+ }
458
+
459
+ set openDuration(value) {
460
+ isValidType("number", { value });
461
+
462
+ if (this._durations.open !== value) {
463
+ this._durations.open = value;
464
+ this._setTransitionDurations();
465
+ }
466
+ }
467
+
468
+ /**
469
+ * The duration time (in milliseconds) for the transition from open to closed states.
470
+ *
471
+ * If closeDuration is set to -1, the transitionDuration value will be used instead.
472
+ *
473
+ * Setting this value will also set the --graupl-close-transition-duration CSS custom property on the disclosure.
474
+ *
475
+ * @type {number}
476
+ *
477
+ * @see _durations.close
478
+ */
479
+ get closeDuration() {
480
+ if (this._durations.close === -1) return this.transitionDuration;
481
+
482
+ return this._durations.close;
483
+ }
484
+
485
+ set closeDuration(value) {
486
+ isValidType("number", { value });
487
+
488
+ if (this._durations.close !== value) {
489
+ this._durations.close = value;
490
+ this._setTransitionDurations();
491
+ }
492
+ }
493
+
494
+ /**
495
+ * The width of the screen (in pixels) that the disclosure will automatically open/close itself.
496
+ *
497
+ * @type {number}
498
+ *
499
+ * @see _breakpointWidth
500
+ */
501
+ get minWidth() {
502
+ return this._breakpointWidth;
503
+ }
504
+
505
+ set minWidth(value) {
506
+ isValidType("number", { value });
507
+
508
+ if (this._breakpointWidth !== value) {
509
+ this._breakpointWidth = value;
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Whether to close the disclosure when it loses focus in the DOM.
515
+ *
516
+ * @type {boolean}
517
+ *
518
+ * @see _closeOnBlur
519
+ */
520
+ get closeOnBlur() {
521
+ return this._closeOnBlur;
522
+ }
523
+
524
+ set closeOnBlur(value) {
525
+ isValidType("boolean", { value });
526
+
527
+ if (this._closeOnBlur !== value) {
528
+ this._closeOnBlur = value;
529
+ }
530
+ }
531
+
532
+ /**
533
+ * The current state of the disclosure's focus.
534
+ *
535
+ * @type {string}
536
+ *
537
+ * @see _focusState
538
+ */
539
+ get focusState() {
540
+ return this._focusState;
541
+ }
542
+
543
+ set focusState(value) {
544
+ isValidState({ value });
545
+
546
+ if (this._focusState !== value) {
547
+ this._focusState = value;
548
+ }
549
+ }
550
+
551
+ /**
552
+ * The last event triggered on the disclosure.
553
+ *
554
+ * @type {string}
555
+ *
556
+ * @see _currentEvent
557
+ */
558
+ get currentEvent() {
559
+ return this._currentEvent;
560
+ }
561
+
562
+ set currentEvent(value) {
563
+ isValidEvent({ value });
564
+
565
+ if (this._currentEvent !== value) {
566
+ this._currentEvent = value;
567
+ }
568
+ }
569
+
570
+ /**
571
+ * The prefix to use for CSS custom properties.
572
+ *
573
+ * @type {string}
574
+ *
575
+ * @see _prefix
576
+ */
577
+ get prefix() {
578
+ return this._prefix;
579
+ }
580
+
581
+ set prefix(value) {
582
+ isValidType("string", { value });
583
+
584
+ if (this._prefix !== value) {
585
+ this._prefix = value;
586
+ this._setTransitionDurations();
587
+ }
588
+ }
589
+
590
+ /**
591
+ * The open state of the disclosure.
592
+ *
593
+ * @readonly
594
+ *
595
+ * @type {boolean}
596
+ *
597
+ * @see _open
598
+ */
599
+ get isOpen() {
600
+ return this._open.value;
601
+ }
602
+
603
+ /**
604
+ * The open state of the disclosure that the user specifically triggered.
605
+ *
606
+ * @readonly
607
+ *
608
+ * @type {boolean}
609
+ *
610
+ * @see _open
611
+ */
612
+ get hasOpened() {
613
+ return this._open.committed;
614
+ }
615
+
616
+ /**
617
+ * A flag to check if the disclosure's focus methods should _actually_ move the focus in the DOM.
618
+ *
619
+ * This will be `false` unless any of the following criteria are met:
620
+ * - The disclosure's current event is "keyboard".
621
+ *
622
+ * @readonly
623
+ *
624
+ * @type {boolean}
625
+ */
626
+ get shouldFocus() {
627
+ let check = false;
628
+
629
+ if (this.currentEvent === "keyboard") {
630
+ check = true;
631
+ }
632
+
633
+ return check;
634
+ }
635
+
636
+ /**
637
+ * An array of error messages generated by the disclosure.
638
+ *
639
+ * @readonly
640
+ *
641
+ * @type {string[]}
642
+ *
643
+ * @see _errors
644
+ */
645
+ get errors() {
646
+ return this._errors;
647
+ }
648
+
649
+ /**
650
+ * Validates all aspects on the disclosure to ensure proper functionality.
651
+ *
652
+ * @protected
653
+ *
654
+ * @return {boolean} - The result of the validation.
655
+ */
656
+ _validate() {
657
+ let check = true;
658
+
659
+ // HTML element checks.
660
+ const htmlElementChecks = isValidInstance(HTMLElement, {
661
+ disclosureElement: this._dom.disclosure,
662
+ controllerElement: this._dom.controller,
663
+ });
664
+
665
+ if (!htmlElementChecks.status) {
666
+ this._errors.push(htmlElementChecks.error.message);
667
+ check = false;
668
+ }
669
+
670
+ // Query selector checks.
671
+ const querySelectorChecks = isQuerySelector({
672
+ disclosureContentSelector: this._selectors.content,
673
+ });
674
+
675
+ if (!querySelectorChecks.status) {
676
+ this._errors.push(querySelectorChecks.error.message);
677
+ check = false;
678
+ }
679
+
680
+ // Class list checks.
681
+ const classes = {};
682
+
683
+ for (const className of Object.keys(this._classes)) {
684
+ if (this._classes[className] === "") {
685
+ continue;
686
+ }
687
+
688
+ classes[`${className}Class`] = this._classes[className];
689
+ }
690
+
691
+ const classListChecks = isValidClassList(classes);
692
+
693
+ if (!classListChecks.status) {
694
+ this._errors.push(classListChecks.error.message);
695
+ check = false;
696
+ }
697
+
698
+ // Duration checks.
699
+ const durations = {};
700
+
701
+ for (const durationName of Object.keys(this._durations)) {
702
+ durations[`${durationName}Duration`] = this._durations[durationName];
703
+ }
704
+
705
+ const durationChecks = isValidType("number", durations);
706
+
707
+ if (!durationChecks.status) {
708
+ this._errors.push(durationChecks.error.message);
709
+ check = false;
710
+ }
711
+
712
+ // Prefix check.
713
+ const prefixCheck = isValidType("string", { prefix: this._prefix });
714
+
715
+ if (!prefixCheck.status) {
716
+ this._errors.push(prefixCheck.error.message);
717
+ check = false;
718
+ }
719
+
720
+ return check;
721
+ }
722
+
723
+ /**
724
+ * Generates a key for the disclosure.
725
+ *
726
+ * @param {boolean} [regenerate = false] - A flag to determine if the key should be regenerated.
727
+ */
728
+ _generateKey(regenerate = false) {
729
+ if (this.key === "" || regenerate) {
730
+ this.key = Math.random()
731
+ .toString(36)
732
+ .replace(/[^a-z]+/g, "")
733
+ .substring(0, 10);
734
+ }
735
+ }
736
+
737
+ /**
738
+ * Sets the IDs of the disclosure and it's controller if they do not already exist.
739
+ *
740
+ * The generated IDs use the key and follow the format:
741
+ * - disclosure: `disclosure-${key}`
742
+ * - controller: `disclosure-controller-${key}`
743
+ *
744
+ * @protected
745
+ */
746
+ _setIds() {
747
+ this.dom.disclosure.id = this.dom.disclosure.id || `disclosure-${this.key}`;
748
+ this.dom.controller.id =
749
+ this.dom.controller.id || `disclosure-controller-${this.key}`;
750
+ }
751
+
752
+ /**
753
+ * Sets the ARIA attributes on the disclosure and its controller.
754
+ *
755
+ * The first steps are to ensure that the controlled has `aria-expanded` set to "false"
756
+ * if it's not already explicity set to "true".
757
+ *
758
+ * Then, set the `aria-controls` attribute on the controller to the disclosure's ID.
759
+ *
760
+ * Finally, ensure the controller element has a role of "button" if it is not a native button element.
761
+ *
762
+ * @protected
763
+ */
764
+ _setAriaAttributes() {
765
+ // If the controller element doesn't have aria-expanded set to true, set it to false.
766
+ if (this.dom.controller.getAttribute("aria-expanded") !== "true") {
767
+ this.dom.controller.setAttribute("aria-expanded", "false");
768
+ }
769
+
770
+ // Set the aria-controls attribute on the controller to the disclosure's ID.
771
+ this.dom.controller.setAttribute("aria-controls", this.dom.disclosure.id);
772
+
773
+ // If the controller element is not a button, set its role to button.
774
+ if (!isTag("button", { controller: this.dom.controller })) {
775
+ this.dom.controller.setAttribute("role", "button");
776
+ }
777
+ }
778
+
779
+ /**
780
+ * Sets the transition durations of the disclosure as a CSS custom properties.
781
+ *
782
+ * The custom properties are:
783
+ * - `--graupl-disclosure-transition-duration`,
784
+ * - `--graupl-disclosure-open-transition-duration`, and
785
+ * - `--graupl-disclosure-close-transition-duration`.
786
+ *
787
+ * The prefix of `graupl-` can be changed by setting the disclosure's prefix value.
788
+ *
789
+ * @protected
790
+ */
791
+ _setTransitionDurations() {
792
+ this.dom.disclosure.style.setProperty(
793
+ `--${this.prefix}disclosure-transition-duration`,
794
+ `${this.transitionDuration}ms`
795
+ );
796
+
797
+ this.dom.disclosure.style.setProperty(
798
+ `--${this.prefix}disclosure-open-transition-duration`,
799
+ `${this.openDuration}ms`
800
+ );
801
+
802
+ this.dom.disclosure.style.setProperty(
803
+ `--${this.prefix}disclosure-close-transition-duration`,
804
+ `${this.closeDuration}ms`
805
+ );
806
+ }
807
+
808
+ /**
809
+ * Sets DOM elements.
810
+ *
811
+ * Elements listed in _domLock cannot be set using this method.
812
+ *
813
+ * @protected
814
+ *
815
+ * @param {string} elementType - The type of element to populate.
816
+ * @param {Object<HTMLElement,boolean>} [options = {}] - The options for setting the DOM element type.
817
+ * @param {HTMLElement} [options.base = this.dom.disclosure] - The element used as the base for the querySelector.
818
+ * @param {boolean} [options.overwrite = true] - A flag to set if the existing elements will be overwritten.
819
+ * @param {boolean} [options.strict = true] - A flag to set if the elements must be direct children of the base.
820
+ */
821
+ _setDOMElementType(
822
+ elementType,
823
+ { base = this.dom.disclosure, overwrite = true, strict = true } = {}
824
+ ) {
825
+ if (typeof this.selectors[elementType] === "string") {
826
+ if (this._domLock.includes(elementType)) {
827
+ throw new Error(
828
+ `Graupl ${this.constructor.name}: "${elementType}" element cannot be set through _setDOMElementType.`
829
+ );
830
+ }
831
+
832
+ if (base !== this.dom.disclosure) isValidInstance(HTMLElement, { base });
833
+
834
+ // Get the all elements matching the selector in the base.
835
+ const domElements = Array.from(
836
+ base.querySelectorAll(this.selectors[elementType])
837
+ );
838
+
839
+ // Filter the elements so only direct children of the base are kept.
840
+ const filteredElements = domElements.filter((item) =>
841
+ strict ? item.parentElement === base : true
842
+ );
843
+
844
+ if (overwrite) {
845
+ if (Array.isArray(this._dom[elementType])) {
846
+ this._dom[elementType] = filteredElements;
847
+ } else {
848
+ this._dom[elementType] = filteredElements[0] || null;
849
+ }
850
+ } else {
851
+ if (Array.isArray(this._dom[elementType])) {
852
+ this._dom[elementType] = [
853
+ ...this._dom[elementType],
854
+ ...filteredElements,
855
+ ];
856
+ } else {
857
+ this._dom[elementType] = filteredElements[0] || null;
858
+ }
859
+ }
860
+ } else {
861
+ throw new Error(
862
+ `Graupl ${this.constructor.name}: "${elementType}" is not a valid element type.`
863
+ );
864
+ }
865
+ }
866
+
867
+ /**
868
+ * Resets DOM elements.
869
+ *
870
+ * Elements listed in _domLock cannot be reset using this method.
871
+ *
872
+ * @protected
873
+ *
874
+ * @param {string} elementType - The type of element to clear.
875
+ */
876
+ _resetDOMElementType(elementType) {
877
+ if (typeof this.selectors[elementType] === "string") {
878
+ if (this._domLock.includes(elementType)) {
879
+ throw new Error(
880
+ `Graupl ${this.constructor.name}: "${elementType}" element cannot be reset through _resetDOMElementType.`
881
+ );
882
+ }
883
+
884
+ if (Array.isArray(this._dom[elementType])) {
885
+ this._dom[elementType] = [];
886
+ } else {
887
+ this._dom[elementType] = null;
888
+ }
889
+ } else {
890
+ throw new Error(
891
+ `Graupl ${this.constructor.name}: "${elementType}" is not a valid element type.`
892
+ );
893
+ }
894
+ }
895
+
896
+ /**
897
+ * Sets all DOM elements within the disclosure.
898
+ *
899
+ * Utilizes _setDOMElementType and
900
+ * _resetDOMElementType.
901
+ *
902
+ * @protected
903
+ */
904
+ _setDOMElements() {
905
+ this._resetDOMElementType("content");
906
+ this._setDOMElementType("content");
907
+ }
908
+
909
+ /**
910
+ * Expands the disclosure.
911
+ *
912
+ * Sets the controller's `aria-expanded` to "true", adds the
913
+ * open class to the disclosure, and removes the closed class from the disclosure.
914
+ *
915
+ * @protected
916
+ *
917
+ * @fires grauplDisclosureExpand
918
+ *
919
+ * @param {boolean} [emit = true] - Emit the expand event once expanded.
920
+ * @param {boolean} [transition = true] - Respect the transition class.
921
+ */
922
+ _expand(emit = true, transition = true) {
923
+ this.dom.controller.setAttribute("aria-expanded", "true");
924
+
925
+ // If we're dealing with transition classes, then we need to utilize
926
+ // requestAnimationFrame to add the transition class, remove the close class,
927
+ // add the open class, and finally remove the transition class.
928
+ if (transition && this.transitionlass !== "") {
929
+ // this.dom.disclosure.style.height = `${this.dom.disclosure.getBoundingClientRect().height}px`;
930
+ // console.log(this.dom.disclosure.style.height);
931
+ addClass(this.transitionClass, this.dom.disclosure);
932
+
933
+ requestAnimationFrame(() => {
934
+ removeClass(this.closeClass, this.dom.disclosure);
935
+
936
+ // this.dom.disclosure.style.height = `${this.dom.disclosure.getBoundingClientRect().height}px`;
937
+ // console.log(this.dom.disclosure.style.height);
938
+
939
+ requestAnimationFrame(() => {
940
+ addClass(this.openClass, this.dom.disclosure);
941
+
942
+ // this.dom.disclosure.style.height = `${this.dom.content.getBoundingClientRect().height}px`;
943
+ // console.log(this.dom.disclosure.style.height);
944
+
945
+ requestAnimationFrame(() => {
946
+ setTimeout(() => {
947
+ removeClass(this.transitionClass, this.dom.disclosure);
948
+
949
+ // this.dom.disclosure.style.height = "";
950
+ // console.log(this.dom.disclosure.style.height);
951
+ }, this.openDuration);
952
+ });
953
+ });
954
+ });
955
+ } else {
956
+ // Add the open class
957
+ addClass(this.openClass, this.dom.disclosure);
958
+
959
+ // Remove the close class.
960
+ removeClass(this.closeClass, this.dom.disclosure);
961
+ }
962
+
963
+ this.dom.content.removeAttribute("inert");
964
+
965
+ if (emit) {
966
+ this.dom.controller.dispatchEvent(this._expandEvent);
967
+ }
968
+ }
969
+
970
+ /**
971
+ * Collapses the disclosure.
972
+ *
973
+ * Sets the controller's `aria-expanded` to "false", adds the
974
+ * close class to the disclosure, and removes the open class from the disclosure.
975
+ *
976
+ * @protected
977
+ *
978
+ * @fires grauplDisclosureCollapse
979
+ *
980
+ * @param {boolean} [emit = true] - Emit the collapse event once collapsed.
981
+ * @param {boolean} [transition = true] - Respect the transition class.
982
+ */
983
+ _collapse(emit = true, transition = true) {
984
+ this.dom.controller.setAttribute("aria-expanded", "false");
985
+
986
+ // If we're dealing with transition classes, then we need to utilize
987
+ // requestAnimationFrame to add the transition class, remove the open class,
988
+ // add the close class, and finally remove the transition class.
989
+ if (transition && this.transitionClass !== "") {
990
+ // this.dom.disclosure.style.height = `${this.dom.content.getBoundingClientRect().height}px`;
991
+ // console.log(this.dom.disclosure.style.height);
992
+
993
+ addClass(this.transitionClass, this.dom.disclosure);
994
+
995
+ requestAnimationFrame(() => {
996
+ removeClass(this.openClass, this.dom.disclosure);
997
+
998
+ // this.dom.disclosure.style.height = `${this.dom.content.getBoundingClientRect().height}px`;
999
+ // console.log(this.dom.disclosure.style.height);
1000
+
1001
+ requestAnimationFrame(() => {
1002
+ addClass(this.closeClass, this.dom.disclosure);
1003
+
1004
+ // this.dom.disclosure.style.height = `${this.dom.disclosure.getBoundingClientRect().height}px`;
1005
+ // console.log(this.dom.disclosure.style.height);
1006
+
1007
+ requestAnimationFrame(() => {
1008
+ setTimeout(() => {
1009
+ removeClass(this.transitionClass, this.dom.disclosure);
1010
+
1011
+ this.dom.content.innert = true;
1012
+
1013
+ // this.dom.disclosure.style.height = "";
1014
+ // console.log(this.dom.disclosure.style.height);
1015
+ }, this.closeDuration);
1016
+ });
1017
+ });
1018
+ });
1019
+ } else {
1020
+ // Add the close class
1021
+ addClass(this.closeClass, this.dom.disclosure);
1022
+
1023
+ // Remove the open class.
1024
+ removeClass(this.openClass, this.dom.disclosure);
1025
+ }
1026
+
1027
+ this.dom.content.setAttribute("inert", "true");
1028
+
1029
+ if (emit) {
1030
+ this.dom.controller.dispatchEvent(this._collapseEvent);
1031
+ }
1032
+ }
1033
+
1034
+ _handleResize() {
1035
+ if (this._breakpointWidth <= 0) {
1036
+ return;
1037
+ }
1038
+
1039
+ this._observer = new ResizeObserver((entries) => {
1040
+ requestAnimationFrame(() => {
1041
+ for (const entry of entries) {
1042
+ const boxSize = Array.isArray(entry.contentBoxSize)
1043
+ ? entry.contentBoxSize[0]
1044
+ : entry.contentBoxSize;
1045
+ const inlineSize =
1046
+ boxSize && typeof boxSize.inlineSize === "number"
1047
+ ? boxSize.inlineSize
1048
+ : entry.contentRect.width;
1049
+
1050
+ if (typeof inlineSize !== "number") continue;
1051
+
1052
+ const belowBreakpoint = inlineSize <= this.minWidth;
1053
+ const aboveBreakpoint = inlineSize > this.minWidth;
1054
+
1055
+ if (belowBreakpoint && this.isOpen) {
1056
+ this.close({ preserveState: true });
1057
+ } else if (aboveBreakpoint && !this.isOpen && this.hasOpened) {
1058
+ this.open();
1059
+ }
1060
+ }
1061
+ });
1062
+ });
1063
+ this._observer.observe(document.body);
1064
+ }
1065
+
1066
+ /**
1067
+ * Handles focus events throughout the disclosure.
1068
+ *
1069
+ * - Adds a `focusout` listener to the disclosure so when the disclosure loses focus it will close.
1070
+ */
1071
+ _handleFocus() {
1072
+ this.dom.disclosure.addEventListener("focusout", (event) => {
1073
+ if (
1074
+ !this.closeOnBlur ||
1075
+ this.currentEvent !== "keyboard" ||
1076
+ event.relatedTarget === null ||
1077
+ this.dom.disclosure.contains(event.relatedTarget) ||
1078
+ this.dom.controller === event.relatedTarget
1079
+ ) {
1080
+ return;
1081
+ }
1082
+
1083
+ this.close();
1084
+ });
1085
+ }
1086
+
1087
+ /**
1088
+ * Handles click events throughout the disclosure.
1089
+ *
1090
+ * - Adds a `pointerup` listener to the controller that toggles the disclosure.
1091
+ * - Adds a `pointerup` listener to the `document` so if the user clicks outside the disclosure it will close.
1092
+ */
1093
+ _handleClick() {
1094
+ this.dom.controller.addEventListener("pointerup", (event) => {
1095
+ this.currentEvent = "mouse";
1096
+
1097
+ if (event.button !== 0) return;
1098
+
1099
+ preventEvent(event);
1100
+ this.toggle();
1101
+ });
1102
+
1103
+ document.addEventListener("pointerup", (event) => {
1104
+ if (this.focusState !== "self" || !this.closeOnBlur) return;
1105
+
1106
+ this.currentEvent = "mouse";
1107
+
1108
+ if (
1109
+ !this.dom.disclosure.contains(event.target) &&
1110
+ this.dom.controller !== event.target
1111
+ ) {
1112
+ this.close();
1113
+ }
1114
+ });
1115
+ }
1116
+
1117
+ /**
1118
+ * Handles keydown events throughout the disclosure.
1119
+ *
1120
+ * This method exists to assist the _handleKeyup method.
1121
+ *
1122
+ * - Adds a `keydown` listener to the controller.
1123
+ * - Blocks propagation on "Space" and "Enter" keys.
1124
+ * - Adds a `keydown` listener to the disclosure.
1125
+ * - Blocks propagation on "Escape" keys.
1126
+ */
1127
+ _handleKeydown() {
1128
+ this.dom.controller.addEventListener("keydown", (event) => {
1129
+ this.currentEvent = "keyboard";
1130
+
1131
+ const key = keyPress(event);
1132
+
1133
+ switch (key) {
1134
+ case "Space":
1135
+ case "Enter":
1136
+ preventEvent(event);
1137
+
1138
+ break;
1139
+ }
1140
+ });
1141
+
1142
+ this.dom.disclosure.addEventListener("keydown", (event) => {
1143
+ this.currentEvent = "keyboard";
1144
+
1145
+ const key = keyPress(event);
1146
+
1147
+ switch (key) {
1148
+ case "Escape":
1149
+ preventEvent(event);
1150
+
1151
+ break;
1152
+ }
1153
+ });
1154
+ }
1155
+
1156
+ /**
1157
+ * Handles keyup events throughout the disclosure.
1158
+ *
1159
+ * - Adds a `keyup` listener to the controller.
1160
+ * - Toggles the disclosure on "Space" and "Enter" keys.
1161
+ * - Adds a `keyup` listener to the disclosure.
1162
+ * - Closes the disclosure on "Escape" keys.
1163
+ */
1164
+ _handleKeyup() {
1165
+ this.dom.controller.addEventListener("keyup", (event) => {
1166
+ this.currentEvent = "keyboard";
1167
+
1168
+ const key = keyPress(event);
1169
+
1170
+ switch (key) {
1171
+ case "Space":
1172
+ case "Enter":
1173
+ this.toggle();
1174
+
1175
+ preventEvent(event);
1176
+
1177
+ break;
1178
+ }
1179
+ });
1180
+
1181
+ this.dom.disclosure.addEventListener("keyup", (event) => {
1182
+ this.currentEvent = "keyboard";
1183
+
1184
+ const key = keyPress(event);
1185
+
1186
+ switch (key) {
1187
+ case "Escape":
1188
+ this.close();
1189
+
1190
+ preventEvent(event);
1191
+
1192
+ break;
1193
+ }
1194
+ });
1195
+ }
1196
+
1197
+ /**
1198
+ * Opens the disclosure.
1199
+ *
1200
+ * Sets the disclosure's focus state to "self", calls expand, and sets isOpen to `true`.
1201
+ *
1202
+ * @public
1203
+ *
1204
+ * @param {Object<boolean>} [options = {}] - Options for opening the disclosure.
1205
+ * @param {boolean} [options.force = false] - Whether to force the open action.
1206
+ * @param {boolean} [options.preserveState = false] - Whether to preserve the open state.
1207
+ */
1208
+ open({ force = false, preserveState = false } = {}) {
1209
+ if (this.isOpen && !force) return;
1210
+
1211
+ // Set the focus state.
1212
+ this.focusState = "self";
1213
+
1214
+ // Expand the disclosure.
1215
+ this._expand();
1216
+
1217
+ // Set the open state.
1218
+ this._open.value = true;
1219
+
1220
+ if (!preserveState) {
1221
+ this._open.commit();
1222
+ }
1223
+ }
1224
+
1225
+ /**
1226
+ * Opens the disclosure without entering it.
1227
+ *
1228
+ * Sets the disclosure's focus state to "none", calls expand, and sets isOpen to `true`.
1229
+ *
1230
+ * @public
1231
+ *
1232
+ * @param {Object<boolean>} [options = {}] - Options for previewing the disclosure.
1233
+ * @param {boolean} [options.force = false] - Whether to force the preview action.
1234
+ * @param {boolean} [options.preserveState = false] - Whether to preserve the open state.
1235
+ */
1236
+ preview({ force = false, preserveState = false } = {}) {
1237
+ if (this.isOpen && !force) return;
1238
+
1239
+ // Set the focus state.
1240
+ this.focusState = "none";
1241
+
1242
+ // Expand the disclosure.
1243
+ this._expand();
1244
+
1245
+ // Set the open state.
1246
+ this._open.value = true;
1247
+
1248
+ if (!preserveState) {
1249
+ this._open.commit();
1250
+ }
1251
+ }
1252
+
1253
+ /**
1254
+ * Closes the disclosure.
1255
+ *
1256
+ * Sets the disclosure's focus state to "none", calls collapse, and sets isOpen to `false`.
1257
+ *
1258
+ * @public
1259
+ *
1260
+ * @param {Object<boolean>} [options = {}] - Options for closing the disclosure.
1261
+ * @param {boolean} [options.force = false] - Whether to force the close action.
1262
+ * @param {boolean} [options.preserveState = false] - Whether to preserve the open state.
1263
+ */
1264
+ close({ force = false, preserveState = false } = {}) {
1265
+ if (!this.isOpen && !force) return;
1266
+
1267
+ // Set the focus state.
1268
+ this.focusState = "none";
1269
+
1270
+ // Collapse the disclosure.
1271
+ this._collapse();
1272
+
1273
+ // Set the open state.
1274
+ this._open.value = false;
1275
+
1276
+ if (!preserveState) {
1277
+ this._open.commit();
1278
+ }
1279
+ }
1280
+
1281
+ /**
1282
+ * Toggles the open state of the disclosure.
1283
+ *
1284
+ * @public
1285
+ *
1286
+ * @param {Object<boolean>} [options = {}] - Options for toggling the disclosure.
1287
+ * @param {boolean} [options.force = false] - Whether to force the open or close action.
1288
+ * @param {boolean} [options.preserveState = false] - Whether to preserve the open state.
1289
+ */
1290
+ toggle({ force = false, preserveState = false } = {}) {
1291
+ if (this.isOpen) {
1292
+ this.close({ force, preserveState });
1293
+ } else {
1294
+ this.open({ force, preserveState });
1295
+ }
1296
+ }
1297
+ }
1298
+
1299
+ export default Disclosure;