@oscarpalmer/oui 0.33.0 → 0.35.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 (58) hide show
  1. package/dist/js/combobox/constants.combobox.d.mts +2 -0
  2. package/dist/js/combobox/constants.combobox.mjs +46 -0
  3. package/dist/js/combobox/data.combobox.d.mts +18 -0
  4. package/dist/js/combobox/data.combobox.mjs +2 -0
  5. package/dist/js/combobox/dom.combobox.d.mts +110 -0
  6. package/dist/js/combobox/dom.combobox.mjs +2 -0
  7. package/dist/js/combobox/event.combobox.d.mts +17 -0
  8. package/dist/js/combobox/event.combobox.mjs +2 -0
  9. package/dist/js/combobox/form.combobox.d.mts +6 -0
  10. package/dist/js/combobox/form.combobox.mjs +2 -0
  11. package/dist/js/combobox/index.d.mts +2 -0
  12. package/dist/js/combobox/index.mjs +195 -0
  13. package/dist/js/combobox/misc.combobox.d.mts +2 -0
  14. package/dist/js/combobox/misc.combobox.mjs +2 -0
  15. package/dist/js/combobox/models.combobox.d.mts +2 -0
  16. package/dist/js/combobox/models.combobox.mjs +1 -0
  17. package/dist/js/combobox/state.combobox.d.mts +13 -0
  18. package/dist/js/combobox/state.combobox.mjs +2 -0
  19. package/dist/js/constants.combobox-h_kyDegw.d.mts +248 -0
  20. package/dist/js/data.combobox-B8HMkDOh.mjs +1030 -0
  21. package/dist/js/dialog.mjs +3 -2
  22. package/dist/js/floatable/floatable.embedded.mjs +4 -4
  23. package/dist/js/focus-trap/focus-trap.embedded.mjs +3 -3
  24. package/dist/js/index.d.mts +14 -643
  25. package/dist/js/index.mjs +19 -4875
  26. package/dist/js/internal/draggable.mjs +6 -6
  27. package/dist/js/internal/navigable.mjs +3 -3
  28. package/dist/js/l10n.d.mts +20 -0
  29. package/dist/js/l10n.mjs +80 -0
  30. package/dist/js/menu.d.mts +1 -1
  31. package/dist/js/menu.mjs +3 -2
  32. package/dist/js/movable/movable.embedded.mjs +3 -3
  33. package/dist/js/popover.mjs +1 -1
  34. package/dist/js/resizable/resizable.embedded.mjs +7 -7
  35. package/dist/js/sortable/sortable.embedded.mjs +7 -6
  36. package/dist/js/splitter.mjs +7 -13
  37. package/dist/js/toolbar.d.mts +1 -1
  38. package/dist/js/tooltip.mjs +3 -3
  39. package/package.json +4 -4
  40. package/src/js/combobox/constants.combobox.ts +62 -0
  41. package/src/js/combobox/data.combobox.ts +236 -0
  42. package/src/js/combobox/dom.combobox.ts +825 -0
  43. package/src/js/combobox/event.combobox.ts +454 -0
  44. package/src/js/combobox/form.combobox.ts +116 -0
  45. package/src/js/combobox/index.ts +313 -0
  46. package/src/js/combobox/misc.combobox.ts +188 -0
  47. package/src/js/combobox/models.combobox.ts +94 -0
  48. package/src/js/combobox/state.combobox.ts +247 -0
  49. package/src/js/index.ts +1 -1
  50. package/src/js/internal/draggable.ts +3 -3
  51. package/src/js/l10n.ts +192 -0
  52. package/src/js/movable/movable.embedded.ts +2 -2
  53. package/src/js/resizable/resizable.embedded.ts +3 -3
  54. package/src/js/sortable/sortable.embedded.ts +2 -2
  55. package/src/js/splitter.ts +2 -2
  56. package/dist/js/combobox.d.mts +0 -91
  57. package/dist/js/combobox.mjs +0 -797
  58. package/src/js/combobox.ts +0 -2410
