@oscarpalmer/oui 0.37.0 → 0.39.0

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 (183) hide show
  1. package/dist/css/a11y.css +1 -0
  2. package/dist/css/badge.css +12 -8
  3. package/dist/css/button.css +111 -14
  4. package/dist/css/color-picker.css +859 -0
  5. package/dist/css/color.css +86 -87
  6. package/dist/css/combobox.css +105 -4
  7. package/dist/css/content.css +8 -6
  8. package/dist/css/floatable.css +3 -3
  9. package/dist/css/form.css +106 -6
  10. package/dist/css/icon.css +99 -0
  11. package/dist/css/listbox.css +5 -0
  12. package/dist/css/message.css +6 -3
  13. package/dist/css/mixins/badge.mixins.css +2 -0
  14. package/dist/css/mixins/button.mixins.css +3 -0
  15. package/dist/css/mixins/message.mixins.css +1 -0
  16. package/dist/css/mixins/misc.mixins.css +2 -0
  17. package/dist/css/mixins/shadow.mixins.css +1 -0
  18. package/dist/css/reset.css +3 -3
  19. package/dist/css/splitter.css +1 -1
  20. package/dist/css/tabs.css +115 -23
  21. package/dist/css/typography.css +90 -0
  22. package/dist/js/color-picker/constants.color-picker.d.mts +2 -0
  23. package/dist/js/color-picker/constants.color-picker.mjs +59 -0
  24. package/dist/js/color-picker/dom.color-picker.d.mts +8 -0
  25. package/dist/js/color-picker/dom.color-picker.mjs +191 -0
  26. package/dist/js/color-picker/event.color-picker.d.mts +9 -0
  27. package/dist/js/color-picker/event.color-picker.mjs +2 -0
  28. package/dist/js/color-picker/form.color-picker.d.mts +5 -0
  29. package/dist/js/color-picker/form.color-picker.mjs +24 -0
  30. package/dist/js/color-picker/index.d.mts +2 -0
  31. package/dist/js/color-picker/index.mjs +303 -0
  32. package/dist/js/color-picker/misc.color-picker.d.mts +2 -0
  33. package/dist/js/color-picker/misc.color-picker.mjs +2 -0
  34. package/dist/js/color-picker/models.color-picker.d.mts +2 -0
  35. package/dist/js/color-picker/models.color-picker.mjs +24 -0
  36. package/dist/js/color-picker/output.color-picker.d.mts +8 -0
  37. package/dist/js/color-picker/output.color-picker.mjs +233 -0
  38. package/dist/js/color-picker/state.color-picker.d.mts +8 -0
  39. package/dist/js/color-picker/state.color-picker.mjs +2 -0
  40. package/dist/js/combobox/constants.combobox.d.mts +1 -1
  41. package/dist/js/combobox/dom.combobox.d.mts +12 -13
  42. package/dist/js/combobox/dom.combobox.mjs +12 -5
  43. package/dist/js/combobox/event.combobox.d.mts +20 -21
  44. package/dist/js/combobox/event.combobox.mjs +1 -1
  45. package/dist/js/combobox/index.d.mts +1 -1
  46. package/dist/js/combobox/index.mjs +56 -2
  47. package/dist/js/combobox/misc.combobox.d.mts +1 -1
  48. package/dist/js/combobox/misc.combobox.mjs +1 -1
  49. package/dist/js/combobox/models.combobox.d.mts +12 -13
  50. package/dist/js/combobox/state.combobox.d.mts +8 -9
  51. package/dist/js/combobox/state.combobox.mjs +1 -1
  52. package/dist/js/constants.color-picker-B-gFjL8m.d.mts +364 -0
  53. package/dist/js/constants.combobox-8EMRmv09.d.mts +18 -0
  54. package/dist/js/constants.listbox-DGnX32kv.d.mts +8 -0
  55. package/dist/js/dialog.d.mts +2 -2
  56. package/dist/js/dialog.mjs +1 -1
  57. package/dist/js/{draggable-a6w0J1OD.d.mts → draggable-B5-70SZm.d.mts} +5 -4
  58. package/dist/js/event.color-picker-BEpq0Adu.mjs +485 -0
  59. package/dist/js/floatable/floatable.embedded.d.mts +1 -1
  60. package/dist/js/floatable/floatable.embedded.mjs +11 -8
  61. package/dist/js/floatable/floatable.standalone.d.mts +1 -1
  62. package/dist/js/{floatable.embedded-Bct4U5Dm.d.mts → floatable.embedded-DQYkXt8X.d.mts} +1 -1
  63. package/dist/js/focus-trap/focus-trap.embedded.d.mts +1 -1
  64. package/dist/js/focus-trap/focus-trap.standalone.d.mts +1 -1
  65. package/dist/js/{focus-trap.embedded-DR8NpZfr.d.mts → focus-trap.embedded-CjyFQYow.d.mts} +2 -2
  66. package/dist/js/{index-EfkUSals.d.mts → index-C5D3-oiI.d.mts} +41 -3
  67. package/dist/js/index.d.mts +8 -7
  68. package/dist/js/index.mjs +6 -3
  69. package/dist/js/internal/attributable.d.mts +2 -3
  70. package/dist/js/internal/draggable.d.mts +1 -1
  71. package/dist/js/internal/draggable.mjs +1 -1
  72. package/dist/js/internal/listbox/listbox.constants.d.mts +7 -10
  73. package/dist/js/internal/listbox/listbox.constants.mjs +15 -16
  74. package/dist/js/internal/listbox/listbox.data.d.mts +9 -10
  75. package/dist/js/internal/listbox/listbox.data.mjs +1 -1
  76. package/dist/js/internal/listbox/listbox.dom.d.mts +19 -20
  77. package/dist/js/internal/listbox/listbox.dom.mjs +1 -1
  78. package/dist/js/internal/listbox/listbox.event.d.mts +3 -4
  79. package/dist/js/internal/listbox/listbox.event.mjs +4 -4
  80. package/dist/js/internal/listbox/listbox.form.d.mts +4 -5
  81. package/dist/js/internal/listbox/listbox.form.mjs +16 -15
  82. package/dist/js/internal/listbox/listbox.misc.d.mts +10 -11
  83. package/dist/js/internal/listbox/listbox.misc.mjs +1 -1
  84. package/dist/js/internal/listbox/listbox.model.d.mts +1 -1
  85. package/dist/js/internal/listbox/listbox.state.d.mts +5 -6
  86. package/dist/js/internal/listbox/listbox.state.mjs +3 -3
  87. package/dist/js/internal/misc.d.mts +4 -4
  88. package/dist/js/internal/misc.mjs +9 -1
  89. package/dist/js/internal/navigable.d.mts +9 -10
  90. package/dist/js/internal/navigable.mjs +1 -0
  91. package/dist/js/listbox/constants.listbox.d.mts +1 -1
  92. package/dist/js/listbox/event.listbox.d.mts +6 -7
  93. package/dist/js/listbox/event.listbox.mjs +1 -1
  94. package/dist/js/listbox/index.d.mts +43 -5
  95. package/dist/js/listbox/index.mjs +56 -2
  96. package/dist/js/listbox/misc.listbox.d.mts +1 -1
  97. package/dist/js/listbox/misc.listbox.mjs +1 -1
  98. package/dist/js/listbox/state.listbox.d.mts +7 -8
  99. package/dist/js/listbox/state.listbox.mjs +1 -1
  100. package/dist/js/{listbox.data-BWd0PCy7.mjs → listbox.data-DQ7-x2uY.mjs} +8 -7
  101. package/dist/js/{listbox.model-CRReGOmu.d.mts → listbox.model-BHPA7GM4.d.mts} +3 -3
  102. package/dist/js/menu.mjs +1 -1
  103. package/dist/js/{misc.listbox-BI-XsTOf.mjs → misc.listbox-DKB-j0UK.mjs} +4 -5
  104. package/dist/js/{misc.listbox-gnoJhrTr.d.mts → misc.listbox-mZMuYOxg.d.mts} +1 -1
  105. package/dist/js/movable/movable.embedded.d.mts +1 -1
  106. package/dist/js/movable/movable.standalone.d.mts +1 -1
  107. package/dist/js/popover.d.mts +2 -2
  108. package/dist/js/popover.mjs +1 -1
  109. package/dist/js/resizable/resizable.embedded.d.mts +1 -1
  110. package/dist/js/resizable/resizable.embedded.mjs +1 -1
  111. package/dist/js/resizable/resizable.standalone.d.mts +1 -1
  112. package/dist/js/{resizable.embedded-3qoR0Bjm.d.mts → resizable.embedded-dvhdfQgt.d.mts} +1 -1
  113. package/dist/js/sortable/sortable.embedded.d.mts +1 -1
  114. package/dist/js/sortable/sortable.standalone.d.mts +1 -1
  115. package/dist/js/splitter.d.mts +2 -3
  116. package/dist/js/splitter.mjs +2 -2
  117. package/dist/js/{state.combobox-4xq0FPCF.mjs → state.combobox-BWDFHwoi.mjs} +9 -9
  118. package/dist/js/tabs.d.mts +4 -5
  119. package/dist/js/tabs.mjs +4 -7
  120. package/dist/js/tooltip.d.mts +1 -1
  121. package/package.json +24 -22
  122. package/dist/js/constants.combobox-DmRAMC64.d.mts +0 -26
  123. package/dist/js/constants.listbox-DRsXUxd3.d.mts +0 -14
  124. package/src/css/a11y.scss +0 -13
  125. package/src/css/badge.scss +0 -62
  126. package/src/css/button.scss +0 -50
  127. package/src/css/color.scss +0 -115
  128. package/src/css/combobox.scss +0 -252
  129. package/src/css/content.scss +0 -180
  130. package/src/css/flex.scss +0 -107
  131. package/src/css/floatable.scss +0 -152
  132. package/src/css/form.scss +0 -573
  133. package/src/css/listbox.scss +0 -120
  134. package/src/css/message.scss +0 -73
  135. package/src/css/mixins.scss +0 -419
  136. package/src/css/reset.scss +0 -392
  137. package/src/css/splitter.scss +0 -89
  138. package/src/css/table.scss +0 -107
  139. package/src/css/tabs.scss +0 -85
  140. package/src/js/combobox/constants.combobox.ts +0 -83
  141. package/src/js/combobox/dom.combobox.ts +0 -407
  142. package/src/js/combobox/event.combobox.ts +0 -365
  143. package/src/js/combobox/index.ts +0 -320
  144. package/src/js/combobox/misc.combobox.ts +0 -104
  145. package/src/js/combobox/models.combobox.ts +0 -54
  146. package/src/js/combobox/state.combobox.ts +0 -246
  147. package/src/js/dialog.ts +0 -255
  148. package/src/js/floatable/floatable.embedded.ts +0 -332
  149. package/src/js/floatable/floatable.standalone.ts +0 -6
  150. package/src/js/focus-trap/focus-trap.embedded.ts +0 -411
  151. package/src/js/focus-trap/focus-trap.standalone.ts +0 -1
  152. package/src/js/index.ts +0 -15
  153. package/src/js/internal/attributable.ts +0 -73
  154. package/src/js/internal/draggable.ts +0 -657
  155. package/src/js/internal/listbox/listbox.constants.ts +0 -25
  156. package/src/js/internal/listbox/listbox.data.ts +0 -287
  157. package/src/js/internal/listbox/listbox.dom.ts +0 -445
  158. package/src/js/internal/listbox/listbox.event.ts +0 -129
  159. package/src/js/internal/listbox/listbox.form.ts +0 -115
  160. package/src/js/internal/listbox/listbox.misc.ts +0 -135
  161. package/src/js/internal/listbox/listbox.model.ts +0 -169
  162. package/src/js/internal/listbox/listbox.state.ts +0 -82
  163. package/src/js/internal/misc.ts +0 -27
  164. package/src/js/internal/navigable.ts +0 -315
  165. package/src/js/l10n.ts +0 -192
  166. package/src/js/listbox/constants.listbox.ts +0 -61
  167. package/src/js/listbox/event.listbox.ts +0 -106
  168. package/src/js/listbox/index.ts +0 -302
  169. package/src/js/listbox/misc.listbox.ts +0 -70
  170. package/src/js/listbox/state.listbox.ts +0 -196
  171. package/src/js/menu.ts +0 -348
  172. package/src/js/movable/movable.embedded.ts +0 -318
  173. package/src/js/movable/movable.standalone.ts +0 -2
  174. package/src/js/popover.ts +0 -229
  175. package/src/js/resizable/resizable.embedded.ts +0 -413
  176. package/src/js/resizable/resizable.standalone.ts +0 -5
  177. package/src/js/sortable/sortable.embedded.ts +0 -527
  178. package/src/js/sortable/sortable.standalone.ts +0 -2
  179. package/src/js/splitter.ts +0 -583
  180. package/src/js/tabs.ts +0 -447
  181. package/src/js/toolbar.ts +0 -63
  182. package/src/js/tooltip.ts +0 -367
  183. /package/dist/css/{mixins.css → mixins/input.mixins.css} +0 -0
