@tallyui/primitives 0.1.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.d.ts +1370 -0
- package/dist/index.js +3954 -0
- package/package.json +47 -0
- package/src/__tests__/accordion.test.tsx +370 -0
- package/src/__tests__/alert-dialog.test.tsx +127 -0
- package/src/__tests__/animated.test.tsx +184 -0
- package/src/__tests__/checkbox.test.tsx +90 -0
- package/src/__tests__/collapsible.test.tsx +149 -0
- package/src/__tests__/combobox.test.tsx +292 -0
- package/src/__tests__/context-menu.test.tsx +226 -0
- package/src/__tests__/dialog.test.tsx +81 -0
- package/src/__tests__/dropdown-menu.test.tsx +119 -0
- package/src/__tests__/hooks.test.tsx +46 -0
- package/src/__tests__/hover-card.test.tsx +77 -0
- package/src/__tests__/image.test.tsx +53 -0
- package/src/__tests__/label.test.tsx +50 -0
- package/src/__tests__/list.test.tsx +63 -0
- package/src/__tests__/popover.test.tsx +63 -0
- package/src/__tests__/portal.test.tsx +66 -0
- package/src/__tests__/progress.test.tsx +70 -0
- package/src/__tests__/radio-group.test.tsx +164 -0
- package/src/__tests__/select.test.tsx +76 -0
- package/src/__tests__/separator.test.tsx +37 -0
- package/src/__tests__/slider.test.tsx +124 -0
- package/src/__tests__/slot.test.tsx +56 -0
- package/src/__tests__/switch.test.tsx +70 -0
- package/src/__tests__/table.test.tsx +172 -0
- package/src/__tests__/tabs.test.tsx +67 -0
- package/src/__tests__/text.test.tsx +59 -0
- package/src/__tests__/toast.test.tsx +137 -0
- package/src/__tests__/toggle-group.test.tsx +167 -0
- package/src/__tests__/toggle.test.tsx +60 -0
- package/src/__tests__/tooltip.test.tsx +77 -0
- package/src/__tests__/types.test-d.ts +106 -0
- package/src/__tests__/utils.test.ts +47 -0
- package/src/accordion/accordion.tsx +232 -0
- package/src/accordion/index.ts +12 -0
- package/src/accordion/types.ts +56 -0
- package/src/alert-dialog/alert-dialog.tsx +296 -0
- package/src/alert-dialog/index.ts +25 -0
- package/src/alert-dialog/types.ts +50 -0
- package/src/animated/index.ts +3 -0
- package/src/animated/presence.tsx +27 -0
- package/src/animated/types.ts +36 -0
- package/src/animated/use-presence.ts +44 -0
- package/src/checkbox/checkbox.tsx +106 -0
- package/src/checkbox/index.ts +2 -0
- package/src/checkbox/types.ts +19 -0
- package/src/collapsible/collapsible.tsx +135 -0
- package/src/collapsible/index.ts +2 -0
- package/src/collapsible/types.ts +25 -0
- package/src/combobox/combobox.tsx +325 -0
- package/src/combobox/index.ts +24 -0
- package/src/combobox/types.ts +84 -0
- package/src/context-menu/context-menu.tsx +654 -0
- package/src/context-menu/index.ts +40 -0
- package/src/context-menu/types.ts +117 -0
- package/src/dialog/dialog.tsx +260 -0
- package/src/dialog/index.ts +23 -0
- package/src/dialog/types.ts +47 -0
- package/src/dropdown-menu/dropdown-menu.tsx +654 -0
- package/src/dropdown-menu/index.ts +40 -0
- package/src/dropdown-menu/types.ts +117 -0
- package/src/hooks/index.ts +6 -0
- package/src/hooks/use-augmented-ref.ts +32 -0
- package/src/hooks/use-controllable-state.ts +87 -0
- package/src/hooks/use-relative-position.ts +253 -0
- package/src/hooks/use-relative-position.web.ts +9 -0
- package/src/hover-card/hover-card.tsx +271 -0
- package/src/hover-card/index.ts +17 -0
- package/src/hover-card/types.ts +50 -0
- package/src/image/image.tsx +29 -0
- package/src/image/index.ts +2 -0
- package/src/image/types.ts +10 -0
- package/src/index.ts +38 -0
- package/src/label/index.ts +2 -0
- package/src/label/label.tsx +45 -0
- package/src/label/types.ts +11 -0
- package/src/list/index.ts +2 -0
- package/src/list/list.tsx +54 -0
- package/src/list/types.ts +14 -0
- package/src/popover/index.ts +19 -0
- package/src/popover/popover.tsx +314 -0
- package/src/popover/types.ts +51 -0
- package/src/portal.tsx +60 -0
- package/src/progress/index.ts +2 -0
- package/src/progress/progress.tsx +92 -0
- package/src/progress/types.ts +16 -0
- package/src/radio-group/index.ts +2 -0
- package/src/radio-group/radio-group.tsx +160 -0
- package/src/radio-group/types.ts +31 -0
- package/src/select/index.ts +34 -0
- package/src/select/select.tsx +465 -0
- package/src/select/types.ts +112 -0
- package/src/separator/index.ts +2 -0
- package/src/separator/separator.tsx +32 -0
- package/src/separator/types.ts +8 -0
- package/src/slider/index.ts +2 -0
- package/src/slider/slider.tsx +139 -0
- package/src/slider/types.ts +35 -0
- package/src/slot.tsx +160 -0
- package/src/switch/index.ts +2 -0
- package/src/switch/switch.tsx +92 -0
- package/src/switch/types.ts +18 -0
- package/src/table/index.ts +12 -0
- package/src/table/table.tsx +160 -0
- package/src/table/types.ts +30 -0
- package/src/tabs/index.ts +15 -0
- package/src/tabs/tabs.tsx +140 -0
- package/src/tabs/types.ts +36 -0
- package/src/text/index.ts +2 -0
- package/src/text/text.tsx +40 -0
- package/src/text/types.ts +5 -0
- package/src/toast/index.ts +2 -0
- package/src/toast/toast.tsx +189 -0
- package/src/toast/types.ts +35 -0
- package/src/toggle/index.ts +2 -0
- package/src/toggle/toggle.tsx +59 -0
- package/src/toggle/types.ts +11 -0
- package/src/toggle-group/index.ts +2 -0
- package/src/toggle-group/toggle-group.tsx +123 -0
- package/src/toggle-group/types.ts +36 -0
- package/src/tooltip/index.ts +17 -0
- package/src/tooltip/tooltip.tsx +272 -0
- package/src/tooltip/types.ts +49 -0
- package/src/types.ts +129 -0
- package/src/uniwind-env.d.ts +31 -0
- package/src/utils.ts +67 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Pressable,
|
|
4
|
+
View,
|
|
5
|
+
type GestureResponderEvent,
|
|
6
|
+
type LayoutChangeEvent,
|
|
7
|
+
type LayoutRectangle,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
import { useAugmentedRef, useControllableState, useRelativePosition, type LayoutPosition } from '../hooks';
|
|
10
|
+
import { Portal as RNPortal } from '../portal';
|
|
11
|
+
import { Slot } from '../slot';
|
|
12
|
+
import type {
|
|
13
|
+
CloseProps,
|
|
14
|
+
ContentProps,
|
|
15
|
+
OverlayProps,
|
|
16
|
+
PortalProps,
|
|
17
|
+
RootContext as RootContextType,
|
|
18
|
+
RootProps,
|
|
19
|
+
TriggerProps,
|
|
20
|
+
} from './types';
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Internal context (nativeID for aria linkage)
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
const InternalContext = React.createContext<{ nativeID: string } | null>(null);
|
|
27
|
+
|
|
28
|
+
function useInternalContext() {
|
|
29
|
+
const context = React.useContext(InternalContext);
|
|
30
|
+
if (!context) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
'Popover compound components cannot be rendered outside the Popover.Root component'
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return context;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// Root context (open state + positioning data)
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
const RootContext = React.createContext<RootContextType | null>(null);
|
|
43
|
+
|
|
44
|
+
function useRootContext(): RootContextType {
|
|
45
|
+
const context = React.useContext(RootContext);
|
|
46
|
+
if (!context) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
'Popover compound components cannot be rendered outside the Popover.Root component'
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return context;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Root
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
function Root({ open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, children }: RootProps) {
|
|
59
|
+
const nativeID = React.useId();
|
|
60
|
+
const [open = false, onOpenChange] = useControllableState({
|
|
61
|
+
prop: openProp,
|
|
62
|
+
defaultProp: defaultOpen,
|
|
63
|
+
onChange: onOpenChangeProp,
|
|
64
|
+
});
|
|
65
|
+
const [triggerPosition, setTriggerPosition] = React.useState<LayoutPosition | null>(null);
|
|
66
|
+
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<InternalContext.Provider value={{ nativeID }}>
|
|
70
|
+
<RootContext.Provider
|
|
71
|
+
value={{
|
|
72
|
+
open,
|
|
73
|
+
onOpenChange,
|
|
74
|
+
triggerPosition,
|
|
75
|
+
setTriggerPosition,
|
|
76
|
+
contentLayout,
|
|
77
|
+
setContentLayout,
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
{children}
|
|
81
|
+
</RootContext.Provider>
|
|
82
|
+
</InternalContext.Provider>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Root.displayName = 'PopoverRoot';
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Trigger
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
93
|
+
{ asChild, onPress: onPressProp, disabled, ...props },
|
|
94
|
+
ref
|
|
95
|
+
) {
|
|
96
|
+
const { open, onOpenChange, setTriggerPosition } = useRootContext();
|
|
97
|
+
|
|
98
|
+
const augmentedRef = useAugmentedRef<View>({
|
|
99
|
+
ref,
|
|
100
|
+
methods: {
|
|
101
|
+
open: () => {
|
|
102
|
+
onOpenChange(true);
|
|
103
|
+
augmentedRef.current?.measure?.((_x, _y, width, height, pageX, pageY) => {
|
|
104
|
+
setTriggerPosition({ width, pageX, pageY, height });
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
close: () => {
|
|
108
|
+
setTriggerPosition(null);
|
|
109
|
+
onOpenChange(false);
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const onPress = React.useCallback(
|
|
115
|
+
(ev: GestureResponderEvent) => {
|
|
116
|
+
if (disabled) return;
|
|
117
|
+
augmentedRef.current?.measure?.((_x, _y, width, height, pageX, pageY) => {
|
|
118
|
+
setTriggerPosition({ width, pageX, pageY, height });
|
|
119
|
+
});
|
|
120
|
+
onOpenChange(!open);
|
|
121
|
+
onPressProp?.(ev);
|
|
122
|
+
},
|
|
123
|
+
[augmentedRef, disabled, onOpenChange, onPressProp, open, setTriggerPosition]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const Component = asChild ? Slot : Pressable;
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<Component
|
|
130
|
+
ref={augmentedRef}
|
|
131
|
+
role="button"
|
|
132
|
+
aria-disabled={disabled || undefined}
|
|
133
|
+
onPress={onPress}
|
|
134
|
+
disabled={disabled || undefined}
|
|
135
|
+
{...props}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
Trigger.displayName = 'PopoverTrigger';
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Portal
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
function Portal({ forceMount, hostName, children }: PortalProps) {
|
|
147
|
+
const internalValue = useInternalContext();
|
|
148
|
+
const rootValue = useRootContext();
|
|
149
|
+
|
|
150
|
+
if (!forceMount && !rootValue.open) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<RNPortal hostName={hostName} name={`${internalValue.nativeID}_portal`}>
|
|
156
|
+
<InternalContext.Provider value={internalValue}>
|
|
157
|
+
<RootContext.Provider value={rootValue}>{children}</RootContext.Provider>
|
|
158
|
+
</InternalContext.Provider>
|
|
159
|
+
</RNPortal>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
Portal.displayName = 'PopoverPortal';
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// Overlay
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
const Overlay = React.forwardRef<View, OverlayProps>(function Overlay(
|
|
170
|
+
{ asChild, forceMount, onPress: onPressProp, closeOnPress = true, ...props },
|
|
171
|
+
ref
|
|
172
|
+
) {
|
|
173
|
+
const { open, onOpenChange, setTriggerPosition, setContentLayout } = useRootContext();
|
|
174
|
+
|
|
175
|
+
const onPress = React.useCallback(
|
|
176
|
+
(ev: GestureResponderEvent) => {
|
|
177
|
+
if (closeOnPress) {
|
|
178
|
+
setTriggerPosition(null);
|
|
179
|
+
setContentLayout(null);
|
|
180
|
+
onOpenChange(false);
|
|
181
|
+
}
|
|
182
|
+
onPressProp?.(ev);
|
|
183
|
+
},
|
|
184
|
+
[closeOnPress, onOpenChange, onPressProp, setContentLayout, setTriggerPosition]
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
if (!forceMount && !open) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const Component = asChild ? Slot : Pressable;
|
|
192
|
+
|
|
193
|
+
return <Component ref={ref} onPress={onPress} {...props} />;
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
Overlay.displayName = 'PopoverOverlay';
|
|
197
|
+
|
|
198
|
+
// ---------------------------------------------------------------------------
|
|
199
|
+
// Content
|
|
200
|
+
// ---------------------------------------------------------------------------
|
|
201
|
+
|
|
202
|
+
const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
203
|
+
{
|
|
204
|
+
asChild,
|
|
205
|
+
forceMount,
|
|
206
|
+
align = 'start',
|
|
207
|
+
side = 'bottom',
|
|
208
|
+
sideOffset = 0,
|
|
209
|
+
alignOffset = 0,
|
|
210
|
+
avoidCollisions = true,
|
|
211
|
+
insets,
|
|
212
|
+
style,
|
|
213
|
+
disablePositioningStyle,
|
|
214
|
+
onLayout: onLayoutProp,
|
|
215
|
+
...props
|
|
216
|
+
},
|
|
217
|
+
ref
|
|
218
|
+
) {
|
|
219
|
+
const { open, triggerPosition, contentLayout, setContentLayout } = useRootContext();
|
|
220
|
+
const { nativeID } = useInternalContext();
|
|
221
|
+
|
|
222
|
+
const positionStyle = useRelativePosition({
|
|
223
|
+
align,
|
|
224
|
+
avoidCollisions,
|
|
225
|
+
triggerPosition,
|
|
226
|
+
contentLayout,
|
|
227
|
+
alignOffset,
|
|
228
|
+
insets,
|
|
229
|
+
sideOffset,
|
|
230
|
+
side,
|
|
231
|
+
disablePositioningStyle,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const onLayout = React.useCallback(
|
|
235
|
+
(event: LayoutChangeEvent) => {
|
|
236
|
+
setContentLayout(event.nativeEvent.layout);
|
|
237
|
+
onLayoutProp?.(event);
|
|
238
|
+
},
|
|
239
|
+
[onLayoutProp, setContentLayout]
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
if (!forceMount && !open) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const Component = asChild ? Slot : View;
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<Component
|
|
250
|
+
ref={ref}
|
|
251
|
+
role="dialog"
|
|
252
|
+
nativeID={nativeID}
|
|
253
|
+
aria-labelledby={`${nativeID}_title`}
|
|
254
|
+
aria-describedby={`${nativeID}_description`}
|
|
255
|
+
aria-modal={true}
|
|
256
|
+
style={[positionStyle, style]}
|
|
257
|
+
onLayout={onLayout}
|
|
258
|
+
{...props}
|
|
259
|
+
/>
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
Content.displayName = 'PopoverContent';
|
|
264
|
+
|
|
265
|
+
// ---------------------------------------------------------------------------
|
|
266
|
+
// Close
|
|
267
|
+
// ---------------------------------------------------------------------------
|
|
268
|
+
|
|
269
|
+
const Close = React.forwardRef<View, CloseProps>(function Close(
|
|
270
|
+
{ asChild, onPress: onPressProp, disabled, ...props },
|
|
271
|
+
ref
|
|
272
|
+
) {
|
|
273
|
+
const { onOpenChange, setTriggerPosition, setContentLayout } = useRootContext();
|
|
274
|
+
|
|
275
|
+
const onPress = React.useCallback(
|
|
276
|
+
(ev: GestureResponderEvent) => {
|
|
277
|
+
if (disabled) return;
|
|
278
|
+
setTriggerPosition(null);
|
|
279
|
+
setContentLayout(null);
|
|
280
|
+
onOpenChange(false);
|
|
281
|
+
onPressProp?.(ev);
|
|
282
|
+
},
|
|
283
|
+
[disabled, onOpenChange, onPressProp, setContentLayout, setTriggerPosition]
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const Component = asChild ? Slot : Pressable;
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
<Component
|
|
290
|
+
ref={ref}
|
|
291
|
+
role="button"
|
|
292
|
+
aria-disabled={disabled || undefined}
|
|
293
|
+
onPress={onPress}
|
|
294
|
+
disabled={disabled || undefined}
|
|
295
|
+
{...props}
|
|
296
|
+
/>
|
|
297
|
+
);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
Close.displayName = 'PopoverClose';
|
|
301
|
+
|
|
302
|
+
// ---------------------------------------------------------------------------
|
|
303
|
+
// Exports
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
|
|
306
|
+
export {
|
|
307
|
+
Close,
|
|
308
|
+
Content,
|
|
309
|
+
Overlay,
|
|
310
|
+
Portal,
|
|
311
|
+
Root,
|
|
312
|
+
Trigger,
|
|
313
|
+
useRootContext,
|
|
314
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ForceMountable,
|
|
3
|
+
PositionedContentProps,
|
|
4
|
+
SlottablePressableProps,
|
|
5
|
+
SlottableViewProps,
|
|
6
|
+
} from '../types';
|
|
7
|
+
|
|
8
|
+
interface RootProps {
|
|
9
|
+
open?: boolean;
|
|
10
|
+
defaultOpen?: boolean;
|
|
11
|
+
onOpenChange?: (open: boolean) => void;
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TriggerProps extends SlottablePressableProps {}
|
|
16
|
+
|
|
17
|
+
interface PortalProps extends ForceMountable {
|
|
18
|
+
hostName?: string;
|
|
19
|
+
children?: React.ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface OverlayProps extends SlottablePressableProps, ForceMountable {
|
|
23
|
+
closeOnPress?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ContentProps extends SlottableViewProps, PositionedContentProps {}
|
|
27
|
+
|
|
28
|
+
interface CloseProps extends SlottablePressableProps {}
|
|
29
|
+
|
|
30
|
+
interface RootContext {
|
|
31
|
+
open: boolean;
|
|
32
|
+
onOpenChange: (open: boolean) => void;
|
|
33
|
+
triggerPosition: LayoutPosition | null;
|
|
34
|
+
setTriggerPosition: (position: LayoutPosition | null) => void;
|
|
35
|
+
contentLayout: LayoutRectangle | null;
|
|
36
|
+
setContentLayout: (layout: LayoutRectangle | null) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Re-export from hooks for convenience
|
|
40
|
+
import type { LayoutPosition } from '../hooks';
|
|
41
|
+
import type { LayoutRectangle } from 'react-native';
|
|
42
|
+
|
|
43
|
+
export type {
|
|
44
|
+
CloseProps,
|
|
45
|
+
ContentProps,
|
|
46
|
+
OverlayProps,
|
|
47
|
+
PortalProps,
|
|
48
|
+
RootContext,
|
|
49
|
+
RootProps,
|
|
50
|
+
TriggerProps,
|
|
51
|
+
};
|
package/src/portal.tsx
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { create } from 'zustand';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_PORTAL_HOST = 'INTERNAL_PRIMITIVE_DEFAULT_HOST_NAME';
|
|
5
|
+
|
|
6
|
+
type PortalMap = Map<string, React.ReactNode>;
|
|
7
|
+
type PortalHostMap = Map<string, PortalMap>;
|
|
8
|
+
|
|
9
|
+
const usePortal = create<{ map: PortalHostMap }>(() => ({
|
|
10
|
+
map: new Map<string, PortalMap>().set(DEFAULT_PORTAL_HOST, new Map<string, React.ReactNode>()),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
const updatePortal = (hostName: string, name: string, children: React.ReactNode) => {
|
|
14
|
+
usePortal.setState((prev) => {
|
|
15
|
+
const next = new Map(prev.map);
|
|
16
|
+
const portal = next.get(hostName) ?? new Map<string, React.ReactNode>();
|
|
17
|
+
portal.set(name, children);
|
|
18
|
+
next.set(hostName, portal);
|
|
19
|
+
return { map: next };
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const removePortal = (hostName: string, name: string) => {
|
|
24
|
+
usePortal.setState((prev) => {
|
|
25
|
+
const next = new Map(prev.map);
|
|
26
|
+
const portal = next.get(hostName) ?? new Map<string, React.ReactNode>();
|
|
27
|
+
portal.delete(name);
|
|
28
|
+
next.set(hostName, portal);
|
|
29
|
+
return { map: next };
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function PortalHost({ name = DEFAULT_PORTAL_HOST }: { name?: string }) {
|
|
34
|
+
const portalMap =
|
|
35
|
+
usePortal((state) => state.map).get(name) ?? new Map<string, React.ReactNode>();
|
|
36
|
+
if (portalMap.size === 0) return null;
|
|
37
|
+
return <>{Array.from(portalMap.values())}</>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function Portal({
|
|
41
|
+
name,
|
|
42
|
+
hostName = DEFAULT_PORTAL_HOST,
|
|
43
|
+
children,
|
|
44
|
+
}: {
|
|
45
|
+
name: string;
|
|
46
|
+
hostName?: string;
|
|
47
|
+
children: React.ReactNode;
|
|
48
|
+
}) {
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
updatePortal(hostName, name, children);
|
|
51
|
+
}, [hostName, name, children]);
|
|
52
|
+
|
|
53
|
+
React.useEffect(() => {
|
|
54
|
+
return () => {
|
|
55
|
+
removePortal(hostName, name);
|
|
56
|
+
};
|
|
57
|
+
}, [hostName, name]);
|
|
58
|
+
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import { Slot } from '../slot';
|
|
4
|
+
import type { RootContext as RootContextType, RootProps, IndicatorProps } from './types';
|
|
5
|
+
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Constants
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
const DEFAULT_MAX = 100;
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Context
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
const RootContext = React.createContext<RootContextType | null>(null);
|
|
17
|
+
|
|
18
|
+
function useRootContext(): RootContextType {
|
|
19
|
+
const context = React.useContext(RootContext);
|
|
20
|
+
if (!context) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'Progress compound components cannot be rendered outside the Progress.Root component'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
return context;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Helpers
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
function defaultGetValueLabel(value: number, max: number) {
|
|
33
|
+
return `${Math.round((value / max) * 100)}%`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isValidValueNumber(value: any, max: number): value is number {
|
|
37
|
+
return typeof value === 'number' && !isNaN(value) && value <= max && value >= 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Root
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
const Root = React.forwardRef<View, RootProps>(function Root(
|
|
45
|
+
{ asChild, value: valueProp, max: maxProp, getValueLabel = defaultGetValueLabel, ...props },
|
|
46
|
+
ref
|
|
47
|
+
) {
|
|
48
|
+
const max = maxProp ?? DEFAULT_MAX;
|
|
49
|
+
const isIndeterminate = valueProp === null || valueProp === undefined;
|
|
50
|
+
const value = isIndeterminate ? null : isValidValueNumber(valueProp, max) ? valueProp : 0;
|
|
51
|
+
|
|
52
|
+
const Component = asChild ? Slot : View;
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<RootContext.Provider value={{ value, max }}>
|
|
56
|
+
<Component
|
|
57
|
+
ref={ref}
|
|
58
|
+
role="progressbar"
|
|
59
|
+
aria-valuemin={0}
|
|
60
|
+
aria-valuemax={max}
|
|
61
|
+
aria-valuenow={value ?? undefined}
|
|
62
|
+
aria-valuetext={value != null ? getValueLabel(value, max) : undefined}
|
|
63
|
+
{...props}
|
|
64
|
+
/>
|
|
65
|
+
</RootContext.Provider>
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
Root.displayName = 'ProgressRoot';
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// Indicator
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
const Indicator = React.forwardRef<View, IndicatorProps>(function Indicator(
|
|
76
|
+
{ asChild, ...props },
|
|
77
|
+
ref
|
|
78
|
+
) {
|
|
79
|
+
useRootContext();
|
|
80
|
+
|
|
81
|
+
const Component = asChild ? Slot : View;
|
|
82
|
+
|
|
83
|
+
return <Component ref={ref} {...props} />;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
Indicator.displayName = 'ProgressIndicator';
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Exports
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
export { Root, Indicator, useRootContext };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SlottableViewProps } from '../types';
|
|
2
|
+
|
|
3
|
+
interface RootProps extends SlottableViewProps {
|
|
4
|
+
value?: number | null;
|
|
5
|
+
max?: number;
|
|
6
|
+
getValueLabel?: (value: number, max: number) => string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface IndicatorProps extends SlottableViewProps {}
|
|
10
|
+
|
|
11
|
+
interface RootContext {
|
|
12
|
+
value: number | null;
|
|
13
|
+
max: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type { RootProps, IndicatorProps, RootContext };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Pressable, View, type GestureResponderEvent } from 'react-native';
|
|
3
|
+
import { useControllableState } from '../hooks';
|
|
4
|
+
import { Slot } from '../slot';
|
|
5
|
+
import type {
|
|
6
|
+
RootProps,
|
|
7
|
+
ItemProps,
|
|
8
|
+
IndicatorProps,
|
|
9
|
+
RootContext as RootContextType,
|
|
10
|
+
ItemContext as ItemContextType,
|
|
11
|
+
} from './types';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Context
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
const RootContext = React.createContext<RootContextType | null>(null);
|
|
18
|
+
|
|
19
|
+
function useRootContext(): RootContextType {
|
|
20
|
+
const context = React.useContext(RootContext);
|
|
21
|
+
if (!context) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
'RadioGroup compound components cannot be rendered outside the RadioGroup.Root component'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return context;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ItemContext = React.createContext<ItemContextType | null>(null);
|
|
30
|
+
|
|
31
|
+
function useItemContext(): ItemContextType {
|
|
32
|
+
const context = React.useContext(ItemContext);
|
|
33
|
+
if (!context) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
'RadioGroup.Indicator must be rendered inside a RadioGroup.Item component'
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Root
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
const Root = React.forwardRef<View, RootProps>(function Root(
|
|
46
|
+
{
|
|
47
|
+
asChild,
|
|
48
|
+
value: valueProp,
|
|
49
|
+
defaultValue,
|
|
50
|
+
onValueChange: onValueChangeProp,
|
|
51
|
+
disabled = false,
|
|
52
|
+
...props
|
|
53
|
+
},
|
|
54
|
+
ref
|
|
55
|
+
) {
|
|
56
|
+
const [value, setValue] = useControllableState({
|
|
57
|
+
prop: valueProp,
|
|
58
|
+
defaultProp: defaultValue,
|
|
59
|
+
onChange: onValueChangeProp,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const onValueChange = React.useCallback(
|
|
63
|
+
(itemValue: string) => {
|
|
64
|
+
setValue(itemValue);
|
|
65
|
+
},
|
|
66
|
+
[setValue]
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const Component = asChild ? Slot : View;
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<RootContext.Provider value={{ value, disabled, onValueChange }}>
|
|
73
|
+
<Component ref={ref} role="radiogroup" {...props} />
|
|
74
|
+
</RootContext.Provider>
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
Root.displayName = 'RadioGroupRoot';
|
|
79
|
+
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
// Item
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
const Item = React.forwardRef<View, ItemProps>(function Item(
|
|
85
|
+
{
|
|
86
|
+
asChild,
|
|
87
|
+
value: itemValue,
|
|
88
|
+
disabled: disabledProp = false,
|
|
89
|
+
onPress: onPressProp,
|
|
90
|
+
...props
|
|
91
|
+
},
|
|
92
|
+
ref
|
|
93
|
+
) {
|
|
94
|
+
const { disabled: rootDisabled, value: rootValue, onValueChange } = useRootContext();
|
|
95
|
+
|
|
96
|
+
const isDisabled = rootDisabled || disabledProp;
|
|
97
|
+
const checked = rootValue === itemValue;
|
|
98
|
+
|
|
99
|
+
const onPress = React.useCallback(
|
|
100
|
+
(ev: GestureResponderEvent) => {
|
|
101
|
+
if (isDisabled) return;
|
|
102
|
+
onValueChange(itemValue);
|
|
103
|
+
onPressProp?.(ev);
|
|
104
|
+
},
|
|
105
|
+
[isDisabled, onValueChange, itemValue, onPressProp]
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const Component = asChild ? Slot : Pressable;
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<ItemContext.Provider value={{ value: itemValue, checked, disabled: isDisabled }}>
|
|
112
|
+
<Component
|
|
113
|
+
ref={ref}
|
|
114
|
+
role="radio"
|
|
115
|
+
aria-checked={checked}
|
|
116
|
+
aria-disabled={isDisabled || undefined}
|
|
117
|
+
onPress={onPress}
|
|
118
|
+
disabled={isDisabled || undefined}
|
|
119
|
+
{...props}
|
|
120
|
+
/>
|
|
121
|
+
</ItemContext.Provider>
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
Item.displayName = 'RadioGroupItem';
|
|
126
|
+
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
// Indicator
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
const Indicator = React.forwardRef<View, IndicatorProps>(function Indicator(
|
|
132
|
+
{ asChild, forceMount, ...props },
|
|
133
|
+
ref
|
|
134
|
+
) {
|
|
135
|
+
const { checked, disabled } = useItemContext();
|
|
136
|
+
|
|
137
|
+
if (!forceMount && !checked) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const Component = asChild ? Slot : View;
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<Component
|
|
145
|
+
ref={ref}
|
|
146
|
+
role="presentation"
|
|
147
|
+
aria-hidden={!(forceMount || checked)}
|
|
148
|
+
aria-disabled={disabled || undefined}
|
|
149
|
+
{...props}
|
|
150
|
+
/>
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
Indicator.displayName = 'RadioGroupIndicator';
|
|
155
|
+
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// Exports
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
export { Root, Item, Indicator, useRootContext, useItemContext };
|