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.

cleanup

Roman Pominova19d9f75e957
diff --git a/apps/designer/app/canvas/shared/use-shortcuts.ts b/apps/designer/app/canvas/shared/use-shortcuts.ts
index 67310ea495..b41f1a83e7 100644
--- a/apps/designer/app/canvas/shared/use-shortcuts.ts
+++ b/apps/designer/app/canvas/shared/use-shortcuts.ts
@@ -72,7 +72,6 @@ export const useShortcuts = () => {
esc: publishCancelCurrentDrag,
} as const;
- // @todo: doesnt allow to delete when navigator is in focus
useHotkeys(
"backspace, delete",
shortcutHandlerMap.delete,
diff --git a/apps/designer/app/designer/shared/tree/tree-node.tsx b/apps/designer/app/designer/shared/tree/tree-node.tsx
index 189e342260..ac5e85553c 100644
--- a/apps/designer/app/designer/shared/tree/tree-node.tsx
+++ b/apps/designer/app/designer/shared/tree/tree-node.tsx
@@ -1,4 +1,4 @@
-import { forwardRef, useCallback, useRef, useEffect } from "react";
+import { useCallback, useRef, useEffect } from "react";
import { type Instance, components } from "@webstudio-is/react-sdk";
import {
Flex,
@@ -183,122 +183,120 @@ type TreeNodeProps = {
onExpandTransitionEnd?: () => void;
};
-export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
- ({ instance, level = 0, parentIsSelected, ...commonProps }, ref) => {
- const {
- getIsExpanded,
- animate = true,
- setIsExpanded,
- selectedInstanceId,
- onSelect,
- onExpandTransitionEnd,
- disableHoverStates = false,
- } = commonProps;
+export const TreeNode = ({
+ instance,
+ level = 0,
+ parentIsSelected,
+ ...commonProps
+}: TreeNodeProps) => {
+ const {
+ getIsExpanded,
+ animate = true,
+ setIsExpanded,
+ selectedInstanceId,
+ onSelect,
+ onExpandTransitionEnd,
+ disableHoverStates = false,
+ } = commonProps;
- const collapsibleContentRef = useRef<HTMLDivElement>(null);
+ const collapsibleContentRef = useRef<HTMLDivElement>(null);
- const isAlwaysExpanded = level === 0;
- const isExpandable = getIsExpandable(instance);
- const isExpanded = getIsExpanded(instance) || isAlwaysExpanded;
- const isSelected = instance.id === selectedInstanceId;
+ const isAlwaysExpanded = level === 0;
+ const isExpandable = getIsExpandable(instance);
+ const isExpanded = getIsExpanded(instance) || isAlwaysExpanded;
+ const isSelected = instance.id === selectedInstanceId;
- const makeSelected = () => {
- if (isSelected === false) {
- onSelect?.(instance.id);
- }
- };
-
- const shouldRenderExpandButton = isExpandable && isAlwaysExpanded === false;
+ const makeSelected = () => {
+ if (isSelected === false) {
+ onSelect?.(instance.id);
+ }
+ };
- const { Icon, label } = components[instance.component];
+ const shouldRenderExpandButton = isExpandable && isAlwaysExpanded === false;
- const CollapsibleContent = animate
- ? CollapsibleContentAnimated
- : CollapsibleContentUnanimated;
+ const { Icon, label } = components[instance.component];
- const handleAnimationEnd = useCallback(
- (event: React.AnimationEvent<HTMLDivElement>) => {
- if (event.target === collapsibleContentRef.current) {
- onExpandTransitionEnd?.();
- }
- },
- [onExpandTransitionEnd]
- );
+ const CollapsibleContent = animate
+ ? CollapsibleContentAnimated
+ : CollapsibleContentUnanimated;
- // If ther's no animation, we need to manually trigger onExpandTransitionEnd
- const prevIsExpanded = useRef(isExpanded);
- useEffect(() => {
- if (animate === false && isExpanded !== prevIsExpanded.current) {
+ const handleAnimationEnd = useCallback(
+ (event: React.AnimationEvent<HTMLDivElement>) => {
+ if (event.target === collapsibleContentRef.current) {
onExpandTransitionEnd?.();
}
- prevIsExpanded.current = isExpanded;
- }, [animate, onExpandTransitionEnd, isExpanded]);
+ },
+ [onExpandTransitionEnd]
+ );
+
+ // If ther's no animation, we need to manually trigger onExpandTransitionEnd
+ const prevIsExpanded = useRef(isExpanded);
+ useEffect(() => {
+ if (animate === false && isExpanded !== prevIsExpanded.current) {
+ onExpandTransitionEnd?.();
+ }
+ prevIsExpanded.current = isExpanded;
+ }, [animate, onExpandTransitionEnd, isExpanded]);
- return (
- <Collapsible.Root
- ref={ref}
- open={isExpanded}
- onOpenChange={(isOpen) => setIsExpanded?.(instance, isOpen)}
- data-drop-target-id={instance.id}
- >
- <ItemWrapper
+ return (
+ <Collapsible.Root
+ open={isExpanded}
+ onOpenChange={(isOpen) => setIsExpanded?.(instance, isOpen)}
+ data-drop-target-id={instance.id}
+ >
+ <ItemWrapper isSelected={isSelected} parentIsSelected={parentIsSelected}>
+ <NestingLines
isSelected={isSelected}
- parentIsSelected={parentIsSelected}
- >
- <NestingLines
- isSelected={isSelected}
- level={level}
- followedByExpandButton={shouldRenderExpandButton}
- />
- {shouldRenderExpandButton && (
- <CollapsibleTrigger
- // We don't want a separate focusable control inside a tree item.
- // tabIndex makes it skipped over when tabbing.
- // It still can be focused using mouse, but we handle this elsewhere.
- tabIndex={-1}
- >
- {isExpanded ? <TriangleDownIcon /> : <TriangleRightIcon />}
- </CollapsibleTrigger>
- )}
- <ItemButton
- type="button"
- data-drag-item-id={instance.id}
- data-item-button-id={instance.id}
- onFocus={makeSelected}
- enableHoverState={disableHoverStates === false}
- >
- <Icon />
- <Label size="1" variant={isSelected ? "loContrast" : "contrast"}>
- {label}
- </Label>
- </ItemButton>
- </ItemWrapper>
- {isExpandable && (
- <CollapsibleContent
- onAnimationEnd={handleAnimationEnd}
- ref={collapsibleContentRef}
+ level={level}
+ followedByExpandButton={shouldRenderExpandButton}
+ />
+ {shouldRenderExpandButton && (
+ <CollapsibleTrigger
+ // We don't want a separate focusable control inside a tree item.
+ // tabIndex makes it skipped over when tabbing.
+ // It still can be focused using mouse, but we handle this elsewhere.
+ tabIndex={-1}
>
- {
- // CollapsibleContent doesn't render children when collapsed.
- // No need to worry about optimizing this.
- instance.children.flatMap((child) =>
- typeof child === "string"
- ? []
- : [
- <TreeNode
- key={child.id}
- instance={child}
- level={level + 1}
- parentIsSelected={isSelected || parentIsSelected}
- {...commonProps}
- />,
- ]
- )
- }
- </CollapsibleContent>
+ {isExpanded ? <TriangleDownIcon /> : <TriangleRightIcon />}
+ </CollapsibleTrigger>
)}
- </Collapsible.Root>
- );
- }
-);
-TreeNode.displayName = "TreeNode";
+ <ItemButton
+ type="button"
+ data-drag-item-id={instance.id}
+ data-item-button-id={instance.id}
+ onFocus={makeSelected}
+ enableHoverState={disableHoverStates === false}
+ >
+ <Icon />
+ <Label size="1" variant={isSelected ? "loContrast" : "contrast"}>
+ {label}
+ </Label>
+ </ItemButton>
+ </ItemWrapper>
+ {isExpandable && (
+ <CollapsibleContent
+ onAnimationEnd={handleAnimationEnd}
+ ref={collapsibleContentRef}
+ >
+ {
+ // CollapsibleContent doesn't render children when collapsed.
+ // No need to worry about optimizing this.
+ instance.children.flatMap((child) =>
+ typeof child === "string"
+ ? []
+ : [
+ <TreeNode
+ key={child.id}
+ instance={child}
+ level={level + 1}
+ parentIsSelected={isSelected || parentIsSelected}
+ {...commonProps}
+ />,
+ ]
+ )
+ }
+ </CollapsibleContent>
+ )}
+ </Collapsible.Root>
+ );
+};
diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx b/apps/designer/app/designer/shared/tree/tree.stories.tsx
similarity index 98%
rename from apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx
rename to apps/designer/app/designer/shared/tree/tree.stories.tsx
index 8c13f9a419..099423f130 100644
--- a/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.stories.tsx
@@ -63,6 +63,7 @@ export const StressTest = ({ animate }: { animate: boolean }) => {
)
)
}
+ onDelete={() => null}
/>
</Flex>
);
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index c0cd6bfbd1..ff1f868b92 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -212,9 +212,12 @@ export const Tree = ({
]);
const getFallbackDropTarget = () => {
+ const element = rootNodeRef.current?.querySelector(
+ `[data-drop-target-id="${root.id}"]`
+ );
return {
data: root,
- element: rootNodeRef.current as HTMLElement,
+ element: element as HTMLElement,
final: true,
};
};
@@ -383,7 +386,12 @@ export const Tree = ({
pt: 2,
pb: 2,
}}
- ref={autoScrollHandlers.targetRef}
+ ref={(element) => {
+ rootNodeRef.current = element;
+ autoScrollHandlers.targetRef(element);
+ dragHandlers.rootRef(element);
+ dropHandlers.rootRef(element);
+ }}
onScroll={dropHandlers.handleScroll}
>
<Box
@@ -401,11 +409,6 @@ export const Tree = ({
setIsExpanded={setIsExpanded}
onExpandTransitionEnd={dropHandlers.handleDomMutation}
disableHoverStates={dragItem !== undefined}
- ref={(element) => {
- rootNodeRef.current = element;
- dragHandlers.rootRef(element);
- dropHandlers.rootRef(element);
- }}
/>
</Box>
{finalDropTarget &&
diff --git a/packages/design-system/src/components/primitives/dnd/use-drag-cursor.ts b/packages/design-system/src/components/primitives/dnd/use-drag-cursor.ts
index e9185f2ce0..0f62cae7b6 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag-cursor.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag-cursor.ts
@@ -2,22 +2,13 @@ import { useEffect } from "react";
export const useDragCursor = (isDragging: boolean, cursor = "grabbing") => {
useEffect(() => {
- // possible
- if (document.body === null) {
- return;
- }
-
if (isDragging) {
- const originalValue = document.body.style.getPropertyValue("cursor");
- const originalPriority =
- document.body.style.getPropertyPriority("cursor");
- document.body.style.setProperty("cursor", cursor, "important");
+ const html = document.documentElement;
+ const originalValue = html.style.getPropertyValue("cursor");
+ const originalPriority = html.style.getPropertyPriority("cursor");
+ html.style.setProperty("cursor", cursor, "important");
return () => {
- document.body.style.setProperty(
- "cursor",
- originalValue,
- originalPriority
- );
+ html.style.setProperty("cursor", originalValue, originalPriority);
};
}
}, [isDragging, cursor]);