@sanity/ui 5.0.0-alpha.24 → 5.0.0-alpha.26
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.d.ts +145 -147
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +256 -179
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/src/components/avatar/avatar.props.ts +4 -2
- package/src/components/avatar-counter/avatarCounter.props.ts +4 -2
- package/src/components/avatar-stack/avatarStack.props.ts +4 -2
- package/src/components/badge/Badge.tsx +2 -5
- package/src/components/badge/badge.props.ts +15 -15
- package/src/components/box/Box.tsx +2 -5
- package/src/components/box/box.props.ts +18 -12
- package/src/components/button/Button.tsx +2 -2
- package/src/components/button/button.props.ts +49 -43
- package/src/components/card/Card.tsx +2 -2
- package/src/components/card/card.props.ts +19 -13
- package/src/components/checkbox/Checkbox.tsx +3 -2
- package/src/components/checkbox/checkbox.props.ts +4 -2
- package/src/components/code/code.props.ts +5 -3
- package/src/components/container/Container.tsx +2 -2
- package/src/components/container/container.props.ts +17 -11
- package/src/components/dialog/Dialog.tsx +13 -7
- package/src/components/dialog/dialog.props.ts +11 -3
- package/src/components/divider/divider.props.ts +4 -2
- package/src/components/eyebrow/Eyebrow.tsx +2 -5
- package/src/components/eyebrow/eyebrow.props.ts +19 -12
- package/src/components/flex/Flex.tsx +2 -5
- package/src/components/flex/flex.props.ts +19 -12
- package/src/components/grid/Grid.tsx +2 -5
- package/src/components/grid/grid.props.ts +20 -16
- package/src/components/h-stack/HStack.tsx +2 -5
- package/src/components/h-stack/hStack.props.ts +7 -4
- package/src/components/heading/Heading.tsx +2 -5
- package/src/components/heading/heading.props.ts +18 -13
- package/src/components/hotkeys/hotkeys.props.ts +4 -2
- package/src/components/icon/icon.props.ts +3 -3
- package/src/components/icon-button/IconButton.tsx +2 -4
- package/src/components/icon-button/iconButton.props.ts +17 -17
- package/src/components/index.css +1 -0
- package/src/components/indicator/Indicator.tsx +2 -2
- package/src/components/indicator/indicator.props.ts +17 -11
- package/src/components/indicator-stack/IndicatorStack.tsx +2 -4
- package/src/components/indicator-stack/indicatorStack.props.ts +13 -7
- package/src/components/inline/Inline.tsx +2 -2
- package/src/components/inline/inline.props.ts +17 -11
- package/src/components/kbd/kbd.props.ts +4 -2
- package/src/components/link/link.props.ts +4 -2
- package/src/components/list/List.tsx +47 -97
- package/src/components/list/list.css +28 -9
- package/src/components/list/list.props.ts +59 -68
- package/src/components/popover/popover.props.ts +7 -5
- package/src/components/press-area/PressArea.tsx +2 -4
- package/src/components/press-area/press-area.css +9 -3
- package/src/components/press-area/pressArea.props.ts +13 -7
- package/src/components/radio/radio.props.ts +2 -1
- package/src/components/search-input/SearchInput.tsx +90 -0
- package/src/components/search-input/search-input.css +125 -0
- package/src/components/search-input/searchInput.props.ts +18 -0
- package/src/components/select/Select.tsx +6 -1
- package/src/components/select/select.css +10 -12
- package/src/components/select/select.props.ts +4 -2
- package/src/components/skip-to-content/skipToContent.props.ts +4 -2
- package/src/components/spinner/spinner.props.ts +4 -2
- package/src/components/switch/switch.props.ts +4 -2
- package/src/components/tabs/tabs.props.ts +8 -9
- package/src/components/text/Text.tsx +2 -5
- package/src/components/text/text.props.ts +19 -12
- package/src/components/text-area/textArea.props.ts +4 -5
- package/src/components/text-input/textInput.props.ts +5 -3
- package/src/components/tooltip/tooltip.props.ts +7 -5
- package/src/components/tooltip-group/TooltipGroup.tsx +8 -9
- package/src/components/tooltip-group/tooltipGroup.props.ts +13 -7
- package/src/components/v-stack/VStack.tsx +2 -5
- package/src/components/v-stack/vStack.props.ts +7 -4
- package/src/components/visually-hidden/VisuallyHidden.tsx +2 -4
- package/src/components/visually-hidden/visuallyHidden.props.ts +18 -12
- package/src/index.ts +1 -0
- package/src/types/Interactive.ts +3 -1
- package/src/types/Polymorphic.ts +9 -0
- package/src/types/Tone.ts +1 -1
- package/src/utils/composeRefs.test.ts +130 -0
- package/src/utils/composeRefs.ts +52 -0
- package/src/utils/getProps.test.ts +18 -0
- package/src/utils/getProps.ts +31 -16
- package/src/version.ts +2 -2
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {createRef} from 'react'
|
|
2
|
+
import {describe, expect, it, vi} from 'vitest'
|
|
3
|
+
|
|
4
|
+
import {composeRefs} from './composeRefs'
|
|
5
|
+
|
|
6
|
+
type Node = {id: string}
|
|
7
|
+
|
|
8
|
+
const node: Node = {id: 'node'}
|
|
9
|
+
|
|
10
|
+
describe('composeRefs', () => {
|
|
11
|
+
it('sets ref objects to the node', () => {
|
|
12
|
+
const refA = createRef<Node>()
|
|
13
|
+
const refB = createRef<Node>()
|
|
14
|
+
|
|
15
|
+
composeRefs(refA, refB)(node)
|
|
16
|
+
|
|
17
|
+
expect(refA.current).toBe(node)
|
|
18
|
+
expect(refB.current).toBe(node)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('calls callback refs with the node', () => {
|
|
22
|
+
const callbackA = vi.fn()
|
|
23
|
+
const callbackB = vi.fn()
|
|
24
|
+
|
|
25
|
+
composeRefs<Node>(callbackA, callbackB)(node)
|
|
26
|
+
|
|
27
|
+
expect(callbackA).toHaveBeenCalledExactlyOnceWith(node)
|
|
28
|
+
expect(callbackB).toHaveBeenCalledExactlyOnceWith(node)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('sets ref objects and calls callback refs together', () => {
|
|
32
|
+
const ref = createRef<Node>()
|
|
33
|
+
const callback = vi.fn()
|
|
34
|
+
|
|
35
|
+
composeRefs<Node>(ref, callback)(node)
|
|
36
|
+
|
|
37
|
+
expect(ref.current).toBe(node)
|
|
38
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith(node)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('ignores null and undefined refs', () => {
|
|
42
|
+
const ref = createRef<Node>()
|
|
43
|
+
|
|
44
|
+
expect(() => composeRefs<Node>(null, undefined, ref)(node)).not.toThrow()
|
|
45
|
+
expect(ref.current).toBe(node)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('returns a cleanup function', () => {
|
|
49
|
+
const cleanup = composeRefs(createRef<Node>())(node)
|
|
50
|
+
|
|
51
|
+
expect(cleanup).toBeTypeOf('function')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('returns a cleanup function when no refs are given', () => {
|
|
55
|
+
const cleanup = composeRefs<Node>()(node)
|
|
56
|
+
|
|
57
|
+
expect(cleanup).toBeTypeOf('function')
|
|
58
|
+
expect(() => (cleanup as () => void)()).not.toThrow()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('cleanup', () => {
|
|
62
|
+
it('sets ref objects to null', () => {
|
|
63
|
+
const ref = createRef<Node>()
|
|
64
|
+
const cleanup = composeRefs(ref)(node) as () => void
|
|
65
|
+
|
|
66
|
+
cleanup()
|
|
67
|
+
|
|
68
|
+
expect(ref.current).toBeNull()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('calls callback refs with null if they do not return a cleanup function', () => {
|
|
72
|
+
const callback = vi.fn()
|
|
73
|
+
const cleanup = composeRefs<Node>(callback)(node) as () => void
|
|
74
|
+
|
|
75
|
+
cleanup()
|
|
76
|
+
|
|
77
|
+
expect(callback).toHaveBeenCalledTimes(2)
|
|
78
|
+
expect(callback).toHaveBeenLastCalledWith(null)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('calls the cleanup function of a callback ref in place of calling the ref with null', () => {
|
|
82
|
+
const callbackCleanup = vi.fn()
|
|
83
|
+
const callback = vi.fn(() => callbackCleanup)
|
|
84
|
+
const cleanup = composeRefs<Node>(callback)(node) as () => void
|
|
85
|
+
|
|
86
|
+
cleanup()
|
|
87
|
+
|
|
88
|
+
expect(callbackCleanup).toHaveBeenCalledOnce()
|
|
89
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith(node)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('handles a mix of ref objects, callback refs, and callback refs with cleanup functions', () => {
|
|
93
|
+
const ref = createRef<Node>()
|
|
94
|
+
const callback = vi.fn()
|
|
95
|
+
const callbackCleanup = vi.fn()
|
|
96
|
+
const callbackWithCleanup = vi.fn(() => callbackCleanup)
|
|
97
|
+
|
|
98
|
+
const cleanup = composeRefs<Node>(
|
|
99
|
+
ref,
|
|
100
|
+
callback,
|
|
101
|
+
callbackWithCleanup,
|
|
102
|
+
null,
|
|
103
|
+
)(node) as () => void
|
|
104
|
+
cleanup()
|
|
105
|
+
|
|
106
|
+
expect(ref.current).toBeNull()
|
|
107
|
+
expect(callback).toHaveBeenLastCalledWith(null)
|
|
108
|
+
expect(callbackCleanup).toHaveBeenCalledOnce()
|
|
109
|
+
expect(callbackWithCleanup).toHaveBeenCalledExactlyOnceWith(node)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('runs cleanups in the same order as the refs', () => {
|
|
113
|
+
const calls: string[] = []
|
|
114
|
+
const callbackA = () => () => {
|
|
115
|
+
calls.push('a')
|
|
116
|
+
}
|
|
117
|
+
const callbackB = (value: Node | null) => {
|
|
118
|
+
if (value === null) calls.push('b')
|
|
119
|
+
}
|
|
120
|
+
const callbackC = () => () => {
|
|
121
|
+
calls.push('c')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const cleanup = composeRefs<Node>(callbackA, callbackB, callbackC)(node) as () => void
|
|
125
|
+
cleanup()
|
|
126
|
+
|
|
127
|
+
expect(calls).toEqual(['a', 'b', 'c'])
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on `@radix-ui/react-compose-refs` by WorkOS (MIT License):
|
|
3
|
+
* https://github.com/radix-ui/primitives/blob/main/packages/react/compose-refs/src/compose-refs.tsx
|
|
4
|
+
*
|
|
5
|
+
* Changes from the original:
|
|
6
|
+
* - React 19+ only. The composed callback ref always returns a cleanup function, so React never
|
|
7
|
+
* calls it with `null` on detach.
|
|
8
|
+
* - No `useComposedRefs` hook. The React Compiler memoizes `composeRefs(...)` calls in components,
|
|
9
|
+
* so the composed ref stays the same between renders while its refs stay the same.
|
|
10
|
+
*/
|
|
11
|
+
import {type Ref, type RefCallback} from 'react'
|
|
12
|
+
|
|
13
|
+
type PossibleRef<T> = Ref<T> | undefined
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sets a ref to a value. Returns the cleanup function of a callback ref, if it returns one.
|
|
17
|
+
*/
|
|
18
|
+
function setRef<T>(ref: PossibleRef<T>, value: T | null) {
|
|
19
|
+
if (typeof ref === 'function') {
|
|
20
|
+
return ref(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (ref !== null && ref !== undefined) {
|
|
24
|
+
ref.current = value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return undefined
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Composes multiple refs (callback refs and ref objects) into one callback ref.
|
|
32
|
+
*
|
|
33
|
+
* On detach, the composed ref calls the cleanup function of each callback ref that returned one.
|
|
34
|
+
* It sets all other refs to `null`.
|
|
35
|
+
*/
|
|
36
|
+
export function composeRefs<T>(...refs: PossibleRef<T>[]): RefCallback<T> {
|
|
37
|
+
return (node) => {
|
|
38
|
+
const cleanups = refs.map((ref) => setRef(ref, node))
|
|
39
|
+
|
|
40
|
+
return () => {
|
|
41
|
+
for (let i = 0; i < refs.length; i++) {
|
|
42
|
+
const cleanup = cleanups[i]
|
|
43
|
+
|
|
44
|
+
if (typeof cleanup === 'function') {
|
|
45
|
+
cleanup()
|
|
46
|
+
} else {
|
|
47
|
+
setRef(refs[i], null)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -124,12 +124,30 @@ describe('getProps', () => {
|
|
|
124
124
|
expect(result.style).toEqual({})
|
|
125
125
|
})
|
|
126
126
|
|
|
127
|
+
it('generates responsive className with undefined values and style based on strings', () => {
|
|
128
|
+
const result = getProps({width: ['100px', undefined, '300px']}, layoutProps)
|
|
129
|
+
expect(result.className).toBe('sui-width sui-width-bp-2')
|
|
130
|
+
expect(result.style).toEqual({
|
|
131
|
+
'--width': '100px',
|
|
132
|
+
'--width-bp-2': '300px',
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
127
136
|
it('generates responsive className with null values', () => {
|
|
128
137
|
const result = getProps({padding: [1, 2, null, 3]}, layoutProps)
|
|
129
138
|
expect(result.className).toBe('sui-p1 sui-p2-bp-1 sui-p3-bp-3')
|
|
130
139
|
expect(result.style).toEqual({})
|
|
131
140
|
})
|
|
132
141
|
|
|
142
|
+
it('generates responsive className with null values and style based on strings', () => {
|
|
143
|
+
const result = getProps({width: ['100px', null, '300px']}, layoutProps)
|
|
144
|
+
expect(result.className).toBe('sui-width sui-width-bp-2')
|
|
145
|
+
expect(result.style).toEqual({
|
|
146
|
+
'--width': '100px',
|
|
147
|
+
'--width-bp-2': '300px',
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
133
151
|
it('generates responsive className without unsupported values', () => {
|
|
134
152
|
const result = getProps({padding: [1, 2, 10, 3]}, layoutProps)
|
|
135
153
|
expect(result.className).toBe('sui-p1 sui-p2-bp-1 sui-p3-bp-3')
|
package/src/utils/getProps.ts
CHANGED
|
@@ -17,8 +17,9 @@ export function getProps(
|
|
|
17
17
|
): ComponentProps {
|
|
18
18
|
const {allComponentProps, allPropDefs} = flattenCompositeProps(componentProps, propDefs)
|
|
19
19
|
const restProps: ComponentProps = {}
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const classNames: string[] = []
|
|
21
|
+
const style = (componentProps?.style ? {...componentProps.style} : {}) as React.CSSProperties &
|
|
22
|
+
Record<string, string | number | undefined>
|
|
22
23
|
|
|
23
24
|
for (const key in allComponentProps) {
|
|
24
25
|
const propDef = allPropDefs?.[key]
|
|
@@ -32,32 +33,48 @@ export function getProps(
|
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
if (Array.isArray(propValue)) {
|
|
35
|
-
// @TODO: consider fixing this O(n^2) time complexity
|
|
36
|
-
// oxlint-disable-next-line no-accumulating-spread
|
|
37
36
|
for (let i = 0, len = Math.min(propValue.length, BREAKPOINTS_LENGTH); i < len; i++) {
|
|
38
37
|
const resolvedPropDef = getResolvedPropDef(propValue[i], propDef)
|
|
39
38
|
|
|
40
|
-
if (!resolvedPropDef) {
|
|
39
|
+
if (!resolvedPropDef || propValue[i] === null || propValue[i] === undefined) {
|
|
41
40
|
continue
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
className =
|
|
45
|
-
|
|
43
|
+
const className = getClassName(propValue[i], resolvedPropDef, i)
|
|
44
|
+
if (className) {
|
|
45
|
+
classNames.push(className)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const styleKey = getStyleKey(resolvedPropDef, i)
|
|
49
|
+
if (styleKey) {
|
|
50
|
+
style[styleKey] = propValue[i] as string | number
|
|
51
|
+
}
|
|
46
52
|
}
|
|
47
53
|
} else {
|
|
48
54
|
const resolvedPropDef = getResolvedPropDef(propValue, propDef)
|
|
49
55
|
|
|
50
|
-
if (!resolvedPropDef) {
|
|
56
|
+
if (!resolvedPropDef || propValue === null || propValue === undefined) {
|
|
51
57
|
restProps[key] = propValue
|
|
52
58
|
continue
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
className =
|
|
56
|
-
|
|
61
|
+
const className = getClassName(propValue, resolvedPropDef)
|
|
62
|
+
if (className) {
|
|
63
|
+
classNames.push(className)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const styleKey = getStyleKey(resolvedPropDef)
|
|
67
|
+
if (styleKey) {
|
|
68
|
+
style[styleKey] = propValue as string | number
|
|
69
|
+
}
|
|
57
70
|
}
|
|
58
71
|
}
|
|
59
72
|
|
|
60
|
-
return {
|
|
73
|
+
return {
|
|
74
|
+
...restProps,
|
|
75
|
+
className: clsx(componentProps?.className, ...classNames),
|
|
76
|
+
style,
|
|
77
|
+
}
|
|
61
78
|
}
|
|
62
79
|
|
|
63
80
|
function getResolvedPropDef(propValue: any, propDef?: PropDef) {
|
|
@@ -97,14 +114,12 @@ function getClassName(propValue: any, propDef: PropDef, bp?: number) {
|
|
|
97
114
|
return ''
|
|
98
115
|
}
|
|
99
116
|
|
|
100
|
-
function
|
|
117
|
+
function getStyleKey(propDef: PropDef, bp?: number): string | undefined {
|
|
101
118
|
if ((propDef.type === 'string' || propDef.type === 'number') && propDef.variable) {
|
|
102
|
-
return {
|
|
103
|
-
[`${propDef.variable}${bp ? `-bp-${bp}` : ''}`]: propValue,
|
|
104
|
-
}
|
|
119
|
+
return `${propDef.variable}${bp ? `-bp-${bp}` : ''}`
|
|
105
120
|
}
|
|
106
121
|
|
|
107
|
-
return
|
|
122
|
+
return undefined
|
|
108
123
|
}
|
|
109
124
|
|
|
110
125
|
export function flattenCompositeProps(
|
package/src/version.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// This file is generated by scripts/write-version.js. Do not edit.
|
|
2
|
-
export const VERSION = '
|
|
3
|
-
export const HASH = '
|
|
2
|
+
export const VERSION = '500alpha26'
|
|
3
|
+
export const HASH = 'f11fdc'
|