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

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graupl/core",
3
- "version": "1.0.0-beta.20",
3
+ "version": "1.0.0-beta.21",
4
4
  "description": "The core module of Graupl, providing essential styles and utilities.",
5
5
  "main": "dist/js/graupl.cjs.js",
6
6
  "module": "dist/js/graupl.es.js",
@@ -120,6 +120,15 @@ class Disclosure {
120
120
  */
121
121
  _open = new TransactionalValue(false);
122
122
 
123
+ /**
124
+ * A value to force the disclosure open when the breakpoint width is passed.
125
+ *
126
+ * @protected
127
+ *
128
+ * @type {boolean}
129
+ */
130
+ _shouldOpen = false;
131
+
123
132
  /**
124
133
  * Whether or not to close the disclosure when it loses focus in the DOM.
125
134
  *
@@ -213,7 +222,8 @@ class Disclosure {
213
222
  * @param {boolean} [options.openDuration = -1] - The duration of the transition from "closed" to "open" states (in milliseconds).
214
223
  * @param {boolean} [options.closeDuration = -1] - The duration of the transition from "open" to "closed" states (in milliseconds).
215
224
  * @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.
225
+ * @param {boolean} [options.minWidth = -1] - The width of the screen (in pixels) that the disclosure will automatically open/close itself.
226
+ * @param {boolean} [options.autoOpen = false] - Whether to automatically open when above the minWidth.
217
227
  * @param {?string} [options.prefix = graupl-] - The prefix to use for CSS custom properties.
218
228
  * @param {?(string|string[])} [options.initializeClass = initializing] - The class to apply when a disclosure is initialzing.
219
229
  * @param {boolean} [options.initialize = false] - Whether to initialize the disclosure upon construction.
@@ -230,6 +240,7 @@ class Disclosure {
230
240
  closeDuration = -1,
231
241
  closeOnBlur = false,
232
242
  minWidth = -1,
243
+ autoOpen = false,
233
244
  prefix = "graupl-",
234
245
  initializeClass = "initializing",
235
246
  initialize = false,
@@ -255,8 +266,9 @@ class Disclosure {
255
266
  // Set close on blur.
256
267
  this._closeOnBlur = closeOnBlur;
257
268
 
258
- // Set collapse width.
269
+ // Set collapse width and auto open functionality.
259
270
  this._breakpointWidth = minWidth;
271
+ this._shouldOpen = autoOpen;
260
272
 
261
273
  // Set the prefix.
262
274
  this._prefix = prefix;
@@ -299,9 +311,13 @@ class Disclosure {
299
311
 
300
312
  // Set up the storage.
301
313
  storage.initializeStorage("disclosures");
314
+ storage.initializeStorage("disclosures");
302
315
  storage.pushToStorage("disclosures", this.dom.disclosure.id, this);
303
316
 
304
- if (this.dom.controller.getAttribute("aria-expanded") === "true") {
317
+ if (
318
+ this.dom.controller.getAttribute("aria-expanded") === "true" ||
319
+ (this.shouldOpen && window.matchMedia(this.openQuery).matches)
320
+ ) {
305
321
  this._expand(false, false);
306
322
  } else {
307
323
  this._collapse(false, false);
@@ -613,6 +629,59 @@ class Disclosure {
613
629
  return this._open.committed;
614
630
  }
615
631
 
632
+ /**
633
+ * A value to force opening reguardless of user interaction.
634
+ *
635
+ * @type {boolean}
636
+ *
637
+ * @see _shouldOpen
638
+ */
639
+ get shouldOpen() {
640
+ return this._shouldOpen;
641
+ }
642
+
643
+ set shouldOpen(value) {
644
+ isValidType("boolean", { value });
645
+
646
+ if (this._shouldOpen !== value) {
647
+ this._shouldOpen = value;
648
+ }
649
+ }
650
+
651
+ /**
652
+ * A media query for when the disclosure should open.
653
+ *
654
+ * Will return an empty string if no min width is set.
655
+ *
656
+ * @readonly
657
+ *
658
+ * @type {string}
659
+ */
660
+ get openQuery() {
661
+ if (this.minWidth === -1) {
662
+ return "";
663
+ }
664
+
665
+ return `(width > ${this.minWidth}px)`;
666
+ }
667
+
668
+ /**
669
+ * A media query for when the disclosure should close.
670
+ *
671
+ * Will return an empty string if no min width is set.
672
+ *
673
+ * @readonly
674
+ *
675
+ * @type {string}
676
+ */
677
+ get closeQuery() {
678
+ if (this.minWidth === -1) {
679
+ return "";
680
+ }
681
+
682
+ return `(width <= ${this.minWidth}px)`;
683
+ }
684
+
616
685
  /**
617
686
  * A flag to check if the disclosure's focus methods should _actually_ move the focus in the DOM.
618
687
  *
@@ -1036,6 +1105,8 @@ class Disclosure {
1036
1105
  return;
1037
1106
  }
1038
1107
 
1108
+ let width = 0;
1109
+
1039
1110
  this._observer = new ResizeObserver((entries) => {
1040
1111
  requestAnimationFrame(() => {
1041
1112
  for (const entry of entries) {
@@ -1049,14 +1120,22 @@ class Disclosure {
1049
1120
 
1050
1121
  if (typeof inlineSize !== "number") continue;
1051
1122
 
1123
+ if (width === inlineSize) continue;
1124
+
1052
1125
  const belowBreakpoint = inlineSize <= this.minWidth;
1053
1126
  const aboveBreakpoint = inlineSize > this.minWidth;
1054
1127
 
1055
1128
  if (belowBreakpoint && this.isOpen) {
1056
1129
  this.close({ preserveState: true });
1057
- } else if (aboveBreakpoint && !this.isOpen && this.hasOpened) {
1130
+ } else if (
1131
+ aboveBreakpoint &&
1132
+ !this.isOpen &&
1133
+ (this.hasOpened || this.shouldOpen)
1134
+ ) {
1058
1135
  this.open();
1059
1136
  }
1137
+
1138
+ width = inlineSize;
1060
1139
  }
1061
1140
  });
1062
1141
  });