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
update target on scroll & other fixes
Roman Pominovbafe3898281e
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsxindex 4d8e31825f..62640500c5 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx@@ -11,7 +11,6 @@ export const Playground = () => {const [placementIndicatorRect, setPlacementIndicatorRect] = useState<Rect>();const holdRef = useRef<HTMLElement>();const dragItemRef = useRef<HTMLElement>();- const scrollRef = useRef<HTMLElement | null>(null);const handleHoldEnd = () => {holdRef.current?.style.removeProperty("background");@@ -33,7 +32,7 @@ export const Playground = () => {},});- const autoScrollHandlers = useAutoScroll({ target: scrollRef });+ const autoScrollHandlers = useAutoScroll();const dragProps = useDrag({onStart(event) {@@ -67,11 +66,10 @@ export const Playground = () => {{...dragProps}ref={(el) => {dropTargetHandlers.rootRef(el);-- // TODO: do same as above for consistency- scrollRef.current = el;+ autoScrollHandlers.targetRef(el);}}css={{ display: "block", overflow: "auto", height: 500 }}+ onScroll={dropTargetHandlers.handleScroll}><Item background="$cyanA9" /><Item background="$cyanA9" />diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.tsindex 8aafe535b4..522c20247f 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts@@ -1,4 +1,4 @@-import { useRef, type MutableRefObject } from "react";+import { useRef } from "react";// Time between frames of scroll animation in millisecondsconst FRAME_PERIOD = 30;@@ -39,7 +39,6 @@ const getSpeed = (};export type Parameters = {- target: MutableRefObject<HTMLElement | null>;edgeDistanceThreshold?: number;// min/max speed of the scroll animation in pixels per second@@ -47,18 +46,21 @@ export type Parameters = {maxSpeed?: number;};+export type Handlers = {+ handleMove: (pointerCoordinate: { x: number; y: number }) => void;+ setEnabled: (enabled: boolean) => void;+ targetRef: (element: HTMLElement | null) => void;+};+export const useAutoScroll = ({- target,edgeDistanceThreshold = 100,minSpeed = 1,maxSpeed = 500,-}: Parameters): {- handleMove: (pointerCoordinate: { x: number; y: number }) => void;- setEnabled: (enabled: boolean) => void;-} => {+}: Parameters = {}): Handlers => {const state = useRef({+ target: null as HTMLElement | null,enabled: false,- prevTimestamp: undefined as number | undefined,+ prevTimestamp: 0,speedX: 0,speedY: 0,stepScheduled: false,@@ -69,21 +71,21 @@ export const useAutoScroll = ({if (!state.current.enabled ||- target.current === null ||- (Math.round(state.current.speedX * FRAME_PERIOD) === 0 &&- Math.round(state.current.speedY * FRAME_PERIOD) === 0)+ (Math.round((state.current.speedX / 1000) * FRAME_PERIOD) === 0 &&+ Math.round((state.current.speedY / 1000) * FRAME_PERIOD) === 0)) {return;}- if (state.current.prevTimestamp === undefined) {+ const elapsed = timestamp - state.current.prevTimestamp;++ // to avoid a big jump when auto-scroll becomes enabled+ if (elapsed > 3000) {state.current.prevTimestamp = timestamp;scheduleStep();return;}- const elapsed = timestamp - state.current.prevTimestamp;-if (elapsed < FRAME_PERIOD) {scheduleStep();return;@@ -91,7 +93,12 @@ export const useAutoScroll = ({state.current.prevTimestamp = timestamp;- target.current.scrollBy(+ // for TypeScript+ if (state.current.target === null) {+ return;+ }++ state.current.target.scrollBy((state.current.speedX / 1000) * elapsed,(state.current.speedY / 1000) * elapsed);@@ -108,11 +115,11 @@ export const useAutoScroll = ({return {handleMove({ x, y }) {- if (!state.current.enabled || target.current === null) {+ if (!state.current.enabled || state.current.target === null) {return;}- const rect = target.current.getBoundingClientRect();+ const rect = state.current.target.getBoundingClientRect();state.current.speedY = getSpeed(y,@@ -136,8 +143,10 @@ export const useAutoScroll = ({},setEnabled(newEnabled) {state.current.enabled = newEnabled;- state.current.prevTimestamp = undefined;scheduleStep();},+ targetRef: (element) => {+ state.current.target = element;+ },};};diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.tsindex 85ff3a35cd..c1fa71c12e 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts@@ -34,7 +34,7 @@ export const useDrag = ({onMove,onShiftChange,onEnd,-}: Parameters) => {+}: Parameters): React.HTMLAttributes<HTMLElement> => {const state = useRef<State>(initialState);const cancel = () => {diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.tsindex 9a70ef4e77..2893d7b2d6 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts@@ -14,77 +14,110 @@ export type Parameters = {onHold: (event: { target: HTMLElement }) => void;};+export type Handlers = {+ handleMove: (pointerCoordinate: Coordinate) => void;+ handleScroll: () => void;+ handleEnd: () => void;+ rootRef: (element: HTMLElement | null) => void;+};+export const useDropTarget = ({isDropTarget,edgeDistanceThreshold = 3,holdTimeThreshold = 500,onDropTargetChange,onHold,-}: Parameters) => {- const rootRef = useRef<HTMLElement | null>(null);- const targetRef = useRef<HTMLElement>();- const targetRectRef = useRef<DOMRect>();- const areaRef = useRef<Area>("center");- const holdTimeoutRef = useRef<NodeJS.Timeout>();+}: Parameters): Handlers => {+ const state = useRef({+ root: null as HTMLElement | null,+ target: undefined as HTMLElement | undefined,+ area: "center" as Area,+ rect: undefined as DOMRect | undefined,+ holdTimeout: undefined as NodeJS.Timeout | undefined,+ pointerCoordinate: undefined as Coordinate | undefined,+ });++ const clearHoldTimeout = () => {+ if (state.current.holdTimeout) {+ clearTimeout(state.current.holdTimeout);+ state.current.holdTimeout = undefined;+ }+ };const detectHold = (target: HTMLElement) => {- clearTimeout(holdTimeoutRef.current);- holdTimeoutRef.current = setTimeout(() => {- if (target === targetRef.current) {+ clearHoldTimeout();+ state.current.holdTimeout = setTimeout(() => {+ state.current.holdTimeout = undefined;+ if (target === state.current.target) {onHold({ target });}}, holdTimeThreshold);};- return {- handleMove(pointerCoordinate: Coordinate) {- const target = elementFromPoint(pointerCoordinate);- if (target === undefined || rootRef.current === null) {- return;- }+ const detectTarget = () => {+ const pointerCoordinate = state.current.pointerCoordinate;+ if (pointerCoordinate === undefined) {+ return;+ }- const nextTarget = findClosestDropTarget({- root: rootRef.current,- target,- isDropTarget,- });+ const target = elementFromPoint(pointerCoordinate);+ if (target === undefined || state.current.root === null) {+ return;+ }- const hasTargetChanged = nextTarget !== targetRef.current;- targetRef.current = nextTarget;+ const nextTarget = findClosestDropTarget({+ root: state.current.root,+ target,+ isDropTarget,+ });- if (hasTargetChanged) {- targetRectRef.current = nextTarget.getBoundingClientRect();- detectHold(nextTarget);- }+ const hasTargetChanged = nextTarget !== state.current.target;+ state.current.target = nextTarget;- const nextArea = getArea(- pointerCoordinate,- edgeDistanceThreshold,- targetRectRef.current- );-- const hasAreaChanged = nextArea !== areaRef.current;- areaRef.current = nextArea;-- if (- (hasTargetChanged || hasAreaChanged) &&- targetRectRef.current !== undefined- ) {- onDropTargetChange({- rect: targetRectRef.current,- area: nextArea,- target: nextTarget,- });- }+ if (hasTargetChanged) {+ detectHold(nextTarget);+ }++ const nextRect = nextTarget.getBoundingClientRect();++ const hasRectChanged = nextRect !== state.current.rect;+ state.current.rect = nextRect;++ const nextArea = getArea(+ pointerCoordinate,+ edgeDistanceThreshold,+ nextRect+ );++ const hasAreaChanged = nextArea !== state.current.area;+ state.current.area = nextArea;++ if (hasTargetChanged || hasAreaChanged || hasRectChanged) {+ onDropTargetChange({+ rect: nextRect,+ area: nextArea,+ target: nextTarget,+ });+ }+ };++ return {+ handleMove(pointerCoordinate: Coordinate) {+ state.current.pointerCoordinate = pointerCoordinate;+ detectTarget();+ },++ handleScroll() {+ detectTarget();},handleEnd() {// Clear onHold timeout if we ended interaction before it fired- clearTimeout(holdTimeoutRef.current);+ clearHoldTimeout();},rootRef(rootElement: HTMLElement | null) {- rootRef.current = rootElement;+ state.current.root = rootElement;},};};