-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseDetectWrappedElements.ts
More file actions
38 lines (29 loc) · 1.04 KB
/
Copy pathuseDetectWrappedElements.ts
File metadata and controls
38 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useEffect } from 'react';
import { detectWrappedElements } from './detectWrappedElements';
export const wrapChildrenClassName = 'wrap-children';
export const nextIsWrappedClassName = 'next-is-wrapped';
export const hasChildWrappedClassName = 'has-child-wrapped';
export function useDetectWrappedElements(ref: React.RefObject<HTMLElement>) {
useEffect(() => {
const rootElement = ref.current!;
function run() {
detectWrappedElements(
rootElement,
wrapChildrenClassName,
nextIsWrappedClassName,
hasChildWrappedClassName
);
}
window.addEventListener('resize', run);
run();
return () => {
window.removeEventListener('resize', run);
const children = rootElement.getElementsByClassName(nextIsWrappedClassName);
// https://stackoverflow.com/q/22270664#comment33829207_22270685
while (children.length > 0) {
children[0].classList.remove(nextIsWrappedClassName);
}
rootElement.classList.remove(hasChildWrappedClassName);
};
}, [ref]);
}