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
some fixes / cleanup
Roman Pominov325062a128d1
diff --git a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx b/apps/designer/app/designer/features/tree-preview/tree-preview.tsxindex 936a067fbb..0cb4f169e2 100644--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx@@ -1,7 +1,7 @@import produce from "immer";import { type Instance } from "@webstudio-is/react-sdk";import { useMemo } from "react";-import { Tree } from "~/designer/shared/tree";+import { TreeNode } from "~/designer/shared/tree";import { Flex } from "@webstudio-is/design-system";import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";import {@@ -9,6 +9,7 @@ import {insertInstanceMutable,createInstance,findInstanceById,+ getInstancePath,} from "~/shared/tree-utils";export const TreePrevew = () => {@@ -52,9 +53,17 @@ export const TreePrevew = () => {}})(rootInstance);+ const dropTargetPath = getInstancePath(+ rootInstance,+ dropTargetInstanceId+ ).map((item) => item.id);+return {- root: instance,+ instance,selectedInstanceId: dragItemInstance.id,+ getIsExpanded: (instance: Instance) =>+ dropTargetPath.includes(instance.id),+ animate: false,};}, [rootInstance,@@ -65,8 +74,8 @@ export const TreePrevew = () => {return (treeProps && (- <Flex gap="3" direction="column" css={{ padding: "$1" }}>- <Tree {...treeProps} animate={false} />+ <Flex direction="column" css={{ pt: "$1", pb: "$1", width: "100%" }}>+ <TreeNode {...treeProps} /></Flex>));diff --git a/apps/designer/app/designer/shared/tree/index.ts b/apps/designer/app/designer/shared/tree/index.tsindex 8c4cc7182a..be397a1a5a 100644--- a/apps/designer/app/designer/shared/tree/index.ts+++ b/apps/designer/app/designer/shared/tree/index.ts@@ -1,2 +1,2 @@export * from "./sortable-tree";-export * from "./tree";+export * from "./tree-node";diff --git a/apps/designer/app/designer/shared/tree/placement-indicator.tsx b/apps/designer/app/designer/shared/tree/placement-indicator.tsxnew file mode 100644index 0000000000..0f8f1b21d1--- /dev/null+++ b/apps/designer/app/designer/shared/tree/placement-indicator.tsx@@ -0,0 +1,63 @@+import { Box, DropTarget, Rect, styled } from "@webstudio-is/design-system";++const CIRCLE_SIZE = 8;+const LINE_THICKNESS = 2;++// overlap the line with the circle by this much+// to make sure they are connected+const OVERLAP = 1;++const Circle = styled(Box, {+ width: CIRCLE_SIZE,+ height: CIRCLE_SIZE,+ top: (LINE_THICKNESS - CIRCLE_SIZE) / 2,+ left: OVERLAP - CIRCLE_SIZE,+ position: "absolute",+ border: "solid $dropPlacement",+ borderWidth: "$2",+ borderRadius: "50%",+});++const Line = styled(Box, {+ boxSizing: "content-box",+ position: "absolute",+ background: "$dropPlacement",+ pointerEvents: "none",+});++export const PlacementIndicatorLine = ({+ line,+}: {+ line: DropTarget<unknown>["placement"];+}) => (+ <Line+ style={{+ top: line.y - LINE_THICKNESS / 2,+ left: line.x + (CIRCLE_SIZE / 2 - OVERLAP),+ width: line.length - (CIRCLE_SIZE / 2 - OVERLAP),+ height: LINE_THICKNESS,+ }}+ >+ <Circle />+ </Line>+);++const Outline = styled(Box, {+ position: "absolute",+ pointerEvents: "none",+ boxSizing: "border-box",+ border: "solid $dropPlacement",+ borderWidth: "$2",+ borderRadius: "$2",+});++export const PlacementIndicatorOutline = ({ rect }: { rect: Rect }) => (+ <Outline+ style={{+ top: rect.top,+ left: rect.left + 2,+ width: rect.width - 4,+ height: rect.height,+ }}+ />+);diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.tsx b/apps/designer/app/designer/shared/tree/sortable-tree.tsxindex b39ce40438..26031cf644 100644--- a/apps/designer/app/designer/shared/tree/sortable-tree.tsx+++ b/apps/designer/app/designer/shared/tree/sortable-tree.tsx@@ -1,4 +1,4 @@-import { useState, useMemo, useRef } from "react";+import { useState, useMemo, useRef, useCallback, useEffect } from "react";import {type Instance,components,@@ -25,10 +25,18 @@ import {getPlacementIndicatorAlignment,INDENT,} from "./tree-node";-import { useExpandState, type TreeProps } from "./tree";import { CSSProperties } from "@stitches/react";--type Placement = DropTarget<unknown>["placement"];+import {+ PlacementIndicatorLine,+ PlacementIndicatorOutline,+} from "./placement-indicator";++type TreeProps = {+ root: Instance;+ selectedInstanceId?: Instance["id"];+ onSelect?: (instance: Instance) => void;+ animate?: boolean;+};export const SortableTree = ({root,@@ -46,7 +54,6 @@ export const SortableTree = ({const { getIsExpanded, setIsExpanded } = useExpandState({root,selectedInstanceId,- onSelect,});const rootNodeRef = useRef<HTMLElement | null>(null);@@ -69,7 +76,7 @@ export const SortableTree = ({position: number | "end";placement:| { type: "rect"; rect: Rect }- | { type: "line"; line: Placement };+ | { type: "line"; line: DropTarget<unknown>["placement"] };}| undefined>(() => {@@ -301,6 +308,7 @@ export const SortableTree = ({return instance;},onStart: ({ data }) => {+ onSelect?.(data);setDragItem(data);dropHandlers.handleStart();autoScrollHandlers.setEnabled(true);@@ -355,9 +363,7 @@ export const SortableTree = ({<TreeNodeanimate={animate}onSelect={onSelect}- selectedInstanceId={- dragItem === undefined ? selectedInstanceId : dragItem.id- }+ selectedInstanceId={selectedInstanceId}instance={root}level={0}getIsExpanded={getIsExpanded}@@ -377,74 +383,59 @@ export const SortableTree = ({) : (<PlacementIndicatorLine line={finalDropTarget.placement.line} />),-document.body)}</Box>);};-// @todo: if we decide to keep this color, we need to add it to design-system-const PLACEMENT_INDICATOR_COLOR = "#f531b3";--const PlacementIndicatorLine = ({ line }: { line: Placement }) => {- const circleSize = 8;- const lineHeight = 2;-- // overlap the line with the circle by this much- // to make sure they are connected- const ovelap = 1;+const useExpandState = ({+ selectedInstanceId,+ root,+}: {+ root: Instance;+ selectedInstanceId?: Instance["id"];+}) => {+ const [record, setRecord] = useState<Record<Instance["id"], boolean>>({});- // The goal is to put center of the circle at line.x/y+ // We want to automatically expand all parents of the selected instance whenever it changes+ const prevSelectedInstanceId = useRef(selectedInstanceId);+ useEffect(() => {+ if (selectedInstanceId === prevSelectedInstanceId.current) {+ return;+ }+ prevSelectedInstanceId.current = selectedInstanceId;+ if (selectedInstanceId === undefined) {+ return;+ }+ const path = getInstancePath(root, selectedInstanceId).map(+ (instance) => instance.id+ );+ // Don't want to expand the selected instance itself+ path.pop();+ const toExpand = path.filter((id) => record[id] !== true);+ if (toExpand.length === 0) {+ return;+ }+ setRecord((record) => {+ const newRecord = { ...record };+ for (const id of toExpand) {+ newRecord[id] = true;+ }+ return newRecord;+ });+ }, [record, root, selectedInstanceId]);- return (- <Box- style={{- top: line.y - lineHeight / 2,- left: line.x + (circleSize / 2 - ovelap),- width: line.length - (circleSize / 2 - ovelap),- height: lineHeight,- }}- css={{- boxSizing: "content-box",- position: "absolute",- background: PLACEMENT_INDICATOR_COLOR,- pointerEvents: "none",- }}- >- <Box- css={{- width: circleSize,- height: circleSize,- top: (lineHeight - circleSize) / 2,- left: ovelap - circleSize,- position: "absolute",- border: `solid ${PLACEMENT_INDICATOR_COLOR}`,- borderWidth: "$2",- borderRadius: "50%",- }}- />- </Box>+ const getIsExpanded = useCallback(+ (instance: Instance) => {+ return getIsExpandable(instance) && record[instance.id] === true;+ },+ [record]);-};-const PlacementIndicatorOutline = ({ rect }: { rect: Rect }) => {- return (- <Box- style={{- top: rect.top,- left: rect.left + 2,- width: rect.width - 4,- height: rect.height,- }}- css={{- position: "absolute",- pointerEvents: "none",- boxSizing: "border-box",- border: `solid ${PLACEMENT_INDICATOR_COLOR}`,- borderWidth: "$2",- borderRadius: "$2",- }}- />- );+ const setIsExpanded = useCallback((instance: Instance, expanded: boolean) => {+ setRecord((record) => ({ ...record, [instance.id]: expanded }));+ }, []);++ return { getIsExpanded, setIsExpanded };};diff --git a/apps/designer/app/designer/shared/tree/tree-node.tsx b/apps/designer/app/designer/shared/tree/tree-node.tsxindex 7c0a2dd78f..eac32fae33 100644--- a/apps/designer/app/designer/shared/tree/tree-node.tsx+++ b/apps/designer/app/designer/shared/tree/tree-node.tsx@@ -27,7 +27,7 @@ export const getIsExpandable = (instance: Instance) => {);};-const CustomButton = styled("button", {+const ItemButton = styled("button", {all: "unset",display: "flex",alignItems: "center",@@ -61,17 +61,16 @@ const CustomButton = styled("button", {},});-const nestingLineStyles = {+const NestingLine = styled(Box, {width: Math.ceil(INDENT / 2),height: ITEM_HEIGHT,borderRight: "solid",borderRightWidth: "$1",borderColor: "$slate9",-};-const nestingLineStylesSelected = {- ...nestingLineStyles,- borderColor: "$blue7",-};+ variants: {+ isSelected: { true: { borderColor: "$blue7" } },+ },+});const NestingLines = ({isSelected,@@ -93,14 +92,14 @@ const NestingLines = ({return (<>{Array.from({ length: level - 1 }, (_, i) => (- <Box+ <NestingLinekey={i}style={{marginRight:Math.floor(INDENT / 2) +(!followedByExpandButton && i === level - 2 ? INDENT : 0),}}- css={isSelected ? nestingLineStylesSelected : nestingLineStyles}+ isSelected={isSelected}/>))}</>@@ -131,25 +130,66 @@ const CollapsibleContentUnanimated = styled(Collapsible.Content, {overflow: "hidden",});+const CollapsibleTrigger = styled(Collapsible.Trigger, {+ all: "unset",+ display: "flex",+ pr: INDENT - ICONS_SIZE,+ height: ITEM_HEIGHT,+ alignItems: "center",++ // We want the button to take extra space so it's easier to hit+ ml: "-$2",+ pl: "$2",+});++const ItemWrapper = styled(Flex, {+ color: "$hiContrast",+ alignItems: "center",+ pr: ITEM_PADDING,+ pl: ITEM_PADDING,+ position: "relative",+ variants: {+ isSelected: {+ true: { color: "$loContrast", bc: "$blue10" },+ },+ parentIsSelected: {+ true: { bc: "$blue4" },+ },+ },+});++const Label = styled(Text, {+ whiteSpace: "nowrap",+ overflow: "hidden",+ textOverflow: "ellipsis",+ lineHeight: 1.4,+ flexBasis: 0,+ flexGrow: 1,++ // For some reason flexBasis:0 is not enough+ // to stop it growing past the container+ width: 0,+});+type TreeNodeProps = {disableHoverStates?: boolean;instance: Instance;- selectedInstanceId: Instance["id"] | undefined;+ selectedInstanceId?: Instance["id"];parentIsSelected?: boolean;- level: number;+ level?: number;onSelect?: (instance: Instance) => void;animate?: boolean;getIsExpanded: (instance: Instance) => boolean;- setIsExpanded: (instance: Instance, expanded: boolean) => void;+ setIsExpanded?: (instance: Instance, expanded: boolean) => void;onExpandTransitionEnd?: () => void;};export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(- ({ instance, level, parentIsSelected, ...commonProps }, ref) => {+ ({ instance, level = 0, parentIsSelected, ...commonProps }, ref) => {const {getIsExpanded,animate = true,- setIsExpanded,+ setIsExpanded = noop,selectedInstanceId,onSelect = noop,onExpandTransitionEnd = noop,@@ -202,19 +242,9 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(onOpenChange={(isOpen) => setIsExpanded(instance, isOpen)}data-drop-target-id={instance.id}>- <Flex- css={{- color: isSelected ? "$loContrast" : "$hiContrast",- alignItems: "center",- pr: ITEM_PADDING,- pl: ITEM_PADDING,- backgroundColor: isSelected- ? "$blue10"- : parentIsSelected- ? "$blue4"- : "transparent",- position: "relative",- }}+ <ItemWrapper+ isSelected={isSelected}+ parentIsSelected={parentIsSelected}><NestingLinesisSelected={isSelected}@@ -222,49 +252,22 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(followedByExpandButton={shouldRenderExpandButton}/>{shouldRenderExpandButton && (- <Collapsible.Trigger asChild>- <Flex- css={{- pr: INDENT - ICONS_SIZE,- height: ITEM_HEIGHT,- alignItems: "center",-- // We want the button to take extra space so it's easier to hit- ml: "-$2",- pl: "$2",- }}- >- {isExpanded ? <TriangleDownIcon /> : <TriangleRightIcon />}- </Flex>- </Collapsible.Trigger>+ <CollapsibleTrigger>+ {isExpanded ? <TriangleDownIcon /> : <TriangleRightIcon />}+ </CollapsibleTrigger>)}- <CustomButton+ <ItemButtondata-drag-item-id={instance.id}onFocus={makeSelected}onClick={makeSelected}enableHoverState={disableHoverStates === false}><Icon />- <Text- size="1"- variant={isSelected ? "loContrast" : "contrast"}- css={{- whiteSpace: "nowrap",- overflow: "hidden",- textOverflow: "ellipsis",- lineHeight: 1.4,- flexBasis: 0,- flexGrow: 1,-- // For some reason flexBasis:0 is not anough- // to stop it growing past the container- width: 0,- }}- >+ <Label size="1" variant={isSelected ? "loContrast" : "contrast"}>{label}- </Text>- </CustomButton>- </Flex>+ </Label>+ </ItemButton>+ </ItemWrapper>{isExpandable && (<CollapsibleContentonAnimationEnd={handleAnimationEnd}diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsxdeleted file mode 100644index 111f09f29c..0000000000--- a/apps/designer/app/designer/shared/tree/tree.tsx+++ /dev/null@@ -1,87 +0,0 @@-import { useState, useMemo, useCallback } from "react";-import { type Instance } from "@webstudio-is/react-sdk";-import { getInstancePath } from "~/shared/tree-utils";-import { TreeNode, getIsExpandable } from "./tree-node";-import noop from "lodash.noop";--export const useExpandState = ({- selectedInstanceId,- root,- onSelect = noop,-}: {- root: Instance;- selectedInstanceId?: Instance["id"];- onSelect?: (instance: Instance) => void;-}) => {- const [record, setRecord] = useState<Record<Instance["id"], boolean>>({});-- const selectedInstancePath = useMemo(- () =>- selectedInstanceId !== undefined- ? getInstancePath(root, selectedInstanceId).map(- (instance) => instance.id- )- : [],- [root, selectedInstanceId]- );-- const getIsExpanded = useCallback(- (instance: Instance) => {- const isParentOfSelectedInstance =- selectedInstancePath.includes(instance.id) &&- selectedInstanceId !== instance.id;-- return (- getIsExpandable(instance) &&- (record[instance.id] === true || isParentOfSelectedInstance)- );- },- [record, selectedInstanceId, selectedInstancePath]- );-- const setIsExpanded = useCallback(- (instance: Instance, expanded: boolean) => {- setRecord((record) => ({ ...record, [instance.id]: expanded }));-- // To allow user to collapse a parent of a selected instance- // We need to change the selected instance to the parent- if (expanded === false) {- const isParentOfSelectedInstance =- selectedInstancePath.includes(instance.id) &&- selectedInstanceId !== instance.id;- if (isParentOfSelectedInstance) {- onSelect(instance);- }- }- },- [onSelect, selectedInstanceId, selectedInstancePath]- );-- return { getIsExpanded, setIsExpanded };-};--export type TreeProps = {- root: Instance;- selectedInstanceId?: Instance["id"];- onSelect?: (instance: Instance) => void;- animate?: boolean;-};--export const Tree = ({- root,- selectedInstanceId,- onSelect,- ...rest-}: TreeProps) => {- const expandState = useExpandState({ root, selectedInstanceId, onSelect });- return (- <TreeNode- level={0}- instance={root}- selectedInstanceId={selectedInstanceId}- onSelect={onSelect}- {...rest}- {...expandState}- />- );-};diff --git a/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx b/packages/design-system/src/components/primitives/dnd/placement-indicator.tsxindex ef9df206a6..43b51fe45a 100644--- a/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx+++ b/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx@@ -4,7 +4,7 @@ import { type Placement } from "./geometry-utils";const placementStyle = {boxSizing: "content-box",position: "absolute",- background: "#f531b3",+ background: "$dropPlacement",pointerEvents: "none",};diff --git a/packages/design-system/src/stitches.config.ts b/packages/design-system/src/stitches.config.tsindex 5445d8e422..3e626f8cc2 100644--- a/packages/design-system/src/stitches.config.ts+++ b/packages/design-system/src/stitches.config.ts@@ -204,6 +204,7 @@ export const {shadowDark: "hsl(206 22% 7% / 20%)",background: "$slate1",text: "$slate12",+ dropPlacement: "#f531b3",},fonts: {sans: "Inter, -apple-system, system-ui, sans-serif",@@ -478,5 +479,6 @@ export const darkTheme = createTheme({background: "$slate12",text: "$slate1",+ dropPlacement: "#f531b3",},});