diff --git a/frontend/app/element/palette.stories.tsx b/frontend/app/element/palette.stories.tsx index e226b5b06..1ff3021c1 100644 --- a/frontend/app/element/palette.stories.tsx +++ b/frontend/app/element/palette.stories.tsx @@ -1,11 +1,11 @@ // Story for Palette Component import type { Meta, StoryObj } from "@storybook/react"; -import { useRef } from "react"; +import { useEffect, useRef, useState } from "react"; import { Button } from "./button"; import { Palette } from "./palette"; const meta: Meta = { - title: "Components/Palette", + title: "Elements/Palette", component: Palette, args: { className: "custom-palette-class", @@ -26,15 +26,53 @@ type Story = StoryObj; export const DefaultPalette: Story = { render: (args) => { const anchorRef = useRef(null); + const scopeRef = useRef(null); + const [isMenuVisible, setIsMenuVisible] = useState(false); + + const handleAnchorClick = () => { + setIsMenuVisible((prev) => !prev); + }; + + const handleClickOutside = (event: MouseEvent) => { + if (anchorRef.current && !anchorRef.current.contains(event.target as Node)) { + setIsMenuVisible(false); + } + }; + + useEffect(() => { + scopeRef?.current?.addEventListener("mousedown", handleClickOutside); + return () => { + scopeRef?.current?.removeEventListener("mousedown", handleClickOutside); + }; + }, []); return ( -
- - -
This is the Palette content.
-
+ {isMenuVisible && ( + +
+ + Empty +
+
+ )}
); }, diff --git a/frontend/app/element/palette.tsx b/frontend/app/element/palette.tsx index 64b82e39d..fff339c0b 100644 --- a/frontend/app/element/palette.tsx +++ b/frontend/app/element/palette.tsx @@ -9,15 +9,17 @@ import { createPortal } from "react-dom"; import "./palette.less"; interface PaletteProps { - children: React.ReactNode; - className?: string; anchorRef: React.RefObject; scopeRef: React.RefObject; + children: React.ReactNode; + className?: string; } const Palette = memo(({ children, className, anchorRef, scopeRef }: PaletteProps) => { const paletteRef = useRef(null); const domRect = useDimensionsWithExistingRef(scopeRef); + const width = domRect?.width ?? 0; + const height = domRect?.height ?? 0; useEffect(() => { const paletteEl = paletteRef.current; @@ -28,7 +30,7 @@ const Palette = memo(({ children, className, anchorRef, scopeRef }: PaletteProps paletteEl.style.top = `${bottom}px`; paletteEl.style.left = `${left}px`; } - }, [anchorRef]); + }, [width, height, anchorRef]); return createPortal(