Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: detect scroll collision for form error tooltips (#2999)

Ivan Starkova05743ae1202
diff --git a/packages/design-system/src/components/tooltip.stories.tsx b/packages/design-system/src/components/tooltip.stories.tsx
new file mode 100644
index 0000000000..c36955b7c5
--- /dev/null
+++ b/packages/design-system/src/components/tooltip.stories.tsx
@@ -0,0 +1,63 @@
+import { InputErrorsTooltip, TooltipProvider } from "./tooltip";
+import { Button } from "./button";
+import { Box } from "./box";
+
+export default {
+ title: "Library/Tooltip",
+};
+
+const TooltipStory = () => {
+ return (
+ <TooltipProvider>
+ <Box>Some content</Box>
+ {/*
+ <InputErrorsTooltip errors={["Tooltip content"]}>
+ <Button>Hover me</Button>
+ </InputErrorsTooltip>
+ */}
+ <Box>Some content</Box>
+ <Box css={{ height: 100, width: 200, overflowY: "scroll" }}>
+ <Box css={{ height: 2000 }}>
+ <InputErrorsTooltip
+ errors={["Tooltip content"]}
+ side={"right"}
+ open={true}
+ >
+ <Button css={{ width: "100%", my: 10 }}>Hover me</Button>
+ </InputErrorsTooltip>
+ <br />
+ <br />
+ <InputErrorsTooltip
+ errors={["Tooltip content"]}
+ side={"right"}
+ open={true}
+ >
+ <Button css={{ width: "100%", my: 10 }}>Hover me</Button>
+ </InputErrorsTooltip>
+ <br />
+ <br />
+ <InputErrorsTooltip
+ errors={["Tooltip content"]}
+ side={"right"}
+ open={true}
+ >
+ <Button css={{ width: "100%", my: 10 }}>Hover me</Button>
+ </InputErrorsTooltip>
+ <br />
+ <br />
+ <InputErrorsTooltip
+ errors={["Tooltip content"]}
+ side={"right"}
+ open={true}
+ >
+ <Button css={{ width: "100%", my: 10 }}>Hover me</Button>
+ </InputErrorsTooltip>
+ <br />
+ <br />
+ </Box>
+ </Box>
+ </TooltipProvider>
+ );
+};
+
+export { TooltipStory as Tooltip };
diff --git a/packages/design-system/src/components/tooltip.tsx b/packages/design-system/src/components/tooltip.tsx
index d858536a5f..8a8b83325d 100644
--- a/packages/design-system/src/components/tooltip.tsx
+++ b/packages/design-system/src/components/tooltip.tsx
@@ -1,5 +1,11 @@
import type { Ref, ComponentProps, ReactNode, ReactElement } from "react";
-import { forwardRef, useEffect } from "react";
+import { forwardRef, useEffect, useRef, useState } from "react";
+import {
+ autoUpdate,
+ getOverflowAncestors,
+ type ReferenceElement,
+} from "@floating-ui/dom";
+
import { styled } from "../stitches.config";
import * as TooltipPrimitive from "@radix-ui/react-tooltip";
import { useControllableState } from "@radix-ui/react-use-controllable-state";
@@ -122,6 +128,10 @@ export const Tooltip = forwardRef(
}
);
+const isReferenceElement = (value: unknown): value is ReferenceElement => {
+ return value instanceof Element || value instanceof Window;
+};
+
Tooltip.displayName = "Tooltip";
export const InputErrorsTooltip = ({
@@ -138,19 +148,84 @@ export const InputErrorsTooltip = ({
const content = errors?.map((error, index) => (
<Text key={index}>{error}</Text>
));
+
+ const ref = useRef<HTMLDivElement>();
+ // Use collision boundary to hide tooltips if original element out of visible area in the scroll viewport
+ const [collisionBoundary, setCollisionBoundary] = useState<
+ | {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ }
+ | undefined
+ >(undefined);
+
+ useEffect(() => {
+ if (ref.current != null) {
+ const ancestors = getOverflowAncestors(ref.current, [], false);
+ if (ancestors.length === 2) {
+ // Only window and window viewport - do nothing
+ return;
+ }
+
+ const nearestScrollableElement = ancestors[0];
+
+ if (
+ nearestScrollableElement instanceof HTMLElement &&
+ isReferenceElement(ancestors[1])
+ ) {
+ // Track collision boundary size/position changes
+ const cleanup = autoUpdate(
+ ancestors[1],
+ nearestScrollableElement,
+ () => {
+ const rect = nearestScrollableElement.getBoundingClientRect();
+
+ setCollisionBoundary((prev) => {
+ const newY = rect.y;
+ const newHeight = rect.height;
+
+ if (prev?.y === newY && prev.height === newHeight) {
+ return prev;
+ }
+
+ const next = {
+ x: 0,
+ width: window.visualViewport?.width ?? 100000,
+ y: newY,
+ height: newHeight,
+ };
+
+ return next;
+ });
+ }
+ );
+
+ return cleanup;
+ }
+ }
+ }, []);
+
+ // We intentionally always pass non empty content to avoid optimization inside Tooltip
+ // where it renders {children} directly if content is empty.
+ // If this optimization accur, the input will remount which will cause focus loss
+ // and current value loss.
return (
- // We intentionally always pass non empty content to avoid optimization inside Tooltip
- // where it renders {children} directly if content is empty.
- // If this optimization accur, the input will remount which will cause focus loss
- // and current value loss.
- <Tooltip
- {...rest}
- content={content ?? " "}
- open={errors !== undefined && errors.length !== 0}
- side={side ?? "right"}
- css={css}
- >
- {children}
- </Tooltip>
+ <>
+ <Box ref={ref as never} css={{ display: "contents" }}></Box>
+ <Tooltip
+ {...rest}
+ collisionBoundary={collisionBoundary as never}
+ collisionPadding={-8}
+ hideWhenDetached={true}
+ content={content ?? " "}
+ open={errors !== undefined && errors.length !== 0}
+ side={side ?? "right"}
+ css={css}
+ >
+ {children}
+ </Tooltip>
+ </>
);
};