@@ -0,0 +1,313 @@
1
+ import {parse} from '@oscarpalmer/atoms/string';
2
+ import {getAttribute} from '@oscarpalmer/toretto/attribute';
3
+ import {updateLocalizations} from '../l10n';
4
+ import {COMBOBOX_TAGNAME} from './constants.combobox';
5
+ import {setComboboxValue, setComboboxValues} from './data.combobox';
6
+ import {setComboboxDisabled, setComboboxReadonly, setComboboxRequired} from './dom.combobox';
7
+ import {setComboboxFilter, setComboboxLimit} from './misc.combobox';
8
+ import type {OuiComboboxLocalizations, OuiComboboxState, OuiComboboxType} from './models.combobox';
9
+ import {
10
+ attachCombobox,
11
+ comboboxLocalizations,
12
+ createComboboxState,
13
+ detachCombobox,
14
+ } from './state.combobox';
15
+
16
+ // #region Types
17
+
18
+ declare global {
19
+ interface HTMLElementTagNameMap {
20
+ [COMBOBOX_TAGNAME]: OuiComboboxElement;
21
+ }
22
+ }
23
+
24
+ export class OuiComboboxElement extends HTMLElement {
25
+ static formAssociated = true;
26
+
27
+ static observedAttributes = ['max', 'min', 'required', 'value'];
28
+
29
+ readonly #internals: ElementInternals;
30
+
31
+ #restored?: string[];
32
+
33
+ #state!: OuiComboboxState;
34
+
35
+ /**
36
+ * Is the combobox disabled?
37
+ */
38
+ get disabled(): boolean {
39
+ return this.#state?.disabled ?? false;
40
+ }
41
+
42
+ /**
43
+ * Should the combobox be disabled?
44
+ */
45
+ set disabled(value: boolean) {
46
+ if (typeof value === 'boolean' && value !== this.disabled) {
47
+ setComboboxDisabled(this.#state, value);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Current filter value
53
+ */
54
+ get filter(): string {
55
+ return this.#state.filter.element.value;
56
+ }
57
+
58
+ /**
59
+ * Set the filter value
60
+ */
61
+ set filter(value: string) {
62
+ setComboboxFilter(this.#state, value);
63
+ }
64
+
65
+ /**
66
+ * Maximum number of selected options
67
+ */
68
+ get max(): number | undefined {
69
+ return this.#state?.max;
70
+ }
71
+
72
+ /**
73
+ * Set the maximum number of selected options
74
+ */
75
+ set max(value: number) {
76
+ setComboboxLimit('max', this.#state, value);
77
+ }
78
+
79
+ /**
80
+ * Minimum number of selected options
81
+ */
82
+ get min(): number | undefined {
83
+ return this.#state?.min;
84
+ }
85
+
86
+ /**
87
+ * Set the minimum number of selected options
88
+ */
89
+ set min(value: number) {
90
+ setComboboxLimit('min', this.#state, value);
91
+ }
92
+
93
+ /**
94
+ * Does the combobox allow multiple values?
95
+ */
96
+ get multiple(): boolean {
97
+ return this.hasAttribute('multiple');
98
+ }
99
+
100
+ /**
101
+ * Name of the combobox
102
+ */
103
+ get name(): string {
104
+ return getAttribute(this, 'name') ?? '';
105
+ }
106
+
107
+ /**
108
+ * Is the combobox readonly?
109
+ */
110
+ get readonly(): boolean {
111
+ return this.#state?.readonly ?? false;
112
+ }
113
+
114
+ /**
115
+ * Should the combobox be readonly?
116
+ */
117
+ set readonly(value: boolean) {
118
+ if (typeof value === 'boolean' && value !== this.readonly) {
119
+ setComboboxReadonly(this.#state, value);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Is the combobox required?
125
+ */
126
+ get required(): boolean {
127
+ return this.#state?.required ?? false;
128
+ }
129
+
130
+ /**
131
+ * Should the combobox be required?
132
+ */
133
+ set required(value: boolean) {
134
+ if (typeof value === 'boolean' && value !== this.required) {
135
+ setComboboxRequired(this.#state, value);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Selected options
141
+ */
142
+ get selected(): HTMLElement[] {
143
+ return this.#state.options.selected.slice();
144
+ }
145
+
146
+ /**
147
+ * Type of combobox
148
+ */
149
+ get type(): OuiComboboxType {
150
+ return this.#state.type;
151
+ }
152
+
153
+ /**
154
+ * Current value
155
+ */
156
+ get value(): string {
157
+ return this.#state.values.current[0];
158
+ }
159
+
160
+ /**
161
+ * Set the value
162
+ */
163
+ set value(value: unknown) {
164
+ setComboboxValue(this.#state, value);
165
+ }
166
+
167
+ /**
168
+ * Current values
169
+ */
170
+ get values(): string[] {
171
+ return this.#state.values.current.slice();
172
+ }
173
+
174
+ /**
175
+ * Set the values
176
+ */
177
+ set values(value: unknown[]) {
178
+ setComboboxValues(this.#state, value);
179
+ }
180
+
181
+ constructor() {
182
+ super();
183
+
184
+ this.#internals = this.attachInternals();
185
+ }
186
+
187
+ attributeChangedCallback(name: string, _: string | null, value: string | null): void {
188
+ if (this.#state == null) {
189
+ return;
190
+ }
191
+
192
+ switch (name) {
193
+ case 'required':
194
+ this[name] = this.hasAttribute(name);
195
+ break;
196
+
197
+ case 'max':
198
+ case 'min':
199
+ setComboboxLimit(name, this.#state, value);
200
+ break;
201
+
202
+ case 'value':
203
+ this.value = value ?? '';
204
+ break;
205
+
206
+ default:
207
+ break;
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Clear all selected options
213
+ */
214
+ clear(): void {
215
+ setComboboxValues(this.#state, []);
216
+ }
217
+
218
+ connectedCallback(): void {
219
+ if (this.#state == null) {
220
+ this.#initialize();
221
+ } else {
222
+ attachCombobox(this.#state);
223
+ }
224
+ }
225
+
226
+ disconnectedCallback(): void {
227
+ if (this.#state != null) {
228
+ detachCombobox(this.#state);
229
+ }
230
+ }
231
+
232
+ formDisabledCallback(disabled: boolean): void {
233
+ this.disabled = disabled;
234
+ }
235
+
236
+ formResetCallback(): void {
237
+ setComboboxValues(this.#state, this.#state.values.initial, false);
238
+ }
239
+
240
+ formStateRestoreCallback(state: string | File | FormData, reason: unknown): void {
241
+ if (reason !== 'restore') {
242
+ return;
243
+ }
244
+
245
+ const parsed = typeof state === 'string' ? parse(state) : undefined;
246
+
247
+ if (!Array.isArray(parsed)) {
248
+ return;
249
+ }
250
+
251
+ if (this.#state == null) {
252
+ this.#restored = parsed;
253
+ } else {
254
+ setComboboxValues(this.#state, parsed);
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Set combobox localizations for a specific language
260
+ *
261
+ * @param language Language code
262
+ * @param localizations Localizations for language
263
+ */
264
+ setLocalizations(language: string, localizations: Partial<OuiComboboxLocalizations>): void;
265
+
266
+ /**
267
+ * Set combobox localizations for one or more languages
268
+ *
269
+ * @param localizations Localizations for one or more languages
270
+ */
271
+ setLocalizations(localizations: Record<string, Partial<OuiComboboxLocalizations>>): void;
272
+
273
+ setLocalizations(
274
+ first: string | Record<string, Partial<OuiComboboxLocalizations>>,
275
+ second?: Partial<OuiComboboxLocalizations>,
276
+ ): void {
277
+ updateLocalizations(
278
+ comboboxLocalizations,
279
+ typeof first === 'string'
280
+ ? {
281
+ [first]: second!,
282
+ }
283
+ : first,
284
+ this.#state.localizations,
285
+ );
286
+ }
287
+
288
+ #initialize(): void {
289
+ this.tabIndex = -1;
290
+
291
+ this.#state = createComboboxState(this, this.#internals);
292
+
293
+ if (this.#restored != null) {
294
+ setComboboxValues(this.#state, this.#restored, false);
295
+
296
+ this.#restored = undefined;
297
+ }
298
+ }
299
+ }
300
+
301
+ // #endregion
302
+
303
+ // #region Initialization
304
+
305
+ customElements.define(COMBOBOX_TAGNAME, OuiComboboxElement);
306
+
307
+ // #endregion
308
+
309
+ // #region Exports
310
+
311
+ export {setComboboxLocalizations} from './misc.combobox';
312
+
313
+ // #endregion
@@ -0,0 +1,188 @@
1
+ import {getAria} from '@oscarpalmer/toretto/aria';
2
+ import {getAttribute, setAttribute} from '@oscarpalmer/toretto/attribute';
3
+ import type {AnyAriaBooleanAttribute} from '@oscarpalmer/toretto/models';
4
+ import type {OuiComboboxElement} from '.';
5
+ import {updateLocalizations} from '../l10n';
6
+ import {COMBOBOX_AUTOCOMPLETES} from './constants.combobox';
7
+ import {
8
+ updateComboboxAnchor,
9
+ updateComboboxMessage,
10
+ updateComboboxSelectionItems,
11
+ } from './dom.combobox';
12
+ import {onComboboxFilter} from './event.combobox';
13
+ import {updateComboboxValidity} from './form.combobox';
14
+ import type {
15
+ OuiComboboxAutoComplete,
16
+ OuiComboboxAutoCompleteType,
17
+ OuiComboboxLocalizations,
18
+ OuiComboboxState,
19
+ } from './models.combobox';
20
+ import {comboboxLocalizations, comboboxStates} from './state.combobox';
21
+
22
+ // #region Functions
23
+
24
+ export function generateComboboxIds(prefix: string, select: HTMLSelectElement): void {
25
+ const elements = [...select.children].flatMap(child =>
26
+ child instanceof HTMLOptGroupElement
27
+ ? [
28
+ child,
29
+ ...[...child.children].filter(groupChild => groupChild instanceof HTMLOptionElement),
30
+ ]
31
+ : child instanceof HTMLOptionElement
32
+ ? [child]
33
+ : [],
34
+ );
35
+
36
+ const {length} = elements;
37
+
38
+ for (let index = 0; index < length; index += 1) {
39
+ const element = elements[index];
40
+
41
+ if (element.id === '') {
42
+ element.id = `${prefix}${element instanceof HTMLOptGroupElement ? '_group_' : '_option_'}${index}`;
43
+ }
44
+ }
45
+ }
46
+
47
+ export function getComboboxAttributeValue<Value>(
48
+ element: HTMLElement,
49
+ attribute: string,
50
+ possible: Set<Value>,
51
+ defaultValue: Value,
52
+ ): Value {
53
+ const value = getAttribute(element, attribute);
54
+
55
+ return possible.has(value as Value) ? (value as Value) : defaultValue;
56
+ }
57
+
58
+ export function getComboboxAutoComplete(
59
+ element: OuiComboboxElement,
60
+ select: boolean,
61
+ ): OuiComboboxAutoComplete {
62
+ let value: OuiComboboxAutoCompleteType;
63
+
64
+ if (select) {
65
+ value = 'list';
66
+ } else {
67
+ value = getComboboxAttributeValue(element, 'autocomplete', COMBOBOX_AUTOCOMPLETES, 'none');
68
+ }
69
+
70
+ return {
71
+ value,
72
+ both: value === 'both',
73
+ inline: value === 'inline',
74
+ list: value === 'list',
75
+ none: value === 'none',
76
+ };
77
+ }
78
+
79
+ export function getComboboxBooleanAria(
80
+ element: HTMLElement,
81
+ attribute: AnyAriaBooleanAttribute,
82
+ ): boolean {
83
+ const value = getAria(element, attribute);
84
+
85
+ return value === true || value === 'true';
86
+ }
87
+
88
+ export function getComboboxId(): string {
89
+ index += 1;
90
+
91
+ return `oui_combobox_${index}`;
92
+ }
93
+
94
+ export function getComboboxVisibleOptions(state: OuiComboboxState): HTMLElement[] {
95
+ return state.options.all.filter(option => !option.hidden);
96
+ }
97
+
98
+ export function hasTextSelection(element: HTMLElement): element is HTMLInputElement {
99
+ return element instanceof HTMLInputElement && element.selectionStart !== element.selectionEnd;
100
+ }
101
+
102
+ export function setComboboxFilter(state: OuiComboboxState, value: unknown): void {
103
+ if (typeof value !== 'string') {
104
+ return;
105
+ }
106
+
107
+ const type: InputEvent['inputType'] =
108
+ value.length < state.filter.element.value.length ? 'deleteContentBackward' : 'insertText';
109
+
110
+ state.filter.element.value = value;
111
+
112
+ onComboboxFilter(type, state);
113
+ }
114
+
115
+ export function setComboboxLimit(
116
+ type: 'max' | 'min',
117
+ state: OuiComboboxState,
118
+ value?: unknown,
119
+ ): void {
120
+ let actual = value;
121
+
122
+ if (typeof actual === 'string') {
123
+ actual = Number.parseInt(actual, 10);
124
+ }
125
+
126
+ const updateLimit =
127
+ typeof actual === 'number' && !Number.isNaN(actual) && actual >= 0
128
+ ? actual !== state[type]
129
+ : actual == null;
130
+
131
+ if (updateLimit) {
132
+ state[type] = actual as number | undefined;
133
+
134
+ setAttribute(state.host, type, actual);
135
+
136
+ updateComboboxValidity(state);
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Set combobox localizations for a specific language
142
+ *
143
+ * @param language Language code
144
+ * @param localizations Localizations for language
145
+ */
146
+ export function setComboboxLocalizations(
147
+ language: string,
148
+ localizations: Partial<OuiComboboxLocalizations>,
149
+ ): void;
150
+
151
+ /**
152
+ * Set combobox localizations for one or more languages
153
+ *
154
+ * @param localizations Localizations for one or more languages
155
+ */
156
+ export function setComboboxLocalizations(
157
+ localizations: Record<string, Partial<OuiComboboxLocalizations>>,
158
+ ): void;
159
+
160
+ export function setComboboxLocalizations(
161
+ first: string | Record<string, Partial<OuiComboboxLocalizations>>,
162
+ second?: Partial<OuiComboboxLocalizations>,
163
+ ): void {
164
+ updateLocalizations(
165
+ comboboxLocalizations,
166
+ typeof first === 'string'
167
+ ? {
168
+ [first]: second!,
169
+ }
170
+ : first,
171
+ comboboxLocalizations.values,
172
+ () => {
173
+ for (const state of comboboxStates.set) {
174
+ updateComboboxAnchor(state);
175
+ updateComboboxMessage(state, getComboboxVisibleOptions(state).length);
176
+ updateComboboxSelectionItems(state);
177
+ }
178
+ },
179
+ );
180
+ }
181
+
182
+ // #endregion
183
+
184
+ // #region Variables
185
+
186
+ let index = 0;
187
+
188
+ // #endregion
@@ -0,0 +1,94 @@
1
+ import type {RemovableEventListener} from '@oscarpalmer/toretto/models';
2
+ import type {OuiFloatable} from '../floatable/floatable.embedded';
3
+ import type {OuiComboboxElement} from './index';
4
+
5
+ export type OuiComboboxAnchor = {
6
+ container?: HTMLElement;
7
+ element: HTMLElement;
8
+ };
9
+
10
+ export type OuiComboboxAutoComplete = Record<OuiComboboxAutoCompleteType, boolean> & {
11
+ value: OuiComboboxAutoCompleteType;
12
+ };
13
+
14
+ export type OuiComboboxAutoCompleteType = 'both' | 'inline' | 'list' | 'none';
15
+
16
+ export type OuiComboboxFilter = {
17
+ element: HTMLInputElement;
18
+ value: string;
19
+ };
20
+
21
+ export type OuiComboboxGroup = {
22
+ element: HTMLElement;
23
+ header: HTMLElement;
24
+ options: HTMLElement[];
25
+ };
26
+
27
+ export type OuiComboboxLabel = {
28
+ element: HTMLLabelElement;
29
+ id: string;
30
+ };
31
+
32
+ export type OuiComboboxLocalizations = {
33
+ empty: string | (() => string);
34
+ input: string | (() => string);
35
+ invalid: string | (() => string);
36
+ max: string | ((max: number) => string);
37
+ min: string | ((min: number) => string);
38
+ remove: string | ((label: string, value: string) => string);
39
+ results: string | ((amount: number) => string);
40
+ selected: string | ((amount: number) => string);
41
+ };
42
+
43
+ export type OuiComboboxOptions = {
44
+ all: HTMLElement[];
45
+ selected: HTMLElement[];
46
+ };
47
+
48
+ export type OuiComboboxPreview = 'none' | 'set' | 'typed';
49
+
50
+ export type OuiComboboxSelection = {
51
+ container: HTMLElement;
52
+ items: OuiComboboxSelectionItem[];
53
+ };
54
+
55
+ export type OuiComboboxSelectionItem = {
56
+ button: HTMLButtonElement;
57
+ element: HTMLElement;
58
+ };
59
+
60
+ export type OuiComboboxState = {
61
+ active?: HTMLElement;
62
+ anchor: OuiComboboxAnchor;
63
+ autocomplete: OuiComboboxAutoComplete;
64
+ content: HTMLElement;
65
+ disabled: boolean;
66
+ filter: OuiComboboxFilter;
67
+ floatable: OuiFloatable;
68
+ groups?: OuiComboboxGroup[];
69
+ host: OuiComboboxElement;
70
+ id: string;
71
+ internals: ElementInternals;
72
+ label: OuiComboboxLabel;
73
+ listeners: RemovableEventListener[];
74
+ localizations: Record<string, Partial<OuiComboboxLocalizations>>;
75
+ max?: number;
76
+ min?: number;
77
+ message: HTMLElement;
78
+ name: string;
79
+ multiple: boolean;
80
+ options: OuiComboboxOptions;
81
+ readonly: boolean;
82
+ required: boolean;
83
+ select: HTMLSelectElement;
84
+ selection?: OuiComboboxSelection;
85
+ type: OuiComboboxType;
86
+ values: OuiComboboxValues;
87
+ };
88
+
89
+ export type OuiComboboxType = 'autocomplete' | 'select';
90
+
91
+ export type OuiComboboxValues = {
92
+ current: string[];
93
+ initial: string[];
94
+ };