@blockle/blocks-react 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/overlay/Dialog/Dialog.cjs +4 -3
- package/dist/components/overlay/Dialog/Dialog.js +4 -3
- package/dist/components/overlay/Dialog/Dialog.stories.d.ts +1 -0
- package/dist/components/overlay/Popover/Popover.cjs +58 -64
- package/dist/components/overlay/Popover/Popover.d.ts +3 -2
- package/dist/components/overlay/Popover/Popover.js +59 -65
- package/dist/components/overlay/Popover/Popover.stories.d.ts +1 -0
- package/dist/components/overlay/Popover/Popover.test.d.ts +1 -0
- package/dist/components/overlay/Popover/popover-utils.cjs +23 -28
- package/dist/components/overlay/Popover/popover-utils.d.ts +1 -1
- package/dist/components/overlay/Popover/popover-utils.js +23 -28
- package/dist/components/overlay/Tooltip/Tooltip.cjs +4 -4
- package/dist/components/overlay/Tooltip/Tooltip.d.ts +2 -2
- package/dist/components/overlay/Tooltip/Tooltip.js +4 -4
- package/dist/hooks/useRestoreFocus/useRestoreFocus.test.d.ts +1 -0
- package/package.json +3 -3
- package/dist/hooks/useLayer/useLayer.cjs +0 -27
- package/dist/hooks/useLayer/useLayer.js +0 -27
- package/dist/hooks/useVisibilityState/useVisibilityState.cjs +0 -17
- package/dist/hooks/useVisibilityState/useVisibilityState.js +0 -17
|
@@ -20,11 +20,11 @@ const Dialog = ({
|
|
|
20
20
|
size,
|
|
21
21
|
...restProps
|
|
22
22
|
}) => {
|
|
23
|
+
const dialogRef = react.useRef(null);
|
|
23
24
|
const dialogClassName = useComponentStyles.useComponentStyles("dialog", {
|
|
24
25
|
dialog: true,
|
|
25
26
|
variants: { size }
|
|
26
27
|
});
|
|
27
|
-
const dialogRef = react.useRef(null);
|
|
28
28
|
const [enabled, setEnabled] = react.useState(true);
|
|
29
29
|
const [visible, setVisible] = react.useState(open);
|
|
30
30
|
useRestoreFocus.useRestoreFocus(open);
|
|
@@ -52,7 +52,9 @@ const Dialog = ({
|
|
|
52
52
|
if (!blocksCore.hasAnimationDuration(dialogRef.current)) {
|
|
53
53
|
setVisible(false);
|
|
54
54
|
}
|
|
55
|
-
(_a = dialogRef.current) == null ? void 0 : _a.close
|
|
55
|
+
if ((_a = dialogRef.current) == null ? void 0 : _a.close) {
|
|
56
|
+
dialogRef.current.close();
|
|
57
|
+
}
|
|
56
58
|
}
|
|
57
59
|
}, [open, visible]);
|
|
58
60
|
const onAnimationEnd = react.useCallback(() => {
|
|
@@ -68,7 +70,6 @@ const Dialog = ({
|
|
|
68
70
|
"dialog",
|
|
69
71
|
{
|
|
70
72
|
ref: dialogRef,
|
|
71
|
-
"aria-modal": "true",
|
|
72
73
|
open: dataOpen,
|
|
73
74
|
className: blocksCore.classnames(dialog_css.dialog, dialogClassName, className),
|
|
74
75
|
onAnimationEnd,
|
|
@@ -18,11 +18,11 @@ const Dialog = ({
|
|
|
18
18
|
size,
|
|
19
19
|
...restProps
|
|
20
20
|
}) => {
|
|
21
|
+
const dialogRef = useRef(null);
|
|
21
22
|
const dialogClassName = useComponentStyles("dialog", {
|
|
22
23
|
dialog: true,
|
|
23
24
|
variants: { size }
|
|
24
25
|
});
|
|
25
|
-
const dialogRef = useRef(null);
|
|
26
26
|
const [enabled, setEnabled] = useState(true);
|
|
27
27
|
const [visible, setVisible] = useState(open);
|
|
28
28
|
useRestoreFocus(open);
|
|
@@ -50,7 +50,9 @@ const Dialog = ({
|
|
|
50
50
|
if (!hasAnimationDuration(dialogRef.current)) {
|
|
51
51
|
setVisible(false);
|
|
52
52
|
}
|
|
53
|
-
(_a = dialogRef.current) == null ? void 0 : _a.close
|
|
53
|
+
if ((_a = dialogRef.current) == null ? void 0 : _a.close) {
|
|
54
|
+
dialogRef.current.close();
|
|
55
|
+
}
|
|
54
56
|
}
|
|
55
57
|
}, [open, visible]);
|
|
56
58
|
const onAnimationEnd = useCallback(() => {
|
|
@@ -66,7 +68,6 @@ const Dialog = ({
|
|
|
66
68
|
"dialog",
|
|
67
69
|
{
|
|
68
70
|
ref: dialogRef,
|
|
69
|
-
"aria-modal": "true",
|
|
70
71
|
open: dataOpen,
|
|
71
72
|
className: classnames(dialog, dialogClassName, className),
|
|
72
73
|
onAnimationEnd,
|
|
@@ -3,5 +3,6 @@ import { DialogProps } from './Dialog';
|
|
|
3
3
|
declare const _default: Meta;
|
|
4
4
|
export default _default;
|
|
5
5
|
export declare const Default: StoryObj<DialogProps>;
|
|
6
|
+
export declare const Nested: StoryObj<DialogProps>;
|
|
6
7
|
export declare const WithAriaMarkup: StoryObj<DialogProps>;
|
|
7
8
|
export declare const Play: StoryObj<DialogProps>;
|
|
@@ -6,107 +6,101 @@ const blocksCore = require("@blockle/blocks-core");
|
|
|
6
6
|
const react = require("react");
|
|
7
7
|
const useClickOutside = require("../../../hooks/useClickOutside/useClickOutside.cjs");
|
|
8
8
|
const popoverUtils = require("./popover-utils.cjs");
|
|
9
|
-
const useLayer = require("../../../hooks/useLayer/useLayer.cjs");
|
|
10
|
-
const useVisibilityState = require("../../../hooks/useVisibilityState/useVisibilityState.cjs");
|
|
11
9
|
const useComponentStyles = require("../../../hooks/useComponentStyles/useComponentStyles.cjs");
|
|
12
|
-
const useIsomorphicLayoutEffect = require("../../../hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.cjs");
|
|
13
10
|
const useKeyboard = require("../../../hooks/useKeyboard/useKeyboard.cjs");
|
|
14
|
-
const
|
|
11
|
+
const useIsomorphicLayoutEffect = require("../../../hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.cjs");
|
|
15
12
|
const Box = require("../../layout/Box/Box.cjs");
|
|
16
13
|
const Popover = ({
|
|
17
|
-
align = "bottom",
|
|
18
14
|
anchorElement,
|
|
19
15
|
children,
|
|
20
16
|
className,
|
|
21
17
|
onRequestClose,
|
|
22
18
|
open,
|
|
23
|
-
|
|
19
|
+
position = "top",
|
|
20
|
+
sticky,
|
|
24
21
|
style,
|
|
25
22
|
...restProps
|
|
26
23
|
}) => {
|
|
27
|
-
const layer = useLayer.useLayer();
|
|
28
|
-
const [visible, hide] = useVisibilityState.useVisibilityState(open);
|
|
29
|
-
const [position, setPosition] = react.useState({ x: 0, y: 0 });
|
|
30
24
|
const popoverRef = react.useRef(null);
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
const [popoverPosition, setPopoverPosition] = react.useState({ x: 0, y: 0 });
|
|
26
|
+
const [visible, setVisible] = react.useState(open);
|
|
27
|
+
const popoverClassName = useComponentStyles.useComponentStyles("popover", { base: true }, false);
|
|
28
|
+
useKeyboard.useKeyboard("Escape", onRequestClose, { enabled: open });
|
|
29
|
+
useClickOutside.useClickOutside(popoverRef, onRequestClose, { enabled: open });
|
|
30
|
+
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
31
|
+
var _a;
|
|
32
|
+
const element = popoverRef.current;
|
|
33
|
+
if (open && visible) {
|
|
34
|
+
if (!element || typeof element.showPopover !== "function") {
|
|
35
|
+
console.warn(
|
|
36
|
+
"Popover: showPopover method is not available on the element."
|
|
37
|
+
);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
element.showPopover();
|
|
41
|
+
const [x, y] = popoverUtils.getPopoverPosition(position, anchorElement, popoverRef);
|
|
42
|
+
setPopoverPosition({ x, y });
|
|
43
|
+
} else if (open) {
|
|
44
|
+
setVisible(true);
|
|
45
|
+
} else {
|
|
46
|
+
if (!blocksCore.hasAnimationDuration(popoverRef.current)) {
|
|
47
|
+
setVisible(false);
|
|
48
|
+
}
|
|
49
|
+
(_a = popoverRef.current) == null ? void 0 : _a.hidePopover();
|
|
39
50
|
}
|
|
40
|
-
|
|
41
|
-
setPosition({ x: position2[0], y: position2[1] });
|
|
42
|
-
}, [align, anchorElement, visible]);
|
|
51
|
+
}, [open, visible]);
|
|
43
52
|
react.useEffect(() => {
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function handleResize() {
|
|
48
|
-
const position2 = popoverUtils.getPopoverPosition(align, anchorElement, popoverRef);
|
|
49
|
-
setPosition({ x: position2[0], y: position2[1] });
|
|
53
|
+
if (open) {
|
|
54
|
+
const [x, y] = popoverUtils.getPopoverPosition(position, anchorElement, popoverRef);
|
|
55
|
+
setPopoverPosition({ x, y });
|
|
50
56
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
window.removeEventListener("resize", handleResize);
|
|
55
|
-
window.removeEventListener("scroll", handleResize);
|
|
56
|
-
};
|
|
57
|
-
}, [align, anchorElement, open, repositionOnScroll]);
|
|
58
|
-
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
59
|
-
var _a;
|
|
60
|
-
if (!open) {
|
|
61
|
-
(_a = popoverRef.current) == null ? void 0 : _a.removeAttribute("data-open");
|
|
57
|
+
}, [open, anchorElement, position]);
|
|
58
|
+
react.useEffect(() => {
|
|
59
|
+
if (!open || !sticky) {
|
|
62
60
|
return;
|
|
63
61
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(
|
|
62
|
+
function updatePopoverPosition() {
|
|
63
|
+
const [x, y] = popoverUtils.getPopoverPosition(position, anchorElement, popoverRef);
|
|
64
|
+
setPopoverPosition((prev) => {
|
|
65
|
+
if (prev.x === x && prev.y === y) {
|
|
66
|
+
return prev;
|
|
67
|
+
}
|
|
68
|
+
return { x, y };
|
|
68
69
|
});
|
|
69
|
-
}
|
|
70
|
+
}
|
|
71
|
+
window.addEventListener("resize", updatePopoverPosition);
|
|
72
|
+
window.addEventListener("scroll", updatePopoverPosition);
|
|
70
73
|
return () => {
|
|
71
|
-
|
|
74
|
+
window.removeEventListener("resize", updatePopoverPosition);
|
|
75
|
+
window.removeEventListener("scroll", updatePopoverPosition);
|
|
72
76
|
};
|
|
73
|
-
}, [open,
|
|
77
|
+
}, [position, anchorElement, open, sticky]);
|
|
74
78
|
const onAnimationEnd = react.useCallback(() => {
|
|
75
79
|
if (!open) {
|
|
76
|
-
|
|
80
|
+
setVisible(false);
|
|
77
81
|
}
|
|
78
|
-
}, [
|
|
79
|
-
react.useEffect(() => {
|
|
80
|
-
if (open) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (!blocksCore.hasAnimationDuration(popoverRef.current)) {
|
|
84
|
-
hide();
|
|
85
|
-
}
|
|
86
|
-
}, [hide, open]);
|
|
87
|
-
useKeyboard.useKeyboard("Escape", onRequestClose, { enabled: visible });
|
|
88
|
-
useClickOutside.useClickOutside(popoverRef, onRequestClose, { enabled: visible });
|
|
82
|
+
}, [open]);
|
|
89
83
|
if (!visible) {
|
|
90
84
|
return null;
|
|
91
85
|
}
|
|
92
|
-
|
|
93
|
-
return /* @__PURE__ */ jsxRuntime.jsx(Portal.Portal, { container: layer(), children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
86
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
94
87
|
Box.Box,
|
|
95
88
|
{
|
|
96
89
|
ref: popoverRef,
|
|
97
|
-
"data-open":
|
|
90
|
+
"data-open": open ? "" : void 0,
|
|
91
|
+
popover: "manual",
|
|
92
|
+
className: blocksCore.classnames(popoverClassName, className),
|
|
93
|
+
position: "absolute",
|
|
98
94
|
onAnimationEnd,
|
|
99
95
|
onTransitionEnd: onAnimationEnd,
|
|
100
|
-
className: blocksCore.classnames(containerClassName, className),
|
|
101
|
-
position: "absolute",
|
|
102
96
|
style: {
|
|
103
97
|
...style,
|
|
104
|
-
left:
|
|
105
|
-
top:
|
|
98
|
+
left: popoverPosition.x,
|
|
99
|
+
top: popoverPosition.y
|
|
106
100
|
},
|
|
107
101
|
...restProps,
|
|
108
102
|
children
|
|
109
103
|
}
|
|
110
|
-
)
|
|
104
|
+
);
|
|
111
105
|
};
|
|
112
106
|
exports.Popover = Popover;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { HTMLElementProps } from '@blockle/blocks-core';
|
|
2
|
+
export type PopoverAlign = 'top' | 'bottom' | 'left' | 'right';
|
|
2
3
|
export type PopoverProps = {
|
|
3
|
-
align?: 'top' | 'bottom' | 'left' | 'right';
|
|
4
4
|
anchorElement: React.RefObject<HTMLElement | null>;
|
|
5
5
|
children: React.ReactNode;
|
|
6
6
|
className?: string;
|
|
7
7
|
onRequestClose: () => void;
|
|
8
8
|
open: boolean;
|
|
9
|
-
|
|
9
|
+
position?: PopoverAlign;
|
|
10
|
+
sticky?: boolean;
|
|
10
11
|
style?: React.CSSProperties;
|
|
11
12
|
} & HTMLElementProps<HTMLDivElement>;
|
|
12
13
|
export declare const Popover: React.FC<PopoverProps>;
|
|
@@ -1,111 +1,105 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
import { hasAnimationDuration, classnames } from "@blockle/blocks-core";
|
|
4
|
-
import {
|
|
4
|
+
import { useRef, useState, useEffect, useCallback } from "react";
|
|
5
5
|
import { useClickOutside } from "../../../hooks/useClickOutside/useClickOutside.js";
|
|
6
6
|
import { getPopoverPosition } from "./popover-utils.js";
|
|
7
|
-
import { useLayer } from "../../../hooks/useLayer/useLayer.js";
|
|
8
|
-
import { useVisibilityState } from "../../../hooks/useVisibilityState/useVisibilityState.js";
|
|
9
7
|
import { useComponentStyles } from "../../../hooks/useComponentStyles/useComponentStyles.js";
|
|
10
|
-
import { useIsomorphicLayoutEffect } from "../../../hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.js";
|
|
11
8
|
import { useKeyboard } from "../../../hooks/useKeyboard/useKeyboard.js";
|
|
12
|
-
import {
|
|
9
|
+
import { useIsomorphicLayoutEffect } from "../../../hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.js";
|
|
13
10
|
import { Box } from "../../layout/Box/Box.js";
|
|
14
11
|
const Popover = ({
|
|
15
|
-
align = "bottom",
|
|
16
12
|
anchorElement,
|
|
17
13
|
children,
|
|
18
14
|
className,
|
|
19
15
|
onRequestClose,
|
|
20
16
|
open,
|
|
21
|
-
|
|
17
|
+
position = "top",
|
|
18
|
+
sticky,
|
|
22
19
|
style,
|
|
23
20
|
...restProps
|
|
24
21
|
}) => {
|
|
25
|
-
const layer = useLayer();
|
|
26
|
-
const [visible, hide] = useVisibilityState(open);
|
|
27
|
-
const [position, setPosition] = useState({ x: 0, y: 0 });
|
|
28
22
|
const popoverRef = useRef(null);
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
23
|
+
const [popoverPosition, setPopoverPosition] = useState({ x: 0, y: 0 });
|
|
24
|
+
const [visible, setVisible] = useState(open);
|
|
25
|
+
const popoverClassName = useComponentStyles("popover", { base: true }, false);
|
|
26
|
+
useKeyboard("Escape", onRequestClose, { enabled: open });
|
|
27
|
+
useClickOutside(popoverRef, onRequestClose, { enabled: open });
|
|
28
|
+
useIsomorphicLayoutEffect(() => {
|
|
29
|
+
var _a;
|
|
30
|
+
const element = popoverRef.current;
|
|
31
|
+
if (open && visible) {
|
|
32
|
+
if (!element || typeof element.showPopover !== "function") {
|
|
33
|
+
console.warn(
|
|
34
|
+
"Popover: showPopover method is not available on the element."
|
|
35
|
+
);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
element.showPopover();
|
|
39
|
+
const [x, y] = getPopoverPosition(position, anchorElement, popoverRef);
|
|
40
|
+
setPopoverPosition({ x, y });
|
|
41
|
+
} else if (open) {
|
|
42
|
+
setVisible(true);
|
|
43
|
+
} else {
|
|
44
|
+
if (!hasAnimationDuration(popoverRef.current)) {
|
|
45
|
+
setVisible(false);
|
|
46
|
+
}
|
|
47
|
+
(_a = popoverRef.current) == null ? void 0 : _a.hidePopover();
|
|
37
48
|
}
|
|
38
|
-
|
|
39
|
-
setPosition({ x: position2[0], y: position2[1] });
|
|
40
|
-
}, [align, anchorElement, visible]);
|
|
49
|
+
}, [open, visible]);
|
|
41
50
|
useEffect(() => {
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function handleResize() {
|
|
46
|
-
const position2 = getPopoverPosition(align, anchorElement, popoverRef);
|
|
47
|
-
setPosition({ x: position2[0], y: position2[1] });
|
|
51
|
+
if (open) {
|
|
52
|
+
const [x, y] = getPopoverPosition(position, anchorElement, popoverRef);
|
|
53
|
+
setPopoverPosition({ x, y });
|
|
48
54
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
window.removeEventListener("resize", handleResize);
|
|
53
|
-
window.removeEventListener("scroll", handleResize);
|
|
54
|
-
};
|
|
55
|
-
}, [align, anchorElement, open, repositionOnScroll]);
|
|
56
|
-
useIsomorphicLayoutEffect(() => {
|
|
57
|
-
var _a;
|
|
58
|
-
if (!open) {
|
|
59
|
-
(_a = popoverRef.current) == null ? void 0 : _a.removeAttribute("data-open");
|
|
55
|
+
}, [open, anchorElement, position]);
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!open || !sticky) {
|
|
60
58
|
return;
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
(
|
|
60
|
+
function updatePopoverPosition() {
|
|
61
|
+
const [x, y] = getPopoverPosition(position, anchorElement, popoverRef);
|
|
62
|
+
setPopoverPosition((prev) => {
|
|
63
|
+
if (prev.x === x && prev.y === y) {
|
|
64
|
+
return prev;
|
|
65
|
+
}
|
|
66
|
+
return { x, y };
|
|
66
67
|
});
|
|
67
|
-
}
|
|
68
|
+
}
|
|
69
|
+
window.addEventListener("resize", updatePopoverPosition);
|
|
70
|
+
window.addEventListener("scroll", updatePopoverPosition);
|
|
68
71
|
return () => {
|
|
69
|
-
|
|
72
|
+
window.removeEventListener("resize", updatePopoverPosition);
|
|
73
|
+
window.removeEventListener("scroll", updatePopoverPosition);
|
|
70
74
|
};
|
|
71
|
-
}, [open,
|
|
75
|
+
}, [position, anchorElement, open, sticky]);
|
|
72
76
|
const onAnimationEnd = useCallback(() => {
|
|
73
77
|
if (!open) {
|
|
74
|
-
|
|
78
|
+
setVisible(false);
|
|
75
79
|
}
|
|
76
|
-
}, [
|
|
77
|
-
useEffect(() => {
|
|
78
|
-
if (open) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (!hasAnimationDuration(popoverRef.current)) {
|
|
82
|
-
hide();
|
|
83
|
-
}
|
|
84
|
-
}, [hide, open]);
|
|
85
|
-
useKeyboard("Escape", onRequestClose, { enabled: visible });
|
|
86
|
-
useClickOutside(popoverRef, onRequestClose, { enabled: visible });
|
|
80
|
+
}, [open]);
|
|
87
81
|
if (!visible) {
|
|
88
82
|
return null;
|
|
89
83
|
}
|
|
90
|
-
|
|
91
|
-
return /* @__PURE__ */ jsx(Portal, { container: layer(), children: /* @__PURE__ */ jsx(
|
|
84
|
+
return /* @__PURE__ */ jsx(
|
|
92
85
|
Box,
|
|
93
86
|
{
|
|
94
87
|
ref: popoverRef,
|
|
95
|
-
"data-open":
|
|
88
|
+
"data-open": open ? "" : void 0,
|
|
89
|
+
popover: "manual",
|
|
90
|
+
className: classnames(popoverClassName, className),
|
|
91
|
+
position: "absolute",
|
|
96
92
|
onAnimationEnd,
|
|
97
93
|
onTransitionEnd: onAnimationEnd,
|
|
98
|
-
className: classnames(containerClassName, className),
|
|
99
|
-
position: "absolute",
|
|
100
94
|
style: {
|
|
101
95
|
...style,
|
|
102
|
-
left:
|
|
103
|
-
top:
|
|
96
|
+
left: popoverPosition.x,
|
|
97
|
+
top: popoverPosition.y
|
|
104
98
|
},
|
|
105
99
|
...restProps,
|
|
106
100
|
children
|
|
107
101
|
}
|
|
108
|
-
)
|
|
102
|
+
);
|
|
109
103
|
};
|
|
110
104
|
export {
|
|
111
105
|
Popover
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
3
|
+
const blocksCore = require("@blockle/blocks-core");
|
|
4
|
+
function getPopoverPosition(position, anchorRef, popoverRef) {
|
|
4
5
|
if (!anchorRef.current || !popoverRef.current) {
|
|
5
6
|
return [0, 0];
|
|
6
7
|
}
|
|
7
|
-
popoverRef.current.style.transform = "none";
|
|
8
|
-
popoverRef.current.style.transitionDuration = "0s";
|
|
9
8
|
const anchorRect = anchorRef.current.getBoundingClientRect();
|
|
10
9
|
const popoverRect = popoverRef.current.getBoundingClientRect();
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const marginTop =
|
|
10
|
+
const popoverStyles = getComputedStyle(popoverRef.current);
|
|
11
|
+
const [popoverWidth, popoverHeight] = blocksCore.getOriginalElementSize(
|
|
12
|
+
popoverStyles,
|
|
13
|
+
popoverRect.width,
|
|
14
|
+
popoverRect.height
|
|
15
|
+
);
|
|
16
|
+
const marginTop = blocksCore.cssValueToNumber(
|
|
18
17
|
popoverStyles.getPropertyValue("margin-top")
|
|
19
18
|
);
|
|
20
|
-
const marginRight =
|
|
19
|
+
const marginRight = blocksCore.cssValueToNumber(
|
|
21
20
|
popoverStyles.getPropertyValue("margin-right")
|
|
22
21
|
);
|
|
23
|
-
const marginBottom =
|
|
22
|
+
const marginBottom = blocksCore.cssValueToNumber(
|
|
24
23
|
popoverStyles.getPropertyValue("margin-bottom")
|
|
25
24
|
);
|
|
26
|
-
const marginLeft =
|
|
25
|
+
const marginLeft = blocksCore.cssValueToNumber(
|
|
27
26
|
popoverStyles.getPropertyValue("margin-left")
|
|
28
27
|
);
|
|
29
28
|
const marginY = marginTop + marginBottom;
|
|
@@ -34,28 +33,24 @@ function getPopoverPosition(align, anchorRef, popoverRef) {
|
|
|
34
33
|
const docScrollLeft = document.documentElement.scrollLeft;
|
|
35
34
|
const anchorLeft = anchorRect.left + docScrollLeft;
|
|
36
35
|
const anchorTop = anchorRect.top + docScrollTop;
|
|
37
|
-
const topPosition = anchorRect.top - (
|
|
38
|
-
const rightPosition = anchorRect.left + anchorRect.width +
|
|
39
|
-
const bottomPosition = anchorRect.top + anchorRect.height +
|
|
40
|
-
const leftPosition = anchorRect.left -
|
|
41
|
-
const offsetX = anchorLeft - marginLeft - (
|
|
42
|
-
const offsetY = anchorTop - marginTop - (
|
|
43
|
-
|
|
44
|
-
popoverRef.current.style.transitionDuration = "";
|
|
45
|
-
popoverRef.current.style.top = top;
|
|
46
|
-
popoverRef.current.style.left = left;
|
|
47
|
-
switch (align) {
|
|
36
|
+
const topPosition = anchorRect.top - (popoverHeight + marginTop);
|
|
37
|
+
const rightPosition = anchorRect.left + anchorRect.width + popoverWidth;
|
|
38
|
+
const bottomPosition = anchorRect.top + anchorRect.height + popoverHeight;
|
|
39
|
+
const leftPosition = anchorRect.left - popoverWidth;
|
|
40
|
+
const offsetX = anchorLeft - marginLeft - (popoverWidth - anchorRect.width) / 2;
|
|
41
|
+
const offsetY = anchorTop - marginTop - (popoverHeight - anchorRect.height) / 2;
|
|
42
|
+
switch (position) {
|
|
48
43
|
case "top": {
|
|
49
|
-
return topPosition > 0 ? [offsetX, anchorTop -
|
|
44
|
+
return topPosition > 0 ? [offsetX, anchorTop - popoverHeight - marginY] : [offsetX, anchorTop + anchorRect.height];
|
|
50
45
|
}
|
|
51
46
|
case "bottom": {
|
|
52
|
-
return bottomPosition < docHeight || topPosition < 0 ? [offsetX, anchorTop + anchorRect.height] : [offsetX, anchorTop -
|
|
47
|
+
return bottomPosition < docHeight || topPosition < 0 ? [offsetX, anchorTop + anchorRect.height] : [offsetX, anchorTop - popoverHeight - marginY];
|
|
53
48
|
}
|
|
54
49
|
case "left": {
|
|
55
|
-
return leftPosition > docWidth || leftPosition > 0 ? [anchorLeft -
|
|
50
|
+
return leftPosition > docWidth || leftPosition > 0 ? [anchorLeft - popoverWidth - marginX, offsetY] : [anchorLeft + anchorRect.width, offsetY];
|
|
56
51
|
}
|
|
57
52
|
case "right": {
|
|
58
|
-
return rightPosition < docWidth || leftPosition < 0 ? [anchorLeft + anchorRect.width, offsetY] : [anchorLeft -
|
|
53
|
+
return rightPosition < docWidth || leftPosition < 0 ? [anchorLeft + anchorRect.width, offsetY] : [anchorLeft - popoverWidth - marginX, offsetY];
|
|
59
54
|
}
|
|
60
55
|
}
|
|
61
56
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export type PopoverPositions = [x: number, y: number];
|
|
2
|
-
export declare function getPopoverPosition(
|
|
2
|
+
export declare function getPopoverPosition(position: 'top' | 'bottom' | 'left' | 'right', anchorRef: React.RefObject<HTMLElement | null>, popoverRef: React.RefObject<HTMLElement | null>): PopoverPositions;
|
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
import { getOriginalElementSize, cssValueToNumber } from "@blockle/blocks-core";
|
|
2
|
+
function getPopoverPosition(position, anchorRef, popoverRef) {
|
|
2
3
|
if (!anchorRef.current || !popoverRef.current) {
|
|
3
4
|
return [0, 0];
|
|
4
5
|
}
|
|
5
|
-
popoverRef.current.style.transform = "none";
|
|
6
|
-
popoverRef.current.style.transitionDuration = "0s";
|
|
7
6
|
const anchorRect = anchorRef.current.getBoundingClientRect();
|
|
8
7
|
const popoverRect = popoverRef.current.getBoundingClientRect();
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const marginTop =
|
|
8
|
+
const popoverStyles = getComputedStyle(popoverRef.current);
|
|
9
|
+
const [popoverWidth, popoverHeight] = getOriginalElementSize(
|
|
10
|
+
popoverStyles,
|
|
11
|
+
popoverRect.width,
|
|
12
|
+
popoverRect.height
|
|
13
|
+
);
|
|
14
|
+
const marginTop = cssValueToNumber(
|
|
16
15
|
popoverStyles.getPropertyValue("margin-top")
|
|
17
16
|
);
|
|
18
|
-
const marginRight =
|
|
17
|
+
const marginRight = cssValueToNumber(
|
|
19
18
|
popoverStyles.getPropertyValue("margin-right")
|
|
20
19
|
);
|
|
21
|
-
const marginBottom =
|
|
20
|
+
const marginBottom = cssValueToNumber(
|
|
22
21
|
popoverStyles.getPropertyValue("margin-bottom")
|
|
23
22
|
);
|
|
24
|
-
const marginLeft =
|
|
23
|
+
const marginLeft = cssValueToNumber(
|
|
25
24
|
popoverStyles.getPropertyValue("margin-left")
|
|
26
25
|
);
|
|
27
26
|
const marginY = marginTop + marginBottom;
|
|
@@ -32,28 +31,24 @@ function getPopoverPosition(align, anchorRef, popoverRef) {
|
|
|
32
31
|
const docScrollLeft = document.documentElement.scrollLeft;
|
|
33
32
|
const anchorLeft = anchorRect.left + docScrollLeft;
|
|
34
33
|
const anchorTop = anchorRect.top + docScrollTop;
|
|
35
|
-
const topPosition = anchorRect.top - (
|
|
36
|
-
const rightPosition = anchorRect.left + anchorRect.width +
|
|
37
|
-
const bottomPosition = anchorRect.top + anchorRect.height +
|
|
38
|
-
const leftPosition = anchorRect.left -
|
|
39
|
-
const offsetX = anchorLeft - marginLeft - (
|
|
40
|
-
const offsetY = anchorTop - marginTop - (
|
|
41
|
-
|
|
42
|
-
popoverRef.current.style.transitionDuration = "";
|
|
43
|
-
popoverRef.current.style.top = top;
|
|
44
|
-
popoverRef.current.style.left = left;
|
|
45
|
-
switch (align) {
|
|
34
|
+
const topPosition = anchorRect.top - (popoverHeight + marginTop);
|
|
35
|
+
const rightPosition = anchorRect.left + anchorRect.width + popoverWidth;
|
|
36
|
+
const bottomPosition = anchorRect.top + anchorRect.height + popoverHeight;
|
|
37
|
+
const leftPosition = anchorRect.left - popoverWidth;
|
|
38
|
+
const offsetX = anchorLeft - marginLeft - (popoverWidth - anchorRect.width) / 2;
|
|
39
|
+
const offsetY = anchorTop - marginTop - (popoverHeight - anchorRect.height) / 2;
|
|
40
|
+
switch (position) {
|
|
46
41
|
case "top": {
|
|
47
|
-
return topPosition > 0 ? [offsetX, anchorTop -
|
|
42
|
+
return topPosition > 0 ? [offsetX, anchorTop - popoverHeight - marginY] : [offsetX, anchorTop + anchorRect.height];
|
|
48
43
|
}
|
|
49
44
|
case "bottom": {
|
|
50
|
-
return bottomPosition < docHeight || topPosition < 0 ? [offsetX, anchorTop + anchorRect.height] : [offsetX, anchorTop -
|
|
45
|
+
return bottomPosition < docHeight || topPosition < 0 ? [offsetX, anchorTop + anchorRect.height] : [offsetX, anchorTop - popoverHeight - marginY];
|
|
51
46
|
}
|
|
52
47
|
case "left": {
|
|
53
|
-
return leftPosition > docWidth || leftPosition > 0 ? [anchorLeft -
|
|
48
|
+
return leftPosition > docWidth || leftPosition > 0 ? [anchorLeft - popoverWidth - marginX, offsetY] : [anchorLeft + anchorRect.width, offsetY];
|
|
54
49
|
}
|
|
55
50
|
case "right": {
|
|
56
|
-
return rightPosition < docWidth || leftPosition < 0 ? [anchorLeft + anchorRect.width, offsetY] : [anchorLeft -
|
|
51
|
+
return rightPosition < docWidth || leftPosition < 0 ? [anchorLeft + anchorRect.width, offsetY] : [anchorLeft - popoverWidth - marginX, offsetY];
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
}
|
|
@@ -7,9 +7,9 @@ const react = require("react");
|
|
|
7
7
|
const useComponentStyles = require("../../../hooks/useComponentStyles/useComponentStyles.cjs");
|
|
8
8
|
const Popover = require("../Popover/Popover.cjs");
|
|
9
9
|
const Tooltip = ({
|
|
10
|
-
|
|
10
|
+
position = "top",
|
|
11
11
|
children,
|
|
12
|
-
|
|
12
|
+
content,
|
|
13
13
|
colorScheme
|
|
14
14
|
}) => {
|
|
15
15
|
const id = react.useId();
|
|
@@ -63,9 +63,9 @@ const Tooltip = ({
|
|
|
63
63
|
onRequestClose: () => {
|
|
64
64
|
setOpen(false);
|
|
65
65
|
},
|
|
66
|
-
|
|
66
|
+
position,
|
|
67
67
|
className: tooltipClassName,
|
|
68
|
-
children:
|
|
68
|
+
children: content
|
|
69
69
|
}
|
|
70
70
|
)
|
|
71
71
|
] });
|
|
@@ -3,9 +3,9 @@ import { PopoverProps } from '../Popover';
|
|
|
3
3
|
type ReactElement = React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
4
4
|
type TooltipTheme = ComponentThemes['tooltip'];
|
|
5
5
|
export type TooltipProps = {
|
|
6
|
-
|
|
6
|
+
position?: PopoverProps['position'];
|
|
7
7
|
children: ReactElement;
|
|
8
|
-
|
|
8
|
+
content: React.ReactNode;
|
|
9
9
|
colorScheme?: TooltipTheme['variants']['colorScheme'];
|
|
10
10
|
};
|
|
11
11
|
export declare const Tooltip: React.FC<TooltipProps>;
|
|
@@ -5,9 +5,9 @@ import { useId, useRef, useState, useEffect, Children, isValidElement, cloneElem
|
|
|
5
5
|
import { useComponentStyles } from "../../../hooks/useComponentStyles/useComponentStyles.js";
|
|
6
6
|
import { Popover } from "../Popover/Popover.js";
|
|
7
7
|
const Tooltip = ({
|
|
8
|
-
|
|
8
|
+
position = "top",
|
|
9
9
|
children,
|
|
10
|
-
|
|
10
|
+
content,
|
|
11
11
|
colorScheme
|
|
12
12
|
}) => {
|
|
13
13
|
const id = useId();
|
|
@@ -61,9 +61,9 @@ const Tooltip = ({
|
|
|
61
61
|
onRequestClose: () => {
|
|
62
62
|
setOpen(false);
|
|
63
63
|
},
|
|
64
|
-
|
|
64
|
+
position,
|
|
65
65
|
className: tooltipClassName,
|
|
66
|
-
children:
|
|
66
|
+
children: content
|
|
67
67
|
}
|
|
68
68
|
)
|
|
69
69
|
] });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockle/blocks-react",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "React hooks and components for Blockle",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"react": "^19",
|
|
45
45
|
"react-dom": "^19",
|
|
46
|
-
"@blockle/blocks-core": "
|
|
47
|
-
"@blockle/blocks-react-slot": "
|
|
46
|
+
"@blockle/blocks-core": ">=0.x",
|
|
47
|
+
"@blockle/blocks-react-slot": ">=1.x"
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
4
|
-
const react = require("react");
|
|
5
|
-
const useLayer = () => {
|
|
6
|
-
const layerRef = react.useRef(null);
|
|
7
|
-
react.useEffect(
|
|
8
|
-
() => () => {
|
|
9
|
-
if (layerRef.current) {
|
|
10
|
-
layerRef.current.remove();
|
|
11
|
-
layerRef.current = null;
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
[]
|
|
15
|
-
);
|
|
16
|
-
function getLayer() {
|
|
17
|
-
if (!layerRef.current) {
|
|
18
|
-
const div = document.createElement("div");
|
|
19
|
-
div.dataset.layer = "blocks";
|
|
20
|
-
layerRef.current = div;
|
|
21
|
-
document.body.append(layerRef.current);
|
|
22
|
-
}
|
|
23
|
-
return layerRef.current;
|
|
24
|
-
}
|
|
25
|
-
return getLayer;
|
|
26
|
-
};
|
|
27
|
-
exports.useLayer = useLayer;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useRef, useEffect } from "react";
|
|
3
|
-
const useLayer = () => {
|
|
4
|
-
const layerRef = useRef(null);
|
|
5
|
-
useEffect(
|
|
6
|
-
() => () => {
|
|
7
|
-
if (layerRef.current) {
|
|
8
|
-
layerRef.current.remove();
|
|
9
|
-
layerRef.current = null;
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
[]
|
|
13
|
-
);
|
|
14
|
-
function getLayer() {
|
|
15
|
-
if (!layerRef.current) {
|
|
16
|
-
const div = document.createElement("div");
|
|
17
|
-
div.dataset.layer = "blocks";
|
|
18
|
-
layerRef.current = div;
|
|
19
|
-
document.body.append(layerRef.current);
|
|
20
|
-
}
|
|
21
|
-
return layerRef.current;
|
|
22
|
-
}
|
|
23
|
-
return getLayer;
|
|
24
|
-
};
|
|
25
|
-
export {
|
|
26
|
-
useLayer
|
|
27
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
4
|
-
const react = require("react");
|
|
5
|
-
const useVisibilityState = (open) => {
|
|
6
|
-
const [visible, setVisible] = react.useState(open);
|
|
7
|
-
const hide = react.useCallback(() => {
|
|
8
|
-
setVisible(false);
|
|
9
|
-
}, []);
|
|
10
|
-
react.useEffect(() => {
|
|
11
|
-
if (open) {
|
|
12
|
-
setVisible(true);
|
|
13
|
-
}
|
|
14
|
-
}, [open]);
|
|
15
|
-
return [visible, hide];
|
|
16
|
-
};
|
|
17
|
-
exports.useVisibilityState = useVisibilityState;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useState, useCallback, useEffect } from "react";
|
|
3
|
-
const useVisibilityState = (open) => {
|
|
4
|
-
const [visible, setVisible] = useState(open);
|
|
5
|
-
const hide = useCallback(() => {
|
|
6
|
-
setVisible(false);
|
|
7
|
-
}, []);
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
if (open) {
|
|
10
|
-
setVisible(true);
|
|
11
|
-
}
|
|
12
|
-
}, [open]);
|
|
13
|
-
return [visible, hide];
|
|
14
|
-
};
|
|
15
|
-
export {
|
|
16
|
-
useVisibilityState
|
|
17
|
-
};
|