@ulu/frontend 0.1.0-beta.19 → 0.1.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 (169) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/ulu-frontend.min.css +1 -1
  3. package/dist/ulu-frontend.min.js +12 -12
  4. package/docs-dev/changelog/index.html +196 -2
  5. package/docs-dev/demos/accordion/index.html +129 -2
  6. package/docs-dev/demos/basic-hero/index.html +111 -0
  7. package/docs-dev/demos/button/index.html +129 -2
  8. package/docs-dev/demos/callout/index.html +129 -2
  9. package/docs-dev/demos/captioned-figure/index.html +129 -2
  10. package/docs-dev/demos/card/index.html +129 -2
  11. package/docs-dev/demos/css-icons/index.html +129 -2
  12. package/docs-dev/demos/data-grid/index.html +129 -2
  13. package/docs-dev/demos/data-table/index.html +154 -27
  14. package/docs-dev/demos/details-group/index.html +129 -2
  15. package/docs-dev/demos/file-save/index.html +129 -2
  16. package/docs-dev/demos/flipcard/index.html +129 -2
  17. package/docs-dev/demos/form-theme/index.html +129 -2
  18. package/docs-dev/demos/hero/index.html +12 -4
  19. package/docs-dev/demos/image-grid/index.html +12 -4
  20. package/docs-dev/demos/index.html +129 -2
  21. package/docs-dev/demos/list-inline/index.html +129 -2
  22. package/docs-dev/demos/list-lines/index.html +129 -2
  23. package/docs-dev/demos/menu-stack/index.html +129 -2
  24. package/docs-dev/demos/modals/index.html +129 -2
  25. package/docs-dev/demos/nav-strip/index.html +129 -2
  26. package/docs-dev/demos/overlay-section/index.html +129 -2
  27. package/docs-dev/demos/popovers/index.html +129 -2
  28. package/docs-dev/demos/print/index.html +129 -2
  29. package/docs-dev/demos/pull-quote/index.html +129 -2
  30. package/docs-dev/demos/rule/index.html +129 -2
  31. package/docs-dev/demos/scroll-slider/index.html +12 -4
  32. package/docs-dev/demos/scrollpoints/index.html +129 -2
  33. package/docs-dev/demos/slider/index.html +12 -4
  34. package/docs-dev/demos/spoke-spinner/index.html +129 -2
  35. package/docs-dev/demos/tabs/index.html +129 -2
  36. package/docs-dev/demos/tag/index.html +129 -2
  37. package/docs-dev/demos/theme-toggle/index.html +5039 -0
  38. package/docs-dev/demos/tile-grid-overlay/index.html +12 -4
  39. package/docs-dev/demos/tiles/index.html +129 -2
  40. package/docs-dev/demos/tooltip/index.html +129 -2
  41. package/docs-dev/guide/building-stylesheet/index.html +129 -2
  42. package/docs-dev/guide/developing-ulu-scss-module/index.html +129 -2
  43. package/docs-dev/guide/index.html +129 -2
  44. package/docs-dev/index.html +129 -2
  45. package/docs-dev/javascript/events/index.html +129 -2
  46. package/docs-dev/javascript/index.html +129 -2
  47. package/docs-dev/javascript/settings/index.html +129 -2
  48. package/docs-dev/javascript/ui-breakpoints/index.html +129 -2
  49. package/docs-dev/javascript/ui-collapsible/index.html +129 -2
  50. package/docs-dev/javascript/ui-details-group/index.html +129 -2
  51. package/docs-dev/javascript/ui-dialog/index.html +129 -2
  52. package/docs-dev/javascript/ui-flipcard/index.html +129 -2
  53. package/docs-dev/javascript/ui-grid/index.html +129 -2
  54. package/docs-dev/javascript/ui-modal-builder/index.html +129 -2
  55. package/docs-dev/javascript/ui-overflow-scroller/index.html +129 -2
  56. package/docs-dev/javascript/ui-overflow-scroller-pager/index.html +129 -2
  57. package/docs-dev/javascript/ui-page/index.html +129 -2
  58. package/docs-dev/javascript/ui-popover/index.html +129 -2
  59. package/docs-dev/javascript/ui-print/index.html +129 -2
  60. package/docs-dev/javascript/ui-print-details/index.html +129 -2
  61. package/docs-dev/javascript/ui-programmatic-modal/index.html +129 -2
  62. package/docs-dev/javascript/ui-proxy-click/index.html +129 -2
  63. package/docs-dev/javascript/ui-resizer/index.html +129 -2
  64. package/docs-dev/javascript/ui-scroll-slider/index.html +129 -2
  65. package/docs-dev/javascript/ui-scrollpoint/index.html +129 -2
  66. package/docs-dev/javascript/ui-slider/index.html +129 -2
  67. package/docs-dev/javascript/ui-tabs/index.html +129 -2
  68. package/docs-dev/javascript/ui-theme-toggle/index.html +5178 -0
  69. package/docs-dev/javascript/ui-tooltip/index.html +129 -2
  70. package/docs-dev/javascript/utils-class-logger/index.html +129 -2
  71. package/docs-dev/javascript/utils-dom/index.html +193 -6
  72. package/docs-dev/javascript/utils-file-save/index.html +129 -2
  73. package/docs-dev/javascript/utils-floating-ui/index.html +129 -2
  74. package/docs-dev/javascript/utils-id/index.html +129 -2
  75. package/docs-dev/javascript/utils-pause-youtube-video/index.html +129 -2
  76. package/docs-dev/sass/base/color/index.html +129 -2
  77. package/docs-dev/sass/base/elements/index.html +129 -2
  78. package/docs-dev/sass/base/index/index.html +129 -2
  79. package/docs-dev/sass/base/index.html +129 -2
  80. package/docs-dev/sass/base/keyframes/index.html +129 -2
  81. package/docs-dev/sass/base/layout/index.html +129 -2
  82. package/docs-dev/sass/base/normalize/index.html +129 -2
  83. package/docs-dev/sass/base/print/index.html +129 -2
  84. package/docs-dev/sass/base/root/index.html +129 -2
  85. package/docs-dev/sass/base/typography/index.html +129 -2
  86. package/docs-dev/sass/components/accordion/index.html +129 -2
  87. package/docs-dev/sass/components/adaptive-spacing/index.html +129 -2
  88. package/docs-dev/sass/components/badge/index.html +129 -2
  89. package/docs-dev/sass/components/basic-hero/index.html +5145 -0
  90. package/docs-dev/sass/components/button/index.html +129 -2
  91. package/docs-dev/sass/components/button-verbose/index.html +129 -2
  92. package/docs-dev/sass/components/callout/index.html +129 -2
  93. package/docs-dev/sass/components/captioned-figure/index.html +129 -2
  94. package/docs-dev/sass/components/card/index.html +129 -2
  95. package/docs-dev/sass/components/card-grid/index.html +129 -2
  96. package/docs-dev/sass/components/css-icon/index.html +129 -2
  97. package/docs-dev/sass/components/data-grid/index.html +129 -2
  98. package/docs-dev/sass/components/data-table/index.html +129 -2
  99. package/docs-dev/sass/components/fill-context/index.html +129 -2
  100. package/docs-dev/sass/components/flipcard/index.html +129 -2
  101. package/docs-dev/sass/components/flipcard-grid/index.html +129 -2
  102. package/docs-dev/sass/components/form-theme/index.html +133 -6
  103. package/docs-dev/sass/components/hero/index.html +129 -2
  104. package/docs-dev/sass/components/horizontal-rule/index.html +129 -2
  105. package/docs-dev/sass/components/image-grid/index.html +129 -2
  106. package/docs-dev/sass/components/index/index.html +140 -12
  107. package/docs-dev/sass/components/index.html +129 -2
  108. package/docs-dev/sass/components/links/index.html +129 -2
  109. package/docs-dev/sass/components/list-inline/index.html +129 -2
  110. package/docs-dev/sass/components/list-lines/index.html +129 -2
  111. package/docs-dev/sass/components/list-ordered/index.html +129 -2
  112. package/docs-dev/sass/components/list-unordered/index.html +129 -2
  113. package/docs-dev/sass/components/menu-stack/index.html +129 -2
  114. package/docs-dev/sass/components/modal/index.html +129 -2
  115. package/docs-dev/sass/components/nav-strip/index.html +129 -2
  116. package/docs-dev/sass/components/overlay-section/index.html +129 -2
  117. package/docs-dev/sass/components/pager/index.html +129 -2
  118. package/docs-dev/sass/components/placeholder-block/index.html +129 -2
  119. package/docs-dev/sass/components/popover/index.html +129 -2
  120. package/docs-dev/sass/components/pull-quote/index.html +129 -2
  121. package/docs-dev/sass/components/ratio-box/index.html +129 -2
  122. package/docs-dev/sass/components/rule/index.html +129 -2
  123. package/docs-dev/sass/components/scroll-slider/index.html +129 -2
  124. package/docs-dev/sass/components/skip-link/index.html +129 -2
  125. package/docs-dev/sass/components/slider/index.html +129 -2
  126. package/docs-dev/sass/components/spoke-spinner/index.html +129 -2
  127. package/docs-dev/sass/components/tabs/index.html +129 -2
  128. package/docs-dev/sass/components/tag/index.html +129 -2
  129. package/docs-dev/sass/components/tile-button/index.html +129 -2
  130. package/docs-dev/sass/components/tile-grid/index.html +129 -2
  131. package/docs-dev/sass/components/tile-grid-overlay/index.html +129 -2
  132. package/docs-dev/sass/components/vignette/index.html +129 -2
  133. package/docs-dev/sass/components/wysiwyg/index.html +129 -2
  134. package/docs-dev/sass/core/breakpoint/index.html +129 -2
  135. package/docs-dev/sass/core/button/index.html +129 -2
  136. package/docs-dev/sass/core/color/index.html +129 -2
  137. package/docs-dev/sass/core/cssvar/index.html +129 -2
  138. package/docs-dev/sass/core/element/index.html +129 -2
  139. package/docs-dev/sass/core/index.html +129 -2
  140. package/docs-dev/sass/core/layout/index.html +129 -2
  141. package/docs-dev/sass/core/path/index.html +129 -2
  142. package/docs-dev/sass/core/selector/index.html +129 -2
  143. package/docs-dev/sass/core/typography/index.html +129 -2
  144. package/docs-dev/sass/core/units/index.html +129 -2
  145. package/docs-dev/sass/core/utils/index.html +129 -2
  146. package/docs-dev/sass/helpers/color/index.html +129 -2
  147. package/docs-dev/sass/helpers/display/index.html +129 -2
  148. package/docs-dev/sass/helpers/index/index.html +129 -2
  149. package/docs-dev/sass/helpers/index.html +129 -2
  150. package/docs-dev/sass/helpers/print/index.html +129 -2
  151. package/docs-dev/sass/helpers/typography/index.html +129 -2
  152. package/docs-dev/sass/helpers/units/index.html +129 -2
  153. package/docs-dev/sass/helpers/utilities/index.html +129 -2
  154. package/docs-dev/sass/index.html +129 -2
  155. package/js/ui/details-group.js +1 -1
  156. package/js/ui/theme-toggle.js +330 -89
  157. package/js/utils/dom.js +43 -1
  158. package/package.json +3 -2
  159. package/scss/components/_basic-hero.scss +112 -0
  160. package/scss/components/_form-theme.scss +2 -2
  161. package/scss/components/_index.scss +6 -0
  162. package/types/ui/details-group.d.ts +43 -0
  163. package/types/ui/details-group.d.ts.map +1 -0
  164. package/types/ui/index.d.ts +1 -0
  165. package/types/ui/programmatic-modal.d.ts.map +1 -1
  166. package/types/ui/theme-toggle.d.ts +58 -7
  167. package/types/ui/theme-toggle.d.ts.map +1 -1
  168. package/types/utils/dom.d.ts +19 -1
  169. package/types/utils/dom.d.ts.map +1 -1
