-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.tsx
112 lines (101 loc) · 3.19 KB
/
decorator.tsx
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
type ComponentProps,
type ComponentType,
type CSSProperties,
type ReactNode,
useLayoutEffect,
useState,
} from 'react';
type DecoratorProps<
TDecoration extends ComponentType<any>,
TContainer extends keyof JSX.IntrinsicElements = 'div',
> = (Record<string, never> extends ComponentProps<TDecoration>
? { readonly decorationProps?: Omit<ComponentProps<TDecoration>, 'children'> }
: { readonly decorationProps: Omit<ComponentProps<TDecoration>, 'children'> }) & {
readonly children?: ReactNode;
/**
* HTML element which will wrap the `Decorator` component's `children`.
* Defaults to `div`. Properties can be passed to this element by setting
* the `containerProps` property.
*/
readonly container?: TContainer;
/**
* Style applied to the `container` when it has no content. Defaults to
* `{ display: "none" }`.
*
* Common alternatives:
*
* - `{ position: "absolute", clip: "rect(0, 0, 0, 0)", clipPath: "inset(100%)"" }`
* - `{ position: "absolute", "left: -9999px" }`
*/
readonly containerHiddenStyle?: CSSProperties;
/**
* Properties passed to the `container` element.
*/
readonly containerProps?: Omit<ComponentProps<TContainer>, 'children'>;
/**
* Decoration (wrapper) component. Properties can be passed to this component
* by setting the `decorationProps` property.
*/
readonly decoration: TDecoration;
/**
* Properties passed to the `decoration` component.
*/
readonly decorationProps?: unknown;
};
const containerHiddenStyleDefault: CSSProperties = {
display: 'none',
};
/**
* Wrap `children` using the `decoration` component, as long as the children
* render at least one DOM node.
*/
const Decorator = <TDecoration extends ComponentType<any>, TContainer extends keyof JSX.IntrinsicElements = 'div'>({
decoration,
decorationProps,
container = 'div' as TContainer,
containerHiddenStyle = containerHiddenStyleDefault,
containerProps,
children,
}: DecoratorProps<TDecoration, TContainer>): JSX.Element => {
const Decoration = decoration as unknown as ComponentType<any>;
const Container = container as unknown as ComponentType<any>;
const [element, setElement] = useState<Node | null>(null);
const [isEmpty, setIsEmpty] = useState(false);
useLayoutEffect(() => {
if (!element) {
return;
}
let isEmptyPrevious: boolean | undefined;
const update = (): void => {
const isEmptyNow = element.childNodes.length === 0;
if (isEmptyPrevious !== isEmptyNow) {
isEmptyPrevious = isEmptyNow;
setIsEmpty(isEmptyNow);
}
};
const observer = new MutationObserver(update);
observer.observe(element, { childList: true });
update();
return () => {
observer.disconnect();
};
}, [element]);
return isEmpty ? (
<Container
ref={setElement}
data-decorator="empty"
{...containerProps}
style={{ ...containerProps?.style, ...containerHiddenStyle }}
>
{children}
</Container>
) : (
<Decoration {...decorationProps}>
<Container ref={setElement} data-decorator="not-empty" {...containerProps}>
{children}
</Container>
</Decoration>
);
};
export { Decorator, type DecoratorProps };