@optigrit/optigrit-ui 0.0.17 → 0.0.19

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.
@@ -10,8 +10,14 @@ function calculatePopoverStyle(container, defaultPosition) {
10
10
  const rect = container.getBoundingClientRect();
11
11
  const { top, left, bottom, right, width, height } = rect;
12
12
  const [position, align = "center"] = defaultPosition.split("-");
13
- const style = {};
14
- let transform = "";
13
+ const style = {
14
+ top: "auto",
15
+ bottom: "auto",
16
+ left: "auto",
17
+ right: "auto",
18
+ transform: "",
19
+ transformOrigin: ""
20
+ };
15
21
  const round = (v) => Math.round(v);
16
22
  if (["top", "bottom"].includes(position ?? "")) {
17
23
  if (position === "top") style.bottom = `${round(window.innerHeight - top)}px`;
@@ -20,8 +26,8 @@ function calculatePopoverStyle(container, defaultPosition) {
20
26
  if (align === "end") style.right = `${round(window.innerWidth - right)}px`;
21
27
  if (align === "center") {
22
28
  style.left = `${round(left + width / 2)}px`;
23
- transform += " translateX(-50%)";
24
- style.transformOrigin = "0";
29
+ style.transform = "translateX(-50%)";
30
+ style.transformOrigin = "50% 0";
25
31
  }
26
32
  }
27
33
  if (["left", "right"].includes(position ?? "")) {
@@ -31,11 +37,10 @@ function calculatePopoverStyle(container, defaultPosition) {
31
37
  if (align === "end") style.bottom = `${round(window.innerHeight - bottom)}px`;
32
38
  if (align === "center") {
33
39
  style.top = `${round(top + height / 2)}px`;
34
- transform += " translateY(-50%)";
40
+ style.transform = "translateY(-50%)";
35
41
  style.transformOrigin = "50% 0";
36
42
  }
37
43
  }
38
- style.transform = transform.trim();
39
44
  return style;
40
45
  }
41
46
 
@@ -55,9 +60,11 @@ function Popover(props) {
55
60
  } = props;
56
61
  const popoverRef = useRef(null);
57
62
  const popoverPosition = useRef(_position);
63
+ const isFlipping = useRef(false);
58
64
  function handlePopoverIntersect() {
59
- if (!popoverRef.current) return;
65
+ if (!popoverRef.current || isFlipping.current) return;
60
66
  const popoverRect = popoverRef.current.getBoundingClientRect();
67
+ const prevPosition = popoverPosition.current;
61
68
  if (popoverRect.top < 0) {
62
69
  popoverPosition.current = popoverPosition.current.replace("top", "bottom");
63
70
  } else if (popoverRect.bottom > window.innerHeight) {
@@ -68,24 +75,33 @@ function Popover(props) {
68
75
  } else if (popoverRect.right > window.innerWidth) {
69
76
  popoverPosition.current = popoverPosition.current.replace("right", "left");
70
77
  }
71
- handleResize();
78
+ if (prevPosition !== popoverPosition.current) {
79
+ isFlipping.current = true;
80
+ handleResize();
81
+ requestAnimationFrame(() => {
82
+ isFlipping.current = false;
83
+ });
84
+ }
72
85
  }
73
86
  function showPopover() {
74
87
  if (!popoverRef.current) return;
75
88
  popoverPosition.current = _position;
89
+ handleResize();
76
90
  popoverRef.current.style.display = "flex";
77
- setTimeout(() => {
78
- if (!popoverRef.current) return;
79
- popoverRef.current.style.opacity = "1";
80
- popoverRef.current.style.scale = "1";
91
+ popoverRef.current.getBoundingClientRect();
92
+ popoverRef.current.style.opacity = "1";
93
+ popoverRef.current.style.scale = "1";
94
+ requestAnimationFrame(() => {
81
95
  handlePopoverIntersect();
82
- onOpen?.(popoverRef.current);
83
- }, 10);
96
+ if (popoverRef.current) {
97
+ onOpen?.(popoverRef.current);
98
+ }
99
+ });
84
100
  }
85
101
  function hidePopover() {
86
102
  if (!popoverRef.current) return;
87
103
  popoverRef.current.style.opacity = "0";
88
- popoverRef.current.style.scale = "0.8";
104
+ popoverRef.current.style.scale = "0.95";
89
105
  setTimeout(() => {
90
106
  if (!popoverRef.current) return;
91
107
  popoverRef.current.style.display = "none";
@@ -105,13 +121,24 @@ function Popover(props) {
105
121
  useLayoutEffect(() => {
106
122
  if (!popoverRef.current || !targetRef.current) return;
107
123
  handleResize();
108
- const resizeObserver = new ResizeObserver(handlePopoverIntersect);
124
+ const resizeObserver = new ResizeObserver(() => {
125
+ if (popoverRef.current && popoverRef.current.style.display !== "none") {
126
+ handleResize();
127
+ handlePopoverIntersect();
128
+ }
129
+ });
109
130
  resizeObserver.observe(popoverRef.current);
110
131
  resizeObserver.observe(window.document.body);
111
- window.addEventListener("scroll", handlePopoverIntersect, true);
132
+ const scrollHandler = () => {
133
+ if (popoverRef.current && popoverRef.current.style.display !== "none") {
134
+ handleResize();
135
+ handlePopoverIntersect();
136
+ }
137
+ };
138
+ window.addEventListener("scroll", scrollHandler, true);
112
139
  return () => {
113
140
  resizeObserver.disconnect();
114
- window.removeEventListener("scroll", handlePopoverIntersect, true);
141
+ window.removeEventListener("scroll", scrollHandler, true);
115
142
  };
116
143
  }, [targetRef?.current]);
117
144
  useLayoutEffect(() => {
@@ -139,11 +166,11 @@ function Popover(props) {
139
166
  ...popoverProps,
140
167
  ref: popoverRef,
141
168
  style: {
142
- display: "flex",
169
+ display: "none",
143
170
  position: "fixed",
144
- transition: "all 200ms",
171
+ transition: "opacity 200ms ease, scale 200ms ease",
145
172
  opacity: 0,
146
- scale: 0.8,
173
+ scale: 0.95,
147
174
  zIndex: 9999,
148
175
  ...popoverProps.style
149
176
  },
@@ -1,3 +1,9 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
1
7
  // src/theme/ThemeProvider.tsx
2
8
  import { createContext, useContext, useEffect, useState } from "react";
3
9
  import { jsx } from "react/jsx-runtime";
@@ -43,6 +49,7 @@ var useTheme = () => {
43
49
  };
44
50
 
45
51
  export {
52
+ __export,
46
53
  ThemeProvider,
47
54
  useTheme
48
55
  };