@strapi/ui-primitives 2.0.0-rc.9 → 2.0.1

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/index.mjs CHANGED
@@ -17,6 +17,7 @@ import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
17
17
  import { hideOthers } from "aria-hidden";
18
18
  import * as ReactDOM from "react-dom";
19
19
  import { RemoveScroll } from "react-remove-scroll";
20
+ import { useVirtualizer } from "@tanstack/react-virtual";
20
21
  import { clamp } from "@radix-ui/number";
21
22
  import { createCollection as createCollection$1 } from "@radix-ui/react-collection";
22
23
  import { useDirection } from "@radix-ui/react-direction";
@@ -151,6 +152,75 @@ const usePrev = (value) => {
151
152
  });
152
153
  return ref.current;
153
154
  };
155
+ const VirtualizedViewport = React.forwardRef(
156
+ ({ children, estimatedItemSize = 40, overscan = 5, getItemCount, onViewportChange, ...props }, forwardedRef) => {
157
+ const parentRef = React.useRef(null);
158
+ const composedRefs = useComposedRefs(forwardedRef, parentRef, onViewportChange);
159
+ const childArray = React.useMemo(() => React.Children.toArray(children), [children]);
160
+ const virtualizer = useVirtualizer({
161
+ count: getItemCount(),
162
+ getScrollElement: () => parentRef.current,
163
+ estimateSize: () => estimatedItemSize,
164
+ overscan
165
+ });
166
+ const virtualItems = virtualizer.getVirtualItems();
167
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
168
+ /* @__PURE__ */ jsx(
169
+ "style",
170
+ {
171
+ dangerouslySetInnerHTML: {
172
+ __html: `[data-radix-combobox-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-combobox-viewport]::-webkit-scrollbar{display:none}`
173
+ }
174
+ }
175
+ ),
176
+ /* @__PURE__ */ jsx(
177
+ Primitive.div,
178
+ {
179
+ "data-radix-combobox-viewport": "",
180
+ role: "presentation",
181
+ ...props,
182
+ ref: composedRefs,
183
+ style: {
184
+ position: "relative",
185
+ flex: 1,
186
+ overflow: "auto",
187
+ ...props.style
188
+ },
189
+ children: /* @__PURE__ */ jsx(
190
+ "div",
191
+ {
192
+ style: {
193
+ height: `${virtualizer.getTotalSize()}px`,
194
+ width: "100%",
195
+ position: "relative"
196
+ },
197
+ children: virtualItems.map((virtualItem) => {
198
+ const child = childArray[virtualItem.index];
199
+ return /* @__PURE__ */ jsx(
200
+ "div",
201
+ {
202
+ "data-index": virtualItem.index,
203
+ ref: virtualizer.measureElement,
204
+ style: {
205
+ position: "absolute",
206
+ top: 0,
207
+ left: 0,
208
+ width: "100%",
209
+ transform: `translateY(${virtualItem.start}px)`
210
+ },
211
+ children: child
212
+ },
213
+ virtualItem.key
214
+ );
215
+ })
216
+ }
217
+ )
218
+ }
219
+ )
220
+ ] });
221
+ }
222
+ );
223
+ VirtualizedViewport.displayName = "VirtualizedViewport";
154
224
  const OPEN_KEYS$1 = [" ", "Enter", "ArrowUp", "ArrowDown"];
155
225
  const SELECTION_KEYS$1 = ["Enter"];
156
226
  const defaultIsPrintableCharacter = (str) => {
@@ -195,7 +265,11 @@ const Combobox = (props) => {
195
265
  filterValue: filterValueProp,
196
266
  defaultFilterValue,
197
267
  onFilterValueChange,
198
- isPrintableCharacter = defaultIsPrintableCharacter
268
+ isPrintableCharacter = defaultIsPrintableCharacter,
269
+ visible = false,
270
+ virtualized = "auto",
271
+ estimatedItemSize = 40,
272
+ overscan = 5
199
273
  } = props;
200
274
  const [trigger, setTrigger] = React.useState(null);
201
275
  const [viewport, setViewport] = React.useState(null);
@@ -284,6 +358,10 @@ const Combobox = (props) => {
284
358
  onFilterValueChange: setFilterValue,
285
359
  onVisuallyFocussedItemChange: setVisuallyFocussedItem,
286
360
  isPrintableCharacter,
361
+ visible,
362
+ virtualized,
363
+ estimatedItemSize,
364
+ overscan,
287
365
  children
288
366
  }
289
367
  ) });
