@sanity/ui 0.37.5 → 0.37.6

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 (41) hide show
  1. package/lib/dts/components/autocomplete/autocomplete.d.ts.map +1 -1
  2. package/lib/dts/components/autocomplete/autocomplete.styles.d.ts.map +1 -1
  3. package/lib/dts/components/menu/useMenuController.d.ts.map +1 -1
  4. package/lib/dts/components/tab/tab.d.ts +1 -1
  5. package/lib/dts/components/tab/tab.d.ts.map +1 -1
  6. package/lib/dts/components/tree/tree.d.ts +1 -1
  7. package/lib/dts/components/tree/tree.d.ts.map +1 -1
  8. package/lib/dts/components/tree/treeGroup.d.ts +1 -1
  9. package/lib/dts/components/tree/treeGroup.d.ts.map +1 -1
  10. package/lib/dts/components/tree/treeItem.d.ts +1 -1
  11. package/lib/dts/components/tree/treeItem.d.ts.map +1 -1
  12. package/lib/dts/hooks/usePrefersDark.d.ts.map +1 -1
  13. package/lib/dts/primitives/textInput/textInput.d.ts.map +1 -1
  14. package/lib/dts/styles/input/textInputStyle.d.ts.map +1 -1
  15. package/lib/dts/utils/errorBoundary.d.ts.map +1 -1
  16. package/lib/sanity-ui.js +64 -37
  17. package/lib/sanity-ui.js.map +1 -1
  18. package/lib/sanity-ui.modern.js +76 -38
  19. package/lib/sanity-ui.modern.js.map +1 -1
  20. package/lib/sanity-ui.module.js +65 -38
  21. package/lib/sanity-ui.module.js.map +1 -1
  22. package/package.json +10 -10
  23. package/src/components/autocomplete/__workshop__/custom.tsx +10 -4
  24. package/src/components/autocomplete/__workshop__/example.tsx +38 -15
  25. package/src/components/autocomplete/__workshop__/fullscreen.tsx +31 -30
  26. package/src/components/autocomplete/autocomplete.styles.tsx +1 -0
  27. package/src/components/autocomplete/autocomplete.tsx +24 -3
  28. package/src/components/menu/useMenuController.ts +4 -8
  29. package/src/components/tab/tab.tsx +1 -1
  30. package/src/components/tree/__workshop__/basic.perf.ts +32 -0
  31. package/src/components/tree/__workshop__/basic.tsx +92 -56
  32. package/src/components/tree/__workshop__/index.ts +6 -1
  33. package/src/components/tree/tree.tsx +168 -144
  34. package/src/components/tree/treeGroup.tsx +3 -3
  35. package/src/components/tree/treeItem.tsx +4 -4
  36. package/src/hooks/usePrefersDark.ts +10 -4
  37. package/src/primitives/textInput/__workshop__/plain.tsx +49 -18
  38. package/src/primitives/textInput/textInput.tsx +23 -3
  39. package/src/styles/input/textInputStyle.ts +2 -0
  40. package/src/theme/{themeContext.ts → ThemeContext.ts} +0 -0
  41. package/src/utils/errorBoundary.tsx +3 -1
@@ -1,5 +1,5 @@
1
1
  import { useId } from '@reach/auto-id';
2
- import React, { useState, useRef, useEffect, createContext, useContext, useMemo, useLayoutEffect, forwardRef, useCallback, cloneElement, isValidElement, createElement, useReducer, Children, Fragment } from 'react';
2
+ import React, { useState, useRef, useEffect, createContext, useContext, useMemo, useLayoutEffect, forwardRef, useCallback, cloneElement, isValidElement, createElement, useReducer, Children, Fragment, memo } from 'react';
3
3
  import ReactIs, { isElement, isFragment, isValidElementType } from 'react-is';
4
4
  import styled, { ThemeProvider as ThemeProvider$1, useTheme as useTheme$1, css, keyframes } from 'styled-components';
5
5
  import { white as white$1, black as black$1, hues } from '@sanity/color';
