@react-native-reusables/cli 0.0.18 → 0.1.1
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 +2 -37
- package/__generated/components/ui/accordion.tsx +30 -32
- package/__generated/components/ui/alert-dialog.tsx +19 -26
- package/__generated/components/ui/aspect-ratio.tsx +1 -1
- package/__generated/components/ui/avatar.tsx +31 -34
- package/__generated/components/ui/badge.tsx +2 -2
- package/__generated/components/ui/button.tsx +2 -5
- package/__generated/components/ui/card.tsx +39 -51
- package/__generated/components/ui/checkbox.tsx +25 -27
- package/__generated/components/ui/collapsible.tsx +1 -1
- package/__generated/components/ui/context-menu.tsx +28 -31
- package/__generated/components/ui/dialog.tsx +40 -43
- package/__generated/components/ui/dropdown-menu.tsx +27 -22
- package/__generated/components/ui/hover-card.tsx +3 -3
- package/__generated/components/ui/input.tsx +17 -18
- package/__generated/components/ui/label.tsx +21 -22
- package/__generated/components/ui/menubar.tsx +45 -47
- package/__generated/components/ui/navigation-menu.tsx +19 -17
- package/__generated/components/ui/popover.tsx +4 -4
- package/__generated/components/ui/progress.tsx +15 -11
- package/__generated/components/ui/radio-group.tsx +27 -29
- package/__generated/components/ui/select.tsx +55 -65
- package/__generated/components/ui/separator.tsx +16 -17
- package/__generated/components/ui/switch.tsx +59 -61
- package/__generated/components/ui/table.tsx +69 -76
- package/__generated/components/ui/tabs.tsx +49 -52
- package/__generated/components/ui/text.tsx +2 -2
- package/__generated/components/ui/textarea.tsx +20 -21
- package/__generated/components/ui/toggle-group.tsx +5 -7
- package/__generated/components/ui/toggle.tsx +4 -4
- package/__generated/components/ui/tooltip.tsx +4 -5
- package/__generated/components/ui/typography.tsx +2 -2
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -34
- package/__generated/components/primitives/accordion/accordion.tsx +0 -216
- package/__generated/components/primitives/accordion/accordion.web.tsx +0 -295
- package/__generated/components/primitives/accordion/index.ts +0 -1
- package/__generated/components/primitives/accordion/types.ts +0 -45
- package/__generated/components/primitives/alert-dialog/alert-dialog.tsx +0 -237
- package/__generated/components/primitives/alert-dialog/alert-dialog.web.tsx +0 -256
- package/__generated/components/primitives/alert-dialog/index.ts +0 -1
- package/__generated/components/primitives/alert-dialog/types.ts +0 -48
- package/__generated/components/primitives/aspect-ratio.tsx +0 -23
- package/__generated/components/primitives/avatar/index.tsx +0 -95
- package/__generated/components/primitives/avatar/types.ts +0 -10
- package/__generated/components/primitives/checkbox/checkbox.tsx +0 -101
- package/__generated/components/primitives/checkbox/checkbox.web.tsx +0 -114
- package/__generated/components/primitives/checkbox/index.ts +0 -1
- package/__generated/components/primitives/checkbox/types.ts +0 -11
- package/__generated/components/primitives/collapsible/collapsible.tsx +0 -119
- package/__generated/components/primitives/collapsible/collapsible.web.tsx +0 -157
- package/__generated/components/primitives/collapsible/index.ts +0 -1
- package/__generated/components/primitives/collapsible/types.ts +0 -18
- package/__generated/components/primitives/context-menu/context-menu.tsx +0 -648
- package/__generated/components/primitives/context-menu/context-menu.web.tsx +0 -533
- package/__generated/components/primitives/context-menu/index.ts +0 -1
- package/__generated/components/primitives/context-menu/types.ts +0 -90
- package/__generated/components/primitives/dialog/dialog.tsx +0 -211
- package/__generated/components/primitives/dialog/dialog.web.tsx +0 -197
- package/__generated/components/primitives/dialog/index.ts +0 -1
- package/__generated/components/primitives/dialog/types.ts +0 -60
- package/__generated/components/primitives/dropdown-menu/dropdown-menu.tsx +0 -608
- package/__generated/components/primitives/dropdown-menu/dropdown-menu.web.tsx +0 -550
- package/__generated/components/primitives/dropdown-menu/index.ts +0 -1
- package/__generated/components/primitives/dropdown-menu/types.ts +0 -72
- package/__generated/components/primitives/hooks/index.ts +0 -3
- package/__generated/components/primitives/hooks/useAugmentedRef.tsx +0 -29
- package/__generated/components/primitives/hooks/useControllableState.tsx +0 -75
- package/__generated/components/primitives/hooks/useRelativePosition.tsx +0 -227
- package/__generated/components/primitives/hover-card/hover-card.tsx +0 -279
- package/__generated/components/primitives/hover-card/hover-card.web.tsx +0 -150
- package/__generated/components/primitives/hover-card/index.ts +0 -1
- package/__generated/components/primitives/hover-card/types.ts +0 -51
- package/__generated/components/primitives/label/index.ts +0 -1
- package/__generated/components/primitives/label/label.tsx +0 -31
- package/__generated/components/primitives/label/label.web.tsx +0 -36
- package/__generated/components/primitives/label/types.ts +0 -15
- package/__generated/components/primitives/menubar/index.ts +0 -1
- package/__generated/components/primitives/menubar/menubar.tsx +0 -622
- package/__generated/components/primitives/menubar/menubar.web.tsx +0 -548
- package/__generated/components/primitives/menubar/types.ts +0 -77
- package/__generated/components/primitives/navigation-menu/index.ts +0 -1
- package/__generated/components/primitives/navigation-menu/navigation-menu.tsx +0 -315
- package/__generated/components/primitives/navigation-menu/navigation-menu.web.tsx +0 -264
- package/__generated/components/primitives/navigation-menu/types.ts +0 -49
- package/__generated/components/primitives/popover/index.ts +0 -1
- package/__generated/components/primitives/popover/popover.tsx +0 -290
- package/__generated/components/primitives/popover/popover.web.tsx +0 -191
- package/__generated/components/primitives/popover/types.ts +0 -24
- package/__generated/components/primitives/portal.tsx +0 -82
- package/__generated/components/primitives/progress/index.ts +0 -1
- package/__generated/components/primitives/progress/progress.tsx +0 -59
- package/__generated/components/primitives/progress/progress.web.tsx +0 -36
- package/__generated/components/primitives/progress/types.ts +0 -7
- package/__generated/components/primitives/radio-group/index.ts +0 -1
- package/__generated/components/primitives/radio-group/radio-group.tsx +0 -116
- package/__generated/components/primitives/radio-group/radio-group.web.tsx +0 -78
- package/__generated/components/primitives/radio-group/types.ts +0 -15
- package/__generated/components/primitives/select/index.ts +0 -1
- package/__generated/components/primitives/select/select.tsx +0 -466
- package/__generated/components/primitives/select/select.web.tsx +0 -332
- package/__generated/components/primitives/select/types.ts +0 -89
- package/__generated/components/primitives/separator/index.tsx +0 -23
- package/__generated/components/primitives/separator/types.ts +0 -6
- package/__generated/components/primitives/slider/index.ts +0 -1
- package/__generated/components/primitives/slider/slider.tsx +0 -89
- package/__generated/components/primitives/slider/slider.web.tsx +0 -67
- package/__generated/components/primitives/slider/types.ts +0 -24
- package/__generated/components/primitives/slot.tsx +0 -187
- package/__generated/components/primitives/switch/index.ts +0 -1
- package/__generated/components/primitives/switch/switch.tsx +0 -65
- package/__generated/components/primitives/switch/switch.web.tsx +0 -67
- package/__generated/components/primitives/switch/types.ts +0 -11
- package/__generated/components/primitives/table.tsx +0 -55
- package/__generated/components/primitives/tabs/index.ts +0 -1
- package/__generated/components/primitives/tabs/tabs.tsx +0 -133
- package/__generated/components/primitives/tabs/tabs.web.tsx +0 -97
- package/__generated/components/primitives/tabs/types.ts +0 -24
- package/__generated/components/primitives/toast/index.tsx +0 -128
- package/__generated/components/primitives/toast/types.ts +0 -7
- package/__generated/components/primitives/toggle/index.ts +0 -1
- package/__generated/components/primitives/toggle/toggle.tsx +0 -37
- package/__generated/components/primitives/toggle/toggle.web.tsx +0 -26
- package/__generated/components/primitives/toggle/types.ts +0 -7
- package/__generated/components/primitives/toggle-group/index.ts +0 -1
- package/__generated/components/primitives/toggle-group/toggle-group.tsx +0 -125
- package/__generated/components/primitives/toggle-group/toggle-group.web.tsx +0 -124
- package/__generated/components/primitives/toggle-group/types.ts +0 -37
- package/__generated/components/primitives/toolbar/index.ts +0 -1
- package/__generated/components/primitives/toolbar/toolbar.tsx +0 -125
- package/__generated/components/primitives/toolbar/toolbar.web.tsx +0 -129
- package/__generated/components/primitives/toolbar/types.ts +0 -39
- package/__generated/components/primitives/tooltip/index.ts +0 -1
- package/__generated/components/primitives/tooltip/tooltip.tsx +0 -277
- package/__generated/components/primitives/tooltip/tooltip.web.tsx +0 -181
- package/__generated/components/primitives/tooltip/types.ts +0 -42
- package/__generated/components/primitives/types.ts +0 -105
- package/__generated/components/primitives/utils.ts +0 -61
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// This project uses code from WorkOS/Radix Primitives.
|
|
2
|
-
// The code is licensed under the MIT License.
|
|
3
|
-
// https://github.com/radix-ui/primitives/tree/main
|
|
4
|
-
|
|
5
|
-
import * as React from 'react';
|
|
6
|
-
|
|
7
|
-
type UseControllableStateParams<T> = {
|
|
8
|
-
prop?: T | undefined;
|
|
9
|
-
defaultProp?: T | undefined;
|
|
10
|
-
onChange?: (state: T) => void;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
type SetStateFn<T> = (prevState?: T) => T;
|
|
14
|
-
|
|
15
|
-
function useControllableState<T>({
|
|
16
|
-
prop,
|
|
17
|
-
defaultProp,
|
|
18
|
-
onChange = () => {},
|
|
19
|
-
}: UseControllableStateParams<T>) {
|
|
20
|
-
const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange });
|
|
21
|
-
const isControlled = prop !== undefined;
|
|
22
|
-
const value = isControlled ? prop : uncontrolledProp;
|
|
23
|
-
const handleChange = useCallbackRef(onChange);
|
|
24
|
-
|
|
25
|
-
const setValue: React.Dispatch<React.SetStateAction<T | undefined>> = React.useCallback(
|
|
26
|
-
(nextValue) => {
|
|
27
|
-
if (isControlled) {
|
|
28
|
-
const setter = nextValue as SetStateFn<T>;
|
|
29
|
-
const value = typeof nextValue === 'function' ? setter(prop) : nextValue;
|
|
30
|
-
if (value !== prop) handleChange(value as T);
|
|
31
|
-
} else {
|
|
32
|
-
setUncontrolledProp(nextValue);
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
[isControlled, prop, setUncontrolledProp, handleChange]
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
return [value, setValue] as const;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function useUncontrolledState<T>({
|
|
42
|
-
defaultProp,
|
|
43
|
-
onChange,
|
|
44
|
-
}: Omit<UseControllableStateParams<T>, 'prop'>) {
|
|
45
|
-
const uncontrolledState = React.useState<T | undefined>(defaultProp);
|
|
46
|
-
const [value] = uncontrolledState;
|
|
47
|
-
const prevValueRef = React.useRef(value);
|
|
48
|
-
const handleChange = useCallbackRef(onChange);
|
|
49
|
-
|
|
50
|
-
React.useEffect(() => {
|
|
51
|
-
if (prevValueRef.current !== value) {
|
|
52
|
-
handleChange(value as T);
|
|
53
|
-
prevValueRef.current = value;
|
|
54
|
-
}
|
|
55
|
-
}, [value, prevValueRef, handleChange]);
|
|
56
|
-
|
|
57
|
-
return uncontrolledState;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a
|
|
62
|
-
* prop or avoid re-executing effects when passed as a dependency
|
|
63
|
-
*/
|
|
64
|
-
function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined): T {
|
|
65
|
-
const callbackRef = React.useRef(callback);
|
|
66
|
-
|
|
67
|
-
React.useEffect(() => {
|
|
68
|
-
callbackRef.current = callback;
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// https://github.com/facebook/react/issues/19240
|
|
72
|
-
return React.useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export { useControllableState };
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
useWindowDimensions,
|
|
4
|
-
type LayoutRectangle,
|
|
5
|
-
type ScaledSize,
|
|
6
|
-
type ViewStyle,
|
|
7
|
-
} from 'react-native';
|
|
8
|
-
import type { Insets } from '@rnr/types';
|
|
9
|
-
|
|
10
|
-
const POSITION_ABSOLUTE: ViewStyle = {
|
|
11
|
-
position: 'absolute',
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const HIDDEN_CONTENT: ViewStyle = {
|
|
15
|
-
position: 'absolute',
|
|
16
|
-
opacity: 0,
|
|
17
|
-
zIndex: -9999999,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type UseRelativePositionArgs = Omit<
|
|
21
|
-
GetContentStyleArgs,
|
|
22
|
-
'triggerPosition' | 'contentLayout' | 'dimensions'
|
|
23
|
-
> & {
|
|
24
|
-
triggerPosition: LayoutPosition | null;
|
|
25
|
-
contentLayout: LayoutRectangle | null;
|
|
26
|
-
disablePositioningStyle?: boolean;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export function useRelativePosition({
|
|
30
|
-
align,
|
|
31
|
-
avoidCollisions,
|
|
32
|
-
triggerPosition,
|
|
33
|
-
contentLayout,
|
|
34
|
-
alignOffset,
|
|
35
|
-
insets,
|
|
36
|
-
sideOffset,
|
|
37
|
-
side,
|
|
38
|
-
disablePositioningStyle,
|
|
39
|
-
}: UseRelativePositionArgs) {
|
|
40
|
-
const dimensions = useWindowDimensions();
|
|
41
|
-
return React.useMemo(() => {
|
|
42
|
-
if (disablePositioningStyle) {
|
|
43
|
-
return {};
|
|
44
|
-
}
|
|
45
|
-
if (!triggerPosition || !contentLayout) {
|
|
46
|
-
return HIDDEN_CONTENT;
|
|
47
|
-
}
|
|
48
|
-
return getContentStyle({
|
|
49
|
-
align,
|
|
50
|
-
avoidCollisions,
|
|
51
|
-
contentLayout,
|
|
52
|
-
side,
|
|
53
|
-
triggerPosition,
|
|
54
|
-
alignOffset,
|
|
55
|
-
insets,
|
|
56
|
-
sideOffset,
|
|
57
|
-
dimensions,
|
|
58
|
-
});
|
|
59
|
-
}, [triggerPosition, contentLayout, dimensions.width, dimensions.height]);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface LayoutPosition {
|
|
63
|
-
pageY: number;
|
|
64
|
-
pageX: number;
|
|
65
|
-
width: number;
|
|
66
|
-
height: number;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
interface GetPositionArgs {
|
|
70
|
-
dimensions: ScaledSize;
|
|
71
|
-
avoidCollisions: boolean;
|
|
72
|
-
triggerPosition: LayoutPosition;
|
|
73
|
-
contentLayout: LayoutRectangle;
|
|
74
|
-
insets?: Insets;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
interface GetSidePositionArgs extends GetPositionArgs {
|
|
78
|
-
side: 'top' | 'bottom';
|
|
79
|
-
sideOffset: number;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getSidePosition({
|
|
83
|
-
side,
|
|
84
|
-
triggerPosition,
|
|
85
|
-
contentLayout,
|
|
86
|
-
sideOffset,
|
|
87
|
-
insets,
|
|
88
|
-
avoidCollisions,
|
|
89
|
-
dimensions,
|
|
90
|
-
}: GetSidePositionArgs) {
|
|
91
|
-
const insetTop = insets?.top ?? 0;
|
|
92
|
-
const insetBottom = insets?.bottom ?? 0;
|
|
93
|
-
const positionTop = triggerPosition?.pageY - sideOffset - contentLayout.height;
|
|
94
|
-
const positionBottom = triggerPosition.pageY + triggerPosition.height + sideOffset;
|
|
95
|
-
|
|
96
|
-
if (!avoidCollisions) {
|
|
97
|
-
return {
|
|
98
|
-
top: side === 'top' ? positionTop : positionBottom,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (side === 'top') {
|
|
103
|
-
return {
|
|
104
|
-
top: Math.max(insetTop, positionTop),
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
top: Math.min(dimensions.height - insetBottom - contentLayout.height, positionBottom),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface GetAlignPositionArgs extends GetPositionArgs {
|
|
114
|
-
align: 'start' | 'center' | 'end';
|
|
115
|
-
alignOffset: number;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function getAlignPosition({
|
|
119
|
-
align,
|
|
120
|
-
avoidCollisions,
|
|
121
|
-
contentLayout,
|
|
122
|
-
triggerPosition,
|
|
123
|
-
alignOffset,
|
|
124
|
-
insets,
|
|
125
|
-
dimensions,
|
|
126
|
-
}: GetAlignPositionArgs) {
|
|
127
|
-
const insetLeft = insets?.left ?? 0;
|
|
128
|
-
const insetRight = insets?.right ?? 0;
|
|
129
|
-
const maxContentWidth = dimensions.width - insetLeft - insetRight;
|
|
130
|
-
|
|
131
|
-
const contentWidth = Math.min(contentLayout.width, maxContentWidth);
|
|
132
|
-
|
|
133
|
-
let left = getLeftPosition(
|
|
134
|
-
align,
|
|
135
|
-
triggerPosition.pageX,
|
|
136
|
-
triggerPosition.width,
|
|
137
|
-
contentWidth,
|
|
138
|
-
alignOffset,
|
|
139
|
-
insetLeft,
|
|
140
|
-
insetRight,
|
|
141
|
-
dimensions
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
if (avoidCollisions) {
|
|
145
|
-
const doesCollide = left < insetLeft || left + contentWidth > dimensions.width - insetRight;
|
|
146
|
-
if (doesCollide) {
|
|
147
|
-
const spaceLeft = left - insetLeft;
|
|
148
|
-
const spaceRight = dimensions.width - insetRight - (left + contentWidth);
|
|
149
|
-
|
|
150
|
-
if (spaceLeft > spaceRight && spaceLeft >= contentWidth) {
|
|
151
|
-
left = insetLeft;
|
|
152
|
-
} else if (spaceRight >= contentWidth) {
|
|
153
|
-
left = dimensions.width - insetRight - contentWidth;
|
|
154
|
-
} else {
|
|
155
|
-
const centeredPosition = Math.max(
|
|
156
|
-
insetLeft,
|
|
157
|
-
(dimensions.width - contentWidth - insetRight) / 2
|
|
158
|
-
);
|
|
159
|
-
left = centeredPosition;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return { left, maxWidth: maxContentWidth };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function getLeftPosition(
|
|
168
|
-
align: 'start' | 'center' | 'end',
|
|
169
|
-
triggerPageX: number,
|
|
170
|
-
triggerWidth: number,
|
|
171
|
-
contentWidth: number,
|
|
172
|
-
alignOffset: number,
|
|
173
|
-
insetLeft: number,
|
|
174
|
-
insetRight: number,
|
|
175
|
-
dimensions: ScaledSize
|
|
176
|
-
) {
|
|
177
|
-
let left = 0;
|
|
178
|
-
if (align === 'start') {
|
|
179
|
-
left = triggerPageX;
|
|
180
|
-
}
|
|
181
|
-
if (align === 'center') {
|
|
182
|
-
left = triggerPageX + triggerWidth / 2 - contentWidth / 2;
|
|
183
|
-
}
|
|
184
|
-
if (align === 'end') {
|
|
185
|
-
left = triggerPageX + triggerWidth - contentWidth;
|
|
186
|
-
}
|
|
187
|
-
return Math.max(
|
|
188
|
-
insetLeft,
|
|
189
|
-
Math.min(left + alignOffset, dimensions.width - contentWidth - insetRight)
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
type GetContentStyleArgs = GetPositionArgs & GetSidePositionArgs & GetAlignPositionArgs;
|
|
194
|
-
|
|
195
|
-
function getContentStyle({
|
|
196
|
-
align,
|
|
197
|
-
avoidCollisions,
|
|
198
|
-
contentLayout,
|
|
199
|
-
side,
|
|
200
|
-
triggerPosition,
|
|
201
|
-
alignOffset,
|
|
202
|
-
insets,
|
|
203
|
-
sideOffset,
|
|
204
|
-
dimensions,
|
|
205
|
-
}: GetContentStyleArgs) {
|
|
206
|
-
return Object.assign(
|
|
207
|
-
POSITION_ABSOLUTE,
|
|
208
|
-
getSidePosition({
|
|
209
|
-
side,
|
|
210
|
-
triggerPosition,
|
|
211
|
-
contentLayout,
|
|
212
|
-
sideOffset,
|
|
213
|
-
insets,
|
|
214
|
-
avoidCollisions,
|
|
215
|
-
dimensions,
|
|
216
|
-
}),
|
|
217
|
-
getAlignPosition({
|
|
218
|
-
align,
|
|
219
|
-
avoidCollisions,
|
|
220
|
-
triggerPosition,
|
|
221
|
-
contentLayout,
|
|
222
|
-
alignOffset,
|
|
223
|
-
insets,
|
|
224
|
-
dimensions,
|
|
225
|
-
})
|
|
226
|
-
);
|
|
227
|
-
}
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { useAugmentedRef, 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
|
-
HoverCardOverlayProps,
|
|
22
|
-
HoverCardPortalProps,
|
|
23
|
-
HoverCardRootProps,
|
|
24
|
-
HoverCardTriggerRef,
|
|
25
|
-
RootContext,
|
|
26
|
-
} from './types';
|
|
27
|
-
|
|
28
|
-
interface IRootContext extends RootContext {
|
|
29
|
-
open: boolean;
|
|
30
|
-
onOpenChange: (open: boolean) => void;
|
|
31
|
-
triggerPosition: LayoutPosition | null;
|
|
32
|
-
setTriggerPosition: (triggerPosition: LayoutPosition | null) => void;
|
|
33
|
-
contentLayout: LayoutRectangle | null;
|
|
34
|
-
setContentLayout: (contentLayout: LayoutRectangle | null) => void;
|
|
35
|
-
nativeID: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const RootContext = React.createContext<IRootContext | null>(null);
|
|
39
|
-
|
|
40
|
-
const Root = React.forwardRef<ViewRef, SlottableViewProps & HoverCardRootProps>(
|
|
41
|
-
(
|
|
42
|
-
{
|
|
43
|
-
asChild,
|
|
44
|
-
openDelay: _openDelay,
|
|
45
|
-
closeDelay: _closeDelay,
|
|
46
|
-
onOpenChange: onOpenChangeProp,
|
|
47
|
-
...viewProps
|
|
48
|
-
},
|
|
49
|
-
ref
|
|
50
|
-
) => {
|
|
51
|
-
const nativeID = React.useId();
|
|
52
|
-
const [triggerPosition, setTriggerPosition] = React.useState<LayoutPosition | null>(null);
|
|
53
|
-
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
|
|
54
|
-
const [open, setOpen] = React.useState(false);
|
|
55
|
-
|
|
56
|
-
function onOpenChange(value: boolean) {
|
|
57
|
-
setOpen(value);
|
|
58
|
-
onOpenChangeProp?.(value);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const Component = asChild ? Slot.View : View;
|
|
62
|
-
return (
|
|
63
|
-
<RootContext.Provider
|
|
64
|
-
value={{
|
|
65
|
-
open,
|
|
66
|
-
onOpenChange,
|
|
67
|
-
contentLayout,
|
|
68
|
-
nativeID,
|
|
69
|
-
setContentLayout,
|
|
70
|
-
setTriggerPosition,
|
|
71
|
-
triggerPosition,
|
|
72
|
-
}}
|
|
73
|
-
>
|
|
74
|
-
<Component ref={ref} {...viewProps} />
|
|
75
|
-
</RootContext.Provider>
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
Root.displayName = 'RootNativeHoverCard';
|
|
81
|
-
|
|
82
|
-
function useRootContext() {
|
|
83
|
-
const context = React.useContext(RootContext);
|
|
84
|
-
if (!context) {
|
|
85
|
-
throw new Error(
|
|
86
|
-
'HoverCard compound components cannot be rendered outside the HoverCard component'
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
return context;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const Trigger = React.forwardRef<HoverCardTriggerRef, SlottablePressableProps>(
|
|
93
|
-
({ asChild, onPress: onPressProp, disabled = false, ...props }, ref) => {
|
|
94
|
-
const { open, onOpenChange, setTriggerPosition } = useRootContext();
|
|
95
|
-
|
|
96
|
-
const augmentedRef = useAugmentedRef({
|
|
97
|
-
ref,
|
|
98
|
-
methods: {
|
|
99
|
-
open: () => {
|
|
100
|
-
onOpenChange(true);
|
|
101
|
-
augmentedRef.current?.measure((_x, _y, width, height, pageX, pageY) => {
|
|
102
|
-
setTriggerPosition({ width, pageX, pageY: pageY, height });
|
|
103
|
-
});
|
|
104
|
-
},
|
|
105
|
-
close: () => {
|
|
106
|
-
setTriggerPosition(null);
|
|
107
|
-
onOpenChange(false);
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
function onPress(ev: GestureResponderEvent) {
|
|
113
|
-
if (disabled) return;
|
|
114
|
-
augmentedRef.current?.measure((_x, _y, width, height, pageX, pageY) => {
|
|
115
|
-
setTriggerPosition({ width, pageX, pageY: pageY, height });
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
onOpenChange(!open);
|
|
119
|
-
onPressProp?.(ev);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const Component = asChild ? Slot.Pressable : Pressable;
|
|
123
|
-
return (
|
|
124
|
-
<Component
|
|
125
|
-
ref={augmentedRef}
|
|
126
|
-
aria-disabled={disabled ?? undefined}
|
|
127
|
-
role='button'
|
|
128
|
-
onPress={onPress}
|
|
129
|
-
disabled={disabled ?? undefined}
|
|
130
|
-
{...props}
|
|
131
|
-
/>
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
Trigger.displayName = 'TriggerNativeHoverCard';
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @warning when using a custom `<PortalHost />`, you might have to adjust the Content's sideOffset to account for nav elements like headers.
|
|
140
|
-
*/
|
|
141
|
-
function Portal({ forceMount, hostName, children }: HoverCardPortalProps) {
|
|
142
|
-
const value = useRootContext();
|
|
143
|
-
|
|
144
|
-
if (!value.triggerPosition) {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (!forceMount) {
|
|
149
|
-
if (!value.open) {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return (
|
|
155
|
-
<RNPPortal hostName={hostName} name={`${value.nativeID}_portal`}>
|
|
156
|
-
<RootContext.Provider value={value}>{children}</RootContext.Provider>
|
|
157
|
-
</RNPPortal>
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const Overlay = React.forwardRef<PressableRef, SlottablePressableProps & HoverCardOverlayProps>(
|
|
162
|
-
({ asChild, forceMount, onPress: OnPressProp, closeOnPress = true, ...props }, ref) => {
|
|
163
|
-
const { open, onOpenChange, setTriggerPosition, setContentLayout } = useRootContext();
|
|
164
|
-
|
|
165
|
-
function onPress(ev: GestureResponderEvent) {
|
|
166
|
-
if (closeOnPress) {
|
|
167
|
-
setTriggerPosition(null);
|
|
168
|
-
setContentLayout(null);
|
|
169
|
-
onOpenChange(false);
|
|
170
|
-
}
|
|
171
|
-
OnPressProp?.(ev);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (!forceMount) {
|
|
175
|
-
if (!open) {
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const Component = asChild ? Slot.Pressable : Pressable;
|
|
181
|
-
return <Component ref={ref} onPress={onPress} {...props} />;
|
|
182
|
-
}
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
Overlay.displayName = 'OverlayNativeHoverCard';
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* @info `position`, `top`, `left`, and `maxWidth` style properties are controlled internally. Opt out of this behavior by setting `disablePositioningStyle` to `true`.
|
|
189
|
-
*/
|
|
190
|
-
const Content = React.forwardRef<ViewRef, SlottableViewProps & PositionedContentProps>(
|
|
191
|
-
(
|
|
192
|
-
{
|
|
193
|
-
asChild = false,
|
|
194
|
-
forceMount,
|
|
195
|
-
align = 'start',
|
|
196
|
-
side = 'bottom',
|
|
197
|
-
sideOffset = 0,
|
|
198
|
-
alignOffset = 0,
|
|
199
|
-
avoidCollisions = true,
|
|
200
|
-
onLayout: onLayoutProp,
|
|
201
|
-
insets,
|
|
202
|
-
style,
|
|
203
|
-
disablePositioningStyle,
|
|
204
|
-
...props
|
|
205
|
-
},
|
|
206
|
-
ref
|
|
207
|
-
) => {
|
|
208
|
-
const {
|
|
209
|
-
open,
|
|
210
|
-
onOpenChange,
|
|
211
|
-
contentLayout,
|
|
212
|
-
nativeID,
|
|
213
|
-
setContentLayout,
|
|
214
|
-
setTriggerPosition,
|
|
215
|
-
triggerPosition,
|
|
216
|
-
} = useRootContext();
|
|
217
|
-
|
|
218
|
-
React.useEffect(() => {
|
|
219
|
-
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
220
|
-
setTriggerPosition(null);
|
|
221
|
-
setContentLayout(null);
|
|
222
|
-
onOpenChange(false);
|
|
223
|
-
return true;
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
return () => {
|
|
227
|
-
setContentLayout(null);
|
|
228
|
-
backHandler.remove();
|
|
229
|
-
};
|
|
230
|
-
}, []);
|
|
231
|
-
|
|
232
|
-
const positionStyle = useRelativePosition({
|
|
233
|
-
align,
|
|
234
|
-
avoidCollisions,
|
|
235
|
-
triggerPosition,
|
|
236
|
-
contentLayout,
|
|
237
|
-
alignOffset,
|
|
238
|
-
insets,
|
|
239
|
-
sideOffset,
|
|
240
|
-
side,
|
|
241
|
-
disablePositioningStyle,
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
function onLayout(event: LayoutChangeEvent) {
|
|
245
|
-
setContentLayout(event.nativeEvent.layout);
|
|
246
|
-
onLayoutProp?.(event);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (!forceMount) {
|
|
250
|
-
if (!open) {
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const Component = asChild ? Slot.View : View;
|
|
256
|
-
return (
|
|
257
|
-
<Component
|
|
258
|
-
ref={ref}
|
|
259
|
-
role='dialog'
|
|
260
|
-
nativeID={nativeID}
|
|
261
|
-
aria-modal={true}
|
|
262
|
-
style={[positionStyle, style]}
|
|
263
|
-
onLayout={onLayout}
|
|
264
|
-
onStartShouldSetResponder={onStartShouldSetResponder}
|
|
265
|
-
{...props}
|
|
266
|
-
/>
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
Content.displayName = 'ContentNativeHoverCard';
|
|
272
|
-
|
|
273
|
-
export { Content, Overlay, Portal, Root, Trigger, useRootContext };
|
|
274
|
-
|
|
275
|
-
export type { HoverCardTriggerRef };
|
|
276
|
-
|
|
277
|
-
function onStartShouldSetResponder() {
|
|
278
|
-
return true;
|
|
279
|
-
}
|