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.

get rid of UsableElement

Roman Pominovd59dd21df1c8
diff --git a/packages/design-system/src/components/primitives/dnd/dom-utils.ts b/packages/design-system/src/components/primitives/dnd/dom-utils.ts
index 1cb47003f7..a40df7f86d 100644
--- a/packages/design-system/src/components/primitives/dnd/dom-utils.ts
+++ b/packages/design-system/src/components/primitives/dnd/dom-utils.ts
@@ -4,24 +4,10 @@ import {
getRectsOrientation,
} from "./geometry-utils";
-export type UsableElement = HTMLElement | SVGElement;
-
-// Many DOM APIs produce values of type Element,
-// which lacks certain methods that we often need.
-// It's also usually safe to assume that the value is in fact HTMLElement or SVGElement.
-// We use this helper to convince TypeScript that the value is a UsableElement.
-export const toUseableElement = (
- element: EventTarget | Element | undefined | null
-): UsableElement | undefined => {
- if (element instanceof HTMLElement || element instanceof SVGElement) {
- return element;
- }
-};
-
// By looking at a specific child and it's neighbours,
// determines their orientation relative to each other
export const getLocalChildrenOrientation = (
- parent: UsableElement,
+ parent: Element,
childrentRects: Rect[],
childIndex: number
): ChildrenOrientation => {
@@ -52,7 +38,7 @@ export const getLocalChildrenOrientation = (
return getRectsOrientation(previous, current, next);
};
-export const getChildrenRects = (parent: UsableElement) => {
+export const getChildrenRects = (parent: Element) => {
const parentRect = parent.getBoundingClientRect();
// We convert to relative coordinates to be able to store the result in cache.
diff --git a/packages/design-system/src/components/primitives/dnd/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.ts
index 41d862e2fa..b7120609fa 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts
@@ -1,6 +1,5 @@
import { useMove } from "./use-move";
import { useRef, useEffect, useMemo } from "react";
-import { type UsableElement, toUseableElement } from "./dom-utils";
type State =
| {
@@ -22,7 +21,7 @@ const initialState = {
export type UseDragProps<DragItemData> = {
startDistanceThreashold?: number;
shiftDistanceThreshold?: number;
- isDragItem: (element: UsableElement) => DragItemData | false;
+ isDragItem: (element: Element) => DragItemData | false;
onStart: (event: { target: HTMLElement; data: DragItemData }) => void;
onMove: (event: { x: number; y: number }) => void;
onShiftChange?: (event: { shifts: number }) => void;
@@ -64,13 +63,11 @@ export const useDrag = <DragItemData>({
const { onPointerDown } = useMove({
shouldStart: ({ target }) => {
- const targetElement = toUseableElement(target);
-
- if (!targetElement) {
+ if (!target) {
return false;
}
- const data = isDragItem(targetElement);
+ const data = isDragItem(target as Element);
if (data === false) {
return false;
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.ts
index ed6e9068c3..c6aeefc500 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts
@@ -10,18 +10,13 @@ import {
type Rect,
type Placement,
} from "./geometry-utils";
-import {
- type UsableElement,
- toUseableElement,
- getLocalChildrenOrientation,
- getChildrenRects,
-} from "./dom-utils";
+import { getLocalChildrenOrientation, getChildrenRects } from "./dom-utils";
// Partial information about a drop target
// used during the selection of a new drop target
export type PartialDropTarget<Data> = {
data: Data;
- element: UsableElement;
+ element: Element;
};
export type DropTarget<Data> = PartialDropTarget<Data> & {
@@ -35,7 +30,7 @@ export type DropTarget<Data> = PartialDropTarget<Data> & {
// doesn't have to be looked up again in swapDropTarget.
export type UseDropProps<Data> = {
// To check that the element can qualify as a target
- isDropTarget: (target: UsableElement) => Data | false;
+ isDropTarget: (target: Element) => Data | false;
// Distance from an edge to set nearEdge to true in swapDropTarget
edgeDistanceThreshold?: number;
@@ -62,8 +57,8 @@ const getInitialState = <Data>() => {
return {
pointerCoordinates: undefined as { x: number; y: number } | undefined,
dropTarget: undefined as DropTarget<Data> | undefined,
- childrenRectsCache: new WeakMap<UsableElement, Rect[]>(),
- lastCandidateElement: undefined as UsableElement | undefined,
+ childrenRectsCache: new WeakMap<Element, Rect[]>(),
+ lastCandidateElement: undefined as Element | undefined,
lastCandidateIsNearEdge: undefined as boolean | undefined,
};
};
@@ -75,12 +70,12 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
const latestProps = useRef<UseDropProps<Data>>(props);
latestProps.current = props;
- const rootRef = useRef<UsableElement>();
+ const rootRef = useRef<Element | null>(null);
const state = useRef(getInitialState<Data>());
// We want to return a stable object to avoid re-renders when it's a dependency
return useMemo(() => {
- const getChildrenRectsMemoized = (parent: UsableElement) => {
+ const getChildrenRectsMemoized = (parent: Element) => {
const fromCache = state.current.childrenRectsCache.get(parent);
if (fromCache !== undefined) {
return fromCache;
@@ -189,11 +184,9 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
root,
initialElement:
pointerCoordinates &&
- toUseableElement(
- document.elementFromPoint(
- pointerCoordinates.x,
- pointerCoordinates.y
- )
+ document.elementFromPoint(
+ pointerCoordinates.x,
+ pointerCoordinates.y
),
isDropTarget,
});
@@ -203,13 +196,13 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
candidate?.element !== state.current.lastCandidateElement;
state.current.lastCandidateElement = candidate?.element;
const candidateIsNearEdge =
- candidate &&
- pointerCoordinates &&
- isNearEdge(
- pointerCoordinates,
- edgeDistanceThreshold,
- candidate.element.getBoundingClientRect()
- );
+ candidate && pointerCoordinates
+ ? isNearEdge(
+ pointerCoordinates,
+ edgeDistanceThreshold,
+ candidate.element.getBoundingClientRect()
+ )
+ : undefined;
const isNewIsNearEdge =
candidateIsNearEdge !== state.current.lastCandidateIsNearEdge;
state.current.lastCandidateIsNearEdge = candidateIsNearEdge;
@@ -226,17 +219,18 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
}
let didSwap = true;
- while (didSwap || candidate === undefined) {
+ while (didSwap || candidate == null) {
const swappedTo = swapDropTarget(
- candidate &&
- pointerCoordinates && {
- ...candidate,
- nearEdge: isNearEdge(
- pointerCoordinates,
- edgeDistanceThreshold,
- candidate.element.getBoundingClientRect()
- ),
- }
+ candidate && pointerCoordinates
+ ? {
+ ...candidate,
+ nearEdge: isNearEdge(
+ pointerCoordinates,
+ edgeDistanceThreshold,
+ candidate.element.getBoundingClientRect()
+ ),
+ }
+ : undefined
);
didSwap = swappedTo.element !== candidate?.element;
candidate = swappedTo;
@@ -260,7 +254,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
},
rootRef(rootElement) {
- rootRef.current = toUseableElement(rootElement);
+ rootRef.current = rootElement;
},
};
}, []);
@@ -271,17 +265,17 @@ const findClosestDropTarget = <Data>({
initialElement,
isDropTarget,
}: {
- root: UsableElement;
- initialElement: UsableElement | undefined;
- isDropTarget: (target: UsableElement) => Data | false;
+ root: Element;
+ initialElement: Element | undefined | null;
+ isDropTarget: (target: Element) => Data | false;
}): PartialDropTarget<Data> | undefined => {
// The element we get from elementFromPoint() might not be inside the root
if (initialElement === undefined || root.contains(initialElement) === false) {
return undefined;
}
- let currentElement: UsableElement | undefined = initialElement;
- while (currentElement !== undefined) {
+ let currentElement = initialElement;
+ while (currentElement != null) {
const data = isDropTarget(currentElement);
if (data !== false) {
return { data: data, element: currentElement };
@@ -289,6 +283,6 @@ const findClosestDropTarget = <Data>({
if (currentElement === root) {
break;
}
- currentElement = toUseableElement(currentElement.parentElement);
+ currentElement = currentElement.parentElement;
}
};