@@ -2764,16 +2764,20 @@ function useMediaIndex() {
2764
2764
  */
2765
2765
 
2766
2766
  function usePrefersDark() {
2767
- const [dark, setDark] = useState(false);
2767
+ const mq = useMemo(() => {
2768
+ if (typeof window === 'undefined') return undefined;
2769
+ return window.matchMedia('(prefers-color-scheme: dark)');
2770
+ }, []);
2771
+ const [dark, setDark] = useState((mq == null ? void 0 : mq.matches) || false);
2768
2772
  useEffect(() => {
2769
- const mq = window.matchMedia('(prefers-color-scheme: dark)');
2773
+ if (!mq) return undefined;
2770
2774
  setDark(mq.matches);
2771
2775
 
2772
2776
  const handleChange = () => setDark(mq.matches);
2773
2777
 
2774
2778
  mq.addEventListener('change', handleChange);
2775
2779
  return () => mq.removeEventListener('change', handleChange);
2776
- }, []);
2780
+ }, [mq]);
2777
2781
  return dark;
2778
2782
  }
2779
2783
 
@@ -3576,6 +3580,8 @@ const ROOT_STYLE = css(_t$D || (_t$D = _$C`
3576
3580
  &:not([hidden]) {
3577
3581
  display: flex;
3578
3582
  }
3583
+
3584
+ align-items: center;
3579
3585
  `));
3580
3586
  function textInputRootStyle() {
3581
3587
  return ROOT_STYLE;
@@ -5779,7 +5785,8 @@ class ErrorBoundary extends React.Component {
5779
5785
  } = this.state;
5780
5786
 
5781
5787
  if (error) {
5782
- return /*#__PURE__*/React.createElement(Code, null, error.message);
5788
+ const message = typeof (error == null ? void 0 : error.message) === 'string' ? error.message : 'Error';
5789
+ return /*#__PURE__*/React.createElement(Code, null, message);
5783
5790
  }
5784
5791
 
5785
5792
  return this.props.children;
@@ -7557,8 +7564,18 @@ const TextInput = /*#__PURE__*/forwardRef(function TextInput(props, forwardedRef
7557
7564
  size: fontSize
7558
7565
  }, /*#__PURE__*/isValidElement(iconRight) && iconRight, isValidElementType(iconRight) && /*#__PURE__*/createElement(iconRight)))), [border, fontSize, icon, iconRight, padding, radius, $hasClearButton, $hasPrefix, $hasSuffix]); // Render clear button (memoized)
7559
7566
 
7560
- const clearButtonBoxPadding = useMemo(() => padding.map(v => v - 2), [padding]);
7561
- const clearButtonPadding = useMemo(() => padding.map(v => v - 1), [padding]);
7567
+ const clearButtonBoxPadding = useMemo(() => padding.map(v => {
7568
+ if (v === 0) return 0;
7569
+ if (v === 1) return 1;
7570
+ if (v === 2) return 1;
7571
+ return v - 2;
7572
+ }), [padding]);
7573
+ const clearButtonPadding = useMemo(() => padding.map(v => {
7574
+ if (v === 0) return 0;
7575
+ if (v === 1) return 0;
7576
+ if (v === 2) return 1;
7577
+ return v - 1;
7578
+ }), [padding]);
7562
7579
  const clearButtonProps = useMemo(() => typeof clearButton === 'object' ? clearButton : EMPTY_RECORD$1, [clearButton]);