@@ -1,315 +0,0 @@
1
- import {clamp} from '@oscarpalmer/atoms/number';
2
- import {getAria, setAria, setRole} from '@oscarpalmer/toretto/aria';
3
- import {getAttribute} from '@oscarpalmer/toretto/attribute';
4
- import {on} from '@oscarpalmer/toretto/event';
5
- import {findAncestor} from '@oscarpalmer/toretto/find';
6
- import {isHTMLOrSVGElement} from '@oscarpalmer/toretto/is';
7
- import type {AriaRole, RemovableEventListener} from '@oscarpalmer/toretto/models';
8
-
9
- // #region Types
10
-
11
- export class OuiNavigable {
12
- active: OuiNavigableActive = {
13
- index: 0,
14
- };
15
-
16
- element: HTMLElement;
17
-
18
- listeners: RemovableEventListener[];
19
-
20
- options: OuiNavigableOptions;
21
-
22
- orientation: OuiNavigableOrientation;
23
-
24
- constructor(element: HTMLElement, options: OuiNavigableOptions) {
25
- this.element = element;
26
- this.options = options;
27
-
28
- this.listeners = [
29
- on(
30
- element,
31
- 'click',
32
- event => {
33
- onClick(this, event);
34
- },
35
- {
36
- passive: false,
37
- },
38
- ),
39
- on(element, 'focusin', event => {
40
- onFocusin(this, event);
41
- }),
42
- on(
43
- element,
44
- 'keydown',
45
- event => {
46
- onKeydown(this, event);
47
- },
48
- {
49
- passive: false,
50
- },
51
- ),
52
- ];
53
-
54
- this.orientation = options?.orientation ?? getNavigableOrientation(element, options.vertical);
55
-
56
- setAria(element, 'orientation', this.orientation);
57
-
58
- if (options.role != null) {
59
- setRole(element, options.role);
60
- }
61
- }
62
-
63
- destroy(): void {
64
- const listeners = this.listeners.splice(0);
65
-
66
- for (const listener of listeners) {
67
- listener();
68
- }
69
-
70
- this.active.element = undefined;
71
- this.element = undefined as never;
72
- }
73
-
74
- navigate(index: number, focus?: boolean): void {
75
- const items = getItems(this.element, this.options.selector);
76
-
77
- if (index >= 0 && index < items.length) {
78
- onNavigate(this, index, items, focus ?? false);
79
- }
80
- }
81
- }
82
-
83
- type OuiNavigableActive = {
84
- element?: HTMLElement;
85
- index: number;
86
- };
87
-
88
- type OuiNavigableCallback<ReturnType> = (
89
- event: KeyboardEvent,
90
- navigable: OuiNavigable,
91
- ) => ReturnType;
92
-
93
- export type OuiNavigableOptions = {
94
- orientation?: OuiNavigableOrientation;
95
- role?: AriaRole;
96
- selector: string;
97
- vertical: boolean;
98
- onActivate?: (navigable: OuiNavigable) => void;
99
- onDeactivate?: OuiNavigableCallback<void>;
100
- onNavigation?: (navigable: OuiNavigable) => void;
101
- onPreventNavigation?: OuiNavigableCallback<boolean>;
102
- onShouldActivate?: OuiNavigableCallback<boolean>;
103
- onShouldDeactivate?: OuiNavigableCallback<boolean | undefined>;
104
- };
105
-
106
- export type OuiNavigableOrientation = 'horizontal' | 'vertical';
107
-
108
- // #endregion
109
-
110
- // #region Functions
111
-
112
- function getItems(container: HTMLElement, selector: string): HTMLElement[] {
113
- return [...container.querySelectorAll(selector)].filter(isValidItem);
114
- }
115
-
116
- export function getNavigable(element: HTMLElement, options: OuiNavigableOptions): OuiNavigable {
117
- return new OuiNavigable(element, options);
118
- }
119
-
120
- export function getNavigableOrientation(
121
- element: HTMLElement,
122
- vertical: boolean,
123
- aria?: boolean,
124
- ): OuiNavigableOrientation {
125
- const attribute =
126
- (aria ?? true) ? getAria(element, 'orientation') : getAttribute(element, 'orientation');
127
-
128
- return ORIENTATIONS.has(attribute as OuiNavigableOrientation)
129
- ? (attribute as OuiNavigableOrientation)
130
- : vertical
131
- ? ORIENTATION_VERTICAL
132
- : ORIENTATION_HORIZONTAL;
133
- }
134
-
135
- function isValidItem(element: Element): element is HTMLElement {
136
- return 'disabled' in element ? element.disabled !== true : true;
137
- }
138
-
139
- function onClick(navigable: OuiNavigable, event: Event): void {
140
- const item = findAncestor(event, navigable.options.selector) as HTMLElement;
141
- const items = getItems(navigable.element, navigable.options.selector);
142
-
143
- const index = items.indexOf(item);
144
-
145
- if (index === -1) {
146
- return;
147
- }
148
-
149
- if (index !== navigable.active.index) {
150
- onNavigate(navigable, index, items, false);
151
- }
152
-
153
- navigable.options.onActivate?.(navigable);
154
-
155
- item.focus();
156
- }
157
-
158
- function onNavigate(
159
- navigable: OuiNavigable,
160
- index: number,
161
- items: HTMLElement[],
162
- focus: boolean,
163
- ): void {
164
- navigable.active.element = items[index];
165
- navigable.active.index = index;
166
-
167
- updateNavigableFocus(navigable, focus, items);
168
-
169
- navigable.options.onNavigation?.(navigable);
170
- }
171
-
172
- function onFocusin(navigable: OuiNavigable, event: FocusEvent): void {
173
- const item = findAncestor(event, navigable.options.selector);
174
-
175
- if (!isHTMLOrSVGElement(item) || !isValidItem(item)) {
176
- return;
177
- }
178
-
179
- const items = getItems(navigable.element, navigable.options.selector);
180
- const index = items.indexOf(item);
181
-
182
- if (index === -1) {
183
- return;
184
- }
185
-
186
- navigable.active.element = item;
187
- navigable.active.index = index;
188
-
189
- updateNavigableFocus(navigable, false, items);
190
- }
191
-
192
- function onKeydown(navigable: OuiNavigable, event: KeyboardEvent): void {
193
- const escaped = event.key === 'Escape';
194
- const stopPropagation = navigable.options.onShouldDeactivate?.(event, navigable);
195
-
196
- if (escaped || stopPropagation != null) {
197
- if (escaped || stopPropagation) {
198
- event.stopPropagation();
199
- }
200
-
201
- navigable.options.onDeactivate?.(event, navigable);
202
-
203
- return;
204
- }
205
-
206
- if (!KEYS_ALL.has(event.key)) {
207
- return;
208
- }
209
-
210
- const mustActivate = KEYS_ACTIVATE.has(event.key);
211
- const shouldActivate = navigable.options.onShouldActivate?.(event, navigable) === true;
212
-
213
- if (
214
- mustActivate ||
215
- shouldActivate ||
216
- navigable.options.onPreventNavigation?.(event, navigable) === true
217
- ) {
218
- event.stopPropagation();
219
- }
220
-
221
- if (mustActivate || shouldActivate) {
222
- navigable.options.onActivate?.(navigable);
223
-
224
- return;
225
- }
226
-
227
- const items = getItems(navigable.element, navigable.options.selector);
228
- const {length} = items;
229
-
230
- if (length === 0) {
231
- return;
232
- }
233
-
234
- let index: number | undefined;
235
-
236
- if (KEYS_ABSOLUTE.has(event.key)) {
237
- index = event.key === 'Home' ? 0 : items.length - 1;
238
- } else {
239
- const direction = NAVIGABLE_KEYS_HORIZONTAL.has(event.key)
240
- ? ORIENTATION_HORIZONTAL
241
- : ORIENTATION_VERTICAL;
242
-
243
- if (navigable.orientation === direction) {
244
- if (navigable.active.element instanceof HTMLSelectElement) {
245
- event.preventDefault();
246
- }
247
-
248
- index = clamp(
249
- navigable.active.index + (KEYS_NEXT.has(event.key) ? 1 : -1),
250
- 0,
251
- length - 1,
252
- true,
253
- );
254
- }
255
- }
256
-
257
- if (index != null && index !== navigable.active.index) {
258
- onNavigate(navigable, index, items, true);
259
- }
260
- }
261
-
262
- export function updateNavigableFocus(
263
- navigable: OuiNavigable,
264
- focus: boolean,
265
- items?: HTMLElement[],
266
- ): void {
267
- const updated = items ?? getItems(navigable.element, navigable.options.selector);
268
- const {length} = updated;
269
-
270
- if (length === 0) {
271
- return;
272
- }
273
-
274
- for (let index = 0; index < length; index += 1) {
275
- updated[index].tabIndex = index === navigable.active.index ? 0 : -1;
276
- }
277
-
278
- navigable.active.element ??= updated[navigable.active.index];
279
-
280
- if (focus) {
281
- navigable.active.element?.focus();
282
- }
283
- }
284
-
285
- // #endregion
286
-
287
- // #region Variables
288
-
289
- const KEYS_ABSOLUTE = new Set(['End', 'Home']);
290
-
291
- const KEYS_ACTIVATE = new Set(['Enter', ' ']);
292
-
293
- export const NAVIGABLE_KEYS_HORIZONTAL = new Set(['ArrowRight', 'ArrowLeft']);
294
-
295
- export const NAVIGABLE_KEYS_VERTICAL = new Set(['ArrowDown', 'ArrowUp']);
296
-
297
- const KEYS_ALL = new Set([
298
- ...KEYS_ABSOLUTE,
299
- ...KEYS_ACTIVATE,
300
- ...NAVIGABLE_KEYS_HORIZONTAL,
301
- ...NAVIGABLE_KEYS_VERTICAL,
302
- ]);
303
-
304
- const KEYS_NEXT = new Set(['ArrowDown', 'ArrowRight']);
305
-
306
- const ORIENTATION_HORIZONTAL: OuiNavigableOrientation = 'horizontal';
307
-
308
- const ORIENTATION_VERTICAL: OuiNavigableOrientation = 'vertical';
309
-
310
- const ORIENTATIONS = new Set<OuiNavigableOrientation>([
311
- ORIENTATION_HORIZONTAL,
312
- ORIENTATION_VERTICAL,
313
- ]);
314
-
315
- // #endregion
package/src/js/l10n.ts DELETED
@@ -1,192 +0,0 @@
1
- import type {GenericCallback} from '@oscarpalmer/atoms/models';
2
- import {getString} from '@oscarpalmer/atoms/string';
3
-
4
- // #region Types
5
-
6
- type LocalizationParameters<Localization> = Localization extends GenericCallback
7
- ? Parameters<Localization>
8
- : never;
9
-
10
- export class OuiLocalizations<Localizations extends OuiLocalizationsValues> {
11
- readonly defaults: Localizations;
12
-
13
- readonly keys: Set<keyof Localizations>;
14
-
15
- readonly values: Record<string, Partial<Localizations>> = {};
16
-
17
- constructor(defaults: Localizations) {
18
- this.defaults = defaults;
19
- this.keys = new Set(Object.keys(defaults) as Array<keyof Localizations>);
20
- }
21
-
22
- get<Key extends keyof Localizations>(
23
- element: Element,
24
- key: Key,
25
- localizations: Record<string, Partial<Localizations>>,
26
- ): string | GenericCallback {
27
- return getLocalization(this, element, key, localizations);
28
- }
29
-
30
- set(language: string, localizations: Partial<Localizations>): void;
31
-
32
- set(localizations: Record<string, Partial<Localizations>>): void;
33
-
34
- set(
35
- first: string | Record<string, Partial<Localizations>>,
36
- second?: Partial<Localizations>,
37
- ): void {
38
- updateLocalizations(
39
- this,
40
- typeof first === 'string'
41
- ? {
42
- [first]: second!,
43
- }
44
- : first,
45
- this.values,
46
- );
47
- }
48
- }
49
-
50
- export type OuiLocalizationsValues = Record<string, string | GenericCallback>;
51
-
52
- // #endregion
53
-
54
- // #region Functions
55
-
56
- export function getLanguage(element: Element): string {
57
- return (
58
- element.closest('[lang]')?.getAttribute('lang') ?? document.documentElement.lang
59
- ).toLowerCase();
60
- }
61
-
62
- export function getLocalization<
63
- Localizations extends OuiLocalizationsValues,
64
- Key extends keyof Localizations,
65
- >(
66
- localizations: OuiLocalizations<Localizations>,
67
- element: Element,
68
- key: Key,
69
- value: Record<string, Partial<Localizations>>,
70
- ): Localizations[Key] | GenericCallback {
71
- const language = getLanguage(element);
72
-
73
- const values = [value, localizations.values];
74
- const {length} = values;
75
-
76
- let localization: Localizations[Key] | undefined;
77
- let shortLanguage: string | undefined;
78
-
79
- for (let index = 0; index < length; index += 1) {
80
- const value = values[index];
81
-
82
- localization = value?.[language]?.[key];
83
-
84
- if (localization != null) {
85
- break;
86
- }
87
-
88
- shortLanguage ??= language.split('-')[0];
89
-
90
- if (shortLanguage === language) {
91
- continue;
92
- }
93
-
94
- localization = value?.[shortLanguage]?.[key];
95
-
96
- if (localization != null) {
97
- break;
98
- }
99
- }
100
-
101
- return localization ?? localizations.defaults[key];
102
- }
103
-
104
- export function getLocalizations<Localizations extends OuiLocalizationsValues>(
105
- defaults: Localizations,
106
- ): OuiLocalizations<Localizations> {
107
- return new OuiLocalizations(defaults);
108
- }
109
-
110
- export function localize<
111
- Localizations extends OuiLocalizationsValues,
112
- Key extends keyof Localizations,
113
- Localization extends Localizations[Key],
114
- >(
115
- localization: Localization,
116
- data: Record<string, unknown>,
117
- parameters: LocalizationParameters<Localization>,
118
- ): string {
119
- if (typeof localization === 'function') {
120
- return localization(...parameters);
121
- }
122
-
123
- const keys = Object.keys(data);
124
- const {length} = keys;
125
-
126
- let localized = localization as string;
127
-
128
- for (let index = 0; index < length; index += 1) {
129
- const key = keys[index];
130
- const value = data[key];
131
-
132
- localized = localized.replaceAll(`{${key}}`, getString(value));
133
- }
134
-
135
- return localized;
136
- }
137
-
138
- export function updateLocalizations<Localizations extends OuiLocalizationsValues>(
139
- instance: OuiLocalizations<Localizations>,
140
- values: Record<string, Partial<Localizations>>,
141
- destination: Record<string, Partial<Localizations>>,
142
- onAfter?: () => void,
143
- ): void {
144
- if (typeof values !== 'object' || values == null) {
145
- return;
146
- }
147
-
148
- const localizations = destination ?? instance.values;
149
-
150
- const languages = Object.keys(values);
151
- const languagesLength = languages.length;
152
-
153
- let updated = false;
154
-
155
- for (let languagesIndex = 0; languagesIndex < languagesLength; languagesIndex += 1) {
156
- const language = languages[languagesIndex];
157
- const languageValue = values[language];
158
-
159
- if (typeof languageValue !== 'object' || languageValue == null) {
160
- continue;
161
- }
162
-
163
- const normalizedLanguage = language.toLowerCase();
164
-
165
- localizations[normalizedLanguage] ??= {};
166
-
167
- const keys = Object.keys(languageValue);
168
- const keysLength = keys.length;
169
-
170
- for (let keysIndex = 0; keysIndex < keysLength; keysIndex += 1) {
171
- const key = keys[keysIndex];
172
- const keyValue = languageValue[key];
173
-
174
- const normalizedKey = key.toLowerCase() as keyof Localizations;
175
-
176
- if (
177
- instance.keys.has(normalizedKey) &&
178
- (typeof keyValue === 'string' || typeof keyValue === 'function')
179
- ) {
180
- updated = true;
181
-
182
- localizations[normalizedLanguage][normalizedKey] = keyValue as never;
183
- }
184
- }
185
- }
186
-
187
- if (updated && typeof onAfter === 'function') {
188
- onAfter();
189
- }
190
- }
191
-
192
- // #endregion
@@ -1,61 +0,0 @@
1
- import type {OuiListboxAttributes} from '../internal/listbox/listbox.model';
2
-
3
- // #region Variables
4
-
5
- export const LISTBOX_TAGNAME = 'oui-listbox';
6
-
7
- const attributeContent = `${LISTBOX_TAGNAME}-content`;
8
-
9
- const attributeFilter = `${LISTBOX_TAGNAME}-filter`;
10
-
11
- const attributeFilterInput = `${attributeFilter}-input`;
12
-
13
- const attributeGroup = `${LISTBOX_TAGNAME}-group`;
14
-
15
- const attributeHeader = `${LISTBOX_TAGNAME}-header`;
16
-
17
- const attributeHidden = `${LISTBOX_TAGNAME}-hidden`;
18
-
19
- const attributeList = `${LISTBOX_TAGNAME}-list`;
20
-
21
- const attributeListEmpty = `${attributeList}-empty`;
22
-
23
- const attributeMessage = `${LISTBOX_TAGNAME}-message`;
24
-
25
- const attributeOption = `${LISTBOX_TAGNAME}-option`;
26
-
27
- const attributeOptionActive = `${LISTBOX_TAGNAME}-option-active`;
28
-
29
- const messageLabel = `<${LISTBOX_TAGNAME}> must contain a <label> element for the <select> element`;
30
-
31
- const messageSelect = `<${LISTBOX_TAGNAME}> must contain a valid <select> element`;
32
-
33
- const selectorFilter = `[${attributeFilterInput}]`;
34
-
35
- const selectorOption = `[${attributeOption}]`;
36
-
37
- export const LISTBOX_ATTRIBUTES: OuiListboxAttributes = {
38
- active: attributeOptionActive,
39
- content: attributeContent,
40
- empty: attributeListEmpty,
41
- filter: attributeFilter,
42
- group: attributeGroup,
43
- header: attributeHeader,
44
- hidden: attributeHidden,
45
- input: attributeFilterInput,
46
- list: attributeList,
47
- message: attributeMessage,
48
- option: attributeOption,
49
- };
50
-
51
- export const LISTBOX_MESSAGES = {
52
- label: messageLabel,
53
- select: messageSelect,
54
- };
55
-
56
- export const LISTBOX_SELECTORS = {
57
- filter: selectorFilter,
58
- option: selectorOption,
59
- };
60
-
61
- // #endregion
@@ -1,106 +0,0 @@
1
- import {on} from '@oscarpalmer/toretto/event';
2
- import {findAncestor} from '@oscarpalmer/toretto/find';
3
- import {
4
- applyListboxSelection,
5
- filterListbox,
6
- toggleListboxOption,
7
- } from '../internal/listbox/listbox.data';
8
- import {updateListboxPreview} from '../internal/listbox/listbox.dom';
9
- import {processListboxKeydown} from '../internal/listbox/listbox.event';
10
- import type {OuiListboxPreview, OuiListboxState} from '../internal/listbox/listbox.model';
11
- import {LISTBOX_SELECTORS, LISTBOX_TAGNAME} from './constants.listbox';
12
- import {listboxStates} from './state.listbox';
13
-
14
- // #region Types
15
-
16
- // #region Functions
17
-
18
- function onListboxClick(event: Event): void {
19
- const listbox = findAncestor(event, LISTBOX_TAGNAME);
20
- const state = listboxStates.map.get(listbox!);
21
-
22
- if (listbox == null || state == null) {
23
- return;
24
- }
25
-
26
- const option = findAncestor(event, LISTBOX_SELECTORS.option) as HTMLElement | null;
27
-
28
- if (option == null) {
29
- return;
30
- }
31
-
32
- if (state.disabled) {
33
- state.filter.element.focus();
34
- } else if (state.readonly) {
35
- updateListboxPreview('none', state, option, false);
36
- } else {
37
- toggleListboxOption(state, option);
38
- }
39
- }
40
-
41
- export function onListboxEnter(state: OuiListboxState): void {
42
- if (!state.disabled && !state.readonly) {
43
- toggleListboxOption(state, state.active);
44
- }
45
- }
46
-
47
- export function onListboxFilter(
48
- preview: OuiListboxPreview,
49
- state: OuiListboxState,
50
- _: boolean,
51
- active?: HTMLElement,
52
- ): void {
53
- updateListboxPreview(preview, state, active);
54
- }
55
-
56
- function onListboxInput(event: InputEvent): void {
57
- const listbox = findAncestor(event, LISTBOX_TAGNAME);
58
- const state = listboxStates.map.get(listbox!);
59
-
60
- if (state?.filter.element === event.target) {
61
- filterListbox(event.inputType, state);
62
- }
63
- }
64
-
65
- function onListboxKeydown(event: KeyboardEvent): void {
66
- const listbox = findAncestor(event, LISTBOX_TAGNAME);
67
- const state = listboxStates.map.get(listbox!);
68
-
69
- if (state?.filter.element === event.target) {
70
- processListboxKeydown(event, state);
71
- }
72
- }
73
-
74
- export function onListboxNavigation(
75
- preview: OuiListboxPreview,
76
- state: OuiListboxState,
77
- active?: HTMLElement,
78
- ): void {
79
- if (state.multiple || state.readonly) {
80
- updateListboxPreview(preview, state, active);
81
- } else {
82
- applyListboxSelection({
83
- state,
84
- option: active,
85
- added: true,
86
- selected: active == null ? [] : [active],
87
- });
88
- }
89
- }
90
-
91
- export function onListboxToggle(state: OuiListboxState): void {
92
- state.filter.element.focus();
93
- }
94
-
95
- // #endregion
96
-
97
- // #region Initialization
98
-
99
- on(document, 'click', onListboxClick);
100
- on(document, 'input', onListboxInput);
101
-
102
- on(document, 'keydown', onListboxKeydown, {
103
- passive: false,
104
- });
105
-
106
- // #endregion