@react-native-reusables/cli 0.0.9 → 0.0.11
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/README.md +95 -5
- package/dist/generated/components/primitives/accordion/accordion.tsx +216 -0
- package/dist/generated/components/primitives/accordion/accordion.web.tsx +295 -0
- package/dist/generated/components/primitives/accordion/index.ts +1 -0
- package/dist/generated/components/primitives/accordion/types.ts +45 -0
- package/dist/generated/components/primitives/alert-dialog/alert-dialog.tsx +237 -0
- package/dist/generated/components/primitives/alert-dialog/alert-dialog.web.tsx +256 -0
- package/dist/generated/components/primitives/alert-dialog/index.ts +1 -0
- package/dist/generated/components/primitives/alert-dialog/types.ts +48 -0
- package/dist/generated/components/primitives/aspect-ratio.tsx +23 -0
- package/dist/generated/components/primitives/avatar/ types.ts +10 -0
- package/dist/generated/components/primitives/avatar/index.tsx +95 -0
- package/dist/generated/components/primitives/checkbox/checkbox.tsx +101 -0
- package/dist/generated/components/primitives/checkbox/checkbox.web.tsx +114 -0
- package/dist/generated/components/primitives/checkbox/index.ts +1 -0
- package/dist/generated/components/primitives/checkbox/types.ts +11 -0
- package/dist/generated/components/primitives/collapsible/collapsible.tsx +119 -0
- package/dist/generated/components/primitives/collapsible/collapsible.web.tsx +157 -0
- package/dist/generated/components/primitives/collapsible/index.ts +1 -0
- package/dist/generated/components/primitives/collapsible/types.ts +18 -0
- package/dist/generated/components/primitives/context-menu/context-menu.tsx +626 -0
- package/dist/generated/components/primitives/context-menu/context-menu.web.tsx +504 -0
- package/dist/generated/components/primitives/context-menu/index.ts +1 -0
- package/dist/generated/components/primitives/context-menu/types.ts +82 -0
- package/dist/generated/components/primitives/dialog/dialog.tsx +211 -0
- package/dist/generated/components/primitives/dialog/dialog.web.tsx +197 -0
- package/dist/generated/components/primitives/dialog/index.ts +1 -0
- package/dist/generated/components/primitives/dialog/types.ts +60 -0
- package/dist/generated/components/primitives/dropdown-menu/dropdown-menu.tsx +584 -0
- package/dist/generated/components/primitives/dropdown-menu/dropdown-menu.web.tsx +521 -0
- package/dist/generated/components/primitives/dropdown-menu/index.ts +1 -0
- package/dist/generated/components/primitives/dropdown-menu/types.ts +71 -0
- package/dist/generated/components/primitives/hooks/index.ts +3 -0
- package/dist/generated/components/primitives/hooks/useAugmentedRef.tsx +29 -0
- package/dist/generated/components/primitives/hooks/useControllableState.tsx +75 -0
- package/dist/generated/components/primitives/hooks/useRelativePosition.tsx +227 -0
- package/dist/generated/components/primitives/hover-card/hover-card.tsx +271 -0
- package/dist/generated/components/primitives/hover-card/hover-card.web.tsx +145 -0
- package/dist/generated/components/primitives/hover-card/index.ts +1 -0
- package/dist/generated/components/primitives/hover-card/types.ts +42 -0
- package/dist/generated/components/primitives/label/index.ts +1 -0
- package/dist/generated/components/primitives/label/label.tsx +31 -0
- package/dist/generated/components/primitives/label/label.web.tsx +36 -0
- package/dist/generated/components/primitives/label/types.ts +15 -0
- package/dist/generated/components/primitives/menubar/index.ts +1 -0
- package/dist/generated/components/primitives/menubar/menubar.tsx +624 -0
- package/dist/generated/components/primitives/menubar/menubar.web.tsx +543 -0
- package/dist/generated/components/primitives/menubar/types.ts +76 -0
- package/dist/generated/components/primitives/navigation-menu/index.ts +1 -0
- package/dist/generated/components/primitives/navigation-menu/navigation-menu.tsx +315 -0
- package/dist/generated/components/primitives/navigation-menu/navigation-menu.web.tsx +264 -0
- package/dist/generated/components/primitives/navigation-menu/types.ts +49 -0
- package/dist/generated/components/primitives/popover/index.ts +1 -0
- package/dist/generated/components/primitives/popover/popover.tsx +286 -0
- package/dist/generated/components/primitives/popover/popover.web.tsx +179 -0
- package/dist/generated/components/primitives/popover/types.ts +30 -0
- package/dist/generated/components/primitives/portal.tsx +67 -0
- package/dist/generated/components/primitives/progress/index.ts +1 -0
- package/dist/generated/components/primitives/progress/progress.tsx +59 -0
- package/dist/generated/components/primitives/progress/progress.web.tsx +36 -0
- package/dist/generated/components/primitives/progress/types.ts +7 -0
- package/dist/generated/components/primitives/radio-group/index.ts +1 -0
- package/dist/generated/components/primitives/radio-group/radio-group.tsx +116 -0
- package/dist/generated/components/primitives/radio-group/radio-group.web.tsx +78 -0
- package/dist/generated/components/primitives/radio-group/types.ts +15 -0
- package/dist/generated/components/primitives/select/index.ts +1 -0
- package/dist/generated/components/primitives/select/select.tsx +455 -0
- package/dist/generated/components/primitives/select/select.web.tsx +319 -0
- package/dist/generated/components/primitives/select/types.ts +87 -0
- package/dist/generated/components/primitives/separator/ types.ts +6 -0
- package/dist/generated/components/primitives/separator/index.tsx +23 -0
- package/dist/generated/components/primitives/slider/index.ts +1 -0
- package/dist/generated/components/primitives/slider/slider.tsx +89 -0
- package/dist/generated/components/primitives/slider/slider.web.tsx +67 -0
- package/dist/generated/components/primitives/slider/types.ts +24 -0
- package/dist/generated/components/primitives/slot.tsx +187 -0
- package/dist/generated/components/primitives/switch/index.ts +1 -0
- package/dist/generated/components/primitives/switch/switch.tsx +65 -0
- package/dist/generated/components/primitives/switch/switch.web.tsx +67 -0
- package/dist/generated/components/primitives/switch/types.ts +11 -0
- package/dist/generated/components/primitives/table.tsx +55 -0
- package/dist/generated/components/primitives/tabs/index.ts +1 -0
- package/dist/generated/components/primitives/tabs/tabs.tsx +133 -0
- package/dist/generated/components/primitives/tabs/tabs.web.tsx +97 -0
- package/dist/generated/components/primitives/tabs/types.ts +24 -0
- package/dist/generated/components/primitives/toast/ types.ts +7 -0
- package/dist/generated/components/primitives/toast/index.tsx +128 -0
- package/dist/generated/components/primitives/toggle/index.ts +1 -0
- package/dist/generated/components/primitives/toggle/toggle.tsx +37 -0
- package/dist/generated/components/primitives/toggle/toggle.web.tsx +26 -0
- package/dist/generated/components/primitives/toggle/types.ts +7 -0
- package/dist/generated/components/primitives/toggle-group/index.ts +1 -0
- package/dist/generated/components/primitives/toggle-group/toggle-group.tsx +125 -0
- package/dist/generated/components/primitives/toggle-group/toggle-group.web.tsx +124 -0
- package/dist/generated/components/primitives/toggle-group/types.ts +37 -0
- package/dist/generated/components/primitives/toolbar/index.ts +1 -0
- package/dist/generated/components/primitives/toolbar/toolbar.tsx +125 -0
- package/dist/generated/components/primitives/toolbar/toolbar.web.tsx +129 -0
- package/dist/generated/components/primitives/toolbar/types.ts +39 -0
- package/dist/generated/components/primitives/tooltip/index.ts +1 -0
- package/dist/generated/components/primitives/tooltip/tooltip.tsx +271 -0
- package/dist/generated/components/primitives/tooltip/tooltip.web.tsx +167 -0
- package/dist/generated/components/primitives/tooltip/types.ts +44 -0
- package/dist/generated/components/primitives/types.ts +105 -0
- package/dist/generated/components/primitives/utils.ts +61 -0
- package/dist/generated/components/ui/accordion.tsx +127 -0
- package/dist/generated/components/ui/alert-dialog.tsx +167 -0
- package/dist/generated/components/ui/aspect-ratio.tsx +5 -0
- package/dist/generated/components/ui/avatar.tsx +44 -0
- package/dist/generated/components/ui/badge.tsx +51 -0
- package/dist/generated/components/ui/button.tsx +88 -0
- package/dist/generated/components/ui/card.tsx +67 -0
- package/dist/generated/components/ui/checkbox.tsx +34 -0
- package/dist/generated/components/ui/collapsible.tsx +9 -0
- package/dist/generated/components/ui/context-menu.tsx +244 -0
- package/dist/generated/components/ui/dialog.tsx +150 -0
- package/dist/generated/components/ui/dropdown-menu.tsx +244 -0
- package/dist/generated/components/ui/hover-card.tsx +45 -0
- package/dist/generated/components/ui/input.tsx +26 -0
- package/dist/generated/components/ui/label.tsx +28 -0
- package/dist/generated/components/ui/menubar.tsx +260 -0
- package/dist/generated/components/ui/navigation-menu.tsx +177 -0
- package/dist/generated/components/ui/popover.tsx +39 -0
- package/dist/generated/components/ui/radio-group.tsx +38 -0
- package/dist/generated/components/ui/select.tsx +181 -0
- package/dist/generated/components/ui/separator.tsx +23 -0
- package/dist/generated/components/ui/skeleton.tsx +39 -0
- package/dist/generated/components/ui/switch.tsx +97 -0
- package/dist/generated/components/ui/table.tsx +99 -0
- package/dist/generated/components/ui/tabs.tsx +65 -0
- package/dist/generated/components/ui/text.tsx +24 -0
- package/dist/generated/components/ui/textarea.tsx +28 -0
- package/dist/generated/components/ui/toggle-group.tsx +86 -0
- package/dist/generated/components/ui/toggle.tsx +85 -0
- package/dist/generated/components/ui/tooltip.tsx +36 -0
- package/dist/generated/components/ui/typography.tsx +204 -0
- package/package.json +8 -8
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { useControllableState, useRelativePosition, type LayoutPosition } from '@rnr/hooks';
|
|
2
|
+
import { Portal as RNPPortal } from '@rnr/portal';
|
|
3
|
+
import * as Slot from '@rnr/slot';
|
|
4
|
+
import type {
|
|
5
|
+
PositionedContentProps,
|
|
6
|
+
PressableRef,
|
|
7
|
+
SlottablePressableProps,
|
|
8
|
+
SlottableViewProps,
|
|
9
|
+
ViewRef,
|
|
10
|
+
} from '@rnr/types';
|
|
11
|
+
import * as React from 'react';
|
|
12
|
+
import {
|
|
13
|
+
BackHandler,
|
|
14
|
+
Pressable,
|
|
15
|
+
View,
|
|
16
|
+
type GestureResponderEvent,
|
|
17
|
+
type LayoutChangeEvent,
|
|
18
|
+
type LayoutRectangle,
|
|
19
|
+
} from 'react-native';
|
|
20
|
+
import type {
|
|
21
|
+
PopoverOverlayProps,
|
|
22
|
+
PopoverPortalProps,
|
|
23
|
+
PopoverRootProps,
|
|
24
|
+
RootContext,
|
|
25
|
+
} from './types';
|
|
26
|
+
|
|
27
|
+
interface IRootContext extends RootContext {
|
|
28
|
+
triggerPosition: LayoutPosition | null;
|
|
29
|
+
setTriggerPosition: (triggerPosition: LayoutPosition | null) => void;
|
|
30
|
+
contentLayout: LayoutRectangle | null;
|
|
31
|
+
setContentLayout: (contentLayout: LayoutRectangle | null) => void;
|
|
32
|
+
nativeID: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const RootContext = React.createContext<IRootContext | null>(null);
|
|
36
|
+
|
|
37
|
+
const Root = React.forwardRef<ViewRef, SlottableViewProps & PopoverRootProps>(
|
|
38
|
+
({ asChild, open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, ...viewProps }, ref) => {
|
|
39
|
+
const [open = false, onOpenChange] = useControllableState({
|
|
40
|
+
prop: openProp,
|
|
41
|
+
defaultProp: defaultOpen,
|
|
42
|
+
onChange: onOpenChangeProp,
|
|
43
|
+
});
|
|
44
|
+
const nativeID = React.useId();
|
|
45
|
+
const [triggerPosition, setTriggerPosition] = React.useState<LayoutPosition | null>(null);
|
|
46
|
+
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
|
|
47
|
+
|
|
48
|
+
const Component = asChild ? Slot.View : View;
|
|
49
|
+
return (
|
|
50
|
+
<RootContext.Provider
|
|
51
|
+
value={{
|
|
52
|
+
open,
|
|
53
|
+
onOpenChange,
|
|
54
|
+
contentLayout,
|
|
55
|
+
nativeID,
|
|
56
|
+
setContentLayout,
|
|
57
|
+
setTriggerPosition,
|
|
58
|
+
triggerPosition,
|
|
59
|
+
}}
|
|
60
|
+
>
|
|
61
|
+
<Component ref={ref} {...viewProps} />
|
|
62
|
+
</RootContext.Provider>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
Root.displayName = 'RootNativePopover';
|
|
68
|
+
|
|
69
|
+
function usePopoverContext() {
|
|
70
|
+
const context = React.useContext(RootContext);
|
|
71
|
+
if (!context) {
|
|
72
|
+
throw new Error('Popover compound components cannot be rendered outside the Popover component');
|
|
73
|
+
}
|
|
74
|
+
return context;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const Trigger = React.forwardRef<PressableRef, SlottablePressableProps>(
|
|
78
|
+
({ asChild, onPress: onPressProp, disabled = false, ...props }, ref) => {
|
|
79
|
+
const triggerRef = React.useRef<View>(null);
|
|
80
|
+
const { open, onOpenChange, setTriggerPosition } = usePopoverContext();
|
|
81
|
+
|
|
82
|
+
React.useImperativeHandle(
|
|
83
|
+
ref,
|
|
84
|
+
() => {
|
|
85
|
+
if (!triggerRef.current) {
|
|
86
|
+
return new View({});
|
|
87
|
+
}
|
|
88
|
+
return triggerRef.current;
|
|
89
|
+
},
|
|
90
|
+
[triggerRef.current]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
function onPress(ev: GestureResponderEvent) {
|
|
94
|
+
if (disabled) return;
|
|
95
|
+
triggerRef.current?.measure((_x, _y, width, height, pageX, pageY) => {
|
|
96
|
+
setTriggerPosition({ width, pageX, pageY: pageY, height });
|
|
97
|
+
});
|
|
98
|
+
const newValue = !open;
|
|
99
|
+
onOpenChange(newValue);
|
|
100
|
+
onPressProp?.(ev);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
104
|
+
return (
|
|
105
|
+
<Component
|
|
106
|
+
ref={triggerRef}
|
|
107
|
+
aria-disabled={disabled ?? undefined}
|
|
108
|
+
role='button'
|
|
109
|
+
onPress={onPress}
|
|
110
|
+
disabled={disabled ?? undefined}
|
|
111
|
+
{...props}
|
|
112
|
+
/>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
Trigger.displayName = 'TriggerNativePopover';
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @warning when using a custom `<PortalHost />`, you might have to adjust the Content's sideOffset to account for nav elements like headers.
|
|
121
|
+
*/
|
|
122
|
+
function Portal({ forceMount, hostName, children }: PopoverPortalProps) {
|
|
123
|
+
const value = usePopoverContext();
|
|
124
|
+
|
|
125
|
+
if (!value.triggerPosition) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!forceMount) {
|
|
130
|
+
if (!value.open) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<RNPPortal hostName={hostName} name={`${value.nativeID}_portal`}>
|
|
137
|
+
<RootContext.Provider value={value}>{children}</RootContext.Provider>
|
|
138
|
+
</RNPPortal>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const Overlay = React.forwardRef<PressableRef, SlottablePressableProps & PopoverOverlayProps>(
|
|
143
|
+
({ asChild, forceMount, onPress: OnPressProp, closeOnPress = true, ...props }, ref) => {
|
|
144
|
+
const { open, onOpenChange, setTriggerPosition, setContentLayout } = usePopoverContext();
|
|
145
|
+
|
|
146
|
+
function onPress(ev: GestureResponderEvent) {
|
|
147
|
+
if (closeOnPress) {
|
|
148
|
+
setTriggerPosition(null);
|
|
149
|
+
setContentLayout(null);
|
|
150
|
+
onOpenChange(false);
|
|
151
|
+
}
|
|
152
|
+
OnPressProp?.(ev);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!forceMount) {
|
|
156
|
+
if (!open) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
162
|
+
return <Component ref={ref} onPress={onPress} {...props} />;
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
Overlay.displayName = 'OverlayNativePopover';
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @info `position`, `top`, `left`, and `maxWidth` style properties are controlled internally. Opt out of this behavior by setting `disablePositioningStyle` to `true`.
|
|
170
|
+
*/
|
|
171
|
+
const Content = React.forwardRef<ViewRef, SlottableViewProps & PositionedContentProps>(
|
|
172
|
+
(
|
|
173
|
+
{
|
|
174
|
+
asChild = false,
|
|
175
|
+
forceMount,
|
|
176
|
+
align = 'start',
|
|
177
|
+
side = 'bottom',
|
|
178
|
+
sideOffset = 0,
|
|
179
|
+
alignOffset = 0,
|
|
180
|
+
avoidCollisions = true,
|
|
181
|
+
onLayout: onLayoutProp,
|
|
182
|
+
insets,
|
|
183
|
+
style,
|
|
184
|
+
disablePositioningStyle,
|
|
185
|
+
...props
|
|
186
|
+
},
|
|
187
|
+
ref
|
|
188
|
+
) => {
|
|
189
|
+
const {
|
|
190
|
+
open,
|
|
191
|
+
onOpenChange,
|
|
192
|
+
contentLayout,
|
|
193
|
+
nativeID,
|
|
194
|
+
setContentLayout,
|
|
195
|
+
setTriggerPosition,
|
|
196
|
+
triggerPosition,
|
|
197
|
+
} = usePopoverContext();
|
|
198
|
+
|
|
199
|
+
React.useEffect(() => {
|
|
200
|
+
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
201
|
+
setTriggerPosition(null);
|
|
202
|
+
setContentLayout(null);
|
|
203
|
+
onOpenChange(false);
|
|
204
|
+
return true;
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return () => {
|
|
208
|
+
setContentLayout(null);
|
|
209
|
+
backHandler.remove();
|
|
210
|
+
};
|
|
211
|
+
}, []);
|
|
212
|
+
|
|
213
|
+
const positionStyle = useRelativePosition({
|
|
214
|
+
align,
|
|
215
|
+
avoidCollisions,
|
|
216
|
+
triggerPosition,
|
|
217
|
+
contentLayout,
|
|
218
|
+
alignOffset,
|
|
219
|
+
insets,
|
|
220
|
+
sideOffset,
|
|
221
|
+
side,
|
|
222
|
+
disablePositioningStyle,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
function onLayout(event: LayoutChangeEvent) {
|
|
226
|
+
setContentLayout(event.nativeEvent.layout);
|
|
227
|
+
onLayoutProp?.(event);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!forceMount) {
|
|
231
|
+
if (!open) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const Component = asChild ? Slot.View : View;
|
|
237
|
+
return (
|
|
238
|
+
<Component
|
|
239
|
+
ref={ref}
|
|
240
|
+
role='dialog'
|
|
241
|
+
nativeID={nativeID}
|
|
242
|
+
aria-modal={true}
|
|
243
|
+
style={[positionStyle, style]}
|
|
244
|
+
onLayout={onLayout}
|
|
245
|
+
onStartShouldSetResponder={onStartShouldSetResponder}
|
|
246
|
+
{...props}
|
|
247
|
+
/>
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
Content.displayName = 'ContentNativePopover';
|
|
253
|
+
|
|
254
|
+
const Close = React.forwardRef<PressableRef, SlottablePressableProps>(
|
|
255
|
+
({ asChild, onPress: onPressProp, disabled = false, ...props }, ref) => {
|
|
256
|
+
const { onOpenChange, setContentLayout, setTriggerPosition } = usePopoverContext();
|
|
257
|
+
|
|
258
|
+
function onPress(ev: GestureResponderEvent) {
|
|
259
|
+
if (disabled) return;
|
|
260
|
+
setTriggerPosition(null);
|
|
261
|
+
setContentLayout(null);
|
|
262
|
+
onOpenChange(false);
|
|
263
|
+
onPressProp?.(ev);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
267
|
+
return (
|
|
268
|
+
<Component
|
|
269
|
+
ref={ref}
|
|
270
|
+
aria-disabled={disabled ?? undefined}
|
|
271
|
+
role='button'
|
|
272
|
+
onPress={onPress}
|
|
273
|
+
disabled={disabled ?? undefined}
|
|
274
|
+
{...props}
|
|
275
|
+
/>
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
Close.displayName = 'CloseNativePopover';
|
|
281
|
+
|
|
282
|
+
export { Close, Content, Overlay, Portal, Root, Trigger, usePopoverContext };
|
|
283
|
+
|
|
284
|
+
function onStartShouldSetResponder() {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import * as Popover from '@radix-ui/react-popover';
|
|
2
|
+
import { useAugmentedRef, useControllableState } from '@rnr/hooks';
|
|
3
|
+
import * as Slot from '@rnr/slot';
|
|
4
|
+
import type {
|
|
5
|
+
PositionedContentProps,
|
|
6
|
+
PressableRef,
|
|
7
|
+
SlottablePressableProps,
|
|
8
|
+
SlottableViewProps,
|
|
9
|
+
ViewRef,
|
|
10
|
+
} from '@rnr/types';
|
|
11
|
+
import * as React from 'react';
|
|
12
|
+
import { Pressable, View, type GestureResponderEvent } from 'react-native';
|
|
13
|
+
import type {
|
|
14
|
+
PopoverOverlayProps,
|
|
15
|
+
PopoverPortalProps,
|
|
16
|
+
PopoverRootProps,
|
|
17
|
+
RootContext,
|
|
18
|
+
} from './types';
|
|
19
|
+
|
|
20
|
+
const RootContext = React.createContext<RootContext | null>(null);
|
|
21
|
+
|
|
22
|
+
const Root = React.forwardRef<ViewRef, SlottableViewProps & PopoverRootProps>(
|
|
23
|
+
({ asChild, open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, ...viewProps }, ref) => {
|
|
24
|
+
const [open = false, onOpenChange] = useControllableState({
|
|
25
|
+
prop: openProp,
|
|
26
|
+
defaultProp: defaultOpen,
|
|
27
|
+
onChange: onOpenChangeProp,
|
|
28
|
+
});
|
|
29
|
+
const Component = asChild ? Slot.View : View;
|
|
30
|
+
return (
|
|
31
|
+
<RootContext.Provider value={{ open, onOpenChange }}>
|
|
32
|
+
<Popover.Root open={open} defaultOpen={defaultOpen} onOpenChange={onOpenChange}>
|
|
33
|
+
<Component ref={ref} {...viewProps} />
|
|
34
|
+
</Popover.Root>
|
|
35
|
+
</RootContext.Provider>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
Root.displayName = 'RootWebPopover';
|
|
41
|
+
|
|
42
|
+
function usePopoverContext() {
|
|
43
|
+
const context = React.useContext(RootContext);
|
|
44
|
+
if (!context) {
|
|
45
|
+
throw new Error('Popover compound components cannot be rendered outside the Popover component');
|
|
46
|
+
}
|
|
47
|
+
return context;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const Trigger = React.forwardRef<PressableRef, SlottablePressableProps>(
|
|
51
|
+
({ asChild, onPress: onPressProp, role: _role, disabled, ...props }, ref) => {
|
|
52
|
+
const augmentedRef = useAugmentedRef({ ref });
|
|
53
|
+
const { onOpenChange, open } = usePopoverContext();
|
|
54
|
+
function onPress(ev: GestureResponderEvent) {
|
|
55
|
+
if (onPressProp) {
|
|
56
|
+
onPressProp(ev);
|
|
57
|
+
}
|
|
58
|
+
onOpenChange(!open);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
React.useLayoutEffect(() => {
|
|
62
|
+
if (augmentedRef.current) {
|
|
63
|
+
const augRef = augmentedRef.current as unknown as HTMLButtonElement;
|
|
64
|
+
augRef.dataset.state = open ? 'open' : 'closed';
|
|
65
|
+
augRef.type = 'button';
|
|
66
|
+
}
|
|
67
|
+
}, [open]);
|
|
68
|
+
|
|
69
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
70
|
+
return (
|
|
71
|
+
<Popover.Trigger disabled={disabled ?? undefined} asChild>
|
|
72
|
+
<Component
|
|
73
|
+
ref={augmentedRef}
|
|
74
|
+
onPress={onPress}
|
|
75
|
+
role='button'
|
|
76
|
+
disabled={disabled}
|
|
77
|
+
{...props}
|
|
78
|
+
/>
|
|
79
|
+
</Popover.Trigger>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
Trigger.displayName = 'TriggerWebPopover';
|
|
85
|
+
|
|
86
|
+
function Portal({ forceMount, container, children }: PopoverPortalProps) {
|
|
87
|
+
return <Popover.Portal forceMount={forceMount} children={children} container={container} />;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const Overlay = React.forwardRef<PressableRef, SlottablePressableProps & PopoverOverlayProps>(
|
|
91
|
+
({ asChild, forceMount, ...props }, ref) => {
|
|
92
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
93
|
+
return <Component ref={ref} {...props} />;
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
Overlay.displayName = 'OverlayWebPopover';
|
|
98
|
+
|
|
99
|
+
const Content = React.forwardRef<ViewRef, SlottableViewProps & PositionedContentProps>(
|
|
100
|
+
(
|
|
101
|
+
{
|
|
102
|
+
asChild = false,
|
|
103
|
+
forceMount,
|
|
104
|
+
align = 'start',
|
|
105
|
+
side = 'bottom',
|
|
106
|
+
sideOffset = 0,
|
|
107
|
+
alignOffset = 0,
|
|
108
|
+
avoidCollisions = true,
|
|
109
|
+
insets: _insets,
|
|
110
|
+
disablePositioningStyle: _disablePositioningStyle,
|
|
111
|
+
onCloseAutoFocus,
|
|
112
|
+
onEscapeKeyDown,
|
|
113
|
+
onInteractOutside,
|
|
114
|
+
onPointerDownOutside,
|
|
115
|
+
...props
|
|
116
|
+
},
|
|
117
|
+
ref
|
|
118
|
+
) => {
|
|
119
|
+
const Component = asChild ? Slot.View : View;
|
|
120
|
+
return (
|
|
121
|
+
<Popover.Content
|
|
122
|
+
onCloseAutoFocus={onCloseAutoFocus}
|
|
123
|
+
onEscapeKeyDown={onEscapeKeyDown}
|
|
124
|
+
onInteractOutside={onInteractOutside}
|
|
125
|
+
onPointerDownOutside={onPointerDownOutside}
|
|
126
|
+
forceMount={forceMount}
|
|
127
|
+
align={align}
|
|
128
|
+
side={side}
|
|
129
|
+
sideOffset={sideOffset}
|
|
130
|
+
alignOffset={alignOffset}
|
|
131
|
+
avoidCollisions={avoidCollisions}
|
|
132
|
+
>
|
|
133
|
+
<Component ref={ref} {...props} />
|
|
134
|
+
</Popover.Content>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
Content.displayName = 'ContentWebPopover';
|
|
140
|
+
|
|
141
|
+
const Close = React.forwardRef<PressableRef, SlottablePressableProps>(
|
|
142
|
+
({ asChild, onPress: onPressProp, disabled, ...props }, ref) => {
|
|
143
|
+
const augmentedRef = useAugmentedRef({ ref });
|
|
144
|
+
const { onOpenChange, open } = usePopoverContext();
|
|
145
|
+
|
|
146
|
+
function onPress(ev: GestureResponderEvent) {
|
|
147
|
+
if (onPressProp) {
|
|
148
|
+
onPressProp(ev);
|
|
149
|
+
}
|
|
150
|
+
onOpenChange(!open);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
React.useLayoutEffect(() => {
|
|
154
|
+
if (augmentedRef.current) {
|
|
155
|
+
const augRef = augmentedRef.current as unknown as HTMLButtonElement;
|
|
156
|
+
augRef.type = 'button';
|
|
157
|
+
}
|
|
158
|
+
}, []);
|
|
159
|
+
|
|
160
|
+
const Component = asChild ? Slot.Pressable : Pressable;
|
|
161
|
+
return (
|
|
162
|
+
<>
|
|
163
|
+
<Popover.Close disabled={disabled ?? undefined} asChild>
|
|
164
|
+
<Component
|
|
165
|
+
ref={augmentedRef}
|
|
166
|
+
onPress={onPress}
|
|
167
|
+
role='button'
|
|
168
|
+
disabled={disabled}
|
|
169
|
+
{...props}
|
|
170
|
+
/>
|
|
171
|
+
</Popover.Close>
|
|
172
|
+
</>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
Close.displayName = 'CloseWebPopover';
|
|
178
|
+
|
|
179
|
+
export { Close, Content, Overlay, Portal, Root, Trigger, usePopoverContext };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ForceMountable } from '@rnr/types';
|
|
2
|
+
|
|
3
|
+
interface RootContext {
|
|
4
|
+
open: boolean;
|
|
5
|
+
onOpenChange: (value: boolean) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface PopoverRootProps {
|
|
9
|
+
open?: boolean;
|
|
10
|
+
defaultOpen?: boolean;
|
|
11
|
+
onOpenChange?: (value: boolean) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface PopoverPortalProps extends ForceMountable {
|
|
15
|
+
children: React.ReactNode;
|
|
16
|
+
/**
|
|
17
|
+
* Platform: NATIVE ONLY
|
|
18
|
+
*/
|
|
19
|
+
hostName?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Platform: WEB ONLY
|
|
22
|
+
*/
|
|
23
|
+
container?: HTMLElement | null | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface PopoverOverlayProps extends ForceMountable {
|
|
27
|
+
closeOnPress?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type { PopoverRootProps, PopoverPortalProps, PopoverOverlayProps, RootContext };
|
|
@@ -0,0 +1,67 @@
|
|
|
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(
|
|
11
|
+
DEFAULT_PORTAL_HOST,
|
|
12
|
+
new Map<string, React.ReactNode>()
|
|
13
|
+
),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const updatePortal = (
|
|
17
|
+
hostName: string,
|
|
18
|
+
name: string,
|
|
19
|
+
children: React.ReactNode
|
|
20
|
+
) => {
|
|
21
|
+
usePortal.setState((prev) => {
|
|
22
|
+
const next = new Map(prev.map);
|
|
23
|
+
const portal = next.get(hostName) ?? new Map<string, React.ReactNode>();
|
|
24
|
+
portal.set(name, children);
|
|
25
|
+
next.set(hostName, portal);
|
|
26
|
+
return { map: next };
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const removePortal = (hostName: string, name: string) => {
|
|
30
|
+
usePortal.setState((prev) => {
|
|
31
|
+
const next = new Map(prev.map);
|
|
32
|
+
const portal = next.get(hostName) ?? new Map<string, React.ReactNode>();
|
|
33
|
+
portal.delete(name);
|
|
34
|
+
next.set(hostName, portal);
|
|
35
|
+
return { map: next };
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function PortalHost({ name = DEFAULT_PORTAL_HOST }: { name?: string }) {
|
|
40
|
+
const portalMap =
|
|
41
|
+
usePortal((state) => state.map).get(name) ??
|
|
42
|
+
new Map<string, React.ReactNode>();
|
|
43
|
+
if (portalMap.size === 0) return null;
|
|
44
|
+
return <>{Array.from(portalMap.values())}</>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function Portal({
|
|
48
|
+
name,
|
|
49
|
+
hostName = DEFAULT_PORTAL_HOST,
|
|
50
|
+
children,
|
|
51
|
+
}: {
|
|
52
|
+
name: string;
|
|
53
|
+
hostName?: string;
|
|
54
|
+
children: React.ReactNode;
|
|
55
|
+
}) {
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
updatePortal(hostName, name, children);
|
|
58
|
+
}, [hostName, name, children]);
|
|
59
|
+
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
return () => {
|
|
62
|
+
removePortal(hostName, name);
|
|
63
|
+
};
|
|
64
|
+
}, [hostName, name]);
|
|
65
|
+
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './progress';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import * as Slot from '@rnr/slot';
|
|
4
|
+
import type { SlottableViewProps, ViewRef } from '@rnr/types';
|
|
5
|
+
import type { ProgressRootProps } from './types';
|
|
6
|
+
|
|
7
|
+
// This project uses code from WorkOS/Radix Primitives.
|
|
8
|
+
// The code is licensed under the MIT License.
|
|
9
|
+
// https://github.com/radix-ui/primitives/tree/main
|
|
10
|
+
|
|
11
|
+
const DEFAULT_MAX = 100;
|
|
12
|
+
|
|
13
|
+
const Root = React.forwardRef<ViewRef, SlottableViewProps & ProgressRootProps>(
|
|
14
|
+
(
|
|
15
|
+
{ asChild, value: valueProp, max: maxProp, getValueLabel = defaultGetValueLabel, ...props },
|
|
16
|
+
ref
|
|
17
|
+
) => {
|
|
18
|
+
const max = maxProp ?? DEFAULT_MAX;
|
|
19
|
+
const value = isValidValueNumber(valueProp, max) ? valueProp : 0;
|
|
20
|
+
|
|
21
|
+
const Component = asChild ? Slot.View : View;
|
|
22
|
+
return (
|
|
23
|
+
<Component
|
|
24
|
+
role='progressbar'
|
|
25
|
+
ref={ref}
|
|
26
|
+
aria-valuemax={max}
|
|
27
|
+
aria-valuemin={0}
|
|
28
|
+
aria-valuenow={value}
|
|
29
|
+
aria-valuetext={getValueLabel(value, max)}
|
|
30
|
+
accessibilityValue={{
|
|
31
|
+
min: 0,
|
|
32
|
+
max,
|
|
33
|
+
now: value,
|
|
34
|
+
text: getValueLabel(value, max),
|
|
35
|
+
}}
|
|
36
|
+
{...props}
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
Root.displayName = 'RootProgress';
|
|
43
|
+
|
|
44
|
+
const Indicator = React.forwardRef<ViewRef, SlottableViewProps>(({ asChild, ...props }, ref) => {
|
|
45
|
+
const Component = asChild ? Slot.View : View;
|
|
46
|
+
return <Component ref={ref} role='presentation' {...props} />;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
Indicator.displayName = 'IndicatorProgress';
|
|
50
|
+
|
|
51
|
+
export { Indicator, Root };
|
|
52
|
+
|
|
53
|
+
function defaultGetValueLabel(value: number, max: number) {
|
|
54
|
+
return `${Math.round((value / max) * 100)}%`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isValidValueNumber(value: any, max: number): value is number {
|
|
58
|
+
return typeof value === 'number' && !isNaN(value) && value <= max && value >= 0;
|
|
59
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as Progress from '@radix-ui/react-progress';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { View } from 'react-native';
|
|
4
|
+
import * as Slot from '@rnr/slot';
|
|
5
|
+
import type { SlottableViewProps, ViewRef } from '@rnr/types';
|
|
6
|
+
import type { ProgressRootProps } from './types';
|
|
7
|
+
|
|
8
|
+
const ProgressContext = React.createContext<ProgressRootProps | null>(null);
|
|
9
|
+
|
|
10
|
+
const Root = React.forwardRef<ViewRef, SlottableViewProps & ProgressRootProps>(
|
|
11
|
+
({ asChild, value, max, getValueLabel, ...props }, ref) => {
|
|
12
|
+
const Component = asChild ? Slot.View : View;
|
|
13
|
+
return (
|
|
14
|
+
<ProgressContext.Provider value={{ value, max }}>
|
|
15
|
+
<Progress.Root value={value} max={max} getValueLabel={getValueLabel} asChild>
|
|
16
|
+
<Component ref={ref} {...props} />
|
|
17
|
+
</Progress.Root>
|
|
18
|
+
</ProgressContext.Provider>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
Root.displayName = 'RootProgress';
|
|
24
|
+
|
|
25
|
+
const Indicator = React.forwardRef<ViewRef, SlottableViewProps>(({ asChild, ...props }, ref) => {
|
|
26
|
+
const Component = asChild ? Slot.View : View;
|
|
27
|
+
return (
|
|
28
|
+
<Progress.Indicator asChild>
|
|
29
|
+
<Component ref={ref} {...props} />
|
|
30
|
+
</Progress.Indicator>
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
Indicator.displayName = 'IndicatorProgress';
|
|
35
|
+
|
|
36
|
+
export { Indicator, Root };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './radio-group';
|