From 2cdf649cc9f7fd2fdaffc8419b1832d5db88a6b5 Mon Sep 17 00:00:00 2001 From: Oleg Isonen Date: Mon, 13 Jun 2022 13:40:40 +0200 Subject: [PATCH] cleanup outlines --- .../workspace/canvas-tools/outlines.tsx | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/app/designer/features/workspace/canvas-tools/outlines.tsx b/app/designer/features/workspace/canvas-tools/outlines.tsx index 47c66510da..e1d323de12 100644 --- a/app/designer/features/workspace/canvas-tools/outlines.tsx +++ b/app/designer/features/workspace/canvas-tools/outlines.tsx @@ -3,8 +3,9 @@ import { styled } from "~/shared/design-system"; import { useSelectedInstanceRect } from "~/shared/nano-states"; import { useSelectedInstanceData } from "~/designer/shared/nano-states"; import { primitives } from "~/shared/component"; +import { type Instance } from "@webstudio-is/sdk"; -const useStyle = () => { +const useSelectedInstanceOutlineStyle = () => { const [rect] = useSelectedInstanceRect(); return useMemo(() => { if (rect === undefined) return; @@ -26,9 +27,8 @@ type LabelRefCallback = (element: HTMLElement | null) => void; * - else if instance height is more than 250px - bottom * - else inside-top - last resort because it covers a bit of the instance content */ -const useLabelPosition = (): [LabelRefCallback, LabelPosition] => { +const useLabelPosition = (instanceRect: DOMRect): [LabelRefCallback, LabelPosition] => { const [position, setPosition] = useState("top"); - const [instanceRect] = useSelectedInstanceRect(); const ref = useCallback( (element) => { @@ -70,7 +70,7 @@ const Outline = styled( } ); -const Label = styled( +const LabelContainer = styled( "div", { position: "absolute", @@ -111,24 +111,30 @@ const Label = styled( } ); -export const SelectedInstanceOutline = () => { - const style = useStyle(); - const [selectedInstanceData] = useSelectedInstanceData(); - const [labelRef, position] = useLabelPosition(); +const Label = ({component, instanceRect}: {component: Instance['component'], instanceRect: DOMRect}) => { + const [labelRef, position] = useLabelPosition(instanceRect); + const primitive = primitives[component]; + const { Icon } = primitive; + return ( + + + {primitive.label} + + ); +}; - if (style === undefined || selectedInstanceData === undefined) { +export const SelectedInstanceOutline = () => { + const style = useSelectedInstanceOutlineStyle(); + const [selectedInstanceData] = useSelectedInstanceData(); + const [instanceRect] = useSelectedInstanceRect(); + + if (style === undefined || selectedInstanceData === undefined || instanceRect === undefined) { return null; } - const primitive = primitives[selectedInstanceData.component]; - const { Icon } = primitive; - return ( - + ); -}; +}; \ No newline at end of file