2024-02-19 18:59:45 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2024-09-18 05:33:03 +08:00
|
|
|
import * as ReactDOM from 'react-dom/client';
|
2024-06-11 03:31:30 +08:00
|
|
|
import { GenericContentItemProps } from './types';
|
2024-02-19 18:59:45 +08:00
|
|
|
|
2024-06-11 03:31:30 +08:00
|
|
|
const GenericContentItem: React.FC<GenericContentItemProps> = (props) => {
|
2024-02-19 18:59:45 +08:00
|
|
|
const {
|
|
|
|
renderFunction,
|
2024-09-28 01:48:05 +08:00
|
|
|
width,
|
2024-02-19 18:59:45 +08:00
|
|
|
} = props;
|
|
|
|
const elementRef = useRef(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-18 05:33:03 +08:00
|
|
|
let rootRef: ReactDOM.Root | null;
|
2024-02-19 18:59:45 +08:00
|
|
|
if (elementRef.current && renderFunction) {
|
2024-09-18 05:33:03 +08:00
|
|
|
rootRef = renderFunction(elementRef.current);
|
2024-02-19 18:59:45 +08:00
|
|
|
}
|
2024-09-18 05:33:03 +08:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
// extensible area injected by content functions have to
|
|
|
|
// be explicitly unmounted, because plugins use a different
|
|
|
|
// instance of ReactDOM
|
|
|
|
if (rootRef) rootRef.unmount();
|
|
|
|
};
|
2024-10-22 02:11:10 +08:00
|
|
|
}, [elementRef, renderFunction]);
|
2024-02-19 18:59:45 +08:00
|
|
|
|
2024-09-28 01:48:05 +08:00
|
|
|
const style: React.CSSProperties = {
|
|
|
|
height: '100%',
|
|
|
|
overflow: 'hidden',
|
|
|
|
};
|
|
|
|
if (width) {
|
|
|
|
style.width = width;
|
|
|
|
}
|
2024-02-19 18:59:45 +08:00
|
|
|
return (
|
|
|
|
<div
|
2024-09-28 01:48:05 +08:00
|
|
|
style={style}
|
2024-02-19 18:59:45 +08:00
|
|
|
ref={elementRef}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-11 03:31:30 +08:00
|
|
|
export default GenericContentItem;
|