@sanity/ui 1.7.12 → 1.8.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.
- package/dist/index.cjs.mjs +3 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.esm.js +99 -25
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +101 -24
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useDelayed.test.ts +66 -0
- package/src/hooks/useDelayedState.ts +29 -0
- package/src/primitives/tooltip/__workshop__/props.tsx +86 -11
- package/src/primitives/tooltip/index.ts +1 -0
- package/src/primitives/tooltip/tooltip.test.tsx +278 -0
- package/src/primitives/tooltip/tooltip.tsx +59 -12
- package/src/primitives/tooltip/tooltipDelayGroup/index.ts +4 -0
- package/src/primitives/tooltip/tooltipDelayGroup/tooltipDelayGroupContext.tsx +13 -0
- package/src/primitives/tooltip/tooltipDelayGroup/tooltipDelayGroupProvider.tsx +61 -0
- package/src/primitives/tooltip/tooltipDelayGroup/types.ts +11 -0
- package/src/primitives/tooltip/tooltipDelayGroup/useTooltipDelayGroup.ts +12 -0
- package/src/primitives/types.ts +10 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {useMemo} from 'react'
|
|
2
|
+
import {useDelayedState} from '../../../hooks/useDelayedState'
|
|
3
|
+
import {Delay} from '../../types'
|
|
4
|
+
import {TooltipDelayGroupContext} from './tooltipDelayGroupContext'
|
|
5
|
+
import {TooltipDelayGroupContextValue} from './types'
|
|
6
|
+
import {useTooltipDelayGroup} from './useTooltipDelayGroup'
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* */
|
|
10
|
+
export interface TooltipDelayGroupProviderProps {
|
|
11
|
+
children?: React.ReactNode
|
|
12
|
+
/**
|
|
13
|
+
* @public Handles the delays to open or close a tooltip inside a group
|
|
14
|
+
*
|
|
15
|
+
* If only a `number` is passed, it will be used for both opening and closing.
|
|
16
|
+
*
|
|
17
|
+
* If an object `{open: number; close:number}` is passed, it can be used to set different delays for each action.
|
|
18
|
+
*/
|
|
19
|
+
delay: Delay
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
* Provides context for a group of tooltip elements that should share a delay
|
|
25
|
+
* which temporarily becomes 1 ms after the first floating element of the group opens.
|
|
26
|
+
*/
|
|
27
|
+
export function TooltipDelayGroupProvider(
|
|
28
|
+
props: TooltipDelayGroupProviderProps,
|
|
29
|
+
): React.ReactElement {
|
|
30
|
+
const {children, delay} = props
|
|
31
|
+
const [isGroupActive, setIsGroupActive] = useDelayedState(false)
|
|
32
|
+
const [openTooltipId, setOpenTooltipId] = useDelayedState<string | null>(null)
|
|
33
|
+
|
|
34
|
+
const isInsideContext = useTooltipDelayGroup()
|
|
35
|
+
|
|
36
|
+
if (isInsideContext) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
'TooltipDelayGroupProvider cannot be nested inside another TooltipDelayGroupProvider',
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const openDelay = typeof delay === 'number' ? delay : delay?.open || 0
|
|
43
|
+
const closeDelay = typeof delay === 'number' ? delay : delay?.close || 0
|
|
44
|
+
|
|
45
|
+
const value: TooltipDelayGroupContextValue = useMemo(
|
|
46
|
+
() => ({
|
|
47
|
+
isGroupActive: isGroupActive,
|
|
48
|
+
setIsGroupActive: setIsGroupActive,
|
|
49
|
+
openTooltipId: openTooltipId,
|
|
50
|
+
setOpenTooltipId: setOpenTooltipId,
|
|
51
|
+
// When the group is active, we want the next tooltip to open immediately.
|
|
52
|
+
openDelay: isGroupActive ? 1 : openDelay,
|
|
53
|
+
closeDelay: closeDelay,
|
|
54
|
+
}),
|
|
55
|
+
[closeDelay, isGroupActive, openDelay, openTooltipId, setIsGroupActive, setOpenTooltipId],
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<TooltipDelayGroupContext.Provider value={value}>{children}</TooltipDelayGroupContext.Provider>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @beta
|
|
3
|
+
*/
|
|
4
|
+
export interface TooltipDelayGroupContextValue {
|
|
5
|
+
isGroupActive: boolean
|
|
6
|
+
setIsGroupActive: (nextState: React.SetStateAction<boolean>, delay?: number | undefined) => void
|
|
7
|
+
setOpenTooltipId: (nextId: string | null, delay?: number | undefined) => void
|
|
8
|
+
openDelay: number
|
|
9
|
+
closeDelay: number
|
|
10
|
+
openTooltipId: string | null
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {useContext} from 'react'
|
|
2
|
+
import {TooltipDelayGroupContext} from './tooltipDelayGroupContext'
|
|
3
|
+
import {TooltipDelayGroupContextValue} from './types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @beta
|
|
7
|
+
*/
|
|
8
|
+
export function useTooltipDelayGroup(): TooltipDelayGroupContextValue | null {
|
|
9
|
+
const value = useContext(TooltipDelayGroupContext)
|
|
10
|
+
|
|
11
|
+
return value
|
|
12
|
+
}
|
package/src/primitives/types.ts
CHANGED
|
@@ -129,3 +129,13 @@ export interface ResponsiveShadowProps {
|
|
|
129
129
|
export interface ResponsiveWidthProps {
|
|
130
130
|
width?: number | 'auto' | (number | 'auto')[]
|
|
131
131
|
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @beta
|
|
135
|
+
*/
|
|
136
|
+
export type Delay =
|
|
137
|
+
| number
|
|
138
|
+
| Partial<{
|
|
139
|
+
open: number
|
|
140
|
+
close: number
|
|
141
|
+
}>
|