Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watermark container size changes, resulting in incomplete display or disappearance of watermarks #48660

Closed
wants to merge 8 commits into from
9 changes: 7 additions & 2 deletions components/watermark/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@

// Used for nest case like Modal, Drawer
const [subElements, setSubElements] = React.useState(new Set<HTMLElement>());

// Used for re-rendering the watermark Container
const [containerKey, setContainerKey] = React.useState(0);
// Nest elements should also support watermark
const targetElements = React.useMemo(() => {
const list = container ? [container] : [];
Expand Down Expand Up @@ -207,7 +208,8 @@

// ============================= Effect =============================
// Append watermark to the container
const [appendWatermark, removeWatermark, isWatermarkEle] = useWatermark(markStyle);
const [appendWatermark, removeWatermark, isWatermarkEle, isWatermarkContainer] =
useWatermark(markStyle);

useEffect(() => {
if (watermarkInfo) {
Expand All @@ -222,6 +224,8 @@
mutations.forEach((mutation) => {
if (reRendering(mutation, isWatermarkEle)) {
syncWatermark();
} else if (reRendering(mutation, isWatermarkContainer)) {
setContainerKey((x) => x + 1);

Check warning on line 228 in components/watermark/index.tsx

View check run for this annotation

Codecov / codecov/patch

components/watermark/index.tsx#L228

Added line #L228 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能用 key 来做重新渲染,这样内部的子元素也会全部重新渲染。这里应该是只有 container 和 watermark 保活才行。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要检测一下 heightscrollHeight 变化,然后重新设置一下 div 的 style。

}
});
};
Expand Down Expand Up @@ -280,6 +284,7 @@

return (
<div
key={containerKey}
ref={setContainer}
className={classNames(className, rootClassName)}
style={{ position: 'relative', ...style }}
Expand Down
5 changes: 4 additions & 1 deletion components/watermark/useWatermark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function useWatermark(
appendWatermark: AppendWatermark,
removeWatermark: (container: HTMLElement) => void,
isWatermarkEle: (ele: Node) => boolean,
isWatermarkContainer: (ele: Node) => boolean,
] {
const [watermarkMap] = React.useState(() => new Map<HTMLElement, HTMLDivElement>());

Expand Down Expand Up @@ -70,5 +71,7 @@ export default function useWatermark(

const isWatermarkEle = (ele: any) => Array.from(watermarkMap.values()).includes(ele);

return [appendWatermark, removeWatermark, isWatermarkEle];
const isWatermarkContainer = (ele: any) => Array.from(watermarkMap.keys()).includes(ele);

return [appendWatermark, removeWatermark, isWatermarkEle, isWatermarkContainer];
}