@acusti/dropdown 0.51.0 → 0.53.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.
package/dist/Dropdown.js CHANGED
@@ -1,11 +1,11 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
2
1
  import { c } from "react/compiler-runtime";
3
2
  import { SYSTEM_UI_FONT, Style } from "@acusti/styling";
4
3
  import useBoundingClientRect from "@acusti/use-bounding-client-rect";
5
4
  import useKeyboardEvents, { isEventTargetUsingKeyEvent } from "@acusti/use-keyboard-events";
6
5
  import clsx from "clsx";
7
- import { Children, useState, useId, useRef, useEffect, isValidElement, Fragment } from "react";
6
+ import { Children, Fragment, isValidElement, useEffect, useRef, useState } from "react";
8
7
  import { getBestMatch } from "@acusti/matchmaking";
8
+ import { jsx, jsxs } from "react/jsx-runtime";
9
9
  const ROOT_CLASS_NAME = "uktdropdown";
10
10
  const ROOT_SELECTOR = `.${ROOT_CLASS_NAME}`;
11
11
  const BODY_CLASS_NAME = `${ROOT_CLASS_NAME}-body`;
@@ -39,6 +39,7 @@ ${TRIGGER_SELECTOR} {
39
39
  }
40
40
  ${ROOT_SELECTOR} {
41
41
  width: max-content;
42
+ anchor-scope: --uktdd-anchor;
42
43
  }
