2023-08-08 16:48:26 +08:00
|
|
|
import * as React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { useEvent } from 'rc-util';
|
2023-08-08 16:48:26 +08:00
|
|
|
|
|
|
|
export interface WatermarkContextProps {
|
|
|
|
add: (ele: HTMLElement) => void;
|
|
|
|
remove: (ele: HTMLElement) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function voidFunc() {}
|
|
|
|
|
|
|
|
const WatermarkContext = React.createContext<WatermarkContextProps>({
|
|
|
|
add: voidFunc,
|
|
|
|
remove: voidFunc,
|
|
|
|
});
|
|
|
|
|
|
|
|
export function usePanelRef(panelSelector?: string) {
|
|
|
|
const watermark = React.useContext(WatermarkContext);
|
|
|
|
|
|
|
|
const panelEleRef = React.useRef<HTMLElement>();
|
|
|
|
const panelRef = useEvent((ele: HTMLElement | null) => {
|
|
|
|
if (ele) {
|
|
|
|
const innerContentEle = panelSelector ? ele.querySelector<HTMLElement>(panelSelector)! : ele;
|
|
|
|
watermark.add(innerContentEle);
|
|
|
|
panelEleRef.current = innerContentEle;
|
|
|
|
} else {
|
|
|
|
watermark.remove(panelEleRef.current!);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return panelRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WatermarkContext;
|