7563
7580
  const clearButtonNode = useMemo(() => !disabled && !readOnly && clearButton && /*#__PURE__*/React.createElement(RightCard, {
7564
7581
  forwardedAs: "span",
@@ -7571,7 +7588,7 @@ const TextInput = /*#__PURE__*/forwardRef(function TextInput(props, forwardedRef
7571
7588
  icon: CloseIcon,
7572
7589
  mode: "bleed",
7573
7590
  padding: clearButtonPadding,
7574
- radius: radius.map(r => r - 1)
7591
+ radius: radius
7575
7592
  }, clearButtonProps, {
7576
7593
  onClick: handleClearClick,
7577
7594
  onMouseDown: handleClearMouseDown
@@ -7823,6 +7840,7 @@ let _$b = t => t,
7823
7840
 
7824
7841
  const Root$9 = styled.div(_t$b || (_t$b = _$b`
7825
7842
  /* position: relative; */
7843
+ line-height: 0;
7826
7844
  `));
7827
7845
  /**
7828
7846
  * @internal
@@ -7993,7 +8011,7 @@ const AUTOCOMPLETE_POPOVER_PLACEMENT = 'bottom-start';
7993
8011
 
7994
8012
  const AUTOCOMPLETE_POPOVER_FALLBACK_PLACEMENTS = ['top-start'];
7995
8013
 
7996
- const _excluded$f = ["border", "customValidity", "disabled", "filterOption", "fontSize", "icon", "id", "listBox", "loading", "onBlur", "onChange", "onFocus", "onQueryChange", "onSelect", "openButton", "options", "padding", "popover", "prefix", "radius", "readOnly", "relatedElements", "renderOption", "renderPopover", "renderValue", "value"];
8014
+ const _excluded$f = ["border", "customValidity", "disabled", "filterOption", "fontSize", "icon", "id", "listBox", "loading", "onBlur", "onChange", "onFocus", "onQueryChange", "onSelect", "openButton", "options", "padding", "popover", "prefix", "radius", "readOnly", "relatedElements", "renderOption", "renderPopover", "renderValue", "suffix", "value"];
7997
8015
 
7998
8016
  const defaultRenderValue = (value, option) => option ? option.value : value;
7999
8017
 
@@ -8026,6 +8044,7 @@ const InnerAutocomplete = /*#__PURE__*/forwardRef(function InnerAutocomplete(pro
8026
8044
  renderOption: renderOptionProp,
8027
8045
  renderPopover,
8028
8046
  renderValue = defaultRenderValue,
8047
+ suffix,
8029
8048
  value: valueProp
8030
8049
  } = props,
8031
8050
  restProps = _objectWithoutPropertiesLoose(props, _excluded$f);
@@ -8294,8 +8313,18 @@ const InnerAutocomplete = /*#__PURE__*/forwardRef(function InnerAutocomplete(pro
8294
8313
 
8295
8314
  return undefined;
8296
8315
  }, [disabled, handleClearButtonFocus, loading, value]);
8297
- const openButtonBoxPadding = useMemo(() => padding.map(v => v - 2), [padding]);
8298
- const openButtonPadding = useMemo(() => padding.map(v => v - 1), [padding]);
8316
+ const openButtonBoxPadding = useMemo(() => padding.map(v => {
8317
+ if (v === 0) return 0;
8318
+ if (v === 1) return 1;
8319
+ if (v === 2) return 1;
8320
+ return v - 2;
8321
+ }), [padding]);
8322
+ const openButtonPadding = useMemo(() => padding.map(v => {
8323
+ if (v === 0) return 0;
8324
+ if (v === 1) return 0;
8325
+ if (v === 2) return 1;
8326
+ return v - 1;
8327
+ }), [padding]);
8299
8328
  const openButtonProps = useMemo(() => typeof openButton === 'object' ? openButton : EMPTY_RECORD, [openButton]);
8300
8329
  const handleOpenClick = useCallback(event => {
8301
8330
  dispatch({
@@ -8361,7 +8390,7 @@ const InnerAutocomplete = /*#__PURE__*/forwardRef(function InnerAutocomplete(pro
8361
8390
  ref: setRef,
8362
8391
  role: "combobox",
8363
8392
  spellCheck: false,
8364
- suffix: openButtonNode,
8393
+ suffix: suffix || openButtonNode,
8365
8394
  value: inputValue
8366
8395
  }));
8367
8396
  const handleListBoxKeyDown = useCallback(event => {
@@ -8983,8 +9012,8 @@ function useMenuController(props) {
8983
9012
  const elementsRef = useRef([]);
8984
9013
  const [rootElement, setRootElement] = useState(null);
8985
9014
  const [activeIndex, _setActiveIndex] = useState(-1);
8986
- const activeElement = elementsRef.current[activeIndex] || null;
8987
9015
  const activeIndexRef = useRef(activeIndex);
9016
+ const activeElement = elementsRef.current[activeIndex] || null;
8988
9017
  const mounted = Boolean(rootElement);
8989
9018
  const setActiveIndex = useCallback(nextActiveIndex => {
8990
9019
  _setActiveIndex(nextActiveIndex);
@@ -9059,7 +9088,7 @@ function useMenuController(props) {
9059
9088
 
9060
9089
  const focusableLen = focusableElements.length;
9061
9090
  if (focusableLen === 0) return;
9062
- const focusedElement = elementsRef.current[activeIndex];
9091
+ const focusedElement = elementsRef.current[activeIndexRef.current];
9063
9092
  let focusedIndex = focusableElements.indexOf(focusedElement);
9064
9093
  focusedIndex = (focusedIndex - 1 + focusableLen) % focusableLen;
9065
9094
  const el = focusableElements[focusedIndex];
@@ -9076,7 +9105,7 @@ function useMenuController(props) {
9076
9105
 
9077
9106
  const focusableLen = focusableElements.length;
9078
9107
  if (focusableLen === 0) return;
9079
- const focusedElement = elementsRef.current[activeIndex];
9108
+ const focusedElement = elementsRef.current[activeIndexRef.current];
9080
9109
  let focusedIndex = focusableElements.indexOf(focusedElement);
9081
9110
  focusedIndex = (focusedIndex + 1) % focusableLen;
9082
9111
  const el = focusableElements[focusedIndex];
@@ -9088,7 +9117,7 @@ function useMenuController(props) {
9088
9117
  if (onKeyDown) {
9089
9118
  onKeyDown(event);
9090
9119
  }
9091
- }, [activeIndex, onKeyDown, originElement, setActiveIndex]);
9120
+ }, [onKeyDown, originElement, setActiveIndex]);
9092
9121
  const handleItemMouseEnter = useCallback(event => {
9093
9122
  const element = event.currentTarget;
9094
9123
  const currentIndex = elementsRef.current.indexOf(element);
@@ -10459,18 +10488,26 @@ const _excluded$2 = ["children", "space"];
10459
10488
  * @beta
10460
10489
  */
10461
10490
 
10462
- function Tree(props) {
10491
+ const Tree = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Tree(props, ref) {
10463
10492
  const {
10464
10493
  children,
10465
10494
  space = 1
10466
10495
  } = props,
10467
10496
  restProps = _objectWithoutPropertiesLoose(props, _excluded$2);
10468
10497
 
10498
+ const forwardedRef = useForwardedRef(ref);
10469
10499
  const [focusedElement, setFocusedElement] = useState(null);
10500
+ const focusedElementRef = useRef(focusedElement);
10470
10501
  const path = useMemo(() => [], []);
10471
- const rootRef = useRef(null);
10472
10502
  const [itemElements, setItemElements] = useState([]);
10473
10503
  const [state, setState] = useState({});
10504
+ const stateRef = useRef(state);
10505
+ useEffect(() => {
10506
+ focusedElementRef.current = focusedElement;
10507
+ }, [focusedElement]);
10508
+ useEffect(() => {
10509
+ stateRef.current = state;
10510
+ }, [state]);
10474
10511
  const registerItem = useCallback((element, path, expanded, selected) => {
10475
10512
  setState(s => _extends({}, s, {
10476
10513
  [path]: {
@@ -10515,12 +10552,12 @@ function Tree(props) {
10515
10552
  state
10516
10553
  }), [focusedElement, itemElements, path, registerItem, setExpanded, space, state]);
10517
10554
  const handleKeyDown = useCallback(event => {
10518
- if (!focusedElement) return;
10555
+ if (!focusedElementRef.current) return;
10519
10556
 
10520
10557
  if (event.key === 'ArrowDown') {
10521
10558
  event.preventDefault();
10522
10559
 
10523
- const nextEl = _findNextItemElement(state, itemElements, focusedElement);
10560
+ const nextEl = _findNextItemElement(stateRef.current, itemElements, focusedElementRef.current);
10524
10561
 
10525
10562
  if (nextEl) {
10526
10563
  _focusItemElement(nextEl);
@@ -10534,7 +10571,7 @@ function Tree(props) {
10534
10571
  if (event.key === 'ArrowUp') {
10535
10572
  event.preventDefault();
10536
10573
 
10537
- const prevEl = _findPrevItemElement(state, itemElements, focusedElement);
10574
+ const prevEl = _findPrevItemElement(stateRef.current, itemElements, focusedElementRef.current);
10538
10575
 
10539
10576
  if (prevEl) {
10540
10577
  _focusItemElement(prevEl);
@@ -10547,9 +10584,9 @@ function Tree(props) {
10547
10584
 
10548
10585
  if (event.key === 'ArrowLeft') {
10549
10586
  event.preventDefault();
10550
- const itemKey = focusedElement.getAttribute('data-tree-key');
10587
+ const itemKey = focusedElementRef.current.getAttribute('data-tree-key');
10551
10588
  if (!itemKey) return;
10552
- const itemState = state[itemKey];
10589
+ const itemState = stateRef.current[itemKey];
10553
10590
  if (!itemState) return;
10554
10591
 
10555
10592
  if (itemState.expanded) {
@@ -10566,7 +10603,7 @@ function Tree(props) {
10566
10603
  const itemPath = itemKey.split('/');
10567
10604
  itemPath.pop();
10568
10605
  const parentKey = itemPath.join('/');
10569
- const parentState = parentKey && state[parentKey];
10606
+ const parentState = parentKey && stateRef.current[parentKey];
10570
10607
 
10571
10608
  if (parentState) {
10572
10609
  parentState.element.focus();
@@ -10578,13 +10615,13 @@ function Tree(props) {
10578
10615
  }
10579
10616
 
10580
10617
  if (event.key === 'ArrowRight') {
10581
- var _state$focusedKey;
10618
+ var _stateRef$current$foc;
10582
10619
 
10583
10620
  event.preventDefault();
10584
- const focusedKey = focusedElement.getAttribute('data-tree-key');
10621
+ const focusedKey = focusedElementRef.current.getAttribute('data-tree-key');
10585
10622
  if (!focusedKey) return;
10586
10623
 
10587
- if (!((_state$focusedKey = state[focusedKey]) != null && _state$focusedKey.expanded)) {
10624
+ if (!((_stateRef$current$foc = stateRef.current[focusedKey]) != null && _stateRef$current$foc.expanded)) {
10588
10625
  setState(s => {
10589
10626
  const itemState = s[focusedKey];
10590
10627
  if (!itemState) return s;
@@ -10598,14 +10635,14 @@ function Tree(props) {
10598
10635
 
10599
10636
  return;
10600
10637
  }
10601
- }, [focusedElement, itemElements, state]);
10638
+ }, [itemElements]);
10602
10639
  useEffect(() => {
10603
- if (!rootRef.current) return;
10640
+ if (!forwardedRef.current) return;
10604
10641
 
10605
- const _itemElements = Array.from(rootRef.current.querySelectorAll('[data-ui="TreeItem"]'));
10642
+ const _itemElements = Array.from(forwardedRef.current.querySelectorAll('[data-ui="TreeItem"]'));
10606
10643
 
10607
10644
  setItemElements(_itemElements);
10608
- }, [children]);
10645
+ }, [children, forwardedRef]);
10609
10646
  return /*#__PURE__*/React.createElement(TreeContext.Provider, {
10610
10647
  value: contextValue
10611
10648
  }, /*#__PURE__*/React.createElement(Stack, _extends({
@@ -10613,11 +10650,12 @@ function Tree(props) {
10613
10650
  "data-ui": "Tree"
10614
10651
  }, restProps, {
10615
10652
  onKeyDown: handleKeyDown,
10616
- ref: rootRef,
10653
+ ref: forwardedRef,
10617
10654
  role: "tree",
10618
10655
  space: space
10619
10656
  }), children));
10620
- }
10657
+ }));
10658
+ Tree.displayName = 'Tree';
10621
10659
 
10622
10660
  let _$1 = t => t,
10623
10661
  _t$1,
@@ -10743,7 +10781,7 @@ function useTree() {
10743
10781
  }
10744
10782
 
10745
10783
  const _excluded$1 = ["children", "expanded"];
10746
- function TreeGroup(props) {
10784
+ const TreeGroup = /*#__PURE__*/memo(function TreeGroup(props) {
10747
10785
  const {
10748
10786
  children,
10749
10787
  expanded = false
@@ -10760,13 +10798,13 @@ function TreeGroup(props) {
10760
10798
  role: "group",
10761
10799
  space: tree.space
10762
10800
  }), children);
10763
- }
10801
+ });
10764
10802
 
10765
10803
  const _excluded = ["children", "expanded", "fontSize", "href", "icon", "id", "muted", "onClick", "padding", "selected", "space", "text", "weight"];
10766
10804
 
10767
10805
  let _ = t => t,
10768
10806
  _t;
10769
- const Root = styled.li(treeItemRootStyle, treeItemRootColorStyle);
10807
+ const Root = /*#__PURE__*/memo(styled.li(treeItemRootStyle, treeItemRootColorStyle));
10770
10808
  const TreeItemBox = styled(Box).attrs({
10771
10809
  forwardedAs: 'a'
10772
10810
  })(treeItemBoxStyle);
@@ -10780,7 +10818,7 @@ const ToggleArrowText = styled(Text)(_t || (_t = _`
10780
10818
  * @beta
10781
10819
  */
10782
10820
 
10783
- function TreeItem(props) {
10821
+ const TreeItem = /*#__PURE__*/memo(function TreeItem(props) {
10784
10822
  const {
10785
10823
  children,
10786
10824
  expanded: expandedProp = false,
@@ -10913,7 +10951,7 @@ function TreeItem(props) {
10913
10951
  }, children && /*#__PURE__*/React.createElement(TreeGroup, {
10914
10952
  expanded: expanded
10915
10953
  }, children)));
10916
- }
10954
+ });
10917
10955
 
10918
10956
  export { Autocomplete, Avatar, AvatarCounter, AvatarStack, Badge, BoundaryElementProvider, Box, Breadcrumbs, Button, Card, Checkbox, Code, CodeSkeleton, Container, Dialog, DialogContext, DialogProvider, ElementQuery, ErrorBoundary, Flex, Grid, Heading, HeadingSkeleton, Hotkeys, Inline, KBD, Label, LabelSkeleton, Layer, LayerProvider, Menu, MenuButton, MenuDivider, MenuGroup, MenuItem, Popover, Portal, PortalProvider, Radio, RO as ResizeObserver, Select, Skeleton, Spinner, SrOnly, Stack, Switch, Tab, TabList, TabPanel, Text, TextArea, TextInput, TextSkeleton, ThemeColorProvider, ThemeProvider, Toast, ToastProvider, Tooltip, Tree, TreeItem, VirtualList, _hasFocus, _isEnterToClickElement, _isScrollable, _raf, _raf2, attemptFocus, createColorTheme, fillCSSObject, focusFirstDescendant, focusLastDescendant, getResponsiveProp, getResponsiveSpace, hexToRgb, hslToRgb, isFocusable, isHTMLAnchorElement, isHTMLButtonElement, isHTMLElement, isHTMLInputElement, isHTMLSelectElement, isHTMLTextAreaElement, multiply$1 as multiply, parseColor, rem, responsive, responsiveCodeFontStyle, responsiveHeadingFont, responsiveLabelFont, responsiveTextAlignStyle, responsiveTextFont, rgbToHex, rgbToHsl, rgba, screen$1 as screen, studioTheme, useBoundaryElement, useClickOutside, useCustomValidity, useDialog, useElementRect, useForwardedRef, useGlobalKeyDown, useLayer, useMediaIndex, usePortal, usePrefersDark, useResponsiveProp, useRootTheme, useTheme, useToast, useTree };
10919
10957
  //# sourceMappingURL=sanity-ui.modern.js.map