@blockle/blocks-react 1.0.4 → 1.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.
@@ -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 Portal = require("../Portal/Portal.cjs");
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
- repositionOnScroll,
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 containerClassName = useComponentStyles.useComponentStyles(
32
- "popover",
33
- { base: true },
34
- false
35
- );
36
- react.useLayoutEffect(() => {
37
- if (!visible) {
38
- return;
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
- const position2 = popoverUtils.getPopoverPosition(align, anchorElement, popoverRef);
41
- setPosition({ x: position2[0], y: position2[1] });
42
- }, [align, anchorElement, visible]);
51
+ }, [open, visible]);
43
52
  react.useEffect(() => {
44
- if (!open || !repositionOnScroll) {
45
- return;
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
- window.addEventListener("resize", handleResize);
52
- window.addEventListener("scroll", handleResize);
53
- return () => {
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
- let timer = requestAnimationFrame(() => {
65
- timer = requestAnimationFrame(() => {
66
- var _a2;
67
- (_a2 = popoverRef.current) == null ? void 0 : _a2.setAttribute("data-open", "");
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
- cancelAnimationFrame(timer);
74
+ window.removeEventListener("resize", updatePopoverPosition);
75
+ window.removeEventListener("scroll", updatePopoverPosition);
72
76
  };
73
- }, [open, visible]);
77
+ }, [position, anchorElement, open, sticky]);
74
78
  const onAnimationEnd = react.useCallback(() => {
75
79
  if (!open) {
76
- hide();
80
+ setVisible(false);
77
81
  }
78
- }, [hide, open]);
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
- const dataOpen = typeof window === "undefined" && open ? "" : void 0;
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": dataOpen,
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: position.x,
105
- top: position.y
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
- repositionOnScroll?: boolean;
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 { useState, useRef, useLayoutEffect, useEffect, useCallback } from "react";
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 { Portal } from "../Portal/Portal.js";
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
- repositionOnScroll,
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 containerClassName = useComponentStyles(
30
- "popover",
31
- { base: true },
32
- false
33
- );
34
- useLayoutEffect(() => {
35
- if (!visible) {
36
- return;
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
- const position2 = getPopoverPosition(align, anchorElement, popoverRef);
39
- setPosition({ x: position2[0], y: position2[1] });
40
- }, [align, anchorElement, visible]);
49
+ }, [open, visible]);
41
50
  useEffect(() => {
42
- if (!open || !repositionOnScroll) {
43
- return;
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
- window.addEventListener("resize", handleResize);
50
- window.addEventListener("scroll", handleResize);
51
- return () => {
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
- let timer = requestAnimationFrame(() => {
63
- timer = requestAnimationFrame(() => {
64
- var _a2;
65
- (_a2 = popoverRef.current) == null ? void 0 : _a2.setAttribute("data-open", "");
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
- cancelAnimationFrame(timer);
72
+ window.removeEventListener("resize", updatePopoverPosition);
73
+ window.removeEventListener("scroll", updatePopoverPosition);
70
74
  };
71
- }, [open, visible]);
75
+ }, [position, anchorElement, open, sticky]);
72
76
  const onAnimationEnd = useCallback(() => {
73
77
  if (!open) {
74
- hide();
78
+ setVisible(false);
75
79
  }
76
- }, [hide, open]);
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
- const dataOpen = typeof window === "undefined" && open ? "" : void 0;
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": dataOpen,
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: position.x,
103
- top: position.y
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
@@ -3,3 +3,4 @@ import { PopoverProps } from './Popover';
3
3
  declare const _default: Meta;
4
4
  export default _default;
5
5
  export declare const Default: StoryObj<PopoverProps>;
6
+ export declare const Interactive: StoryObj<PopoverProps>;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,29 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function getPopoverPosition(align, anchorRef, popoverRef) {
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
- let popoverStyles = getComputedStyle(popoverRef.current);
12
- const top = popoverStyles.getPropertyValue("top");
13
- const left = popoverStyles.getPropertyValue("left");
14
- popoverRef.current.style.left = "0";
15
- popoverRef.current.style.top = "0";
16
- popoverStyles = getComputedStyle(popoverRef.current);
17
- const marginTop = Number.parseFloat(
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 = Number.parseFloat(
19
+ const marginRight = blocksCore.cssValueToNumber(
21
20
  popoverStyles.getPropertyValue("margin-right")
22
21
  );
23
- const marginBottom = Number.parseFloat(
22
+ const marginBottom = blocksCore.cssValueToNumber(
24
23
  popoverStyles.getPropertyValue("margin-bottom")
25
24
  );
26
- const marginLeft = Number.parseFloat(
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 - (popoverRect.height + marginTop);
38
- const rightPosition = anchorRect.left + anchorRect.width + popoverRect.width;
39
- const bottomPosition = anchorRect.top + anchorRect.height + popoverRect.height;
40
- const leftPosition = anchorRect.left - popoverRect.width;
41
- const offsetX = anchorLeft - marginLeft - (popoverRect.width - anchorRect.width) / 2;
42
- const offsetY = anchorTop - marginTop - (popoverRect.height - anchorRect.height) / 2;
43
- popoverRef.current.style.transform = "";
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 - popoverRect.height - marginY] : [offsetX, anchorTop + anchorRect.height];
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 - popoverRect.height - marginY];
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 - popoverRect.width - marginX, offsetY] : [anchorLeft + anchorRect.width, offsetY];
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 - popoverRect.width - marginX, offsetY];
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(align: 'top' | 'bottom' | 'left' | 'right', anchorRef: React.RefObject<HTMLElement | null>, popoverRef: React.RefObject<HTMLElement | null>): PopoverPositions;
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
- function getPopoverPosition(align, anchorRef, popoverRef) {
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
- let popoverStyles = getComputedStyle(popoverRef.current);
10
- const top = popoverStyles.getPropertyValue("top");
11
- const left = popoverStyles.getPropertyValue("left");
12
- popoverRef.current.style.left = "0";
13
- popoverRef.current.style.top = "0";
14
- popoverStyles = getComputedStyle(popoverRef.current);
15
- const marginTop = Number.parseFloat(
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 = Number.parseFloat(
17
+ const marginRight = cssValueToNumber(
19
18
  popoverStyles.getPropertyValue("margin-right")
20
19
  );
21
- const marginBottom = Number.parseFloat(
20
+ const marginBottom = cssValueToNumber(
22
21
  popoverStyles.getPropertyValue("margin-bottom")
23
22
  );
24
- const marginLeft = Number.parseFloat(
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 - (popoverRect.height + marginTop);
36
- const rightPosition = anchorRect.left + anchorRect.width + popoverRect.width;
37
- const bottomPosition = anchorRect.top + anchorRect.height + popoverRect.height;
38
- const leftPosition = anchorRect.left - popoverRect.width;
39
- const offsetX = anchorLeft - marginLeft - (popoverRect.width - anchorRect.width) / 2;
40
- const offsetY = anchorTop - marginTop - (popoverRect.height - anchorRect.height) / 2;
41
- popoverRef.current.style.transform = "";
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 - popoverRect.height - marginY] : [offsetX, anchorTop + anchorRect.height];
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 - popoverRect.height - marginY];
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 - popoverRect.width - marginX, offsetY] : [anchorLeft + anchorRect.width, offsetY];
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 - popoverRect.width - marginX, offsetY];
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
- align = "top",
10
+ position = "top",
11
11
  children,
12
- label,
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
- align,
66
+ position,
67
67
  className: tooltipClassName,
68
- children: label
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
- align?: PopoverProps['align'];
6
+ position?: PopoverProps['position'];
7
7
  children: ReactElement;
8
- label: React.ReactNode;
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
- align = "top",
8
+ position = "top",
9
9
  children,
10
- label,
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
- align,
64
+ position,
65
65
  className: tooltipClassName,
66
- children: label
66
+ children: content
67
67
  }
68
68
  )
69
69
  ] });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockle/blocks-react",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "React hooks and components for Blockle",
5
5
  "type": "module",
6
6
  "exports": {
@@ -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
- };