kelt-ui-kit-react 1.9.4 → 1.9.6
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/index.js +1039 -978
- package/package.json +1 -1
- package/src/expands/expand/expand.tsx +108 -3
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
2
|
import { Icon } from "../../icon/Icon";
|
|
3
3
|
import { ExpandInterface } from "./expand.interface";
|
|
4
4
|
|
|
@@ -17,6 +17,8 @@ export const Expand = ({
|
|
|
17
17
|
onChange,
|
|
18
18
|
}: ExpandProps): JSX.Element => {
|
|
19
19
|
const [active, setActive] = useState<boolean>(false);
|
|
20
|
+
const expandRef = useRef<HTMLDivElement>(null);
|
|
21
|
+
|
|
20
22
|
useEffect(() => {
|
|
21
23
|
if (item && currentExpand) {
|
|
22
24
|
if (currentExpand.id === item.id) {
|
|
@@ -29,7 +31,108 @@ export const Expand = ({
|
|
|
29
31
|
} else {
|
|
30
32
|
setActive(false);
|
|
31
33
|
}
|
|
32
|
-
}, [currentExpand, setActive, item]);
|
|
34
|
+
}, [currentExpand, setActive, item, active]);
|
|
35
|
+
|
|
36
|
+
const scrollToHeader = useCallback(() => {
|
|
37
|
+
const expandElement = expandRef.current;
|
|
38
|
+
if (!expandElement) return;
|
|
39
|
+
|
|
40
|
+
let scrollParent: HTMLElement | null = expandElement.parentElement;
|
|
41
|
+
while (scrollParent) {
|
|
42
|
+
const style = window.getComputedStyle(scrollParent);
|
|
43
|
+
const isScrollable =
|
|
44
|
+
/(auto|scroll)/.test(style.overflow + style.overflowY) &&
|
|
45
|
+
scrollParent.scrollHeight > scrollParent.clientHeight;
|
|
46
|
+
|
|
47
|
+
if (isScrollable) {
|
|
48
|
+
const parentRect = scrollParent.getBoundingClientRect();
|
|
49
|
+
const targetRect = expandElement.getBoundingClientRect();
|
|
50
|
+
const offsetTop =
|
|
51
|
+
targetRect.top - parentRect.top + scrollParent.scrollTop;
|
|
52
|
+
|
|
53
|
+
scrollParent.scrollTo({
|
|
54
|
+
top: offsetTop,
|
|
55
|
+
behavior: "smooth",
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
scrollParent = scrollParent.parentElement;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const header =
|
|
63
|
+
expandElement.querySelector<HTMLElement>(".expand-header") ||
|
|
64
|
+
expandElement;
|
|
65
|
+
header.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (active) {
|
|
70
|
+
const timer1 = setTimeout(() => {
|
|
71
|
+
scrollToHeader();
|
|
72
|
+
}, 100);
|
|
73
|
+
const timer2 = setTimeout(() => {
|
|
74
|
+
scrollToHeader();
|
|
75
|
+
}, 350);
|
|
76
|
+
return () => {
|
|
77
|
+
clearTimeout(timer1);
|
|
78
|
+
clearTimeout(timer2);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}, [active, scrollToHeader]);
|
|
82
|
+
|
|
83
|
+
const handleFocusCapture = useCallback(
|
|
84
|
+
(e: React.FocusEvent<HTMLDivElement>) => {
|
|
85
|
+
const target = e.target as HTMLElement;
|
|
86
|
+
if (
|
|
87
|
+
active &&
|
|
88
|
+
(target.tagName === "INPUT" ||
|
|
89
|
+
target.tagName === "TEXTAREA" ||
|
|
90
|
+
target.tagName === "SELECT")
|
|
91
|
+
) {
|
|
92
|
+
const expandElement = expandRef.current;
|
|
93
|
+
if (!expandElement) return;
|
|
94
|
+
|
|
95
|
+
const performScroll = () => {
|
|
96
|
+
let scrollParent: HTMLElement | null = expandElement.parentElement;
|
|
97
|
+
while (scrollParent) {
|
|
98
|
+
const style = window.getComputedStyle(scrollParent);
|
|
99
|
+
const isScrollable =
|
|
100
|
+
/(auto|scroll)/.test(style.overflow + style.overflowY) &&
|
|
101
|
+
scrollParent.scrollHeight > scrollParent.clientHeight;
|
|
102
|
+
|
|
103
|
+
if (isScrollable) {
|
|
104
|
+
const parentRect = scrollParent.getBoundingClientRect();
|
|
105
|
+
const headerRect = expandElement.getBoundingClientRect();
|
|
106
|
+
const headerOffsetTop =
|
|
107
|
+
headerRect.top - parentRect.top + scrollParent.scrollTop;
|
|
108
|
+
|
|
109
|
+
// Si on a déjà scrollé vers ou au-delà du header, on ne remonte JAMAIS au header
|
|
110
|
+
if (scrollParent.scrollTop >= headerOffsetTop - 10) {
|
|
111
|
+
const targetRect = target.getBoundingClientRect();
|
|
112
|
+
if (targetRect.bottom > parentRect.bottom) {
|
|
113
|
+
target.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Si on n'a pas encore scrollé jusqu'au header, on y va
|
|
119
|
+
scrollParent.scrollTo({
|
|
120
|
+
top: headerOffsetTop,
|
|
121
|
+
behavior: "smooth",
|
|
122
|
+
});
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
scrollParent = scrollParent.parentElement;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
performScroll();
|
|
130
|
+
// Répéter après l'ouverture du clavier virtuel (spécifique tablettes / mobiles Android)
|
|
131
|
+
setTimeout(performScroll, 300);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
[active],
|
|
135
|
+
);
|
|
33
136
|
|
|
34
137
|
const handleChange = useCallback(
|
|
35
138
|
(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
|
|
@@ -38,11 +141,13 @@ export const Expand = ({
|
|
|
38
141
|
onChange(item);
|
|
39
142
|
}
|
|
40
143
|
},
|
|
41
|
-
[onChange, item]
|
|
144
|
+
[onChange, item],
|
|
42
145
|
);
|
|
43
146
|
return (
|
|
44
147
|
<div
|
|
148
|
+
ref={expandRef}
|
|
45
149
|
onClick={handleChange}
|
|
150
|
+
onFocusCapture={handleFocusCapture}
|
|
46
151
|
className={`expand ${active ? "expand--active" : ""} ${className ?? ""}`}
|
|
47
152
|
>
|
|
48
153
|
<div className={`expand-header`}>
|