@@ -691,6 +769,22 @@ const VIEWPORT_NAME$1 = "ComboboxViewport";
691
769
  const ComboboxViewport = React.forwardRef((props, forwardedRef) => {
692
770
  const comboboxContext = useComboboxContext(VIEWPORT_NAME$1);
693
771
  const composedRefs = useComposedRefs(forwardedRef, comboboxContext.onViewportChange);
772
+ const childArray = React.useMemo(() => React.Children.toArray(props.children), [props.children]);
773
+ const itemCount = childArray.length;
774
+ const shouldVirtualize = comboboxContext.virtualized === true || comboboxContext.virtualized === "auto" && itemCount > 100;
775
+ if (shouldVirtualize) {
776
+ return /* @__PURE__ */ jsx(Collection$1.Slot, { scope: void 0, children: /* @__PURE__ */ jsx(
777
+ VirtualizedViewport,
778
+ {
779
+ ...props,
780
+ ref: composedRefs,
781
+ getItemCount: () => itemCount,
782
+ estimatedItemSize: comboboxContext.estimatedItemSize,
783
+ overscan: comboboxContext.overscan,
784
+ onViewportChange: comboboxContext.onViewportChange
785
+ }
786
+ ) });
787
+ }
694
788
  return /* @__PURE__ */ jsxs(Fragment, { children: [
695
789
  /* @__PURE__ */ jsx(
696
790
  "style",
@@ -851,18 +945,31 @@ const ComboboxItemIndicator = React.forwardRef((props, forwardedRef) => {
851
945
  ComboboxItemIndicator.displayName = ITEM_INDICATOR_NAME$1;
852
946
  const NO_VALUE_FOUND_NAME = "ComboboxNoValueFound";
853
947
  const ComboboxNoValueFound = React.forwardRef((props, ref) => {
854
- const { textValue = "", filterValue = "", locale, autocomplete } = useComboboxContext(NO_VALUE_FOUND_NAME);
948
+ const {
949
+ textValue = "",
950
+ filterValue = "",
951
+ visible = false,
952
+ locale,
953
+ autocomplete
954
+ } = useComboboxContext(NO_VALUE_FOUND_NAME);
855
955
  const [items, setItems] = React.useState([]);
856
956
  const { subscribe } = useCollection$1(void 0);
857
957
  const { startsWith, contains } = useFilter(locale, { sensitivity: "base" });
858
958
  React.useEffect(() => {
859
959
  const unsub = subscribe((state) => {
860
- setItems(state);
960
+ if (visible) {
961
+ const filteredItems = state.filter((item) => item.type !== "create");
962
+ setItems(filteredItems);
963
+ } else {
964
+ setItems(state);
965
+ }
861
966
  });
862
967
  return () => {
863
968
  unsub();
864
969
  };
865
- }, [subscribe]);
970
+ }, [visible, subscribe]);
971
+ if (autocomplete.type === "none" && items.length > 0)
972
+ return null;
866
973
  if (autocomplete.type === "list" && autocomplete.filter === "startsWith" && items.some((item) => startsWith(item.textValue, textValue))) {
867
974
  return null;
868
975
  }
@@ -909,7 +1016,7 @@ const ComboboxCreateItem = React.forwardRef((props, ref) => {
909
1016
  unsub();
910
1017
  };
911
1018
  }, [textValue, subscribe, getItems]);
912
- if (!textValue || !show) {
1019
+ if ((!textValue || !show) && !context.visible) {
913
1020
  return null;
914
1021
  }
915
1022
  return /* @__PURE__ */ jsx(