@@ -1,129 +1,370 @@
1
- // Progressive Enhancement turns select elements into accessible autocomplete fields
1
+ /**
2
+ * @module ui/theme-toggle
3
+ */
2
4
 
3
- // import { getName } from "@ulu/frontend/js/events/index.js";
4
5
  import { getName } from "../events/index.js";
6
+ import { getDatasetJson, getElements, resolveClasses } from "../utils/dom.js";
7
+ import { hasRequiredProps } from "@ulu/utils/object.js";
5
8
 
6
- const attrs = {
7
- trigger: "data-site-theme-toggle",
8
- icon: "data-site-theme-toggle-icon",
9
- init: "data-site-theme-toggle-init",
9
+ /**
10
+ * Default data attributes
11
+ */
12
+ export const attrs = {
13
+ init: "data-ulu-theme-toggle-init",
14
+ toggle: "data-ulu-theme-toggle",
15
+ toggleIcon: "data-ulu-theme-toggle-icon",
16
+ toggleLabel: "data-ulu-theme-toggle-label",
17
+ toggleRemote: "data-ulu-theme-toggle-remote",
18
+ state: "data-ulu-theme-toggle-state",
10
19
  };
11
20
 
21
+ // Utils for selecting things based on attributes
12
22
  const attrSelector = key => `[${ attrs[key] }]`;
13
23
  const attrSelectorInitial = key => `${ attrSelector(key) }:not([${ attrs.init }])`;
24
+ const queryAllInitial = key => document.querySelectorAll(attrSelectorInitial(key));
25
+ const queryRemotes = group => document.querySelectorAll(
26
+ `[${ attrs.toggleRemote }="${ group }"]`
27
+ );
28
+ const queryRemotesInitial = group => document.querySelectorAll(
29
+ `[${ attrs.toggleRemote }="${ group }"]:not([${ attrs.init }])`
30
+ );
31
+ const debugLog = (...msgs) => console.log("Theme Toggle:", ...msgs);
32
+ const requiredToggleProps = ["target"];
33
+ const checkToggleProps = hasRequiredProps(requiredToggleProps);
34
+ const when = (cond, fn) => cond ? fn() : null; // Consider adding as util
14
35
 
15
- // @dan change to options and remove options
16
- // add a preferred print theme option
17
- export const options = {
18
- darkTheme: "theme-dark",
19
- lightTheme: "theme-light",
20
- defaultTheme: "dark",
21
- darkIcon: "fa-solid fa-moon",
22
- lightIcon: "fa-solid fa-sun",
36
+ /**
37
+ * Default Options
38
+ * - Can be overridden using data-attributes
39
+ */
40
+ export const defaults = {
41
+ /**
42
+ * Object of each theme that should be toggle/cycled through
43
+ */
44
+ themes: {
45
+ light: {
46
+ label: "Light",
47
+ value: "light",
48
+ iconClass: "fas fa-moon",
49
+ targetClass: "theme-light",
50
+ mediaQuery: "(prefers-color-scheme: light)"
51
+ },
52
+ dark: {
53
+ label: "Dark",
54
+ iconClass: "fas fa-sun",
55
+ targetClass: "theme-dark",
56
+ mediaQuery: "(prefers-color-scheme: dark)"
57
+ }
58
+ },
59
+ /**
60
+ * Required this is the element(s) that should be changed by a specific toggle
61
+ * - The element should have data-ulu-theme-toggle-target="SOME_IDENTIFIER"
62
+ */
63
+ target: "body",
64
+ /**
65
+ * Optional group to link remote toggles (toggles that follow the main one and can toggle too)
66
+ */
67
+ group: null,
68
+ /**
69
+ * Optional callback to do something when the state changes
70
+ */
71
+ onChange(_ctx) {},
72
+ /**
73
+ * The initial state for this component
74
+ * - May be overridden by saved preference or media query if options are enabled
75
+ */
76
+ initialState: "light",
77
+ /**
78
+ * Check the OS systems user preference via 'preferenceQuery' option
79
+ */
80
+ checkMediaQuery: false,
81
+ /**
82
+ * Will store the preference in local storage so it persists between page loads
83
+ */
84
+ savePreference: false,
85
+ /**
86
+ * The key that will be used to store the preference in local storage
87
+ * - This will be used as prefix in combination with group if defined
88
+ */
89
+ storagePrefix: "ulu-theme-",
90
+ /**
91
+ * Output information to console for debugging
92
+ */
93
+ debug: false
23
94
  };
24
95
 
25
- const body = document.querySelector("[data-site-theme]");
26
- let currentTheme = body.classList.contains(options.darkTheme) ? options.darkTheme : options.lightTheme;
27
- // used to see if machine preference differs from default theme
28
- const defaultThemeInverse = options.defaultTheme === "dark" ? "light" : "dark";
96
+
97
+ // Current default objects (user can override these)
98
+ let currentDefaults = { ...defaults };
99
+
100
+ /**
101
+ * @param {Object} options Change options used as default for dialogs, can then be overridden by data attribute settings on element
102
+ */
103
+ export function setDefaults(options) {
104
+ currentDefaults = Object.assign({}, currentDefaults, options);
105
+ }
29
106
 
30
107
  /**
31
108
  * Initialize everything in document
32
109
  * - This will only initialize elements once, it is safe to call on page changes
33
110
  */
34
111
  export function init() {
35
- // switch to light theme for printing
36
- document.addEventListener(getName("beforePrint"), () => printSetup());
37
- // switch back to original theme after printing
38
- document.addEventListener(getName("afterPrint"), () => printTearDown());
39
- // document.addEventListener(getName("pageModified"), () => setup());
112
+ document.addEventListener(getName("pageModified"), setup);
40
113
  setup();
41
114
  }
42
115
 
43
- export function setup(context = document) {
44
- const body = context.querySelector("[data-site-theme]");
45
- // Initial theme on load
46
- setupTheme(body);
47
- // Add toggle event listener to buttons
48
- // @daniel add the init attribute
49
- const elements = context.querySelectorAll(attrSelectorInitial("trigger"));
50
- elements.forEach(element => {
51
- element.setAttribute(attrs.init, "");
52
- element.addEventListener("click", () => {
53
- changeTheme(body);
116
+ /**
117
+ * Query and setup all
118
+ */
119
+ export function setup() {
120
+ queryAllInitial("toggle").forEach(setupToggle);
121
+ }
122
+
123
+ /**
124
+ * Sets up a single toggle
125
+ * @param {HTMLElement} toggle A toggle to be setup
126
+ */
127
+ export function setupToggle(toggle, passedOptions) {
128
+ const elementOptions = getDatasetJson(toggle, "uluThemeToggle");
129
+ const options = Object.assign({}, defaults, passedOptions, elementOptions);
130
+
131
+ if (!checkToggleProps(options)) {
132
+ console.error(`Missing a required option: ${ requiredToggleProps.join(", ") }`);
133
+ return;
134
+ }
135
+
136
+ const group = options.group;
137
+ const ctx = { toggle, options };
138
+ const initialKey = resolveInitial(options);
139
+
140
+ if (!initialKey) {
141
+ console.error("Unable to resolve initial key");
142
+ return;
143
+ }
144
+
145
+ setState(initialKey, ctx);
146
+
147
+ toggle.addEventListener("click", onToggleClick);
148
+ toggle.setAttribute(attrs.init, "");
149
+
150
+ // Remotes listeners are attached initially and then we also
151
+ // update them vs toggles which would be updated by the main pageModified
152
+ // event in init
153
+ attachRemotes();
154
+ document.addEventListener(getName("pageModified"), attachRemotes);
155
+
156
+ /**
157
+ * Instance function to get the next theme in cycle
158
+ */
159
+ function toggleState(event) {
160
+ const targets = getElements(options.target);
161
+ const lastKey = targets[0].dataset.uluThemeToggleState;
162
+ const key = getNextThemeKey(lastKey, options);
163
+ if (!key) {
164
+ console.error("Issue getting next theme key");
165
+ return;
166
+ }
167
+ setState(key, { ...ctx, event });
168
+ }
169
+
170
+ /**
171
+ * Handler for click for both toggle and remote toggles
172
+ */
173
+ function onToggleClick(event) {
174
+ toggleState(event);
175
+ }
176
+
177
+ /**
178
+ * Utility to attach remote handlers
179
+ * - Used initially and when page is modified
180
+ */
181
+ function attachRemotes() {
182
+ if (!group) return;
183
+ const remotes = queryRemotesInitial(group);
184
+ remotes.forEach(remote => {
185
+ remote.addEventListener("click", onToggleClick);
186
+ remote.setAttribute(attrs.init, "");
54
187
  });
55
- });
56
- // Initial icon setup
57
- changeIcons();
188
+ }
189
+
190
+ /**
191
+ * This only cleans up remotes that are still in DOM
192
+ * - For ones that have been removed we don't store any references to them
193
+ */
194
+ function cleanupRemotes() {
195
+ if (!group) return;
196
+ const remotes = queryRemotesInitial(group);
197
+ remotes.forEach(remote => {
198
+ remote.removeEventListener("click", onToggleClick);
199
+ remote.removeAttribute(attrs.init, "");
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Function to cleanup listeners and remove init attributes
205
+ */
206
+ function destroy() {
207
+ toggle.removeEventListener("click", onToggleClick);
208
+ toggle.removeAttribute(attrs.init, "");
209
+ cleanupRemotes();
210
+ document.removeEventListener(getName("pageModified"), attachRemotes);
211
+ }
212
+
213
+ return {
214
+ destroy,
215
+ toggle,
216
+ options,
217
+ toggleState,
218
+ setState(themeKey) {
219
+ setState(themeKey, ctx);
220
+ }
221
+ };
58
222
  }
59
223
 
224
+
225
+
60
226
  /**
61
- *
62
- * @param {Element} body Sets up initial theme on load based on user preference.
227
+ * Change the state of target/toggle
63
228
  */
64
- function setupTheme(body) {
65
- const sitePreference = localStorage.getItem("data-theme");
66
- const machinePreference = window.matchMedia && window.matchMedia(`(prefers-color-scheme: ${defaultThemeInverse})`).matches;
67
- if(sitePreference && sitePreference != currentTheme){
68
- // Check if local storage has site specific preference. And that preference is not the default.
69
- changeTheme(body);
70
- } else if (machinePreference) {
71
- // Check if user system preference differs from default theme.
72
- changeTheme(body);
73
- }
229
+ function setState(key, ctx) {
230
+ if (!key) {
231
+ console.error("Missing key");
232
+ return;
233
+ }
234
+
235
+ const { toggle, options } = ctx;
236
+ const { themes, group } = options;
237
+ const elements = {
238
+ targets: getElements(options.target),
239
+ toggles: [toggle, ...(group ? queryRemotes(group) : [])]
240
+ };
241
+
242
+ if (!elements.targets.length || !elements.toggles.length) {
243
+ console.error("Issue setting state, couldn't find needed elements", elements);
244
+ return;
245
+ }
246
+
247
+ const theme = themes[key];
248
+ const otherThemes = getOtherThemes(key, themes);
249
+ const stateCtx = {
250
+ ...ctx,
251
+ key,
252
+ elements,
253
+ theme,
254
+ otherThemes
255
+ };
256
+
257
+ if (options.debug) {
258
+ debugLog("set state context", stateCtx);
259
+ }
260
+
261
+ // Prepare classes to remove
262
+ const otherTargetClasses = concatThemeClasses(otherThemes, "targetClass");
263
+ const otherIconClasses = concatThemeClasses(otherThemes, "iconClass");
264
+
265
+ // Update all targets
266
+ elements.targets.forEach(element => {
267
+ element.setAttribute(attrs.state, key);
268
+ element.classList.remove(...otherTargetClasses);
269
+ element.classList.add(...resolveClasses(theme.targetClass));
270
+ });
271
+
272
+ // Update all toggles and inner children
273
+ elements.toggles.forEach(element => {
274
+ const label = element.querySelector(attrSelector("toggleLabel"));
275
+ const icon = element.querySelector(attrSelector("toggleIcon"));
276
+ if (label) {
277
+ label.textContent = theme.label;
278
+ }
279
+ if (icon) {
280
+ icon.classList.remove(...otherIconClasses);
281
+ icon.classList.add(...resolveClasses(theme.iconClass));
282
+ }
283
+ element.setAttribute(attrs.state, key);
284
+ });
285
+
286
+ // Optional callback if user want to set other things (ie. data-theme or something)
287
+ if (options.onChange) {
288
+ options.onChange(stateCtx);
289
+ }
290
+
291
+ if (options.savePreference) {
292
+ localStorage.setItem(getStorageKey(options), key);
293
+ }
74
294
  }
75
295
 
76
296
  /**
77
- *
78
- * @param {Element} body Changes the theme of the body.
297
+ * Function determines what the initial state is
298
+ * - Check OS preference, saved preference, or initialState depending on options
299
+ * @return {String} The resolved initial theme's key
79
300
  */
80
- function changeTheme(body) {
81
- let newTheme;
82
- let oldTheme;
83
- if (body.classList.contains(options.darkTheme)) {
84
- oldTheme = options.darkTheme;
85
- newTheme = options.lightTheme;
86
- } else if (body.classList.contains(options.lightTheme)) {
87
- oldTheme = options.lightTheme;
88
- newTheme = options.darkTheme;
301
+ function resolveInitial(options) {
302
+ const { savePreference, checkMediaQuery, themes, initialState } = options;
303
+ const storageKey = getStorageKey(options);
304
+ const saved = when(savePreference, () => localStorage.getItem(storageKey));
305
+ const mediaQueryPreference = when(checkMediaQuery, () => getMatchingThemeQuery(themes));
306
+ const resolved = saved || mediaQueryPreference || initialState;
307
+
308
+ if (options.debug) {
309
+ debugLog("Preference Saved:", saved);
310
+ debugLog("Media Query Preference:", mediaQueryPreference);
311
+ debugLog("Initial State:", initialState);
89
312
  }
90
- body.classList.remove(oldTheme);
91
- body.classList.add(newTheme);
92
- localStorage.setItem("data-theme", newTheme);
93
- currentTheme = newTheme;
94
- changeIcons();
313
+
314
+ if (!resolved) {
315
+ console.error("Failed to resolve initial theme (pass 'initialState' to options)");
316
+ }
317
+
318
+ return resolved;
95
319
  }
96
320
 
97
321
  /**
98
- *
99
- * @param {Element} body Used to check for theme.
100
- * @param {Element} context Used to find the icons.
322
+ * Check each theme for a matching media query
323
+ * @return {String} Matching theme key
101
324
  */
102
- function changeIcons(context = document) {
103
- const icons = context.querySelectorAll(attrSelectorInitial("icon"));
104
- icons.forEach(icon => {
105
- if (currentTheme == options.lightTheme) {
106
- icon.classList = options.darkIcon;
107
- } else {
108
- icon.classList = options.lightIcon;
325
+ function getMatchingThemeQuery(themes) {
326
+ const found = Object.entries(themes).find(([_key, theme]) => {
327
+ if (theme.mediaQuery) {
328
+ return window.matchMedia(theme.mediaQuery).matches;
109
329
  }
110
330
  });
331
+ // Return just the key
332
+ return found ? found[0] : null;
111
333
  }
112
334
 
113
- // run on beforeprint event
114
- function printSetup() {
115
- const body = document.querySelector("body");
116
- if (body.classList.contains(options.darkTheme)) {
117
- body.classList.remove(options.darkTheme);
118
- body.classList.add(options.lightTheme);
119
- }
335
+ /**
336
+ * Get the next key in the themes based on the currentKey
337
+ */
338
+ function getNextThemeKey(activeKey, options) {
339
+ const { themes } = options;
340
+ const keys = Object.keys(themes);
341
+ const index = keys.findIndex(theme => theme === activeKey);
342
+ // If not found return first, else calculate next index (wrapping)
343
+ const nextIndex = index === -1 ? 0 : (index + 1) % keys.length;
344
+ return keys[nextIndex];
120
345
  }
121
346
 
122
- // run on afterprint event
123
- function printTearDown() {
124
- const body = document.querySelector("body");
125
- if (!body.classList.contains(currentTheme)) {
126
- body.classList.remove(options.lightTheme);
127
- body.classList.add(options.darkTheme);
128
- }
347
+ /**
348
+ * Get all other theme object except the current
349
+ */
350
+ function getOtherThemes(currentKey, themes) {
351
+ const all = Object.entries(themes);
352
+ return all.filter(([key]) => key !== currentKey).map(([_key, value]) => value);
353
+ }
354
+
355
+ /**
356
+ * Concatenates multiple class properties into one array
357
+ */
358
+ function concatThemeClasses(themes, property) {
359
+ return themes.reduce((acc, theme) => {
360
+ return acc.concat(resolveClasses(theme[property]));
361
+ }, []);
362
+ }
363
+
364
+ /**
365
+ * Creates the storage key (either prefix or prefix with group name)
366
+ */
367
+ function getStorageKey(options) {
368
+ const { storagePrefix, group } = options;
369
+ return group ? `${ storagePrefix }${ group }` : storagePrefix;
129
370
  }
package/js/utils/dom.js CHANGED
@@ -98,6 +98,7 @@ export function setPositionClasses(parent, classes = {
98
98
  * Resolve a target to Element
99
99
  * @param {String|Node} target The selector or node/element
100
100
  * @param {Object} context [document] The context to query possible selectors from
101
+ * @return {HTMLElement} The element or null if not found
101
102
  */
102
103
  export function getElement(target, context = document) {
103
104
  if (typeof target === "string") {
@@ -105,11 +106,52 @@ export function getElement(target, context = document) {
105
106
  } else if (target instanceof Element) {
106
107
  return target;
107
108
  } else {
108
- console.warn("Unable to getElement()", target);
109
+ console.warn("getElement: Invalid target type (expected String/Node)", target);
109
110
  return null;
110
111
  }
111
112
  }
112
113
 
114
+ /**
115
+ * Resolve a target to Elements
116
+ * @param {String|Node} target The selector or node/element
117
+ * @param {Object} context [document] The context to query possible selectors from
118
+ * @return {Array} The elements or null if not found
119
+ */
120
+ export function getElements(target, context = document) {
121
+ if (typeof target === "string") {
122
+ return [...context.querySelectorAll(target)];
123
+ } else if (target instanceof Element) {
124
+ return [target];
125
+ } else if (Array.isArray(target) || target instanceof NodeList) {
126
+ return [...target];
127
+ } else {
128
+ console.warn("getElement: Invalid target type (expected String/Node/Array/Node List)", target);
129
+ return null;
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Resolves a class input (string or array) into a consistent array of class names.
135
+ * @param {string|string[]} input - The class input, which can be a string, an array of strings, or any other value.
136
+ * @returns {string[]} An array of class names. Returns an empty array for invalid or falsy input.
137
+ * @example
138
+ * resolveClassArray("fas fa-check my-class"); // Returns ["fas", "fa-check", "my-class"]
139
+ * resolveClassArray(["another-class", "yet-another-class"]); // Returns ["another-class", "yet-another-class"]
140
+ * resolveClassArray("single-class"); // Returns ["single-class"]
141
+ */
142
+ export function resolveClasses(classes) {
143
+ if (typeof classes === "string") {
144
+ return classes.split(" ").filter(c => c !== ""); // Split and remove empty strings
145
+ } else if (Array.isArray(classes)) {
146
+ return classes;
147
+ } else if (!classes) {
148
+ return [];
149
+ } else {
150
+ console.warn("resolveClassArray: Invalid class input type.", classes);
151
+ return [];
152
+ }
153
+ }
154
+
113
155
  /**
114
156
  * Sets a CSS custom property equal to the scrollbar width
115
157
  * @param {Node} element The element that is the child of a scrollabel container
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ulu/frontend",
3
- "version": "0.1.0-beta.19",
3
+ "version": "0.1.0-beta.20",
4
4
  "description": "Modular Sass Theming Library",
5
5
  "browser": "js/index.js",
6
6
  "main": "index.js",
@@ -30,7 +30,8 @@
30
30
  "docs:build:prod": "IS_PRODUCTION=true npx @11ty/eleventy --config=docs.eleventy.js",
31
31
  "docs:assets": "vite --config docs.vite.config.js --force",
32
32
  "docs:assets:build": "vite build --config docs.vite.config.js",
33
- "docs:assets:build:prod": "IS_PRODUCTION=true vite build --config docs.vite.config.js"
33
+ "docs:assets:build:prod": "IS_PRODUCTION=true vite build --config docs.vite.config.js",
34
+ "publish" : "npm run build && npm run types && npm run docs:build:prod && npm run docs:assets:build:prod"
34
35
  },
35
36
  "repository": {
36
37
  "type": "git",
@@ -0,0 +1,112 @@
1
+ ////
2
+ /// @group basic-hero
3
+ /// Basic styling for a hero
4
+ ////
5
+
6
+ @use "sass:map";
7
+ @use "sass:math";
8
+ @use "sass:meta";
9
+ @use "sass:list";
10
+
11
+ @use "../breakpoint";
12
+ @use "../selector";
13
+ @use "../utils";
14
+ @use "../typography";
15
+ @use "../color";
16
+
17
+ // Used for function fallback
18
+ $-fallbacks: (
19
+ "main-max-width" : (
20
+ "function" : meta.get-function("get", false, "typography"),
21
+ "property" : "max-width"
22
+ ),
23
+ "main-min-width" : (
24
+ "function" : meta.get-function("get", false, "typography"),
25
+ "property" : "max-width-small"
26
+ ),
27
+ );
28
+
29
+ /// Module Settings
30
+ /// @type Map
31
+ /// @prop {CssValue} text-align [center] Alignment of text within hero.
32
+ /// @prop {Color} background-color ["color-hero-background"] Background color of the hero
33
+
34
+ $config: (
35
+ "background-color" : "background-gray",
36
+ "padding-top": 3rem,
37
+ "padding-bottom" : 2rem,
38
+ "gap" : (4rem 2rem),
39
+ "main-max-width" : true,
40
+ "main-min-width" : true,
41
+ "media-max-width" : 20rem,
42
+ "media-vertical-align" : center
43
+ ) !default;
44
+
45
+ /// Change modules $config
46
+ /// @param {Map} $changes Map of changes
47
+ /// @example scss
48
+ /// @include ulu.component-basic-hero-set(( "property" : value ));
49
+
50
+ @mixin set($changes) {
51
+ $config: map.merge($config, $changes) !global;
52
+ }
53
+
54
+ /// Get a config option
55
+ /// @param {Map} $name Name of property
56
+ /// @example scss
57
+ /// @include ulu.component-basic-hero-get("property");
58
+
59
+ @function get($name) {
60
+ $value: utils.require-map-get($config, $name, "basic-hero [config]");
61
+ @return utils.function-fallback($name, $value, $-fallbacks);
62
+ }
63
+
64
+ /// Prints component styles
65
+ /// @example scss
66
+ /// @include ulu.component-basic-hero-styles();
67
+
68
+ @mixin styles {
69
+ $prefix: selector.class("basic-hero");
70
+
71
+ #{ $prefix } {
72
+ background-color: color.get(get("background-color"));
73
+ }
74
+ #{ $prefix }__content {
75
+ display: flex;
76
+ align-items: stretch;
77
+ flex-wrap: wrap;
78
+ justify-content: space-between;
79
+ gap: get("gap");
80
+ padding-top: get("padding-top");
81
+ padding-bottom: get("padding-bottom");
82
+ }
83
+ #{ $prefix }__content-main {
84
+ $min-width: if(get("main-min-width"), get("main-min-width"), get("main-max-width"));
85
+ // Using max/min-width so that we don't need to add selectors to change flex-basis
86
+ // when centered. Using flex-basis: 0 to allow flexbox to decide the items width
87
+ // allows it to expand/shrink within. min(100%... Never larger than parent
88
+ min-width: min(100%, $min-width);
89
+ max-width: get("main-max-width");
90
+ flex-shrink: 1;
91
+ flex-grow: 1;
92
+ flex-basis: 0;
93
+ }
94
+ #{ $prefix }__content-media {
95
+ display: flex;
96
+ align-items: center;
97
+ align-self: get("media-vertical-align");
98
+ justify-content: center;
99
+ flex: 0 0 auto;
100
+ width: min(100%, get("media-max-width"));
101
+ }
102
+
103
+ // Modifiers
104
+ #{ $prefix }--center {
105
+ #{ $prefix }__content {
106
+ flex-direction: column;
107
+ flex-wrap: nowrap;
108
+ text-align: center;
109
+ align-items: center;
110
+ }
111
+ }
112
+ }
@@ -15,8 +15,8 @@
15
15
 
16
16
  /// Module Settings
17
17
  /// @type Map
18
- /// @prop {} color [inherit]
19
- /// @prop {} color-placeholder ["type-tertiary"]
18
+ /// @prop {CssValue} color [inherit] Color of the text of the form.
19
+ /// @prop {CssValue} color-placeholder ["type-tertiary"] Color for the placeh
20
20
  /// @prop {} drupal [false]
21
21
  /// @prop {} error-color ["danger"]
22
22
  /// @prop {} error-highlight-color [rgb(251, 242, 242)]