@tamagui/popover 1.88.13 → 1.89.0-1706308641099

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,391 @@
1
+ import "@tamagui/polyfill-dev";
2
+ import { Adapt, useAdaptParent } from "@tamagui/adapt";
3
+ import { Animate } from "@tamagui/animate";
4
+ import { ResetPresence } from "@tamagui/animate-presence";
5
+ import { hideOthers } from "@tamagui/aria-hidden";
6
+ import { useComposedRefs } from "@tamagui/compose-refs";
7
+ import { isWeb } from "@tamagui/constants";
8
+ import { Stack, Theme, View, createStyledContext, useEvent, useGet, useMedia, useThemeName } from "@tamagui/core";
9
+ import { FloatingOverrideContext } from "@tamagui/floating";
10
+ import { FocusScope } from "@tamagui/focus-scope";
11
+ import { composeEventHandlers, withStaticProperties } from "@tamagui/helpers";
12
+ import { Popper, PopperAnchor, PopperArrow, PopperContent, PopperContentFrame, PopperContext, usePopperContext } from "@tamagui/popper";
13
+ import { Portal, PortalHost, PortalItem } from "@tamagui/portal";
14
+ import { RemoveScroll } from "@tamagui/remove-scroll";
15
+ import { Sheet, SheetController } from "@tamagui/sheet";
16
+ import { YStack } from "@tamagui/stacks";
17
+ import { useControllableState } from "@tamagui/use-controllable-state";
18
+ import * as React from "react";
19
+ import { Platform, ScrollView } from "react-native-web";
20
+ import { useFloatingContext } from "./useFloatingContext.mjs";
21
+ import { jsx, jsxs } from "react/jsx-runtime";
22
+ const POPOVER_SCOPE = "PopoverScope",
23
+ PopoverContext = createStyledContext({}),
24
+ usePopoverContext = PopoverContext.useStyledContext,
25
+ PopoverAnchor = React.forwardRef(function (props, forwardedRef) {
26
+ const {
27
+ __scopePopover,
28
+ ...rest
29
+ } = props,
30
+ context = usePopoverContext(__scopePopover),
31
+ {
32
+ onCustomAnchorAdd,
33
+ onCustomAnchorRemove
34
+ } = context || {};
35
+ return React.useEffect(() => (onCustomAnchorAdd(), () => onCustomAnchorRemove()), [onCustomAnchorAdd, onCustomAnchorRemove]), /* @__PURE__ */jsx(PopperAnchor, {
36
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
37
+ ...rest,
38
+ ref: forwardedRef
39
+ });
40
+ }),
41
+ PopoverTrigger = React.forwardRef(function (props, forwardedRef) {
42
+ const {
43
+ __scopePopover,
44
+ ...rest
45
+ } = props,
46
+ context = usePopoverContext(__scopePopover),
47
+ anchorTo = context.anchorTo,
48
+ composedTriggerRef = useComposedRefs(forwardedRef, context.triggerRef);
49
+ if (anchorTo) {
50
+ let virtualRef = {
51
+ current: {
52
+ getBoundingClientRect: () => isWeb ? DOMRect.fromRect(anchorTo) : anchorTo,
53
+ ...(!isWeb && {
54
+ measure: c => c(anchorTo?.x, anchorTo?.y, anchorTo?.width, anchorTo?.height),
55
+ measureInWindow: c => c(anchorTo?.x, anchorTo?.y, anchorTo?.width, anchorTo?.height)
56
+ })
57
+ }
58
+ };
59
+ return /* @__PURE__ */jsx(PopperAnchor, {
60
+ virtualRef,
61
+ __scopePopper: __scopePopover || POPOVER_SCOPE
62
+ });
63
+ }
64
+ if (!props.children) return null;
65
+ const trigger = /* @__PURE__ */jsx(View, {
66
+ "aria-haspopup": "dialog",
67
+ "aria-expanded": context.open,
68
+ "data-state": getState(context.open),
69
+ ...rest,
70
+ ref: composedTriggerRef,
71
+ onPress: composeEventHandlers(props.onPress, context.onOpenToggle)
72
+ });
73
+ return context.hasCustomAnchor ? trigger : /* @__PURE__ */jsx(PopperAnchor, {
74
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
75
+ asChild: !0,
76
+ children: trigger
77
+ });
78
+ }),
79
+ PopoverContent = PopperContentFrame.extractable(React.forwardRef(function (props, forwardedRef) {
80
+ const {
81
+ allowPinchZoom,
82
+ trapFocus,
83
+ disableRemoveScroll = !0,
84
+ zIndex,
85
+ __scopePopover,
86
+ ...contentImplProps
87
+ } = props,
88
+ context = usePopoverContext(__scopePopover),
89
+ contentRef = React.useRef(null),
90
+ composedRefs = useComposedRefs(forwardedRef, contentRef),
91
+ isRightClickOutsideRef = React.useRef(!1);
92
+ return React.useEffect(() => {
93
+ if (!context.open) return;
94
+ const content = contentRef.current;
95
+ if (content) return hideOthers(content);
96
+ }, [context.open]), /* @__PURE__ */jsx(PopoverContentPortal, {
97
+ __scopePopover,
98
+ zIndex: props.zIndex,
99
+ children: /* @__PURE__ */jsx(Stack, {
100
+ pointerEvents: context.open ? "auto" : "none",
101
+ children: /* @__PURE__ */jsx(PopoverContentImpl, {
102
+ ...contentImplProps,
103
+ disableRemoveScroll,
104
+ ref: composedRefs,
105
+ __scopePopover,
106
+ trapFocus: trapFocus ?? context.open,
107
+ disableOutsidePointerEvents: !0,
108
+ onCloseAutoFocus: composeEventHandlers(props.onCloseAutoFocus, event => {
109
+ event.preventDefault(), isRightClickOutsideRef.current || context.triggerRef.current?.focus();
110
+ }),
111
+ onPointerDownOutside: composeEventHandlers(props.onPointerDownOutside, event => {
112
+ const originalEvent = event.detail.originalEvent,
113
+ ctrlLeftClick = originalEvent.button === 0 && originalEvent.ctrlKey === !0,
114
+ isRightClick = originalEvent.button === 2 || ctrlLeftClick;
115
+ isRightClickOutsideRef.current = isRightClick;
116
+ }, {
117
+ checkDefaultPrevented: !1
118
+ }),
119
+ onFocusOutside: composeEventHandlers(props.onFocusOutside, event => event.preventDefault(), {
120
+ checkDefaultPrevented: !1
121
+ })
122
+ })
123
+ })
124
+ });
125
+ }));
126
+ function PopoverRepropagateContext(props) {
127
+ return /* @__PURE__ */jsx(PopperContext.Provider, {
128
+ scope: props.scope,
129
+ ...props.popperContext,
130
+ children: /* @__PURE__ */jsx(PopoverContext.Provider, {
131
+ ...props.context,
132
+ children: props.children
133
+ })
134
+ });
135
+ }
136
+ function PopoverContentPortal(props) {
137
+ const {
138
+ __scopePopover
139
+ } = props,
140
+ zIndex = props.zIndex ?? 15e4,
141
+ context = usePopoverContext(__scopePopover),
142
+ popperContext = usePopperContext(__scopePopover || POPOVER_SCOPE),
143
+ themeName = useThemeName();
144
+ let contents = props.children;
145
+ return (Platform.OS === "android" || Platform.OS === "ios") && (contents = /* @__PURE__ */jsx(PopoverRepropagateContext, {
146
+ scope: __scopePopover || POPOVER_SCOPE,
147
+ popperContext,
148
+ context,
149
+ children: props.children
150
+ })), /* @__PURE__ */jsx(Portal, {
151
+ zIndex,
152
+ children: /* @__PURE__ */jsxs(Theme, {
153
+ forceClassName: !0,
154
+ name: themeName,
155
+ children: [!!context.open && !context.breakpointActive && /* @__PURE__ */jsx(YStack, {
156
+ fullscreen: !0,
157
+ onPress: composeEventHandlers(props.onPress, context.onOpenToggle)
158
+ }), contents]
159
+ })
160
+ });
161
+ }
162
+ const PopoverContentImpl = React.forwardRef(function (props, forwardedRef) {
163
+ const {
164
+ trapFocus,
165
+ __scopePopover,
166
+ onOpenAutoFocus,
167
+ onCloseAutoFocus,
168
+ disableOutsidePointerEvents,
169
+ disableFocusScope,
170
+ onEscapeKeyDown,
171
+ onPointerDownOutside,
172
+ onFocusOutside,
173
+ onInteractOutside,
174
+ children,
175
+ disableRemoveScroll,
176
+ freezeContentsWhenHidden,
177
+ ...contentProps
178
+ } = props,
179
+ context = usePopoverContext(__scopePopover),
180
+ {
181
+ open,
182
+ keepChildrenMounted
183
+ } = context,
184
+ popperContext = usePopperContext(__scopePopover || POPOVER_SCOPE),
185
+ [isFullyHidden, setIsFullyHidden] = React.useState(!context.open),
186
+ contents = React.useMemo(() => isWeb ? /* @__PURE__ */jsx("div", {
187
+ style: {
188
+ display: "contents"
189
+ },
190
+ children
191
+ }) : children, [children]);
192
+ if (open && isFullyHidden && setIsFullyHidden(!1), !keepChildrenMounted && isFullyHidden) return null;
193
+ if (context.breakpointActive) {
194
+ const childrenWithoutScrollView = React.Children.toArray(children).map(child => React.isValidElement(child) && child.type === ScrollView ? child.props.children : child);
195
+ let content = childrenWithoutScrollView;
196
+ return Platform.OS === "android" || Platform.OS === "ios" ? content = /* @__PURE__ */jsx(PopperContext.Provider, {
197
+ scope: __scopePopover || POPOVER_SCOPE,
198
+ ...popperContext,
199
+ children: /* @__PURE__ */jsx(ResetPresence, {
200
+ children: childrenWithoutScrollView
201
+ })
202
+ }) : content = /* @__PURE__ */jsx(ResetPresence, {
203
+ children: content
204
+ }), /* @__PURE__ */jsx(PortalItem, {
205
+ hostName: `${context.id}PopoverContents`,
206
+ children: content
207
+ });
208
+ }
209
+ return /* @__PURE__ */jsx(Animate, {
210
+ type: "presence",
211
+ present: !!open,
212
+ keepChildrenMounted,
213
+ onExitComplete: () => {
214
+ setIsFullyHidden(!0);
215
+ },
216
+ children: /* @__PURE__ */jsx(PopperContent, {
217
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
218
+ "data-state": getState(open),
219
+ id: context.contentId,
220
+ ref: forwardedRef,
221
+ ...contentProps,
222
+ children: /* @__PURE__ */jsx(RemoveScroll, {
223
+ enabled: disableRemoveScroll ? !1 : open,
224
+ allowPinchZoom: !0,
225
+ removeScrollBar: !1,
226
+ style: {
227
+ display: "contents"
228
+ },
229
+ children: /* @__PURE__ */jsx(ResetPresence, {
230
+ children: /* @__PURE__ */jsx(FocusScope, {
231
+ loop: !0,
232
+ enabled: disableFocusScope ? !1 : open,
233
+ trapped: trapFocus,
234
+ onMountAutoFocus: onOpenAutoFocus,
235
+ onUnmountAutoFocus: onCloseAutoFocus,
236
+ children: contents
237
+ })
238
+ })
239
+ })
240
+ }, context.contentId)
241
+ });
242
+ }),
243
+ PopoverClose = React.forwardRef(function (props, forwardedRef) {
244
+ const {
245
+ __scopePopover,
246
+ ...rest
247
+ } = props,
248
+ context = usePopoverContext(__scopePopover);
249
+ return /* @__PURE__ */jsx(YStack, {
250
+ ...rest,
251
+ ref: forwardedRef,
252
+ componentName: "PopoverClose",
253
+ onPress: composeEventHandlers(props.onPress, () => context.onOpenChange(!1, "press"))
254
+ });
255
+ }),
256
+ PopoverArrow = PopperArrow.styleable(function (props, forwardedRef) {
257
+ const {
258
+ __scopePopover,
259
+ ...rest
260
+ } = props,
261
+ context = usePopoverContext(__scopePopover);
262
+ return useSheetBreakpointActive(context.sheetBreakpoint) ? null : /* @__PURE__ */jsx(PopperArrow, {
263
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
264
+ componentName: "PopoverArrow",
265
+ ...rest,
266
+ ref: forwardedRef
267
+ });
268
+ }),
269
+ Popover = withStaticProperties(React.forwardRef((props, forwardedRef) => {
270
+ const {
271
+ children,
272
+ open: openProp,
273
+ defaultOpen,
274
+ onOpenChange,
275
+ __scopePopover,
276
+ keepChildrenMounted,
277
+ hoverable,
278
+ disableFocus,
279
+ ...restProps
280
+ } = props,
281
+ id = React.useId(),
282
+ {
283
+ when,
284
+ AdaptProvider
285
+ } = useAdaptParent({
286
+ Contents: React.useCallback(() => /* @__PURE__ */jsx(PortalHost, {
287
+ name: `${id}PopoverContents`
288
+ }), [])
289
+ }),
290
+ sheetBreakpoint = when,
291
+ triggerRef = React.useRef(null),
292
+ [hasCustomAnchor, setHasCustomAnchor] = React.useState(!1),
293
+ viaRef = React.useRef(),
294
+ [open, setOpen] = useControllableState({
295
+ prop: openProp,
296
+ defaultProp: defaultOpen || !1,
297
+ onChange: val => {
298
+ onOpenChange?.(val, viaRef.current);
299
+ }
300
+ }),
301
+ sheetActive = useSheetBreakpointActive(sheetBreakpoint),
302
+ floatingContext = useFloatingContext({
303
+ open,
304
+ setOpen: (val, via) => {
305
+ viaRef.current = via, setOpen(val);
306
+ },
307
+ disable: sheetActive,
308
+ hoverable,
309
+ disableFocus
310
+ }),
311
+ [anchorTo, setAnchorTo] = React.useState();
312
+ React.useImperativeHandle(forwardedRef, () => ({
313
+ anchorTo: setAnchorTo
314
+ }));
315
+ const popoverContext = {
316
+ id,
317
+ sheetBreakpoint,
318
+ contentId: React.useId(),
319
+ triggerRef,
320
+ open,
321
+ breakpointActive: sheetActive,
322
+ onOpenChange: (val, via) => {
323
+ viaRef.current = via, setOpen(val);
324
+ },
325
+ onOpenToggle: useEvent(() => {
326
+ open && sheetActive || setOpen(!open);
327
+ }),
328
+ hasCustomAnchor,
329
+ anchorTo,
330
+ onCustomAnchorAdd: React.useCallback(() => setHasCustomAnchor(!0), []),
331
+ onCustomAnchorRemove: React.useCallback(() => setHasCustomAnchor(!1), []),
332
+ keepChildrenMounted
333
+ },
334
+ contents = /* @__PURE__ */jsx(Popper, {
335
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
336
+ stayInFrame: !0,
337
+ ...restProps,
338
+ children: /* @__PURE__ */jsx(PopoverContext.Provider, {
339
+ scope: __scopePopover,
340
+ ...popoverContext,
341
+ children: /* @__PURE__ */jsx(PopoverSheetController, {
342
+ onOpenChange: setOpen,
343
+ children
344
+ })
345
+ })
346
+ });
347
+ return /* @__PURE__ */jsx(AdaptProvider, {
348
+ children: isWeb ? /* @__PURE__ */jsx(FloatingOverrideContext.Provider, {
349
+ value: floatingContext,
350
+ children: contents
351
+ }) : contents
352
+ });
353
+ }), {
354
+ Anchor: PopoverAnchor,
355
+ Arrow: PopoverArrow,
356
+ Trigger: PopoverTrigger,
357
+ Content: PopoverContent,
358
+ Close: PopoverClose,
359
+ Adapt,
360
+ ScrollView,
361
+ Sheet: Sheet.Controlled
362
+ });
363
+ function getState(open) {
364
+ return open ? "open" : "closed";
365
+ }
366
+ const PopoverSheetController = ({
367
+ __scopePopover,
368
+ ...props
369
+ }) => {
370
+ const context = usePopoverContext(__scopePopover),
371
+ showSheet = useShowPopoverSheet(context),
372
+ breakpointActive = context.breakpointActive,
373
+ getShowSheet = useGet(showSheet);
374
+ return /* @__PURE__ */jsx(SheetController, {
375
+ onOpenChange: val => {
376
+ getShowSheet() && props.onOpenChange(val);
377
+ },
378
+ open: context.open,
379
+ hidden: breakpointActive === !1,
380
+ children: props.children
381
+ });
382
+ },
383
+ useSheetBreakpointActive = breakpoint => {
384
+ const media = useMedia();
385
+ return typeof breakpoint == "boolean" || !breakpoint ? !!breakpoint : media[breakpoint];
386
+ },
387
+ useShowPopoverSheet = context => {
388
+ const breakpointActive = useSheetBreakpointActive(context.sheetBreakpoint);
389
+ return context.open === !1 ? !1 : breakpointActive;
390
+ };
391
+ export { Popover, PopoverAnchor, PopoverArrow, PopoverClose, PopoverContent, PopoverContext, PopoverTrigger, usePopoverContext };
@@ -0,0 +1,2 @@
1
+ export * from "./Popover.mjs";
2
+ export * from "./useFloatingContext.mjs";
@@ -0,0 +1,46 @@
1
+ import { safePolygon, useDismiss, useFloating, useFocus, useHover, useInteractions, useRole } from "@floating-ui/react";
2
+ import { useCallback } from "react";
3
+ const useFloatingContext = ({
4
+ open,
5
+ setOpen,
6
+ disable,
7
+ disableFocus,
8
+ hoverable
9
+ }) => useCallback(props => {
10
+ const floating = useFloating({
11
+ ...props,
12
+ open,
13
+ onOpenChange: (val, event) => {
14
+ const type = event?.type === "mousemove" || event?.type === "mouseenter" || event?.type === "mouseleave" ? "hover" : "press";
15
+ setOpen(val, type);
16
+ }
17
+ }),
18
+ {
19
+ getReferenceProps,
20
+ getFloatingProps
21
+ } = useInteractions([hoverable ? useHover(floating.context, {
22
+ enabled: !disable && hoverable,
23
+ handleClose: safePolygon({
24
+ requireIntent: !0,
25
+ blockPointerEvents: !0,
26
+ buffer: 1
27
+ }),
28
+ ...(hoverable && typeof hoverable == "object" && hoverable)
29
+ }) : useHover(floating.context, {
30
+ enabled: !1
31
+ }), useFocus(floating.context, {
32
+ enabled: !disable && !disableFocus,
33
+ visibleOnly: !0
34
+ }), useRole(floating.context, {
35
+ role: "dialog"
36
+ }), useDismiss(floating.context, {
37
+ enabled: !disable
38
+ })]);
39
+ return {
40
+ ...floating,
41
+ open,
42
+ getReferenceProps,
43
+ getFloatingProps
44
+ };
45
+ }, [open, setOpen, disable, disableFocus, hoverable]);
46
+ export { useFloatingContext };
@@ -0,0 +1,391 @@
1
+ import "@tamagui/polyfill-dev";
2
+ import { Adapt, useAdaptParent } from "@tamagui/adapt";
3
+ import { Animate } from "@tamagui/animate";
4
+ import { ResetPresence } from "@tamagui/animate-presence";
5
+ import { hideOthers } from "@tamagui/aria-hidden";
6
+ import { useComposedRefs } from "@tamagui/compose-refs";
7
+ import { isWeb } from "@tamagui/constants";
8
+ import { Stack, Theme, View, createStyledContext, useEvent, useGet, useMedia, useThemeName } from "@tamagui/core";
9
+ import { FloatingOverrideContext } from "@tamagui/floating";
10
+ import { FocusScope } from "@tamagui/focus-scope";
11
+ import { composeEventHandlers, withStaticProperties } from "@tamagui/helpers";
12
+ import { Popper, PopperAnchor, PopperArrow, PopperContent, PopperContentFrame, PopperContext, usePopperContext } from "@tamagui/popper";
13
+ import { Portal, PortalHost, PortalItem } from "@tamagui/portal";
14
+ import { RemoveScroll } from "@tamagui/remove-scroll";
15
+ import { Sheet, SheetController } from "@tamagui/sheet";
16
+ import { YStack } from "@tamagui/stacks";
17
+ import { useControllableState } from "@tamagui/use-controllable-state";
18
+ import * as React from "react";
19
+ import { Platform, ScrollView } from "react-native-web";
20
+ import { useFloatingContext } from "./useFloatingContext.mjs";
21
+ import { jsx, jsxs } from "react/jsx-runtime";
22
+ const POPOVER_SCOPE = "PopoverScope",
23
+ PopoverContext = createStyledContext({}),
24
+ usePopoverContext = PopoverContext.useStyledContext,
25
+ PopoverAnchor = React.forwardRef(function (props, forwardedRef) {
26
+ const {
27
+ __scopePopover,
28
+ ...rest
29
+ } = props,
30
+ context = usePopoverContext(__scopePopover),
31
+ {
32
+ onCustomAnchorAdd,
33
+ onCustomAnchorRemove
34
+ } = context || {};
35
+ return React.useEffect(() => (onCustomAnchorAdd(), () => onCustomAnchorRemove()), [onCustomAnchorAdd, onCustomAnchorRemove]), /* @__PURE__ */jsx(PopperAnchor, {
36
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
37
+ ...rest,
38
+ ref: forwardedRef
39
+ });
40
+ }),
41
+ PopoverTrigger = React.forwardRef(function (props, forwardedRef) {
42
+ const {
43
+ __scopePopover,
44
+ ...rest
45
+ } = props,
46
+ context = usePopoverContext(__scopePopover),
47
+ anchorTo = context.anchorTo,
48
+ composedTriggerRef = useComposedRefs(forwardedRef, context.triggerRef);
49
+ if (anchorTo) {
50
+ let virtualRef = {
51
+ current: {
52
+ getBoundingClientRect: () => isWeb ? DOMRect.fromRect(anchorTo) : anchorTo,
53
+ ...(!isWeb && {
54
+ measure: c => c(anchorTo?.x, anchorTo?.y, anchorTo?.width, anchorTo?.height),
55
+ measureInWindow: c => c(anchorTo?.x, anchorTo?.y, anchorTo?.width, anchorTo?.height)
56
+ })
57
+ }
58
+ };
59
+ return /* @__PURE__ */jsx(PopperAnchor, {
60
+ virtualRef,
61
+ __scopePopper: __scopePopover || POPOVER_SCOPE
62
+ });
63
+ }
64
+ if (!props.children) return null;
65
+ const trigger = /* @__PURE__ */jsx(View, {
66
+ "aria-haspopup": "dialog",
67
+ "aria-expanded": context.open,
68
+ "data-state": getState(context.open),
69
+ ...rest,
70
+ ref: composedTriggerRef,
71
+ onPress: composeEventHandlers(props.onPress, context.onOpenToggle)
72
+ });
73
+ return context.hasCustomAnchor ? trigger : /* @__PURE__ */jsx(PopperAnchor, {
74
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
75
+ asChild: !0,
76
+ children: trigger
77
+ });
78
+ }),
79
+ PopoverContent = PopperContentFrame.extractable(React.forwardRef(function (props, forwardedRef) {
80
+ const {
81
+ allowPinchZoom,
82
+ trapFocus,
83
+ disableRemoveScroll = !0,
84
+ zIndex,
85
+ __scopePopover,
86
+ ...contentImplProps
87
+ } = props,
88
+ context = usePopoverContext(__scopePopover),
89
+ contentRef = React.useRef(null),
90
+ composedRefs = useComposedRefs(forwardedRef, contentRef),
91
+ isRightClickOutsideRef = React.useRef(!1);
92
+ return React.useEffect(() => {
93
+ if (!context.open) return;
94
+ const content = contentRef.current;
95
+ if (content) return hideOthers(content);
96
+ }, [context.open]), /* @__PURE__ */jsx(PopoverContentPortal, {
97
+ __scopePopover,
98
+ zIndex: props.zIndex,
99
+ children: /* @__PURE__ */jsx(Stack, {
100
+ pointerEvents: context.open ? "auto" : "none",
101
+ children: /* @__PURE__ */jsx(PopoverContentImpl, {
102
+ ...contentImplProps,
103
+ disableRemoveScroll,
104
+ ref: composedRefs,
105
+ __scopePopover,
106
+ trapFocus: trapFocus ?? context.open,
107
+ disableOutsidePointerEvents: !0,
108
+ onCloseAutoFocus: composeEventHandlers(props.onCloseAutoFocus, event => {
109
+ event.preventDefault(), isRightClickOutsideRef.current || context.triggerRef.current?.focus();
110
+ }),
111
+ onPointerDownOutside: composeEventHandlers(props.onPointerDownOutside, event => {
112
+ const originalEvent = event.detail.originalEvent,
113
+ ctrlLeftClick = originalEvent.button === 0 && originalEvent.ctrlKey === !0,
114
+ isRightClick = originalEvent.button === 2 || ctrlLeftClick;
115
+ isRightClickOutsideRef.current = isRightClick;
116
+ }, {
117
+ checkDefaultPrevented: !1
118
+ }),
119
+ onFocusOutside: composeEventHandlers(props.onFocusOutside, event => event.preventDefault(), {
120
+ checkDefaultPrevented: !1
121
+ })
122
+ })
123
+ })
124
+ });
125
+ }));
126
+ function PopoverRepropagateContext(props) {
127
+ return /* @__PURE__ */jsx(PopperContext.Provider, {
128
+ scope: props.scope,
129
+ ...props.popperContext,
130
+ children: /* @__PURE__ */jsx(PopoverContext.Provider, {
131
+ ...props.context,
132
+ children: props.children
133
+ })
134
+ });
135
+ }
136
+ function PopoverContentPortal(props) {
137
+ const {
138
+ __scopePopover
139
+ } = props,
140
+ zIndex = props.zIndex ?? 15e4,
141
+ context = usePopoverContext(__scopePopover),
142
+ popperContext = usePopperContext(__scopePopover || POPOVER_SCOPE),
143
+ themeName = useThemeName();
144
+ let contents = props.children;
145
+ return (Platform.OS === "android" || Platform.OS === "ios") && (contents = /* @__PURE__ */jsx(PopoverRepropagateContext, {
146
+ scope: __scopePopover || POPOVER_SCOPE,
147
+ popperContext,
148
+ context,
149
+ children: props.children
150
+ })), /* @__PURE__ */jsx(Portal, {
151
+ zIndex,
152
+ children: /* @__PURE__ */jsxs(Theme, {
153
+ forceClassName: !0,
154
+ name: themeName,
155
+ children: [!!context.open && !context.breakpointActive && /* @__PURE__ */jsx(YStack, {
156
+ fullscreen: !0,
157
+ onPress: composeEventHandlers(props.onPress, context.onOpenToggle)
158
+ }), contents]
159
+ })
160
+ });
161
+ }
162
+ const PopoverContentImpl = React.forwardRef(function (props, forwardedRef) {
163
+ const {
164
+ trapFocus,
165
+ __scopePopover,
166
+ onOpenAutoFocus,
167
+ onCloseAutoFocus,
168
+ disableOutsidePointerEvents,
169
+ disableFocusScope,
170
+ onEscapeKeyDown,
171
+ onPointerDownOutside,
172
+ onFocusOutside,
173
+ onInteractOutside,
174
+ children,
175
+ disableRemoveScroll,
176
+ freezeContentsWhenHidden,
177
+ ...contentProps
178
+ } = props,
179
+ context = usePopoverContext(__scopePopover),
180
+ {
181
+ open,
182
+ keepChildrenMounted
183
+ } = context,
184
+ popperContext = usePopperContext(__scopePopover || POPOVER_SCOPE),
185
+ [isFullyHidden, setIsFullyHidden] = React.useState(!context.open),
186
+ contents = React.useMemo(() => isWeb ? /* @__PURE__ */jsx("div", {
187
+ style: {
188
+ display: "contents"
189
+ },
190
+ children
191
+ }) : children, [children]);
192
+ if (open && isFullyHidden && setIsFullyHidden(!1), !keepChildrenMounted && isFullyHidden) return null;
193
+ if (context.breakpointActive) {
194
+ const childrenWithoutScrollView = React.Children.toArray(children).map(child => React.isValidElement(child) && child.type === ScrollView ? child.props.children : child);
195
+ let content = childrenWithoutScrollView;
196
+ return Platform.OS === "android" || Platform.OS === "ios" ? content = /* @__PURE__ */jsx(PopperContext.Provider, {
197
+ scope: __scopePopover || POPOVER_SCOPE,
198
+ ...popperContext,
199
+ children: /* @__PURE__ */jsx(ResetPresence, {
200
+ children: childrenWithoutScrollView
201
+ })
202
+ }) : content = /* @__PURE__ */jsx(ResetPresence, {
203
+ children: content
204
+ }), /* @__PURE__ */jsx(PortalItem, {
205
+ hostName: `${context.id}PopoverContents`,
206
+ children: content
207
+ });
208
+ }
209
+ return /* @__PURE__ */jsx(Animate, {
210
+ type: "presence",
211
+ present: !!open,
212
+ keepChildrenMounted,
213
+ onExitComplete: () => {
214
+ setIsFullyHidden(!0);
215
+ },
216
+ children: /* @__PURE__ */jsx(PopperContent, {
217
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
218
+ "data-state": getState(open),
219
+ id: context.contentId,
220
+ ref: forwardedRef,
221
+ ...contentProps,
222
+ children: /* @__PURE__ */jsx(RemoveScroll, {
223
+ enabled: disableRemoveScroll ? !1 : open,
224
+ allowPinchZoom: !0,
225
+ removeScrollBar: !1,
226
+ style: {
227
+ display: "contents"
228
+ },
229
+ children: /* @__PURE__ */jsx(ResetPresence, {
230
+ children: /* @__PURE__ */jsx(FocusScope, {
231
+ loop: !0,
232
+ enabled: disableFocusScope ? !1 : open,
233
+ trapped: trapFocus,
234
+ onMountAutoFocus: onOpenAutoFocus,
235
+ onUnmountAutoFocus: onCloseAutoFocus,
236
+ children: contents
237
+ })
238
+ })
239
+ })
240
+ }, context.contentId)
241
+ });
242
+ }),
243
+ PopoverClose = React.forwardRef(function (props, forwardedRef) {
244
+ const {
245
+ __scopePopover,
246
+ ...rest
247
+ } = props,
248
+ context = usePopoverContext(__scopePopover);
249
+ return /* @__PURE__ */jsx(YStack, {
250
+ ...rest,
251
+ ref: forwardedRef,
252
+ componentName: "PopoverClose",
253
+ onPress: composeEventHandlers(props.onPress, () => context.onOpenChange(!1, "press"))
254
+ });
255
+ }),
256
+ PopoverArrow = PopperArrow.styleable(function (props, forwardedRef) {
257
+ const {
258
+ __scopePopover,
259
+ ...rest
260
+ } = props,
261
+ context = usePopoverContext(__scopePopover);
262
+ return useSheetBreakpointActive(context.sheetBreakpoint) ? null : /* @__PURE__ */jsx(PopperArrow, {
263
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
264
+ componentName: "PopoverArrow",
265
+ ...rest,
266
+ ref: forwardedRef
267
+ });
268
+ }),
269
+ Popover = withStaticProperties(React.forwardRef((props, forwardedRef) => {
270
+ const {
271
+ children,
272
+ open: openProp,
273
+ defaultOpen,
274
+ onOpenChange,
275
+ __scopePopover,
276
+ keepChildrenMounted,
277
+ hoverable,
278
+ disableFocus,
279
+ ...restProps
280
+ } = props,
281
+ id = React.useId(),
282
+ {
283
+ when,
284
+ AdaptProvider
285
+ } = useAdaptParent({
286
+ Contents: React.useCallback(() => /* @__PURE__ */jsx(PortalHost, {
287
+ name: `${id}PopoverContents`
288
+ }), [])
289
+ }),
290
+ sheetBreakpoint = when,
291
+ triggerRef = React.useRef(null),
292
+ [hasCustomAnchor, setHasCustomAnchor] = React.useState(!1),
293
+ viaRef = React.useRef(),
294
+ [open, setOpen] = useControllableState({
295
+ prop: openProp,
296
+ defaultProp: defaultOpen || !1,
297
+ onChange: val => {
298
+ onOpenChange?.(val, viaRef.current);
299
+ }
300
+ }),
301
+ sheetActive = useSheetBreakpointActive(sheetBreakpoint),
302
+ floatingContext = useFloatingContext({
303
+ open,
304
+ setOpen: (val, via) => {
305
+ viaRef.current = via, setOpen(val);
306
+ },
307
+ disable: sheetActive,
308
+ hoverable,
309
+ disableFocus
310
+ }),
311
+ [anchorTo, setAnchorTo] = React.useState();
312
+ React.useImperativeHandle(forwardedRef, () => ({
313
+ anchorTo: setAnchorTo
314
+ }));
315
+ const popoverContext = {
316
+ id,
317
+ sheetBreakpoint,
318
+ contentId: React.useId(),
319
+ triggerRef,
320
+ open,
321
+ breakpointActive: sheetActive,
322
+ onOpenChange: (val, via) => {
323
+ viaRef.current = via, setOpen(val);
324
+ },
325
+ onOpenToggle: useEvent(() => {
326
+ open && sheetActive || setOpen(!open);
327
+ }),
328
+ hasCustomAnchor,
329
+ anchorTo,
330
+ onCustomAnchorAdd: React.useCallback(() => setHasCustomAnchor(!0), []),
331
+ onCustomAnchorRemove: React.useCallback(() => setHasCustomAnchor(!1), []),
332
+ keepChildrenMounted
333
+ },
334
+ contents = /* @__PURE__ */jsx(Popper, {
335
+ __scopePopper: __scopePopover || POPOVER_SCOPE,
336
+ stayInFrame: !0,
337
+ ...restProps,
338
+ children: /* @__PURE__ */jsx(PopoverContext.Provider, {
339
+ scope: __scopePopover,
340
+ ...popoverContext,
341
+ children: /* @__PURE__ */jsx(PopoverSheetController, {
342
+ onOpenChange: setOpen,
343
+ children
344
+ })
345
+ })
346
+ });
347
+ return /* @__PURE__ */jsx(AdaptProvider, {
348
+ children: isWeb ? /* @__PURE__ */jsx(FloatingOverrideContext.Provider, {
349
+ value: floatingContext,
350
+ children: contents
351
+ }) : contents
352
+ });
353
+ }), {
354
+ Anchor: PopoverAnchor,
355
+ Arrow: PopoverArrow,
356
+ Trigger: PopoverTrigger,
357
+ Content: PopoverContent,
358
+ Close: PopoverClose,
359
+ Adapt,
360
+ ScrollView,
361
+ Sheet: Sheet.Controlled
362
+ });
363
+ function getState(open) {
364
+ return open ? "open" : "closed";
365
+ }
366
+ const PopoverSheetController = ({
367
+ __scopePopover,
368
+ ...props
369
+ }) => {
370
+ const context = usePopoverContext(__scopePopover),
371
+ showSheet = useShowPopoverSheet(context),
372
+ breakpointActive = context.breakpointActive,
373
+ getShowSheet = useGet(showSheet);
374
+ return /* @__PURE__ */jsx(SheetController, {
375
+ onOpenChange: val => {
376
+ getShowSheet() && props.onOpenChange(val);
377
+ },
378
+ open: context.open,
379
+ hidden: breakpointActive === !1,
380
+ children: props.children
381
+ });
382
+ },
383
+ useSheetBreakpointActive = breakpoint => {
384
+ const media = useMedia();
385
+ return typeof breakpoint == "boolean" || !breakpoint ? !!breakpoint : media[breakpoint];
386
+ },
387
+ useShowPopoverSheet = context => {
388
+ const breakpointActive = useSheetBreakpointActive(context.sheetBreakpoint);
389
+ return context.open === !1 ? !1 : breakpointActive;
390
+ };
391
+ export { Popover, PopoverAnchor, PopoverArrow, PopoverClose, PopoverContent, PopoverContext, PopoverTrigger, usePopoverContext };
@@ -0,0 +1,2 @@
1
+ export * from "./Popover.mjs";
2
+ export * from "./useFloatingContext.mjs";
@@ -0,0 +1,46 @@
1
+ import { safePolygon, useDismiss, useFloating, useFocus, useHover, useInteractions, useRole } from "@floating-ui/react";
2
+ import { useCallback } from "react";
3
+ const useFloatingContext = ({
4
+ open,
5
+ setOpen,
6
+ disable,
7
+ disableFocus,
8
+ hoverable
9
+ }) => useCallback(props => {
10
+ const floating = useFloating({
11
+ ...props,
12
+ open,
13
+ onOpenChange: (val, event) => {
14
+ const type = event?.type === "mousemove" || event?.type === "mouseenter" || event?.type === "mouseleave" ? "hover" : "press";
15
+ setOpen(val, type);
16
+ }
17
+ }),
18
+ {
19
+ getReferenceProps,
20
+ getFloatingProps
21
+ } = useInteractions([hoverable ? useHover(floating.context, {
22
+ enabled: !disable && hoverable,
23
+ handleClose: safePolygon({
24
+ requireIntent: !0,
25
+ blockPointerEvents: !0,
26
+ buffer: 1
27
+ }),
28
+ ...(hoverable && typeof hoverable == "object" && hoverable)
29
+ }) : useHover(floating.context, {
30
+ enabled: !1
31
+ }), useFocus(floating.context, {
32
+ enabled: !disable && !disableFocus,
33
+ visibleOnly: !0
34
+ }), useRole(floating.context, {
35
+ role: "dialog"
36
+ }), useDismiss(floating.context, {
37
+ enabled: !disable
38
+ })]);
39
+ return {
40
+ ...floating,
41
+ open,
42
+ getReferenceProps,
43
+ getFloatingProps
44
+ };
45
+ }, [open, setOpen, disable, disableFocus, hoverable]);
46
+ export { useFloatingContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/popover",
3
- "version": "1.88.13",
3
+ "version": "1.89.0-1706308641099",
4
4
  "sideEffects": [
5
5
  "*.css"
6
6
  ],
@@ -33,24 +33,24 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@floating-ui/react": "^0.26.6",
36
- "@tamagui/adapt": "1.88.13",
37
- "@tamagui/animate": "1.88.13",
38
- "@tamagui/aria-hidden": "1.88.13",
39
- "@tamagui/compose-refs": "1.88.13",
40
- "@tamagui/constants": "1.88.13",
41
- "@tamagui/core": "1.88.13",
42
- "@tamagui/dismissable": "1.88.13",
43
- "@tamagui/floating": "1.88.13",
44
- "@tamagui/focus-scope": "1.88.13",
45
- "@tamagui/helpers": "1.88.13",
46
- "@tamagui/polyfill-dev": "1.88.13",
47
- "@tamagui/popper": "1.88.13",
48
- "@tamagui/portal": "1.88.13",
49
- "@tamagui/remove-scroll": "1.88.13",
50
- "@tamagui/scroll-view": "1.88.13",
51
- "@tamagui/sheet": "1.88.13",
52
- "@tamagui/stacks": "1.88.13",
53
- "@tamagui/use-controllable-state": "1.88.13",
36
+ "@tamagui/adapt": "1.89.0-1706308641099",
37
+ "@tamagui/animate": "1.89.0-1706308641099",
38
+ "@tamagui/aria-hidden": "1.89.0-1706308641099",
39
+ "@tamagui/compose-refs": "1.89.0-1706308641099",
40
+ "@tamagui/constants": "1.89.0-1706308641099",
41
+ "@tamagui/core": "1.89.0-1706308641099",
42
+ "@tamagui/dismissable": "1.89.0-1706308641099",
43
+ "@tamagui/floating": "1.89.0-1706308641099",
44
+ "@tamagui/focus-scope": "1.89.0-1706308641099",
45
+ "@tamagui/helpers": "1.89.0-1706308641099",
46
+ "@tamagui/polyfill-dev": "1.89.0-1706308641099",
47
+ "@tamagui/popper": "1.89.0-1706308641099",
48
+ "@tamagui/portal": "1.89.0-1706308641099",
49
+ "@tamagui/remove-scroll": "1.89.0-1706308641099",
50
+ "@tamagui/scroll-view": "1.89.0-1706308641099",
51
+ "@tamagui/sheet": "1.89.0-1706308641099",
52
+ "@tamagui/stacks": "1.89.0-1706308641099",
53
+ "@tamagui/use-controllable-state": "1.89.0-1706308641099",
54
54
  "react-freeze": "^1.0.3"
55
55
  },
56
56
  "peerDependencies": {
@@ -58,7 +58,7 @@
58
58
  "react-native": "*"
59
59
  },
60
60
  "devDependencies": {
61
- "@tamagui/build": "1.88.13",
61
+ "@tamagui/build": "1.89.0-1706308641099",
62
62
  "react": "^18.2.0",
63
63
  "react-native": "^0.72.6"
64
64
  },