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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
finally a working label positioning with 3 states: top, bottom, inside
Oleg Isonen4b4157493e81
diff --git a/app/canvas/shared/instance.ts b/app/canvas/shared/instance.tsindex bd725db974..60757cc025 100644--- a/app/canvas/shared/instance.ts+++ b/app/canvas/shared/instance.ts@@ -22,7 +22,6 @@ import store from "immerhin";import { DropData, type SelectedInstanceData } from "~/shared/component";import { useSelectedInstance, useSelectedElement } from "./nano-states";import { rootInstanceContainer, useRootInstance } from "~/shared/nano-states";-import { getBoundingClientRect } from "~/shared/dom-utils";import { useAfterRender } from "./use-after-render";import { useWindowResize } from "~/shared/dom-hooks";@@ -167,7 +166,9 @@ export const useUpdateSelectedInstance = () => {const [selectedInstance, setSelectedInstance] = useSelectedInstance();useEffect(() => {- if (rootInstance === undefined || selectedInstance === undefined) return;+ if (rootInstance === undefined || selectedInstance === undefined) {+ return;+ }const nextSelectedInstance = findInstanceById(rootInstance,selectedInstance.iddiff --git a/app/designer/features/workspace/canvas-tools/outlines.tsx b/app/designer/features/workspace/canvas-tools/outlines.tsxindex 9b736b385b..47c66510da 100644--- a/app/designer/features/workspace/canvas-tools/outlines.tsx+++ b/app/designer/features/workspace/canvas-tools/outlines.tsx@@ -18,25 +18,32 @@ const useStyle = () => {};type LabelPosition = "top" | "inside" | "bottom";+type LabelRefCallback = (element: HTMLElement | null) => void;/*** Detects if there is no space on top and for the label and tells to show it inside.+ * - if there is enough space for the label on top of the instance - top+ * - 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 = () => {+const useLabelPosition = (): [LabelRefCallback, LabelPosition] => {const [position, setPosition] = useState<LabelPosition>("top");const [instanceRect] = useSelectedInstanceRect();- const ref = useCallback((element: HTMLElement | null) => {- if (element === null || instanceRect === undefined) return;- const labelRect = element.getBoundingClientRect();- let nextPosition: LabelPosition = "top";- if (labelRect.height > instanceRect.top) {- nextPosition = instanceRect.height < 250 ? "bottom" : "inside";- }- setPosition(nextPosition);- }, []);+ const ref = useCallback(+ (element) => {+ if (element === null || instanceRect === undefined) return;+ const labelRect = element.getBoundingClientRect();+ let nextPosition: LabelPosition = "top";+ if (labelRect.height > instanceRect.top) {+ nextPosition = instanceRect.height < 250 ? "bottom" : "inside";+ }+ setPosition(nextPosition);+ },+ [instanceRect]+ );- return { ref, position };+ return [ref, position];};const Outline = styled(@@ -97,7 +104,7 @@ const Label = styled(top: 0,},bottom: {- bottom: 0,+ bottom: "-$4",},},},@@ -107,13 +114,15 @@ const Label = styled(export const SelectedInstanceOutline = () => {const style = useStyle();const [selectedInstanceData] = useSelectedInstanceData();- const { ref: labelRef, position } = useLabelPosition();+ const [labelRef, position] = useLabelPosition();- if (style === undefined || selectedInstanceData === undefined) return null;+ if (style === undefined || selectedInstanceData === undefined) {+ return null;+ }const primitive = primitives[selectedInstanceData.component];const { Icon } = primitive;- console.log({ position });+return (<Outline state="selected" style={style}><Label state="selected" position={position} ref={labelRef}>diff --git a/app/shared/dom-hooks/use-measure.ts b/app/shared/dom-hooks/use-measure.tsindex aa8af5a3e8..004b6c8920 100644--- a/app/shared/dom-hooks/use-measure.ts+++ b/app/shared/dom-hooks/use-measure.ts@@ -21,7 +21,7 @@ export const useMeasure = <const observer = useMemo(() => {if (element === null || typeof window === "undefined") return;- return new window.ResizeObserver((entries) => {+ return new window.ResizeObserver(() => {if (element === null) return;requestAnimationFrame(() => {setRect(element.getBoundingClientRect());@@ -35,7 +35,7 @@ export const useMeasure = <return () => {observer.disconnect();};- }, [element]);+ }, [element, observer]);return [ref, rect];};