@yamada-ui/popover 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hirotomo Yamada
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.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @yamada-ui/popover
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ pnpm add @yamada-ui/popover
7
+ ```
8
+
9
+ or
10
+
11
+ ```sh
12
+ $ yarn add @yamada-ui/popover
13
+ ```
14
+
15
+ or
16
+
17
+ ```sh
18
+ $ npm install @yamada-ui/popover
19
+ ```
20
+
21
+ ## Contribution
22
+
23
+ Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
24
+
25
+ ## Licence
26
+
27
+ This package is licensed under the terms of the
28
+ [MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
@@ -0,0 +1,229 @@
1
+ // src/popover.tsx
2
+ import { useMultiComponentStyle, omitThemeProps } from "@yamada-ui/core";
3
+ import { useAnimationObserver } from "@yamada-ui/use-animation";
4
+ import { useDisclosure, useLazyDisclosure } from "@yamada-ui/use-disclosure";
5
+ import { useFocusOnHide, useFocusOnShow, useFocusOnPointerDown } from "@yamada-ui/use-focus";
6
+ import { usePopper } from "@yamada-ui/use-popper";
7
+ import {
8
+ createContext,
9
+ getEventRelatedTarget,
10
+ handlerAll,
11
+ isContains,
12
+ mergeRefs,
13
+ runIfFunc
14
+ } from "@yamada-ui/utils";
15
+ import { useCallback, useEffect, useRef } from "react";
16
+ import { jsx } from "react/jsx-runtime";
17
+ var [PopoverProvider, usePopover] = createContext({
18
+ strict: false,
19
+ name: "PopoverContext"
20
+ });
21
+ var Popover = (props) => {
22
+ const [styles, mergedProps] = useMultiComponentStyle("Popover", props);
23
+ const {
24
+ children,
25
+ initialFocusRef,
26
+ restoreFocus = true,
27
+ autoFocus = true,
28
+ closeOnBlur = true,
29
+ closeOnEsc = true,
30
+ closeOnButton = true,
31
+ trigger = "click",
32
+ openDelay = 200,
33
+ closeDelay = 200,
34
+ isLazy,
35
+ lazyBehavior = "unmount",
36
+ animation = "scale",
37
+ duration,
38
+ ...rest
39
+ } = omitThemeProps(mergedProps);
40
+ const [isOpen, onOpen, onClose, onToggle] = useDisclosure(mergedProps);
41
+ const anchorRef = useRef(null);
42
+ const triggerRef = useRef(null);
43
+ const popoverRef = useRef(null);
44
+ const { present, onAnimationComplete } = useAnimationObserver({ isOpen, ref: popoverRef });
45
+ const openTimeout = useRef(void 0);
46
+ const closeTimeout = useRef(void 0);
47
+ const isHoveringRef = useRef(false);
48
+ const hasBeenOpened = useRef(false);
49
+ if (isOpen)
50
+ hasBeenOpened.current = true;
51
+ const { referenceRef, getPopperProps, forceUpdate, transformOrigin } = usePopper({
52
+ ...rest,
53
+ enabled: isOpen
54
+ });
55
+ useEffect(() => {
56
+ return () => {
57
+ if (openTimeout.current)
58
+ clearTimeout(openTimeout.current);
59
+ if (closeTimeout.current)
60
+ clearTimeout(closeTimeout.current);
61
+ };
62
+ }, []);
63
+ useFocusOnPointerDown({
64
+ enabled: isOpen,
65
+ ref: triggerRef
66
+ });
67
+ useFocusOnHide(popoverRef, {
68
+ focusRef: triggerRef,
69
+ visible: isOpen,
70
+ shouldFocus: restoreFocus && trigger === "click"
71
+ });
72
+ useFocusOnShow(popoverRef, {
73
+ focusRef: initialFocusRef,
74
+ visible: isOpen,
75
+ shouldFocus: autoFocus && trigger === "click"
76
+ });
77
+ const shouldRenderChildren = useLazyDisclosure({
78
+ wasSelected: hasBeenOpened.current,
79
+ enabled: isLazy,
80
+ mode: lazyBehavior,
81
+ isSelected: present
82
+ });
83
+ const getPopoverProps = useCallback(
84
+ (props2 = {}, ref = null) => {
85
+ const popoverProps = {
86
+ ...props2,
87
+ style: {
88
+ ...props2.style,
89
+ transformOrigin
90
+ },
91
+ ref: mergeRefs(popoverRef, ref),
92
+ children: shouldRenderChildren ? props2.children : null,
93
+ tabIndex: -1,
94
+ onKeyDown: handlerAll(props2.onKeyDown, (event) => {
95
+ if (closeOnEsc && event.key === "Escape")
96
+ onClose();
97
+ }),
98
+ onBlur: handlerAll(props2.onBlur, (event) => {
99
+ const relatedTarget = getEventRelatedTarget(event);
100
+ const targetIsPopover = isContains(popoverRef.current, relatedTarget);
101
+ const targetIsTrigger = isContains(triggerRef.current, relatedTarget);
102
+ const isValidBlur = !targetIsPopover && !targetIsTrigger;
103
+ if (isOpen && closeOnBlur && isValidBlur)
104
+ onClose();
105
+ })
106
+ };
107
+ if (trigger === "hover") {
108
+ popoverProps.onMouseEnter = handlerAll(props2.onMouseEnter, () => {
109
+ isHoveringRef.current = true;
110
+ });
111
+ popoverProps.onMouseLeave = handlerAll(props2.onMouseLeave, (event) => {
112
+ if (event.nativeEvent.relatedTarget === null)
113
+ return;
114
+ isHoveringRef.current = false;
115
+ setTimeout(onClose, closeDelay);
116
+ });
117
+ }
118
+ return popoverProps;
119
+ },
120
+ [
121
+ closeDelay,
122
+ closeOnBlur,
123
+ closeOnEsc,
124
+ isOpen,
125
+ onClose,
126
+ shouldRenderChildren,
127
+ transformOrigin,
128
+ trigger
129
+ ]
130
+ );
131
+ const maybeReferenceRef = useCallback(
132
+ (node) => {
133
+ if (anchorRef.current == null)
134
+ referenceRef(node);
135
+ },
136
+ [referenceRef]
137
+ );
138
+ const getTriggerProps = useCallback(
139
+ (props2 = {}, ref = null) => {
140
+ const triggerProps = {
141
+ ...props2,
142
+ ref: mergeRefs(triggerRef, ref, maybeReferenceRef)
143
+ };
144
+ if (trigger === "click")
145
+ triggerProps.onClick = handlerAll(props2.onClick, onToggle);
146
+ if (trigger === "hover") {
147
+ triggerProps.onFocus = handlerAll(props2.onFocus, () => {
148
+ if (openTimeout.current === void 0)
149
+ onOpen();
150
+ });
151
+ triggerProps.onBlur = handlerAll(props2.onBlur, (event) => {
152
+ const relatedTarget = getEventRelatedTarget(event);
153
+ const isValidBlur = !isContains(popoverRef.current, relatedTarget);
154
+ if (isOpen && closeOnBlur && isValidBlur)
155
+ onClose();
156
+ });
157
+ triggerProps.onKeyDown = handlerAll(props2.onKeyDown, (event) => {
158
+ if (event.key === "Escape")
159
+ onClose();
160
+ });
161
+ triggerProps.onMouseEnter = handlerAll(props2.onMouseEnter, () => {
162
+ isHoveringRef.current = true;
163
+ openTimeout.current = window.setTimeout(onOpen, openDelay);
164
+ });
165
+ triggerProps.onMouseLeave = handlerAll(props2.onMouseLeave, () => {
166
+ isHoveringRef.current = false;
167
+ if (openTimeout.current) {
168
+ clearTimeout(openTimeout.current);
169
+ openTimeout.current = void 0;
170
+ }
171
+ closeTimeout.current = window.setTimeout(() => {
172
+ if (isHoveringRef.current === false)
173
+ onClose();
174
+ }, closeDelay);
175
+ });
176
+ }
177
+ return triggerProps;
178
+ },
179
+ [
180
+ closeDelay,
181
+ closeOnBlur,
182
+ isOpen,
183
+ maybeReferenceRef,
184
+ onClose,
185
+ onOpen,
186
+ onToggle,
187
+ openDelay,
188
+ trigger
189
+ ]
190
+ );
191
+ const getAnchorProps = useCallback(
192
+ (props2 = {}, ref = null) => {
193
+ return {
194
+ ...props2,
195
+ ref: mergeRefs(ref, anchorRef, referenceRef)
196
+ };
197
+ },
198
+ [anchorRef, referenceRef]
199
+ );
200
+ return /* @__PURE__ */ jsx(
201
+ PopoverProvider,
202
+ {
203
+ value: {
204
+ isOpen,
205
+ onClose,
206
+ closeOnButton,
207
+ onAnimationComplete,
208
+ forceUpdate,
209
+ getTriggerProps,
210
+ getAnchorProps,
211
+ getPopperProps,
212
+ getPopoverProps,
213
+ animation,
214
+ duration,
215
+ styles
216
+ },
217
+ children: runIfFunc(children, {
218
+ isOpen,
219
+ onClose,
220
+ forceUpdate
221
+ })
222
+ }
223
+ );
224
+ };
225
+
226
+ export {
227
+ usePopover,
228
+ Popover
229
+ };
@@ -0,0 +1,216 @@
1
+ import {
2
+ usePopover
3
+ } from "./chunk-DQEXAU5O.mjs";
4
+
5
+ // src/popover-trigger.tsx
6
+ import { Children as Children2, cloneElement as cloneElement2 } from "react";
7
+
8
+ // src/popover-anchor.tsx
9
+ import { Children, cloneElement } from "react";
10
+ var PopoverAnchor = ({ children }) => {
11
+ const child = Children.only(children);
12
+ const { getAnchorProps } = usePopover();
13
+ return cloneElement(child, getAnchorProps(child.props, child.ref));
14
+ };
15
+
16
+ // src/popover-close-button.tsx
17
+ import { CloseButton } from "@yamada-ui/close-button";
18
+ import { forwardRef } from "@yamada-ui/core";
19
+ import { cx, handlerAll } from "@yamada-ui/utils";
20
+ import { jsx } from "react/jsx-runtime";
21
+ var PopoverCloseButton = forwardRef(
22
+ ({ onClick, ...rest }, ref) => {
23
+ const { styles, onClose } = usePopover();
24
+ const css = {
25
+ position: "absolute",
26
+ ...styles.closeButton
27
+ };
28
+ return /* @__PURE__ */ jsx(
29
+ CloseButton,
30
+ {
31
+ ref,
32
+ className: cx("ui-popover-close-button"),
33
+ __css: css,
34
+ onClick: handlerAll(onClick, (event) => {
35
+ event.stopPropagation();
36
+ onClose == null ? void 0 : onClose();
37
+ }),
38
+ size: "sm",
39
+ ...rest
40
+ }
41
+ );
42
+ }
43
+ );
44
+
45
+ // src/popover-content.tsx
46
+ import { ui, forwardRef as forwardRef2 } from "@yamada-ui/core";
47
+ import { motion } from "@yamada-ui/motion";
48
+ import { scaleFadeProps, slideFadeProps } from "@yamada-ui/transitions";
49
+ import {
50
+ cx as cx2,
51
+ findChildren,
52
+ funcAll,
53
+ getValidChildren,
54
+ omitObject
55
+ } from "@yamada-ui/utils";
56
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
57
+ var getPopoverContentProps = (animation = "scale", duration) => {
58
+ const custom = {
59
+ reverse: true,
60
+ duration,
61
+ enter: { visibility: "visible" },
62
+ transitionEnd: { exit: { visibility: "hidden" } }
63
+ };
64
+ switch (animation) {
65
+ case "scale":
66
+ return {
67
+ ...scaleFadeProps,
68
+ custom: { ...custom, scale: 0.95 }
69
+ };
70
+ case "top":
71
+ return {
72
+ ...slideFadeProps,
73
+ custom: { ...custom, offsetX: 0, offsetY: -16 }
74
+ };
75
+ case "right":
76
+ return {
77
+ ...slideFadeProps,
78
+ custom: { ...custom, offsetX: 16, offsetY: 0 }
79
+ };
80
+ case "left":
81
+ return {
82
+ ...slideFadeProps,
83
+ custom: { ...custom, offsetX: -16, offsetY: 0 }
84
+ };
85
+ case "bottom":
86
+ return {
87
+ ...slideFadeProps,
88
+ custom: { ...custom, offsetX: 0, offsetY: 16 }
89
+ };
90
+ }
91
+ };
92
+ var PopoverContent = forwardRef2(
93
+ ({ as = "section", className, children, w, width, minW, minWidth, zIndex, __css, ...rest }, ref) => {
94
+ var _a, _b, _c, _d;
95
+ const {
96
+ isOpen,
97
+ closeOnButton,
98
+ getPopperProps,
99
+ getPopoverProps,
100
+ onAnimationComplete,
101
+ animation,
102
+ duration,
103
+ styles
104
+ } = usePopover();
105
+ const validChildren = getValidChildren(children);
106
+ const [customPopoverCloseButton, ...cloneChildren] = findChildren(
107
+ validChildren,
108
+ PopoverCloseButton
109
+ );
110
+ const css = {
111
+ position: "relative",
112
+ w: "100%",
113
+ display: "flex",
114
+ flexDirection: "column",
115
+ outline: 0,
116
+ ...omitObject(__css != null ? __css : styles.container, ["zIndex"])
117
+ };
118
+ w = (_b = w != null ? w : width) != null ? _b : (_a = styles.container.w) != null ? _a : styles.container.width;
119
+ minW = (_d = minW != null ? minW : minWidth) != null ? _d : (_c = styles.container.minW) != null ? _c : styles.container.minWidth;
120
+ zIndex = zIndex != null ? zIndex : styles.container.zIndex;
121
+ return /* @__PURE__ */ jsx2(
122
+ ui.div,
123
+ {
124
+ ...getPopperProps({ style: { visibility: isOpen ? "visible" : "hidden" } }),
125
+ className: "ui-popover",
126
+ w,
127
+ minW,
128
+ zIndex,
129
+ children: /* @__PURE__ */ jsxs(
130
+ ui.section,
131
+ {
132
+ as: motion[as],
133
+ className: cx2("ui-popover-content", className),
134
+ ...animation !== "none" ? getPopoverContentProps(animation, duration) : {},
135
+ ...getPopoverProps(rest, ref),
136
+ initial: "exit",
137
+ animate: isOpen ? "enter" : "exit",
138
+ exit: "exit",
139
+ onAnimationComplete: funcAll(onAnimationComplete, rest.onAnimationComplete),
140
+ __css: css,
141
+ children: [
142
+ customPopoverCloseButton != null ? customPopoverCloseButton : closeOnButton ? /* @__PURE__ */ jsx2(PopoverCloseButton, {}) : null,
143
+ cloneChildren
144
+ ]
145
+ }
146
+ )
147
+ }
148
+ );
149
+ }
150
+ );
151
+
152
+ // src/popover-header.tsx
153
+ import { ui as ui2, forwardRef as forwardRef3 } from "@yamada-ui/core";
154
+ import { cx as cx3 } from "@yamada-ui/utils";
155
+ import { jsx as jsx3 } from "react/jsx-runtime";
156
+ var PopoverHeader = forwardRef3(
157
+ ({ className, ...rest }, ref) => {
158
+ const { styles } = usePopover();
159
+ const css = {
160
+ display: "flex",
161
+ alignItems: "center",
162
+ justifyContent: "flex-start",
163
+ ...styles.header
164
+ };
165
+ return /* @__PURE__ */ jsx3(ui2.header, { ref, className: cx3("ui-popover-header", className), __css: css, ...rest });
166
+ }
167
+ );
168
+
169
+ // src/popover-body.tsx
170
+ import { ui as ui3, forwardRef as forwardRef4 } from "@yamada-ui/core";
171
+ import { cx as cx4 } from "@yamada-ui/utils";
172
+ import { jsx as jsx4 } from "react/jsx-runtime";
173
+ var PopoverBody = forwardRef4(({ className, ...rest }, ref) => {
174
+ const { styles } = usePopover();
175
+ const css = {
176
+ display: "flex",
177
+ flexDirection: "column",
178
+ alignItems: "flex-start",
179
+ ...styles.body
180
+ };
181
+ return /* @__PURE__ */ jsx4(ui3.main, { ref, className: cx4("ui-popover-body", className), __css: css, ...rest });
182
+ });
183
+
184
+ // src/popover-footer.tsx
185
+ import { ui as ui4, forwardRef as forwardRef5 } from "@yamada-ui/core";
186
+ import { cx as cx5 } from "@yamada-ui/utils";
187
+ import { jsx as jsx5 } from "react/jsx-runtime";
188
+ var PopoverFooter = forwardRef5(
189
+ ({ className, ...rest }, ref) => {
190
+ const { styles } = usePopover();
191
+ const css = {
192
+ display: "flex",
193
+ alignItems: "center",
194
+ justifyContent: "flex-start",
195
+ ...styles.footer
196
+ };
197
+ return /* @__PURE__ */ jsx5(ui4.footer, { ref, className: cx5("ui-popover-footer", className), __css: css, ...rest });
198
+ }
199
+ );
200
+
201
+ // src/popover-trigger.tsx
202
+ var PopoverTrigger = ({ children }) => {
203
+ const child = Children2.only(children);
204
+ const { getTriggerProps } = usePopover();
205
+ return cloneElement2(child, getTriggerProps(child.props, child.ref));
206
+ };
207
+
208
+ export {
209
+ PopoverTrigger,
210
+ PopoverAnchor,
211
+ PopoverCloseButton,
212
+ PopoverContent,
213
+ PopoverHeader,
214
+ PopoverBody,
215
+ PopoverFooter
216
+ };
@@ -0,0 +1,15 @@
1
+ export { Popover, PopoverProps, usePopover } from './popover.js';
2
+ export { PopoverTrigger } from './popover-trigger.js';
3
+ export { PopoverAnchor } from './popover-anchor.js';
4
+ export { PopoverCloseButton, PopoverCloseButtonProps } from './popover-close-button.js';
5
+ export { PopoverContent, PopoverContentProps } from './popover-content.js';
6
+ export { PopoverHeader, PopoverHeaderProps } from './popover-header.js';
7
+ export { PopoverBody, PopoverBodyProps } from './popover-body.js';
8
+ export { PopoverFooter, PopoverFooterProps } from './popover-footer.js';
9
+ import '@yamada-ui/core';
10
+ import '@yamada-ui/motion';
11
+ import '@yamada-ui/use-disclosure';
12
+ import '@yamada-ui/use-popper';
13
+ import '@yamada-ui/utils';
14
+ import 'react';
15
+ import '@yamada-ui/close-button';