@entur/dropdown 8.0.8 → 8.1.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.
@@ -385,6 +385,38 @@ const useResolvedItems = (itemsOrItemsResolver, debounceTimeout = 250) => {
385
385
  fetchItems: debouncedFetchItems
386
386
  };
387
387
  };
388
+ function getActiveElement(root) {
389
+ const active = root.activeElement;
390
+ if (active?.shadowRoot) {
391
+ return getActiveElement(active.shadowRoot);
392
+ }
393
+ return active;
394
+ }
395
+ function createShadowEnvironment(shadowRoot) {
396
+ return {
397
+ addEventListener: shadowRoot.addEventListener.bind(shadowRoot),
398
+ removeEventListener: shadowRoot.removeEventListener.bind(shadowRoot),
399
+ Node: window.Node,
400
+ document: new Proxy(document, {
401
+ get(target, prop, receiver) {
402
+ if (prop === "activeElement") {
403
+ return getActiveElement(shadowRoot);
404
+ }
405
+ const value = Reflect.get(target, prop, receiver);
406
+ return typeof value === "function" ? value.bind(target) : value;
407
+ }
408
+ })
409
+ };
410
+ }
411
+ function useShadowDomEnvironment(ref) {
412
+ return useMemo(() => {
413
+ const el = ref.current;
414
+ if (!el) return void 0;
415
+ const root = el.getRootNode();
416
+ if (!(root instanceof ShadowRoot)) return void 0;
417
+ return createShadowEnvironment(root);
418
+ }, [ref.current]);
419
+ }
388
420
  const EMPTY_INPUT = "";
389
421
  function lowerCaseFilterTest(item, input) {
390
422
  if (!input) {
@@ -535,6 +567,7 @@ const SearchableDropdown = React.forwardRef(
535
567
  const [showSelectedItem, setShowSelectedItem] = useState(value !== null);
536
568
  const [lastHighlightedIndex, setLastHighlightedIndex] = useState(0);
537
569
  const inputRef = useRef(null);
570
+ const environment = useShadowDomEnvironment(inputRef);
538
571
  const {
539
572
  items: normalizedItems,
540
573
  loading: resolvedItemsLoading,
@@ -549,7 +582,9 @@ const SearchableDropdown = React.forwardRef(
549
582
  if (shouldRefetchItems) fetchItems(inputValue2 ?? EMPTY_INPUT);
550
583
  filterListItems({ inputValue: inputValue2 ?? EMPTY_INPUT });
551
584
  };
552
- const inputHasFocus = typeof document !== "undefined" ? inputRef?.current === document?.activeElement : false;
585
+ const inputHasFocus = typeof document !== "undefined" && inputRef.current === getActiveElement(
586
+ inputRef.current?.getRootNode() ?? document
587
+ );
553
588
  useEffect(() => {
554
589
  filterListItems({ inputValue });
555
590
  }, [normalizedItems]);
@@ -628,6 +663,7 @@ const SearchableDropdown = React.forwardRef(
628
663
  itemToString,
629
664
  selectedItem: value,
630
665
  stateReducer,
666
+ ...environment && { environment },
631
667
  onInputValueChange(changes) {
632
668
  updateListItems({ inputValue: changes.inputValue });
633
669
  },
@@ -850,6 +886,7 @@ const MultiSelect = React.forwardRef(
850
886
  }, ref) => {
851
887
  const [lastHighlightedIndex, setLastHighlightedIndex] = React.useState(0);
852
888
  const inputRef = useRef(null);
889
+ const environment = useShadowDomEnvironment(inputRef);
853
890
  useEffect(() => {
854
891
  if (rest.selectedItem !== void 0)
855
892
  console.warn(
@@ -926,6 +963,7 @@ const MultiSelect = React.forwardRef(
926
963
  // @ts-expect-error prop missing from library types
927
964
  itemToString,
928
965
  itemToKey,
966
+ ...environment && { environment },
929
967
  onSelectedItemsChange({ selectedItems: newSelectedItems }) {
930
968
  onChange(newSelectedItems);
931
969
  }
@@ -1007,6 +1045,7 @@ const MultiSelect = React.forwardRef(
1007
1045
  itemToString,
1008
1046
  selectedItem: null,
1009
1047
  stateReducer,
1048
+ ...environment && { environment },
1010
1049
  onInputValueChange(changes) {
1011
1050
  updateListItems({ inputValue: changes.inputValue });
1012
1051
  },
@@ -1237,6 +1276,8 @@ const Dropdown = React.forwardRef(
1237
1276
  }, ref) => {
1238
1277
  const { items: normalizedItems, loading: resolvedItemsLoading } = useResolvedItems(initialItems);
1239
1278
  const isFilled = selectedItem !== null || placeholder !== void 0;
1279
+ const toggleButtonRef = useRef(null);
1280
+ const environment = useShadowDomEnvironment(toggleButtonRef);
1240
1281
  let floatingRefs;
1241
1282
  const {
1242
1283
  isOpen,
@@ -1251,8 +1292,10 @@ const Dropdown = React.forwardRef(
1251
1292
  items: normalizedItems,
1252
1293
  defaultHighlightedIndex: selectedItem ? void 0 : 0,
1253
1294
  selectedItem,
1295
+ ...environment && { environment },
1254
1296
  stateReducer(state, { changes, type }) {
1255
- const toggleButtonIsFocused = typeof document !== "undefined" && document.activeElement === floatingRefs?.reference.current;
1297
+ const root = toggleButtonRef.current?.getRootNode() ?? document;
1298
+ const toggleButtonIsFocused = getActiveElement(root) === floatingRefs?.reference.current;
1256
1299
  switch (type) {
1257
1300
  case useSelect.stateChangeTypes.ToggleButtonKeyDownArrowDown:
1258
1301
  case useSelect.stateChangeTypes.ToggleButtonKeyDownArrowUp:
@@ -1303,7 +1346,7 @@ const Dropdown = React.forwardRef(
1303
1346
  isFilled
1304
1347
  });
1305
1348
  const toggleButtonProps = getToggleButtonProps({
1306
- ref: mergeRefs(ref, refs.setReference),
1349
+ ref: mergeRefs(ref, refs.setReference, toggleButtonRef),
1307
1350
  "aria-disabled": disabled,
1308
1351
  "aria-label": disabled ? "Disabled dropdown" : "",
1309
1352
  disabled,