@sanity/ui 1.7.0 → 1.7.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/ui",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "sideEffects": false,
5
5
  "types": "./dist/index.d.ts",
6
6
  "source": "./src/index.ts",
@@ -11,5 +11,11 @@ export default defineScope({
11
11
  component: lazy(() => import('./basic')),
12
12
  // options: {perfTests: () => import('./basic.perf')},
13
13
  },
14
+ {
15
+ name: 'tab',
16
+ title: 'Tab',
17
+ component: lazy(() => import('./tabFromElement')),
18
+ // options: {perfTests: () => import('./basic.perf')},
19
+ },
14
20
  ],
15
21
  })
@@ -0,0 +1,83 @@
1
+ import {LinkIcon} from '@sanity/icons'
2
+ import {Box, TextInput, Tree, TreeItem, Text} from '@sanity/ui'
3
+ import {usePerfTest} from '@sanity/ui-workshop/plugin-perf'
4
+ import {useCallback, useState} from 'react'
5
+ import {perfTests} from './basic.perf'
6
+
7
+ export default function BasicStory() {
8
+ const {ref, Wrapper} = usePerfTest(perfTests[0])
9
+
10
+ const [id, setId] = useState('')
11
+ const [focus, setFocus] = useState('')
12
+
13
+ const handleClick = useCallback((event: React.MouseEvent) => {
14
+ event.preventDefault()
15
+
16
+ const testid = event.currentTarget.getAttribute('data-testid')
17
+
18
+ if (testid) setId(testid)
19
+ }, [])
20
+
21
+ const handleFocus = useCallback((event: React.FocusEvent<HTMLDivElement>) => {
22
+ const elementFocus = event.target.getAttribute('data-testid')
23
+ if (elementFocus) setFocus(elementFocus)
24
+ }, [])
25
+
26
+ return (
27
+ <Box padding={[4, 5, 6]}>
28
+ <Box paddingY={3}>
29
+ <Text>
30
+ This example is to demonstrate that when you tab from an outside element (using the
31
+ keyboard to navigate), you can still access the tree and tree item. Press the input
32
+ beneath and start tabbing / using the arrow.
33
+ </Text>
34
+ </Box>
35
+ <Box paddingY={3}>
36
+ <Text>Focus: {focus}</Text>
37
+ </Box>
38
+ <Wrapper>
39
+ <TextInput />
40
+ <Tree ref={ref} space={1} onFocus={handleFocus}>
41
+ <TreeItem data-testid="fruit" onClick={handleClick} expanded text="Fruit">
42
+ <TreeItem
43
+ data-testid="oranges"
44
+ onClick={handleClick}
45
+ selected={id === 'oranges'}
46
+ text="Oranges"
47
+ />
48
+ <TreeItem
49
+ data-testid="pineapples"
50
+ onClick={handleClick}
51
+ text="Pineapples"
52
+ selected={id === 'pineapples'}
53
+ />
54
+ <TreeItem data-testid="apples" onClick={handleClick} text="Apples">
55
+ <TreeItem
56
+ data-testid="apples/macintosh"
57
+ onClick={handleClick}
58
+ href="/apples/macintosh"
59
+ icon={LinkIcon}
60
+ text="Macintosh"
61
+ />
62
+ <TreeItem
63
+ data-testid="apples/granny-smith"
64
+ onClick={handleClick}
65
+ text="Granny Smith"
66
+ />
67
+ <TreeItem data-testid="apples/fuji" onClick={handleClick} text="Fuji" />
68
+ </TreeItem>
69
+ <TreeItem data-testid="bananas" onClick={handleClick} text="Bananas" />
70
+ <TreeItem data-testid="pears" onClick={handleClick} text="Pears">
71
+ <TreeItem data-testid="pears/anjou" onClick={handleClick} text="Anjou" />
72
+ <TreeItem data-testid="pears/bartlett" onClick={handleClick} text="Bartlett" />
73
+ <TreeItem data-testid="pears/bosc" onClick={handleClick} text="Bosc" />
74
+ <TreeItem data-testid="pears/concorde" onClick={handleClick} text="Concorde" />
75
+ <TreeItem data-testid="pears/seckel" onClick={handleClick} text="Seckel" />
76
+ <TreeItem data-testid="pears/starkrimson" onClick={handleClick} text="Starkrimson" />
77
+ </TreeItem>
78
+ </TreeItem>
79
+ </Tree>
80
+ </Wrapper>
81
+ </Box>
82
+ )
83
+ }
@@ -23,7 +23,7 @@ export const Tree = memo(
23
23
  Omit<React.HTMLProps<HTMLDivElement>, 'align' | 'as' | 'height' | 'ref' | 'role' | 'wrap'>,
24
24
  ref: React.ForwardedRef<HTMLDivElement>
25
25
  ): React.ReactElement {
26
- const {children, space = 1, ...restProps} = props
26
+ const {children, space = 1, onFocus, ...restProps} = props
27
27
  const forwardedRef = useForwardedRef(ref)
28
28
  const [focusedElement, setFocusedElement] = useState<HTMLElement | null>(null)
29
29
  const focusedElementRef = useRef(focusedElement)
@@ -183,6 +183,16 @@ export const Tree = memo(
183
183
  [itemElements]
184
184
  )
185
185
 
186
+ const handleFocus = useCallback(
187
+ (event: React.FocusEvent<HTMLDivElement>) => {
188
+ setFocusedElement(event.target)
189
+
190
+ // Call the element's `focus` handler
191
+ onFocus?.(event)
192
+ },
193
+ [onFocus]
194
+ )
195
+
186
196
  useEffect(() => {
187
197
  if (!forwardedRef.current) return
188
198
  const _itemElements = Array.from(
@@ -198,6 +208,7 @@ export const Tree = memo(
198
208
  as="ul"
199
209
  data-ui="Tree"
200
210
  {...restProps}
211
+ onFocus={handleFocus}
201
212
  onKeyDown={handleKeyDown}
202
213
  ref={forwardedRef}
203
214
  role="tree"
@@ -89,7 +89,7 @@ export const TreeItem = memo(function TreeItem(
89
89
 
90
90
  if (
91
91
  target instanceof HTMLElement &&
92
- (target.getAttribute('data-ui') === 'TreeItem__box' ||
92
+ (target.getAttribute('data-ui') === 'TreeItem' ||
93
93
  target.closest('[data-ui="TreeItem__box"]'))
94
94
  ) {
95
95
  event.stopPropagation()