/
transform-component.tsx
52 lines (44 loc) 路 1.25 KB
/
transform-component.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
import React, { useContext, useEffect, useRef } from "react";
import { Context } from "./transform-context";
import styles from "./transform-component.module.css";
type Props = {
children: React.ReactNode;
wrapperClass?: string;
contentClass?: string;
wrapperStyle?: React.CSSProperties;
contentStyle?: React.CSSProperties;
};
export const TransformComponent: React.FC<Props> = ({
children,
wrapperClass = "",
contentClass = "",
wrapperStyle,
contentStyle,
}: Props) => {
const { setComponents } = useContext(Context);
const wrapperRef = useRef<HTMLDivElement | null>(null);
const contentRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const wrapper = wrapperRef.current;
const content = contentRef.current;
if (wrapper !== null && content !== null && setComponents) {
setComponents(wrapper, content);
}
}, []);
return (
<div
ref={wrapperRef}
className={`react-transform-wrapper ${styles.wrapper} ${wrapperClass}`}
style={wrapperStyle}
>
<div
ref={contentRef}
className={`react-transform-component ${styles.content} ${contentClass}`}
style={contentStyle}
>
{children}
</div>
</div>
);
};
export default TransformComponent;