mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 09:49:57 +08:00
8a3870fc31
* docs: add demo * refactor: init * refactor: of it * refactor: simple content * chore: unique func * chore: refactor * chore: support modal watermark * feat: support nest watermark * test: add test case * chore: fix lint * chore: bump rc-image * test: add test case * refactor: use same func
34 lines
868 B
TypeScript
34 lines
868 B
TypeScript
import { useEvent } from 'rc-util';
|
|
import * as React from 'react';
|
|
|
|
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;
|