43
44
  ${ROOT_SELECTOR}.disabled {
44
45
  pointer-events: none;
@@ -46,6 +47,9 @@ ${ROOT_SELECTOR}.disabled {
46
47
  ${ROOT_SELECTOR} > * {
47
48
  cursor: default;
48
49
  }
50
+ ${ROOT_SELECTOR} > :first-child {
51
+ anchor-name: --uktdd-anchor;
52
+ }
49
53
  ${LABEL_SELECTOR} {
50
54
  display: flex;
51
55
  align-items: center;
@@ -56,6 +60,7 @@ ${LABEL_TEXT_SELECTOR} {
56
60
  ${BODY_SELECTOR} {
57
61
  box-sizing: border-box;
58
62
  position: absolute;
63
+ position-anchor: --uktdd-anchor;
59
64
  top: anchor(bottom);
60
65
  left: anchor(left);
61
66
  bottom: auto;
@@ -99,932 +104,696 @@ ${BODY_SELECTOR} [data-ukt-active] {
99
104
  `;
100
105
  const ITEM_SELECTOR = `[data-ukt-item], [data-ukt-value]`;
101
106
  const getItemElements = (dropdownElement) => {
102
- if (!dropdownElement) return null;
103
- const bodyElement = dropdownElement.querySelector(BODY_SELECTOR);
104
- if (!bodyElement) return null;
105
- let items = bodyElement.querySelectorAll(ITEM_SELECTOR);
106
- if (items.length) return items;
107
- items = bodyElement.children;
108
- while (items.length === 1) {
109
- if (items[0].children == null) break;
110
- items = items[0].children;
111
- }
112
- if (items.length === 1) {
113
- items = bodyElement.children;
114
- }
115
- return items;
107
+ if (!dropdownElement) return null;
108
+ const bodyElement = dropdownElement.querySelector(BODY_SELECTOR);
109
+ if (!bodyElement) return null;
110
+ let items = bodyElement.querySelectorAll(ITEM_SELECTOR);
111
+ if (items.length) return items;
112
+ items = bodyElement.children;
113
+ while (items.length === 1) {
114
+ if (items[0].children == null) break;
115
+ items = items[0].children;
116
+ }
117
+ if (items.length === 1) items = bodyElement.children;
118
+ return items;
116
119
  };
117
120
  const getActiveItemElement = (dropdownElement) => {
118
- if (!dropdownElement) return null;
119
- return dropdownElement.querySelector("[data-ukt-active]");
121
+ if (!dropdownElement) return null;
122
+ return dropdownElement.querySelector("[data-ukt-active]");
120
123
  };
121
- const clearItemElementsState = (itemElements) => {
122
- itemElements.forEach((itemElement) => {
123
- if (itemElement.hasAttribute("data-ukt-active")) {
124
- delete itemElement.dataset.uktActive;
125
- }
126
- });
124
+ var clearItemElementsState = (itemElements) => {
125
+ itemElements.forEach((itemElement) => {
126
+ if (itemElement.hasAttribute("data-ukt-active")) delete itemElement.dataset.uktActive;
127
+ });
127
128
  };
128
- const setActiveItem = ({
129
- dropdownElement,
130
- element,
131
- event,
132
- index,
133
- indexAddend,
134
- isExactMatch,
135
- onActiveItem,
136
- text
137
- }) => {
138
- const items = getItemElements(dropdownElement);
139
- if (!items) return;
140
- const itemElements = Array.from(items);
141
- if (!itemElements.length) return;
142
- const lastIndex = itemElements.length - 1;
143
- const currentActiveIndex = itemElements.findIndex((itemElement) => itemElement.hasAttribute("data-ukt-active"));
144
- let nextActiveIndex = currentActiveIndex;
145
- if (typeof index === "number") {
146
- nextActiveIndex = index < 0 ? itemElements.length + index : index;
147
- }
148
- if (element) {
149
- nextActiveIndex = itemElements.findIndex((itemElement) => itemElement === element);
150
- } else if (typeof indexAddend === "number") {
151
- if (currentActiveIndex === -1 && indexAddend === -1) {
152
- nextActiveIndex = lastIndex;
153
- } else {
154
- nextActiveIndex += indexAddend;
155
- }
156
- nextActiveIndex = Math.max(0, Math.min(nextActiveIndex, lastIndex));
157
- } else if (typeof text === "string") {
158
- if (!text) {
159
- clearItemElementsState(itemElements);
160
- return;
161
- }
162
- const itemTexts = itemElements.map((itemElement) => itemElement.innerText);
163
- if (isExactMatch) {
164
- const textToCompare = text.toLowerCase();
165
- nextActiveIndex = itemTexts.findIndex((itemText) => itemText.toLowerCase().startsWith(textToCompare));
166
- if (nextActiveIndex === -1) {
167
- clearItemElementsState(itemElements);
168
- }
169
- } else {
170
- const bestMatch = getBestMatch({
171
- items: itemTexts,
172
- text
173
- });
174
- nextActiveIndex = itemTexts.findIndex((itemText) => itemText === bestMatch);
175
- }
176
- }
177
- const nextActiveItem = items[nextActiveIndex];
178
- if (nextActiveItem == null || nextActiveIndex === currentActiveIndex) return;
179
- clearItemElementsState(itemElements);
180
- nextActiveItem.setAttribute("data-ukt-active", "");
181
- const label = nextActiveItem.innerText;
182
- const value = nextActiveItem.dataset.uktValue ?? label;
183
- onActiveItem?.({
184
- element: nextActiveItem,
185
- event,
186
- label,
187
- value
188
- });
189
- let {
190
- parentElement
191
- } = nextActiveItem;
192
- let scrollableParent = null;
193
- while (!scrollableParent && parentElement && parentElement !== dropdownElement) {
194
- const isScrollable = parentElement.scrollHeight > parentElement.clientHeight + 15;
195
- if (isScrollable) {
196
- scrollableParent = parentElement;
197
- } else {
198
- parentElement = parentElement.parentElement;
199
- }
200
- }
201
- if (scrollableParent) {
202
- const parentRect = scrollableParent.getBoundingClientRect();
203
- const itemRect = nextActiveItem.getBoundingClientRect();
204
- const isAboveTop = itemRect.top < parentRect.top;
205
- const isBelowBottom = itemRect.bottom > parentRect.bottom;
206
- if (isAboveTop || isBelowBottom) {
207
- let {
208
- scrollTop
209
- } = scrollableParent;
210
- if (isAboveTop) {
211
- scrollTop -= parentRect.top - itemRect.top;
212
- } else {
213
- scrollTop += itemRect.bottom - parentRect.bottom;
214
- }
215
- scrollableParent.scrollTop = scrollTop;
216
- }
217
- }
129
+ const setActiveItem = ({ dropdownElement, element, event, index, indexAddend, isExactMatch, onActiveItem, text }) => {
130
+ const items = getItemElements(dropdownElement);
131
+ if (!items) return;
132
+ const itemElements = Array.from(items);
133
+ if (!itemElements.length) return;
134
+ const lastIndex = itemElements.length - 1;
135
+ const currentActiveIndex = itemElements.findIndex((itemElement) => itemElement.hasAttribute("data-ukt-active"));
136
+ let nextActiveIndex = currentActiveIndex;
137
+ if (typeof index === "number") nextActiveIndex = index < 0 ? itemElements.length + index : index;
138
+ if (element) nextActiveIndex = itemElements.findIndex((itemElement) => itemElement === element);
139
+ else if (typeof indexAddend === "number") {
140
+ if (currentActiveIndex === -1 && indexAddend === -1) nextActiveIndex = lastIndex;
141
+ else nextActiveIndex += indexAddend;
142
+ nextActiveIndex = Math.max(0, Math.min(nextActiveIndex, lastIndex));
143
+ } else if (typeof text === "string") {
144
+ if (!text) {
145
+ clearItemElementsState(itemElements);
146
+ return;
147
+ }
148
+ const itemTexts = itemElements.map((itemElement) => itemElement.innerText);
149
+ if (isExactMatch) {
150
+ const textToCompare = text.toLowerCase();
151
+ nextActiveIndex = itemTexts.findIndex((itemText) => itemText.toLowerCase().startsWith(textToCompare));
152
+ if (nextActiveIndex === -1) clearItemElementsState(itemElements);
153
+ } else {
154
+ const bestMatch = getBestMatch({
155
+ items: itemTexts,
156
+ text
157
+ });
158
+ nextActiveIndex = itemTexts.findIndex((itemText) => itemText === bestMatch);
159
+ }
160
+ }
161
+ const nextActiveItem = items[nextActiveIndex];
162
+ if (nextActiveItem == null || nextActiveIndex === currentActiveIndex) return;
163
+ clearItemElementsState(itemElements);
164
+ nextActiveItem.setAttribute("data-ukt-active", "");
165
+ const label = nextActiveItem.innerText;
166
+ const value = nextActiveItem.dataset.uktValue ?? label;
167
+ onActiveItem?.({
168
+ element: nextActiveItem,
169
+ event,
170
+ label,
171
+ value
172
+ });
173
+ let { parentElement } = nextActiveItem;
174
+ let scrollableParent = null;
175
+ while (!scrollableParent && parentElement && parentElement !== dropdownElement) if (parentElement.scrollHeight > parentElement.clientHeight + 15) scrollableParent = parentElement;
176
+ else parentElement = parentElement.parentElement;
177
+ if (scrollableParent) {
178
+ const parentRect = scrollableParent.getBoundingClientRect();
179
+ const itemRect = nextActiveItem.getBoundingClientRect();
180
+ const isAboveTop = itemRect.top < parentRect.top;
181
+ const isBelowBottom = itemRect.bottom > parentRect.bottom;
182
+ if (isAboveTop || isBelowBottom) {
183
+ let { scrollTop } = scrollableParent;
184
+ if (isAboveTop) scrollTop -= parentRect.top - itemRect.top;
185
+ else scrollTop += itemRect.bottom - parentRect.bottom;
186
+ scrollableParent.scrollTop = scrollTop;
187
+ }
188
+ }
218
189
  };
219
- const CHILDREN_ERROR = "@acusti/dropdown requires either 1 child (the dropdown body) or 2 children: the dropdown trigger and the dropdown body.";
220
- const TEXT_INPUT_SELECTOR = "input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea";
190
+ var CHILDREN_ERROR = "@acusti/dropdown requires either 1 child (the dropdown body) or 2 children: the dropdown trigger and the dropdown body.";
191
+ var CLICKABLE_SELECTOR = "button, a[href], input[type=\"button\"], input[type=\"submit\"]";
192
+ var TEXT_INPUT_SELECTOR = "input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea";
221
193
  function Dropdown(t0) {
222
- const $ = c(102);
223
- const {
224
- allowCreate,
225
- allowEmpty: t1,
226
- children,
227
- className,
228
- disabled,
229
- hasItems: t2,
230
- isOpenOnMount,
231
- isSearchable,
232
- keepOpenOnSubmit: t3,
233
- label,
234
- minHeightBody: t4,
235
- minWidthBody: t5,
236
- name,
237
- onActiveItem,
238
- onClick,
239
- onClose,
240
- onMouseDown,
241
- onMouseUp,
242
- onOpen,
243
- onSubmitItem,
244
- placeholder,
245
- style: styleFromProps,
246
- tabIndex,
247
- value
248
- } = t0;
249
- const allowEmpty = t1 === void 0 ? true : t1;
250
- const hasItems = t2 === void 0 ? true : t2;
251
- const keepOpenOnSubmit = t3 === void 0 ? !hasItems : t3;
252
- const minHeightBody = t4 === void 0 ? 30 : t4;
253
- const minWidthBody = t5 === void 0 ? 100 : t5;
254
- const childrenCount = Children.count(children);
255
- if (childrenCount !== 1 && childrenCount !== 2) {
256
- if (childrenCount === 0) {
257
- throw new Error(CHILDREN_ERROR + " Received no children.");
258
- }
259
- console.error(`${CHILDREN_ERROR} Received ${childrenCount} children.`);
260
- }
261
- let trigger;
262
- if (childrenCount > 1) {
263
- trigger = children[0];
264
- }
265
- const [isOpen, setIsOpen] = useState(isOpenOnMount ?? false);
266
- const [isOpening, setIsOpening] = useState(!isOpenOnMount);
267
- const [dropdownElement, setDropdownElement] = useState(null);
268
- const [dropdownBodyElement, setDropdownBodyElement] = useState(null);
269
- const id = useId();
270
- const inputElementRef = useRef(null);
271
- const closingTimerRef = useRef(null);
272
- const isOpeningTimerRef = useRef(null);
273
- const currentInputMethodRef = useRef("mouse");
274
- const clearEnteredCharactersTimerRef = useRef(null);
275
- const enteredCharactersRef = useRef("");
276
- const mouseDownPositionRef = useRef(null);
277
- const allowCreateRef = useRef(allowCreate);
278
- const allowEmptyRef = useRef(allowEmpty);
279
- const hasItemsRef = useRef(hasItems);
280
- const isOpenRef = useRef(isOpen);
281
- const isOpeningRef = useRef(isOpening);
282
- const keepOpenOnSubmitRef = useRef(keepOpenOnSubmit);
283
- const onCloseRef = useRef(onClose);
284
- const onOpenRef = useRef(onOpen);
285
- const onSubmitItemRef = useRef(onSubmitItem);
286
- const valueRef = useRef(value);
287
- let t6;
288
- let t7;
289
- if ($[0] !== allowCreate || $[1] !== allowEmpty || $[2] !== hasItems || $[3] !== isOpen || $[4] !== isOpening || $[5] !== keepOpenOnSubmit || $[6] !== onClose || $[7] !== onOpen || $[8] !== onSubmitItem || $[9] !== value) {
290
- t6 = () => {
291
- allowCreateRef.current = allowCreate;
292
- allowEmptyRef.current = allowEmpty;
293
- hasItemsRef.current = hasItems;
294
- isOpenRef.current = isOpen;
295
- isOpeningRef.current = isOpening;
296
- keepOpenOnSubmitRef.current = keepOpenOnSubmit;
297
- onCloseRef.current = onClose;
298
- onOpenRef.current = onOpen;
299
- onSubmitItemRef.current = onSubmitItem;
300
- valueRef.current = value;
301
- };
302
- t7 = [allowCreate, allowEmpty, hasItems, isOpen, isOpening, keepOpenOnSubmit, onClose, onOpen, onSubmitItem, value];
303
- $[0] = allowCreate;
304
- $[1] = allowEmpty;
305
- $[2] = hasItems;
306
- $[3] = isOpen;
307
- $[4] = isOpening;
308
- $[5] = keepOpenOnSubmit;
309
- $[6] = onClose;
310
- $[7] = onOpen;
311
- $[8] = onSubmitItem;
312
- $[9] = value;
313
- $[10] = t6;
314
- $[11] = t7;
315
- } else {
316
- t6 = $[10];
317
- t7 = $[11];
318
- }
319
- useEffect(t6, t7);
320
- const isMountedRef = useRef(false);
321
- let t8;
322
- let t9;
323
- if ($[12] !== isOpen) {
324
- t8 = () => {
325
- if (!isMountedRef.current) {
326
- isMountedRef.current = true;
327
- if (isOpenRef.current && onOpenRef.current) {
328
- onOpenRef.current();
329
- }
330
- return;
331
- }
332
- if (isOpen && onOpenRef.current) {
333
- onOpenRef.current();
334
- } else {
335
- if (!isOpen && onCloseRef.current) {
336
- onCloseRef.current();
337
- }
338
- }
339
- };
340
- t9 = [isOpen];
341
- $[12] = isOpen;
342
- $[13] = t8;
343
- $[14] = t9;
344
- } else {
345
- t8 = $[13];
346
- t9 = $[14];
347
- }
348
- useEffect(t8, t9);
349
- let t10;
350
- if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
351
- t10 = () => {
352
- setIsOpen(false);
353
- setIsOpening(false);
354
- mouseDownPositionRef.current = null;
355
- if (closingTimerRef.current) {
356
- clearTimeout(closingTimerRef.current);
357
- closingTimerRef.current = null;
358
- }
359
- };
360
- $[15] = t10;
361
- } else {
362
- t10 = $[15];
363
- }
364
- const closeDropdown = t10;
365
- let t11;
366
- if ($[16] !== dropdownElement) {
367
- t11 = (event) => {
368
- if (isOpenRef.current && !keepOpenOnSubmitRef.current) {
369
- closingTimerRef.current = setTimeout(closeDropdown, 90);
370
- }
371
- if (!hasItemsRef.current) {
372
- return;
373
- }
374
- const element = getActiveItemElement(dropdownElement);
375
- if (!element && !allowCreateRef.current) {
376
- if (!allowEmptyRef.current) {
377
- return;
378
- }
379
- if (inputElementRef.current?.value) {
380
- return;
381
- }
382
- }
383
- let itemLabel = element?.innerText ?? "";
384
- if (inputElementRef.current) {
385
- if (!element) {
386
- itemLabel = inputElementRef.current.value;
387
- } else {
388
- inputElementRef.current.value = itemLabel;
389
- }
390
- if (inputElementRef.current === inputElementRef.current.ownerDocument.activeElement) {
391
- inputElementRef.current.blur();
392
- }
393
- }
394
- const nextValue = element?.dataset.uktValue ?? itemLabel;
395
- if (valueRef.current && valueRef.current === nextValue) {
396
- return;
397
- }
398
- if (onSubmitItemRef.current) {
399
- onSubmitItemRef.current({
400
- element,
401
- event,
402
- label: itemLabel,
403
- value: nextValue
404
- });
405
- }
406
- };
407
- $[16] = dropdownElement;
408
- $[17] = t11;
409
- } else {
410
- t11 = $[17];
411
- }
412
- const handleSubmitItem = t11;
413
- let t12;
414
- if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
415
- t12 = (t132) => {
416
- const {
417
- clientX,
418
- clientY
419
- } = t132;
420
- currentInputMethodRef.current = "mouse";
421
- const initialPosition = mouseDownPositionRef.current;
422
- if (!initialPosition) {
423
- return;
424
- }
425
- if (Math.abs(initialPosition.clientX - clientX) < 12 && Math.abs(initialPosition.clientY - clientY) < 12) {
426
- return;
427
- }
428
- setIsOpening(false);
429
- };
430
- $[18] = t12;
431
- } else {
432
- t12 = $[18];
433
- }
434
- const handleMouseMove = t12;
435
- let t13;
436
- if ($[19] !== dropdownElement || $[20] !== onActiveItem) {
437
- t13 = (event_0) => {
438
- if (!hasItemsRef.current) {
439
- return;
440
- }
441
- if (currentInputMethodRef.current !== "mouse") {
442
- return;
443
- }
444
- if (!dropdownElement) {
445
- return;
446
- }
447
- const itemElements = getItemElements(dropdownElement);
448
- if (!itemElements) {
449
- return;
450
- }
451
- const eventTarget = event_0.target;
452
- const item = eventTarget.closest(ITEM_SELECTOR);
453
- const element_0 = item ?? eventTarget;
454
- for (const itemElement of itemElements) {
455
- if (itemElement === element_0) {
456
- setActiveItem({
457
- dropdownElement,
458
- element: element_0,
459
- event: event_0,
460
- onActiveItem
461
- });
462
- return;
463
- }
464
- }
465
- };
466
- $[19] = dropdownElement;
467
- $[20] = onActiveItem;
468
- $[21] = t13;
469
- } else {
470
- t13 = $[21];
471
- }
472
- const handleMouseOver = t13;
473
- let t14;
474
- if ($[22] !== dropdownElement) {
475
- t14 = (event_1) => {
476
- if (!hasItemsRef.current) {
477
- return;
478
- }
479
- const activeItem = getActiveItemElement(dropdownElement);
480
- if (!activeItem) {
481
- return;
482
- }
483
- const eventRelatedTarget = event_1.relatedTarget;
484
- if (activeItem !== event_1.target || activeItem.contains(eventRelatedTarget)) {
485
- return;
486
- }
487
- delete activeItem.dataset.uktActive;
488
- };
489
- $[22] = dropdownElement;
490
- $[23] = t14;
491
- } else {
492
- t14 = $[23];
493
- }
494
- const handleMouseOut = t14;
495
- let t15;
496
- if ($[24] !== onMouseDown) {
497
- t15 = (event_2) => {
498
- if (onMouseDown) {
499
- onMouseDown(event_2);
500
- }
501
- if (isOpenRef.current) {
502
- return;
503
- }
504
- setIsOpen(true);
505
- setIsOpening(true);
506
- mouseDownPositionRef.current = {
507
- clientX: event_2.clientX,
508
- clientY: event_2.clientY
509
- };
510
- isOpeningTimerRef.current = setTimeout(() => {
511
- setIsOpening(false);
512
- isOpeningTimerRef.current = null;
513
- }, 1e3);
514
- };
515
- $[24] = onMouseDown;
516
- $[25] = t15;
517
- } else {
518
- t15 = $[25];
519
- }
520
- const handleMouseDown = t15;
521
- let t16;
522
- if ($[26] !== handleSubmitItem || $[27] !== onMouseUp) {
523
- t16 = (event_3) => {
524
- if (onMouseUp) {
525
- onMouseUp(event_3);
526
- }
527
- if (isOpeningRef.current || !isOpenRef.current || closingTimerRef.current) {
528
- return;
529
- }
530
- const eventTarget_0 = event_3.target;
531
- if (!eventTarget_0.closest(BODY_SELECTOR)) {
532
- if (!isOpeningRef.current && inputElementRef.current !== eventTarget_0.ownerDocument.activeElement) {
533
- closeDropdown();
534
- }
535
- return;
536
- }
537
- if (!hasItemsRef.current) {
538
- return;
539
- }
540
- handleSubmitItem(event_3);
541
- };
542
- $[26] = handleSubmitItem;
543
- $[27] = onMouseUp;
544
- $[28] = t16;
545
- } else {
546
- t16 = $[28];
547
- }
548
- const handleMouseUp = t16;
549
- let t17;
550
- if ($[29] !== dropdownElement || $[30] !== handleSubmitItem || $[31] !== onActiveItem) {
551
- t17 = (event_4) => {
552
- const {
553
- altKey,
554
- ctrlKey,
555
- key,
556
- metaKey
557
- } = event_4;
558
- const eventTarget_1 = event_4.target;
559
- if (!dropdownElement) {
560
- return;
561
- }
562
- const onEventHandled = () => {
563
- event_4.stopPropagation();
564
- event_4.preventDefault();
565
- currentInputMethodRef.current = "keyboard";
566
- };
567
- const isEventTargetingDropdown = dropdownElement.contains(eventTarget_1);
568
- if (!isOpenRef.current) {
569
- if (!isEventTargetingDropdown) {
570
- return;
571
- }
572
- if (key === " " || key === "Enter" || hasItemsRef.current && (key === "ArrowUp" || key === "ArrowDown")) {
573
- onEventHandled();
574
- setIsOpen(true);
575
- }
576
- return;
577
- }
578
- const isTargetUsingKeyEvents = isEventTargetUsingKeyEvent(event_4);
579
- if (hasItemsRef.current && !isTargetUsingKeyEvents) {
580
- let isEditingCharacters = !ctrlKey && !metaKey && /^[A-Za-z0-9]$/.test(key);
581
- if (!isEditingCharacters && enteredCharactersRef.current) {
582
- isEditingCharacters = key === " " || key === "Backspace";
583
- }
584
- if (isEditingCharacters) {
585
- onEventHandled();
586
- if (key === "Backspace") {
587
- enteredCharactersRef.current = enteredCharactersRef.current.slice(0, -1);
588
- } else {
589
- enteredCharactersRef.current = enteredCharactersRef.current + key;
590
- }
591
- setActiveItem({
592
- dropdownElement,
593
- event: event_4,
594
- isExactMatch: allowCreateRef.current,
595
- onActiveItem,
596
- text: enteredCharactersRef.current
597
- });
598
- if (clearEnteredCharactersTimerRef.current) {
599
- clearTimeout(clearEnteredCharactersTimerRef.current);
600
- }
601
- clearEnteredCharactersTimerRef.current = setTimeout(() => {
602
- enteredCharactersRef.current = "";
603
- clearEnteredCharactersTimerRef.current = null;
604
- }, 1500);
605
- return;
606
- }
607
- }
608
- if (key === "Enter" || key === " " && !inputElementRef.current) {
609
- onEventHandled();
610
- handleSubmitItem(event_4);
611
- return;
612
- }
613
- if (key === "Escape" || isEventTargetingDropdown && key === " " && !hasItemsRef.current) {
614
- if (hasItemsRef.current || !isTargetUsingKeyEvents) {
615
- closeDropdown();
616
- }
617
- return;
618
- }
619
- if (hasItemsRef.current) {
620
- if (key === "ArrowUp") {
621
- onEventHandled();
622
- if (altKey || metaKey) {
623
- setActiveItem({
624
- dropdownElement,
625
- event: event_4,
626
- index: 0,
627
- onActiveItem
628
- });
629
- } else {
630
- setActiveItem({
631
- dropdownElement,
632
- event: event_4,
633
- indexAddend: -1,
634
- onActiveItem
635
- });
636
- }
637
- return;
638
- }
639
- if (key === "ArrowDown") {
640
- onEventHandled();
641
- if (altKey || metaKey) {
642
- setActiveItem({
643
- dropdownElement,
644
- event: event_4,
645
- index: -1,
646
- onActiveItem
647
- });
648
- } else {
649
- setActiveItem({
650
- dropdownElement,
651
- event: event_4,
652
- indexAddend: 1,
653
- onActiveItem
654
- });
655
- }
656
- return;
657
- }
658
- }
659
- };
660
- $[29] = dropdownElement;
661
- $[30] = handleSubmitItem;
662
- $[31] = onActiveItem;
663
- $[32] = t17;
664
- } else {
665
- t17 = $[32];
666
- }
667
- const handleKeyDown = t17;
668
- let t18;
669
- if ($[33] !== handleKeyDown) {
670
- t18 = {
671
- ignoreUsedKeyboardEvents: false,
672
- onKeyDown: handleKeyDown
673
- };
674
- $[33] = handleKeyDown;
675
- $[34] = t18;
676
- } else {
677
- t18 = $[34];
678
- }
679
- useKeyboardEvents(t18);
680
- let t19;
681
- if ($[35] !== isOpenOnMount || $[36] !== onActiveItem) {
682
- t19 = (ref) => {
683
- setDropdownElement(ref);
684
- if (!ref) {
685
- return;
686
- }
687
- const {
688
- ownerDocument
689
- } = ref;
690
- let inputElement = inputElementRef.current;
691
- if (!inputElement && ref.firstElementChild) {
692
- if (ref.firstElementChild.matches(TEXT_INPUT_SELECTOR)) {
693
- inputElement = ref.firstElementChild;
694
- } else {
695
- inputElement = ref.firstElementChild.querySelector(TEXT_INPUT_SELECTOR);
696
- }
697
- inputElementRef.current = inputElement;
698
- }
699
- const handleGlobalMouseDown = (t202) => {
700
- const {
701
- target
702
- } = t202;
703
- const eventTarget_2 = target;
704
- if (!ref.contains(eventTarget_2)) {
705
- closeDropdown();
706
- }
707
- };
708
- const handleGlobalMouseUp = (t212) => {
709
- const {
710
- target: target_0
711
- } = t212;
712
- if (!isOpenRef.current || closingTimerRef.current) {
713
- return;
714
- }
715
- if (isOpeningRef.current) {
716
- setIsOpening(false);
717
- if (isOpeningTimerRef.current) {
718
- clearTimeout(isOpeningTimerRef.current);
719
- isOpeningTimerRef.current = null;
720
- }
721
- return;
722
- }
723
- const eventTarget_3 = target_0;
724
- if (!ref.contains(eventTarget_3)) {
725
- closeDropdown();
726
- }
727
- };
728
- const handleGlobalFocusIn = (t222) => {
729
- const {
730
- target: target_1
731
- } = t222;
732
- if (!isOpenRef.current) {
733
- return;
734
- }
735
- const eventTarget_4 = target_1;
736
- if (ref.contains(eventTarget_4) || eventTarget_4.contains(ref)) {
737
- return;
738
- }
739
- closeDropdown();
740
- };
741
- document.addEventListener("focusin", handleGlobalFocusIn);
742
- document.addEventListener("mousedown", handleGlobalMouseDown);
743
- document.addEventListener("mouseup", handleGlobalMouseUp);
744
- if (ownerDocument !== document) {
745
- ownerDocument.addEventListener("focusin", handleGlobalFocusIn);
746
- ownerDocument.addEventListener("mousedown", handleGlobalMouseDown);
747
- ownerDocument.addEventListener("mouseup", handleGlobalMouseUp);
748
- }
749
- if (isOpenOnMount) {
750
- ref.focus();
751
- }
752
- const handleInput = (event_5) => {
753
- if (!isOpenRef.current) {
754
- setIsOpen(true);
755
- }
756
- const input = event_5.target;
757
- const isDeleting = enteredCharactersRef.current.length > input.value.length;
758
- enteredCharactersRef.current = input.value;
759
- if (isDeleting && input.value.length && getActiveItemElement(ref)) {
760
- return;
761
- }
762
- setActiveItem({
763
- dropdownElement: ref,
764
- event: event_5,
765
- isExactMatch: allowCreateRef.current,
766
- onActiveItem,
767
- text: enteredCharactersRef.current
768
- });
769
- };
770
- if (inputElement) {
771
- inputElement.addEventListener("input", handleInput);
772
- }
773
- return () => {
774
- document.removeEventListener("focusin", handleGlobalFocusIn);
775
- document.removeEventListener("mousedown", handleGlobalMouseDown);
776
- document.removeEventListener("mouseup", handleGlobalMouseUp);
777
- if (ownerDocument !== document) {
778
- ownerDocument.removeEventListener("focusin", handleGlobalFocusIn);
779
- ownerDocument.removeEventListener("mousedown", handleGlobalMouseDown);
780
- ownerDocument.removeEventListener("mouseup", handleGlobalMouseUp);
781
- }
782
- if (inputElement) {
783
- inputElement.removeEventListener("input", handleInput);
784
- }
785
- };
786
- };
787
- $[35] = isOpenOnMount;
788
- $[36] = onActiveItem;
789
- $[37] = t19;
790
- } else {
791
- t19 = $[37];
792
- }
793
- const handleRef = t19;
794
- if (!isValidElement(trigger)) {
795
- if (isSearchable) {
796
- const t202 = value ?? "";
797
- let t212;
798
- if ($[38] === Symbol.for("react.memo_cache_sentinel")) {
799
- t212 = () => setIsOpen(true);
800
- $[38] = t212;
801
- } else {
802
- t212 = $[38];
803
- }
804
- let t222;
805
- if ($[39] !== disabled || $[40] !== name || $[41] !== placeholder || $[42] !== t202 || $[43] !== tabIndex) {
806
- t222 = /* @__PURE__ */ jsx("input", { autoComplete: "off", className: TRIGGER_CLASS_NAME, defaultValue: t202, disabled, name, onFocus: t212, placeholder, ref: inputElementRef, tabIndex, type: "text" });
807
- $[39] = disabled;
808
- $[40] = name;
809
- $[41] = placeholder;
810
- $[42] = t202;
811
- $[43] = tabIndex;
812
- $[44] = t222;
813
- } else {
814
- t222 = $[44];
815
- }
816
- trigger = t222;
817
- } else {
818
- let t202;
819
- if ($[45] !== trigger) {
820
- t202 = /* @__PURE__ */ jsx("button", { className: TRIGGER_CLASS_NAME, tabIndex: 0, type: "button", children: trigger });
821
- $[45] = trigger;
822
- $[46] = t202;
823
- } else {
824
- t202 = $[46];
825
- }
826
- trigger = t202;
827
- }
828
- }
829
- if (label) {
830
- let t202;
831
- if ($[47] !== label) {
832
- t202 = /* @__PURE__ */ jsx("div", { className: LABEL_TEXT_CLASS_NAME, children: label });
833
- $[47] = label;
834
- $[48] = t202;
835
- } else {
836
- t202 = $[48];
837
- }
838
- let t212;
839
- if ($[49] !== t202 || $[50] !== trigger) {
840
- t212 = /* @__PURE__ */ jsxs("label", { className: LABEL_CLASS_NAME, children: [
841
- t202,
842
- trigger
843
- ] });
844
- $[49] = t202;
845
- $[50] = trigger;
846
- $[51] = t212;
847
- } else {
848
- t212 = $[51];
849
- }
850
- trigger = t212;
851
- }
852
- const dropdownRect = useBoundingClientRect(dropdownElement);
853
- const dropdownBodyRect = useBoundingClientRect(dropdownBodyElement);
854
- let t20;
855
- if ($[52] !== dropdownBodyElement) {
856
- t20 = getBoundingAncestor(dropdownBodyElement);
857
- $[52] = dropdownBodyElement;
858
- $[53] = t20;
859
- } else {
860
- t20 = $[53];
861
- }
862
- const boundingElement = t20;
863
- const boundingElementRect = useBoundingClientRect(boundingElement);
864
- let maxHeight;
865
- let maxWidth;
866
- if (dropdownBodyRect.top != null && dropdownRect.top != null && boundingElementRect.top != null) {
867
- const maxHeightUp = dropdownBodyRect.bottom - boundingElementRect.top;
868
- const maxHeightDown = boundingElementRect.bottom - dropdownBodyRect.top;
869
- let t212;
870
- if ($[54] !== dropdownBodyRect.top || $[55] !== dropdownRect.top || $[56] !== maxHeightDown || $[57] !== maxHeightUp) {
871
- t212 = Math.round(dropdownBodyRect.top > dropdownRect.top ? maxHeightDown : maxHeightUp);
872
- $[54] = dropdownBodyRect.top;
873
- $[55] = dropdownRect.top;
874
- $[56] = maxHeightDown;
875
- $[57] = maxHeightUp;
876
- $[58] = t212;
877
- } else {
878
- t212 = $[58];
879
- }
880
- maxHeight = t212;
881
- const maxWidthLeft = dropdownBodyRect.right - boundingElementRect.left;
882
- const maxWidthRight = boundingElementRect.right - dropdownBodyRect.left;
883
- let t222;
884
- if ($[59] !== dropdownBodyRect.left || $[60] !== dropdownRect.left || $[61] !== maxWidthLeft || $[62] !== maxWidthRight) {
885
- t222 = Math.round(dropdownBodyRect.left > dropdownRect.left ? maxWidthRight : maxWidthLeft);
886
- $[59] = dropdownBodyRect.left;
887
- $[60] = dropdownRect.left;
888
- $[61] = maxWidthLeft;
889
- $[62] = maxWidthRight;
890
- $[63] = t222;
891
- } else {
892
- t222 = $[63];
893
- }
894
- maxWidth = t222;
895
- }
896
- let t21;
897
- if ($[64] !== maxHeight || $[65] !== minHeightBody) {
898
- t21 = maxHeight != null && maxHeight > minHeightBody ? {
899
- [BODY_MAX_HEIGHT_VAR]: `calc(${maxHeight}px - var(--uktdd-body-buffer))`
900
- } : null;
901
- $[64] = maxHeight;
902
- $[65] = minHeightBody;
903
- $[66] = t21;
904
- } else {
905
- t21 = $[66];
906
- }
907
- let t22;
908
- if ($[67] !== maxWidth || $[68] !== minWidthBody) {
909
- t22 = maxWidth != null && maxWidth > minWidthBody ? {
910
- [BODY_MAX_WIDTH_VAR]: `calc(${maxWidth}px - var(--uktdd-body-buffer))`
911
- } : null;
912
- $[67] = maxWidth;
913
- $[68] = minWidthBody;
914
- $[69] = t22;
915
- } else {
916
- t22 = $[69];
917
- }
918
- let t23;
919
- if ($[70] !== styleFromProps || $[71] !== t21 || $[72] !== t22) {
920
- t23 = {
921
- ...styleFromProps,
922
- ...t21,
923
- ...t22
924
- };
925
- $[70] = styleFromProps;
926
- $[71] = t21;
927
- $[72] = t22;
928
- $[73] = t23;
929
- } else {
930
- t23 = $[73];
931
- }
932
- const style = t23;
933
- const anchorStyles = `[data-ukt-id="${id}"] > :first-child {
934
- anchor-name: --uktdd-anchor${id};
935
- }
936
- [data-ukt-id="${id}"] ${BODY_SELECTOR} {
937
- position-anchor: --uktdd-anchor${id};
938
- }`;
939
- let t24;
940
- if ($[74] === Symbol.for("react.memo_cache_sentinel")) {
941
- t24 = /* @__PURE__ */ jsx(Style, { href: "@acusti/dropdown/Dropdown", children: STYLES });
942
- $[74] = t24;
943
- } else {
944
- t24 = $[74];
945
- }
946
- const t25 = `@acusti/dropdown/Dropdown/${id}`;
947
- let t26;
948
- if ($[75] !== anchorStyles || $[76] !== t25) {
949
- t26 = /* @__PURE__ */ jsx(Style, { href: t25, children: anchorStyles });
950
- $[75] = anchorStyles;
951
- $[76] = t25;
952
- $[77] = t26;
953
- } else {
954
- t26 = $[77];
955
- }
956
- let t27;
957
- if ($[78] !== className || $[79] !== disabled || $[80] !== isOpen || $[81] !== isSearchable) {
958
- t27 = clsx(ROOT_CLASS_NAME, className, {
959
- disabled,
960
- "is-open": isOpen,
961
- "is-searchable": isSearchable
962
- });
963
- $[78] = className;
964
- $[79] = disabled;
965
- $[80] = isOpen;
966
- $[81] = isSearchable;
967
- $[82] = t27;
968
- } else {
969
- t27 = $[82];
970
- }
971
- let t28;
972
- if ($[83] !== children || $[84] !== childrenCount || $[85] !== isOpen) {
973
- t28 = isOpen ? /* @__PURE__ */ jsx("div", { className: BODY_CLASS_NAME, ref: setDropdownBodyElement, children: childrenCount > 1 ? children[1] : children }) : null;
974
- $[83] = children;
975
- $[84] = childrenCount;
976
- $[85] = isOpen;
977
- $[86] = t28;
978
- } else {
979
- t28 = $[86];
980
- }
981
- let t29;
982
- if ($[87] !== handleMouseDown || $[88] !== handleMouseOut || $[89] !== handleMouseOver || $[90] !== handleMouseUp || $[91] !== handleRef || $[92] !== id || $[93] !== onClick || $[94] !== style || $[95] !== t27 || $[96] !== t28 || $[97] !== trigger) {
983
- t29 = /* @__PURE__ */ jsxs("div", { className: t27, "data-ukt-id": id, onClick, onMouseDown: handleMouseDown, onMouseMove: handleMouseMove, onMouseOut: handleMouseOut, onMouseOver: handleMouseOver, onMouseUp: handleMouseUp, ref: handleRef, style, children: [
984
- trigger,
985
- t28
986
- ] });
987
- $[87] = handleMouseDown;
988
- $[88] = handleMouseOut;
989
- $[89] = handleMouseOver;
990
- $[90] = handleMouseUp;
991
- $[91] = handleRef;
992
- $[92] = id;
993
- $[93] = onClick;
994
- $[94] = style;
995
- $[95] = t27;
996
- $[96] = t28;
997
- $[97] = trigger;
998
- $[98] = t29;
999
- } else {
1000
- t29 = $[98];
1001
- }
1002
- let t30;
1003
- if ($[99] !== t26 || $[100] !== t29) {
1004
- t30 = /* @__PURE__ */ jsxs(Fragment, { children: [
1005
- t24,
1006
- t26,
1007
- t29
1008
- ] });
1009
- $[99] = t26;
1010
- $[100] = t29;
1011
- $[101] = t30;
1012
- } else {
1013
- t30 = $[101];
1014
- }
1015
- return t30;
194
+ const $ = c(86);
195
+ const { allowCreate, allowEmpty: t1, children, className, disabled, hasItems: t2, isOpenOnMount, isSearchable, keepOpenOnSubmit: t3, label, minHeightBody: t4, minWidthBody: t5, name, onActiveItem, onClick, onClose, onMouseDown, onMouseUp, onOpen, onSubmitItem, placeholder, style: styleFromProps, tabIndex, value } = t0;
196
+ const allowEmpty = t1 === void 0 ? true : t1;
197
+ const hasItems = t2 === void 0 ? true : t2;
198
+ const keepOpenOnSubmit = t3 === void 0 ? !hasItems : t3;
199
+ const minHeightBody = t4 === void 0 ? 30 : t4;
200
+ const childrenCount = Children.count(children);
201
+ if (childrenCount !== 1 && childrenCount !== 2) {
202
+ if (childrenCount === 0) throw new Error(CHILDREN_ERROR + " Received no children.");
203
+ console.error(`${CHILDREN_ERROR} Received ${childrenCount} children.`);
204
+ }
205
+ let trigger;
206
+ if (childrenCount > 1) trigger = children[0];
207
+ const [isOpen, setIsOpen] = useState(isOpenOnMount ?? false);
208
+ const [isOpening, setIsOpening] = useState(!isOpenOnMount);
209
+ const [dropdownElement, setDropdownElement] = useState(null);
210
+ const [dropdownBodyElement, setDropdownBodyElement] = useState(null);
211
+ const inputElementRef = useRef(null);
212
+ const closingTimerRef = useRef(null);
213
+ const isOpeningTimerRef = useRef(null);
214
+ const currentInputMethodRef = useRef("mouse");
215
+ const clearEnteredCharactersTimerRef = useRef(null);
216
+ const enteredCharactersRef = useRef("");
217
+ const mouseDownPositionRef = useRef(null);
218
+ const allowCreateRef = useRef(allowCreate);
219
+ const allowEmptyRef = useRef(allowEmpty);
220
+ const hasItemsRef = useRef(hasItems);
221
+ const isOpenRef = useRef(isOpen);
222
+ const isOpeningRef = useRef(isOpening);
223
+ const keepOpenOnSubmitRef = useRef(keepOpenOnSubmit);
224
+ const onCloseRef = useRef(onClose);
225
+ const onOpenRef = useRef(onOpen);
226
+ const onSubmitItemRef = useRef(onSubmitItem);
227
+ const valueRef = useRef(value);
228
+ let t6;
229
+ let t7;
230
+ if ($[0] !== allowCreate || $[1] !== allowEmpty || $[2] !== hasItems || $[3] !== isOpen || $[4] !== isOpening || $[5] !== keepOpenOnSubmit || $[6] !== onClose || $[7] !== onOpen || $[8] !== onSubmitItem || $[9] !== value) {
231
+ t6 = () => {
232
+ allowCreateRef.current = allowCreate;
233
+ allowEmptyRef.current = allowEmpty;
234
+ hasItemsRef.current = hasItems;
235
+ isOpenRef.current = isOpen;
236
+ isOpeningRef.current = isOpening;
237
+ keepOpenOnSubmitRef.current = keepOpenOnSubmit;
238
+ onCloseRef.current = onClose;
239
+ onOpenRef.current = onOpen;
240
+ onSubmitItemRef.current = onSubmitItem;
241
+ valueRef.current = value;
242
+ };
243
+ t7 = [
244
+ allowCreate,
245
+ allowEmpty,
246
+ hasItems,
247
+ isOpen,
248
+ isOpening,
249
+ keepOpenOnSubmit,
250
+ onClose,
251
+ onOpen,
252
+ onSubmitItem,
253
+ value
254
+ ];
255
+ $[0] = allowCreate;
256
+ $[1] = allowEmpty;
257
+ $[2] = hasItems;
258
+ $[3] = isOpen;
259
+ $[4] = isOpening;
260
+ $[5] = keepOpenOnSubmit;
261
+ $[6] = onClose;
262
+ $[7] = onOpen;
263
+ $[8] = onSubmitItem;
264
+ $[9] = value;
265
+ $[10] = t6;
266
+ $[11] = t7;
267
+ } else {
268
+ t6 = $[10];
269
+ t7 = $[11];
270
+ }
271
+ useEffect(t6, t7);
272
+ const isMountedRef = useRef(false);
273
+ let t8;
274
+ let t9;
275
+ if ($[12] !== isOpen) {
276
+ t8 = () => {
277
+ if (!isMountedRef.current) {
278
+ isMountedRef.current = true;
279
+ if (isOpenRef.current && onOpenRef.current) onOpenRef.current();
280
+ return;
281
+ }
282
+ if (isOpen && onOpenRef.current) onOpenRef.current();
283
+ else if (!isOpen && onCloseRef.current) onCloseRef.current();
284
+ };
285
+ t9 = [isOpen];
286
+ $[12] = isOpen;
287
+ $[13] = t8;
288
+ $[14] = t9;
289
+ } else {
290
+ t8 = $[13];
291
+ t9 = $[14];
292
+ }
293
+ useEffect(t8, t9);
294
+ let t10;
295
+ if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
296
+ t10 = () => {
297
+ setIsOpen(false);
298
+ setIsOpening(false);
299
+ mouseDownPositionRef.current = null;
300
+ if (closingTimerRef.current != null) {
301
+ clearTimeout(closingTimerRef.current);
302
+ closingTimerRef.current = null;
303
+ }
304
+ };
305
+ $[15] = t10;
306
+ } else t10 = $[15];
307
+ const closeDropdown = t10;
308
+ let t11;
309
+ if ($[16] !== dropdownElement) {
310
+ t11 = (event) => {
311
+ if (isOpenRef.current && !keepOpenOnSubmitRef.current) closingTimerRef.current = setTimeout(closeDropdown, 90);
312
+ if (!hasItemsRef.current) return;
313
+ const element = getActiveItemElement(dropdownElement);
314
+ if (!element && !allowCreateRef.current) {
315
+ if (!allowEmptyRef.current) return;
316
+ if (inputElementRef.current?.value) return;
317
+ }
318
+ let itemLabel = element?.innerText ?? "";
319
+ if (inputElementRef.current) {
320
+ if (!element) itemLabel = inputElementRef.current.value;
321
+ else inputElementRef.current.value = itemLabel;
322
+ if (inputElementRef.current === inputElementRef.current.ownerDocument.activeElement) inputElementRef.current.blur();
323
+ }
324
+ const nextValue = element?.dataset.uktValue ?? itemLabel;
325
+ if (valueRef.current && valueRef.current === nextValue) return;
326
+ if (element) {
327
+ const eventTarget = event.target;
328
+ if (element.matches(CLICKABLE_SELECTOR)) {
329
+ if (element !== eventTarget && !element.contains(eventTarget)) element.click();
330
+ } else {
331
+ const clickableElements = element.querySelectorAll(CLICKABLE_SELECTOR);
332
+ if (clickableElements.length === 1) {
333
+ const clickableElement = clickableElements[0];
334
+ if (clickableElement !== eventTarget && !clickableElement.contains(eventTarget)) clickableElement.click();
335
+ }
336
+ }
337
+ }
338
+ onSubmitItemRef.current?.({
339
+ element,
340
+ event,
341
+ label: itemLabel,
342
+ value: nextValue
343
+ });
344
+ };
345
+ $[16] = dropdownElement;
346
+ $[17] = t11;
347
+ } else t11 = $[17];
348
+ const handleSubmitItem = t11;
349
+ let t12;
350
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
351
+ t12 = (t13) => {
352
+ const { clientX, clientY } = t13;
353
+ currentInputMethodRef.current = "mouse";
354
+ const initialPosition = mouseDownPositionRef.current;
355
+ if (!initialPosition) return;
356
+ if (Math.abs(initialPosition.clientX - clientX) < 12 && Math.abs(initialPosition.clientY - clientY) < 12) return;
357
+ setIsOpening(false);
358
+ };
359
+ $[18] = t12;
360
+ } else t12 = $[18];
361
+ const handleMouseMove = t12;
362
+ let t13;
363
+ if ($[19] !== dropdownElement || $[20] !== onActiveItem) {
364
+ t13 = (event_0) => {
365
+ if (!hasItemsRef.current) return;
366
+ if (currentInputMethodRef.current !== "mouse") return;
367
+ if (!dropdownElement) return;
368
+ const itemElements = getItemElements(dropdownElement);
369
+ if (!itemElements) return;
370
+ const eventTarget_0 = event_0.target;
371
+ const element_0 = eventTarget_0.closest(ITEM_SELECTOR) ?? eventTarget_0;
372
+ for (const itemElement of itemElements) if (itemElement === element_0) {
373
+ setActiveItem({
374
+ dropdownElement,
375
+ element: element_0,
376
+ event: event_0,
377
+ onActiveItem
378
+ });
379
+ return;
380
+ }
381
+ };
382
+ $[19] = dropdownElement;
383
+ $[20] = onActiveItem;
384
+ $[21] = t13;
385
+ } else t13 = $[21];
386
+ const handleMouseOver = t13;
387
+ let t14;
388
+ if ($[22] !== dropdownElement) {
389
+ t14 = (event_1) => {
390
+ if (!hasItemsRef.current) return;
391
+ const activeItem = getActiveItemElement(dropdownElement);
392
+ if (!activeItem) return;
393
+ const eventRelatedTarget = event_1.relatedTarget;
394
+ if (activeItem !== event_1.target || activeItem.contains(eventRelatedTarget)) return;
395
+ delete activeItem.dataset.uktActive;
396
+ };
397
+ $[22] = dropdownElement;
398
+ $[23] = t14;
399
+ } else t14 = $[23];
400
+ const handleMouseOut = t14;
401
+ let t15;
402
+ if ($[24] !== onMouseDown) {
403
+ t15 = (event_2) => {
404
+ if (onMouseDown) onMouseDown(event_2);
405
+ if (isOpenRef.current) return;
406
+ setIsOpen(true);
407
+ setIsOpening(true);
408
+ mouseDownPositionRef.current = {
409
+ clientX: event_2.clientX,
410
+ clientY: event_2.clientY
411
+ };
412
+ isOpeningTimerRef.current = setTimeout(() => {
413
+ setIsOpening(false);
414
+ isOpeningTimerRef.current = null;
415
+ }, 1e3);
416
+ };
417
+ $[24] = onMouseDown;
418
+ $[25] = t15;
419
+ } else t15 = $[25];
420
+ const handleMouseDown = t15;
421
+ let t16;
422
+ if ($[26] !== handleSubmitItem || $[27] !== onMouseUp) {
423
+ t16 = (event_3) => {
424
+ if (onMouseUp) onMouseUp(event_3);
425
+ if (isOpeningRef.current || !isOpenRef.current || closingTimerRef.current != null) return;
426
+ const eventTarget_1 = event_3.target;
427
+ if (!eventTarget_1.closest(BODY_SELECTOR)) {
428
+ if (!isOpeningRef.current && inputElementRef.current !== eventTarget_1.ownerDocument.activeElement) closeDropdown();
429
+ return;
430
+ }
431
+ if (!hasItemsRef.current) return;
432
+ handleSubmitItem(event_3);
433
+ };
434
+ $[26] = handleSubmitItem;
435
+ $[27] = onMouseUp;
436
+ $[28] = t16;
437
+ } else t16 = $[28];
438
+ const handleMouseUp = t16;
439
+ let t17;
440
+ if ($[29] !== dropdownElement || $[30] !== handleSubmitItem || $[31] !== onActiveItem) {
441
+ t17 = (event_4) => {
442
+ const { altKey, ctrlKey, key, metaKey } = event_4;
443
+ const eventTarget_2 = event_4.target;
444
+ if (!dropdownElement) return;
445
+ const onEventHandled = () => {
446
+ event_4.stopPropagation();
447
+ event_4.preventDefault();
448
+ currentInputMethodRef.current = "keyboard";
449
+ };
450
+ const isEventTargetingDropdown = dropdownElement.contains(eventTarget_2);
451
+ if (!isOpenRef.current) {
452
+ if (!isEventTargetingDropdown) return;
453
+ if (key === " " || key === "Enter" || hasItemsRef.current && (key === "ArrowUp" || key === "ArrowDown")) {
454
+ onEventHandled();
455
+ setIsOpen(true);
456
+ }
457
+ return;
458
+ }
459
+ const isTargetUsingKeyEvents = isEventTargetUsingKeyEvent(event_4);
460
+ if (hasItemsRef.current && !isTargetUsingKeyEvents) {
461
+ let isEditingCharacters = !ctrlKey && !metaKey && /^[A-Za-z0-9]$/.test(key);
462
+ if (!isEditingCharacters && enteredCharactersRef.current) isEditingCharacters = key === " " || key === "Backspace";
463
+ if (isEditingCharacters) {
464
+ onEventHandled();
465
+ if (key === "Backspace") enteredCharactersRef.current = enteredCharactersRef.current.slice(0, -1);
466
+ else enteredCharactersRef.current = enteredCharactersRef.current + key;
467
+ setActiveItem({
468
+ dropdownElement,
469
+ event: event_4,
470
+ isExactMatch: allowCreateRef.current,
471
+ onActiveItem,
472
+ text: enteredCharactersRef.current
473
+ });
474
+ if (clearEnteredCharactersTimerRef.current != null) clearTimeout(clearEnteredCharactersTimerRef.current);
475
+ clearEnteredCharactersTimerRef.current = setTimeout(() => {
476
+ enteredCharactersRef.current = "";
477
+ clearEnteredCharactersTimerRef.current = null;
478
+ }, 1500);
479
+ return;
480
+ }
481
+ }
482
+ if (key === "Enter" || key === " " && !inputElementRef.current) {
483
+ onEventHandled();
484
+ handleSubmitItem(event_4);
485
+ return;
486
+ }
487
+ if (key === "Escape" || isEventTargetingDropdown && key === " " && !hasItemsRef.current) {
488
+ if (hasItemsRef.current || !isTargetUsingKeyEvents) closeDropdown();
489
+ return;
490
+ }
491
+ if (hasItemsRef.current) {
492
+ if (key === "ArrowUp") {
493
+ onEventHandled();
494
+ if (altKey || metaKey) setActiveItem({
495
+ dropdownElement,
496
+ event: event_4,
497
+ index: 0,
498
+ onActiveItem
499
+ });
500
+ else setActiveItem({
501
+ dropdownElement,
502
+ event: event_4,
503
+ indexAddend: -1,
504
+ onActiveItem
505
+ });
506
+ return;
507
+ }
508
+ if (key === "ArrowDown") {
509
+ onEventHandled();
510
+ if (altKey || metaKey) setActiveItem({
511
+ dropdownElement,
512
+ event: event_4,
513
+ index: -1,
514
+ onActiveItem
515
+ });
516
+ else setActiveItem({
517
+ dropdownElement,
518
+ event: event_4,
519
+ indexAddend: 1,
520
+ onActiveItem
521
+ });
522
+ return;
523
+ }
524
+ }
525
+ };
526
+ $[29] = dropdownElement;
527
+ $[30] = handleSubmitItem;
528
+ $[31] = onActiveItem;
529
+ $[32] = t17;
530
+ } else t17 = $[32];
531
+ const handleKeyDown = t17;
532
+ let t18;
533
+ if ($[33] !== handleKeyDown) {
534
+ t18 = {
535
+ ignoreUsedKeyboardEvents: false,
536
+ onKeyDown: handleKeyDown
537
+ };
538
+ $[33] = handleKeyDown;
539
+ $[34] = t18;
540
+ } else t18 = $[34];
541
+ useKeyboardEvents(t18);
542
+ let t19;
543
+ if ($[35] !== isOpenOnMount || $[36] !== onActiveItem) {
544
+ t19 = (ref) => {
545
+ setDropdownElement(ref);
546
+ if (!ref) return;
547
+ const { ownerDocument } = ref;
548
+ let inputElement = inputElementRef.current;
549
+ if (!inputElement && ref.firstElementChild) {
550
+ if (ref.firstElementChild.matches(TEXT_INPUT_SELECTOR)) inputElement = ref.firstElementChild;
551
+ else inputElement = ref.firstElementChild.querySelector(TEXT_INPUT_SELECTOR);
552
+ inputElementRef.current = inputElement;
553
+ }
554
+ const handleGlobalMouseDown = (t20) => {
555
+ const { target } = t20;
556
+ const eventTarget_3 = target;
557
+ if (!ref.contains(eventTarget_3)) closeDropdown();
558
+ };
559
+ const handleGlobalMouseUp = (t21) => {
560
+ const { target: target_0 } = t21;
561
+ if (!isOpenRef.current || closingTimerRef.current != null) return;
562
+ if (isOpeningRef.current) {
563
+ setIsOpening(false);
564
+ if (isOpeningTimerRef.current != null) {
565
+ clearTimeout(isOpeningTimerRef.current);
566
+ isOpeningTimerRef.current = null;
567
+ }
568
+ return;
569
+ }
570
+ const eventTarget_4 = target_0;
571
+ if (!ref.contains(eventTarget_4)) closeDropdown();
572
+ };
573
+ const handleGlobalFocusIn = (t22) => {
574
+ const { target: target_1 } = t22;
575
+ if (!isOpenRef.current) return;
576
+ const eventTarget_5 = target_1;
577
+ if (ref.contains(eventTarget_5) || eventTarget_5.contains(ref)) return;
578
+ closeDropdown();
579
+ };
580
+ document.addEventListener("focusin", handleGlobalFocusIn);
581
+ document.addEventListener("mousedown", handleGlobalMouseDown);
582
+ document.addEventListener("mouseup", handleGlobalMouseUp);
583
+ if (ownerDocument !== document) {
584
+ ownerDocument.addEventListener("focusin", handleGlobalFocusIn);
585
+ ownerDocument.addEventListener("mousedown", handleGlobalMouseDown);
586
+ ownerDocument.addEventListener("mouseup", handleGlobalMouseUp);
587
+ }
588
+ if (isOpenOnMount) ref.focus();
589
+ const handleInput = (event_5) => {
590
+ if (!isOpenRef.current) setIsOpen(true);
591
+ const input = event_5.target;
592
+ const isDeleting = enteredCharactersRef.current.length > input.value.length;
593
+ enteredCharactersRef.current = input.value;
594
+ if (isDeleting && input.value.length && getActiveItemElement(ref)) return;
595
+ setActiveItem({
596
+ dropdownElement: ref,
597
+ event: event_5,
598
+ isExactMatch: allowCreateRef.current,
599
+ onActiveItem,
600
+ text: enteredCharactersRef.current
601
+ });
602
+ };
603
+ if (inputElement) inputElement.addEventListener("input", handleInput);
604
+ return () => {
605
+ document.removeEventListener("focusin", handleGlobalFocusIn);
606
+ document.removeEventListener("mousedown", handleGlobalMouseDown);
607
+ document.removeEventListener("mouseup", handleGlobalMouseUp);
608
+ if (ownerDocument !== document) {
609
+ ownerDocument.removeEventListener("focusin", handleGlobalFocusIn);
610
+ ownerDocument.removeEventListener("mousedown", handleGlobalMouseDown);
611
+ ownerDocument.removeEventListener("mouseup", handleGlobalMouseUp);
612
+ }
613
+ if (inputElement) inputElement.removeEventListener("input", handleInput);
614
+ };
615
+ };
616
+ $[35] = isOpenOnMount;
617
+ $[36] = onActiveItem;
618
+ $[37] = t19;
619
+ } else t19 = $[37];
620
+ const handleRef = t19;
621
+ if (!isValidElement(trigger)) if (isSearchable) {
622
+ const t20 = value ?? "";
623
+ let t21;
624
+ if ($[38] === Symbol.for("react.memo_cache_sentinel")) {
625
+ t21 = () => setIsOpen(true);
626
+ $[38] = t21;
627
+ } else t21 = $[38];
628
+ let t22;
629
+ if ($[39] !== disabled || $[40] !== name || $[41] !== placeholder || $[42] !== t20 || $[43] !== tabIndex) {
630
+ t22 = /* @__PURE__ */ jsx("input", {
631
+ autoComplete: "off",
632
+ className: TRIGGER_CLASS_NAME,
633
+ defaultValue: t20,
634
+ disabled,
635
+ name,
636
+ onFocus: t21,
637
+ placeholder,
638
+ ref: inputElementRef,
639
+ tabIndex,
640
+ type: "text"
641
+ });
642
+ $[39] = disabled;
643
+ $[40] = name;
644
+ $[41] = placeholder;
645
+ $[42] = t20;
646
+ $[43] = tabIndex;
647
+ $[44] = t22;
648
+ } else t22 = $[44];
649
+ trigger = t22;
650
+ } else {
651
+ let t20;
652
+ if ($[45] !== trigger) {
653
+ t20 = /* @__PURE__ */ jsx("button", {
654
+ className: TRIGGER_CLASS_NAME,
655
+ tabIndex: 0,
656
+ type: "button",
657
+ children: trigger
658
+ });
659
+ $[45] = trigger;
660
+ $[46] = t20;
661
+ } else t20 = $[46];
662
+ trigger = t20;
663
+ }
664
+ if (label) {
665
+ let t20;
666
+ if ($[47] !== label) {
667
+ t20 = /* @__PURE__ */ jsx("div", {
668
+ className: LABEL_TEXT_CLASS_NAME,
669
+ children: label
670
+ });
671
+ $[47] = label;
672
+ $[48] = t20;
673
+ } else t20 = $[48];
674
+ let t21;
675
+ if ($[49] !== t20 || $[50] !== trigger) {
676
+ t21 = /* @__PURE__ */ jsxs("label", {
677
+ className: LABEL_CLASS_NAME,
678
+ children: [t20, trigger]
679
+ });
680
+ $[49] = t20;
681
+ $[50] = trigger;
682
+ $[51] = t21;
683
+ } else t21 = $[51];
684
+ trigger = t21;
685
+ }
686
+ const dropdownRect = useBoundingClientRect(dropdownElement);
687
+ const dropdownBodyRect = useBoundingClientRect(dropdownBodyElement);
688
+ let t20;
689
+ if ($[52] !== dropdownBodyElement) {
690
+ t20 = getBoundingAncestor(dropdownBodyElement);
691
+ $[52] = dropdownBodyElement;
692
+ $[53] = t20;
693
+ } else t20 = $[53];
694
+ const boundingElementRect = useBoundingClientRect(t20);
695
+ let maxHeight;
696
+ if (dropdownBodyRect.top != null && dropdownRect.top != null && boundingElementRect.top != null) {
697
+ const maxHeightUp = dropdownBodyRect.bottom - boundingElementRect.top;
698
+ const maxHeightDown = boundingElementRect.bottom - dropdownBodyRect.top;
699
+ let t21;
700
+ if ($[54] !== dropdownBodyRect.top || $[55] !== dropdownRect.top || $[56] !== maxHeightDown || $[57] !== maxHeightUp) {
701
+ t21 = Math.round(dropdownBodyRect.top > dropdownRect.top ? maxHeightDown : maxHeightUp);
702
+ $[54] = dropdownBodyRect.top;
703
+ $[55] = dropdownRect.top;
704
+ $[56] = maxHeightDown;
705
+ $[57] = maxHeightUp;
706
+ $[58] = t21;
707
+ } else t21 = $[58];
708
+ maxHeight = t21;
709
+ }
710
+ let t21;
711
+ if ($[59] !== maxHeight || $[60] !== minHeightBody) {
712
+ t21 = maxHeight != null && maxHeight > minHeightBody ? { [BODY_MAX_HEIGHT_VAR]: `calc(${maxHeight}px - var(--uktdd-body-buffer))` } : null;
713
+ $[59] = maxHeight;
714
+ $[60] = minHeightBody;
715
+ $[61] = t21;
716
+ } else t21 = $[61];
717
+ let t22;
718
+ if ($[62] !== styleFromProps || $[63] !== t21) {
719
+ t22 = {
720
+ ...styleFromProps,
721
+ ...t21
722
+ };
723
+ $[62] = styleFromProps;
724
+ $[63] = t21;
725
+ $[64] = t22;
726
+ } else t22 = $[64];
727
+ const style = t22;
728
+ let t23;
729
+ if ($[65] === Symbol.for("react.memo_cache_sentinel")) {
730
+ t23 = /* @__PURE__ */ jsx(Style, {
731
+ href: "@acusti/dropdown/Dropdown",
732
+ children: STYLES
733
+ });
734
+ $[65] = t23;
735
+ } else t23 = $[65];
736
+ let t24;
737
+ if ($[66] !== className || $[67] !== disabled || $[68] !== isOpen || $[69] !== isSearchable) {
738
+ t24 = clsx(ROOT_CLASS_NAME, className, {
739
+ disabled,
740
+ "is-open": isOpen,
741
+ "is-searchable": isSearchable
742
+ });
743
+ $[66] = className;
744
+ $[67] = disabled;
745
+ $[68] = isOpen;
746
+ $[69] = isSearchable;
747
+ $[70] = t24;
748
+ } else t24 = $[70];
749
+ let t25;
750
+ if ($[71] !== children || $[72] !== childrenCount || $[73] !== isOpen) {
751
+ t25 = isOpen ? /* @__PURE__ */ jsx("div", {
752
+ className: BODY_CLASS_NAME,
753
+ ref: setDropdownBodyElement,
754
+ children: childrenCount > 1 ? children[1] : children
755
+ }) : null;
756
+ $[71] = children;
757
+ $[72] = childrenCount;
758
+ $[73] = isOpen;
759
+ $[74] = t25;
760
+ } else t25 = $[74];
761
+ let t26;
762
+ if ($[75] !== handleMouseDown || $[76] !== handleMouseOut || $[77] !== handleMouseOver || $[78] !== handleMouseUp || $[79] !== handleRef || $[80] !== onClick || $[81] !== style || $[82] !== t24 || $[83] !== t25 || $[84] !== trigger) {
763
+ t26 = /* @__PURE__ */ jsxs(Fragment, { children: [t23, /* @__PURE__ */ jsxs("div", {
764
+ className: t24,
765
+ onClick,
766
+ onMouseDown: handleMouseDown,
767
+ onMouseMove: handleMouseMove,
768
+ onMouseOut: handleMouseOut,
769
+ onMouseOver: handleMouseOver,
770
+ onMouseUp: handleMouseUp,
771
+ ref: handleRef,
772
+ style,
773
+ children: [trigger, t25]
774
+ })] });
775
+ $[75] = handleMouseDown;
776
+ $[76] = handleMouseOut;
777
+ $[77] = handleMouseOver;
778
+ $[78] = handleMouseUp;
779
+ $[79] = handleRef;
780
+ $[80] = onClick;
781
+ $[81] = style;
782
+ $[82] = t24;
783
+ $[83] = t25;
784
+ $[84] = trigger;
785
+ $[85] = t26;
786
+ } else t26 = $[85];
787
+ return t26;
1016
788
  }
1017
789
  function getBoundingAncestor(element) {
1018
- while (element?.parentElement) {
1019
- if (element.parentElement.tagName === "BODY") return element.parentElement;
1020
- if (getComputedStyle(element.parentElement).overflowX !== "visible") {
1021
- return element.parentElement;
1022
- }
1023
- element = element.parentElement;
1024
- }
1025
- return null;
790
+ while (element?.parentElement) {
791
+ if (element.parentElement.tagName === "BODY") return element.parentElement;
792
+ if (getComputedStyle(element.parentElement).overflowX !== "visible") return element.parentElement;
793
+ element = element.parentElement;
794
+ }
795
+ return null;
1026
796
  }
1027
- export {
1028
- Dropdown as default
1029
- };
1030
- //# sourceMappingURL=Dropdown.js.map
797
+ export { Dropdown as default };
798
+
799
+ //# sourceMappingURL=Dropdown.js.map