@entur/dropdown 8.0.9 → 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.
@@ -387,6 +387,38 @@ const useResolvedItems = (itemsOrItemsResolver, debounceTimeout = 250) => {
387
387
  fetchItems: debouncedFetchItems
388
388
  };
389
389
  };
390
+ function getActiveElement(root) {
391
+ const active = root.activeElement;
392
+ if (active?.shadowRoot) {
393
+ return getActiveElement(active.shadowRoot);
394
+ }
395
+ return active;
396
+ }
397
+ function createShadowEnvironment(shadowRoot) {
398
+ return {
399
+ addEventListener: shadowRoot.addEventListener.bind(shadowRoot),
400
+ removeEventListener: shadowRoot.removeEventListener.bind(shadowRoot),
401
+ Node: window.Node,
402
+ document: new Proxy(document, {
403
+ get(target, prop, receiver) {
404
+ if (prop === "activeElement") {
405
+ return getActiveElement(shadowRoot);
406
+ }
407
+ const value = Reflect.get(target, prop, receiver);
408
+ return typeof value === "function" ? value.bind(target) : value;
409
+ }
410
+ })
411
+ };
412
+ }
413
+ function useShadowDomEnvironment(ref) {
414
+ return React.useMemo(() => {
415
+ const el = ref.current;
416
+ if (!el) return void 0;
417
+ const root = el.getRootNode();
418
+ if (!(root instanceof ShadowRoot)) return void 0;
419
+ return createShadowEnvironment(root);
420
+ }, [ref.current]);
421
+ }
390
422
  const EMPTY_INPUT = "";
391
423
  function lowerCaseFilterTest(item, input) {
392
424
  if (!input) {
@@ -537,6 +569,7 @@ const SearchableDropdown = React.forwardRef(
537
569
  const [showSelectedItem, setShowSelectedItem] = React.useState(value !== null);
538
570
  const [lastHighlightedIndex, setLastHighlightedIndex] = React.useState(0);
539
571
  const inputRef = React.useRef(null);
572
+ const environment = useShadowDomEnvironment(inputRef);
540
573
  const {
541
574
  items: normalizedItems,
542
575
  loading: resolvedItemsLoading,
@@ -551,7 +584,9 @@ const SearchableDropdown = React.forwardRef(
551
584
  if (shouldRefetchItems) fetchItems(inputValue2 ?? EMPTY_INPUT);
552
585
  filterListItems({ inputValue: inputValue2 ?? EMPTY_INPUT });
553
586
  };
554
- const inputHasFocus = typeof document !== "undefined" ? inputRef?.current === document?.activeElement : false;
587
+ const inputHasFocus = typeof document !== "undefined" && inputRef.current === getActiveElement(
588
+ inputRef.current?.getRootNode() ?? document
589
+ );
555
590
  React.useEffect(() => {
556
591
  filterListItems({ inputValue });
557
592
  }, [normalizedItems]);
@@ -630,6 +665,7 @@ const SearchableDropdown = React.forwardRef(
630
665
  itemToString,
631
666
  selectedItem: value,
632
667
  stateReducer,
668
+ ...environment && { environment },
633
669
  onInputValueChange(changes) {
634
670
  updateListItems({ inputValue: changes.inputValue });
635
671
  },
@@ -852,6 +888,7 @@ const MultiSelect = React.forwardRef(
852
888
  }, ref) => {
853
889
  const [lastHighlightedIndex, setLastHighlightedIndex] = React.useState(0);
854
890
  const inputRef = React.useRef(null);
891
+ const environment = useShadowDomEnvironment(inputRef);
855
892
  React.useEffect(() => {
856
893
  if (rest.selectedItem !== void 0)
857
894
  console.warn(
@@ -928,6 +965,7 @@ const MultiSelect = React.forwardRef(
928
965
  // @ts-expect-error prop missing from library types
929
966
  itemToString,
930
967
  itemToKey,
968
+ ...environment && { environment },
931
969
  onSelectedItemsChange({ selectedItems: newSelectedItems }) {
932
970
  onChange(newSelectedItems);
933
971
  }
@@ -1009,6 +1047,7 @@ const MultiSelect = React.forwardRef(
1009
1047
  itemToString,
1010
1048
  selectedItem: null,
1011
1049
  stateReducer,
1050
+ ...environment && { environment },
1012
1051
  onInputValueChange(changes) {
1013
1052
  updateListItems({ inputValue: changes.inputValue });
1014
1053
  },
@@ -1239,6 +1278,8 @@ const Dropdown = React.forwardRef(
1239
1278
  }, ref) => {
1240
1279
  const { items: normalizedItems, loading: resolvedItemsLoading } = useResolvedItems(initialItems);
1241
1280
  const isFilled = selectedItem !== null || placeholder !== void 0;
1281
+ const toggleButtonRef = React.useRef(null);
1282
+ const environment = useShadowDomEnvironment(toggleButtonRef);
1242
1283
  let floatingRefs;
1243
1284
  const {
1244
1285
  isOpen,
@@ -1253,8 +1294,10 @@ const Dropdown = React.forwardRef(
1253
1294
  items: normalizedItems,
1254
1295
  defaultHighlightedIndex: selectedItem ? void 0 : 0,
1255
1296
  selectedItem,
1297
+ ...environment && { environment },
1256
1298
  stateReducer(state, { changes, type }) {
1257
- const toggleButtonIsFocused = typeof document !== "undefined" && document.activeElement === floatingRefs?.reference.current;
1299
+ const root = toggleButtonRef.current?.getRootNode() ?? document;
1300
+ const toggleButtonIsFocused = getActiveElement(root) === floatingRefs?.reference.current;
1258
1301
  switch (type) {
1259
1302
  case downshift.useSelect.stateChangeTypes.ToggleButtonKeyDownArrowDown:
1260
1303
  case downshift.useSelect.stateChangeTypes.ToggleButtonKeyDownArrowUp:
@@ -1305,7 +1348,7 @@ const Dropdown = React.forwardRef(
1305
1348
  isFilled
1306
1349
  });
1307
1350
  const toggleButtonProps = getToggleButtonProps({
1308
- ref: utils.mergeRefs(ref, refs.setReference),
1351
+ ref: utils.mergeRefs(ref, refs.setReference, toggleButtonRef),
1309
1352
  "aria-disabled": disabled,
1310
1353
  "aria-label": disabled ? "Disabled dropdown" : "",
1311
1354
  disabled,