-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopperElementChildrenWrapper.tsx
More file actions
44 lines (36 loc) · 1.29 KB
/
PopperElementChildrenWrapper.tsx
File metadata and controls
44 lines (36 loc) · 1.29 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
39
40
41
42
43
44
import React, { Ref, useEffect } from "react";
import { Placement } from "@popperjs/core/lib/enums";
import { useEffectSkipFirst } from "@worksolutions/react-utils";
import { PopperArrowProps } from "primitives/Popper/Popper";
import Wrapper from "../../Wrapper";
import Arrow from "./Arrow";
interface PopperChildrenProps {
styles?: any;
style: CSSStyleDeclaration;
placement: Placement;
children: React.ReactNode;
hasArrow?: boolean;
arrowProps: PopperArrowProps;
triggerElement: HTMLElement | undefined;
update: () => void;
}
function PopperElementChildrenWrapper(
{ style, placement, styles, children, hasArrow, arrowProps, triggerElement, update }: PopperChildrenProps,
ref: Ref<HTMLElement>,
) {
useEffectSkipFirst(update, [update, hasArrow]);
useEffect(update, []);
useEffect(() => {
if (!triggerElement) return () => {};
const resizeObserver = new ResizeObserver(update);
resizeObserver.observe(triggerElement);
return () => resizeObserver.disconnect();
}, [triggerElement, update]);
return (
<Wrapper ref={ref} style={style} data-placement={placement} styles={styles}>
{children}
{hasArrow && <Arrow arrowProps={arrowProps} placement={placement} />}
</Wrapper>
);
}
export default React.memo(React.forwardRef(PopperElementChildrenWrapper));