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
Fix spacing issues (#1870)
Ivan Starkov227fecfcb356
diff --git a/apps/builder/app/builder/features/style-panel/sections/space/keyboard.ts b/apps/builder/app/builder/features/style-panel/sections/space/keyboard.tsindex 0590993ade..90bd3c57e6 100644--- a/apps/builder/app/builder/features/style-panel/sections/space/keyboard.ts+++ b/apps/builder/app/builder/features/style-panel/sections/space/keyboard.ts@@ -1,5 +1,5 @@-import { useState } from "react";-import type { FocusEvent, KeyboardEvent, MouseEvent } from "react";+import { useState, useRef } from "react";+import type { FocusEvent, KeyboardEvent } from "react";import type { SpaceStyleProperty } from "./types";const movementKeys = [@@ -28,33 +28,48 @@ export const useKeyboardNavigation = ({const [activeProperty, setActiveProperty] =useState<SpaceStyleProperty>("marginTop");+ const [hoverActiveProperty, setHoverActiveProperty] =+ useState<SpaceStyleProperty>("marginTop");+const [isActive, setIsActive] = useState(false);- const hadnleFocus = (event: FocusEvent<HTMLElement>) => {+ const isMouseInsideRef = useRef(false);++ const handleActiveChange = (value: boolean) => {+ setIsActive(value);++ if (value === false) {+ setActiveProperty(hoverActiveProperty);+ }+ };++ const handleFocus = (event: FocusEvent<Element>) => {if (event.currentTarget.matches(":focus-visible")) {- setIsActive(true);+ handleActiveChange(true);}};const handleBlur = () => {- setIsActive(false);+ handleActiveChange(false);};const handleHover = (property: SpaceStyleProperty | undefined) => {- // switch to mouse navigation if user starts to use mouse- setIsActive(false);-// keep active property in sync with hover (makes UX more intuitive)if (property) {- setActiveProperty(property);+ setHoverActiveProperty(property);+ if (isActive === false) {+ setActiveProperty(property);+ }}};- // switch back to keyboard navigation on mouse leave- const handleMouseLeave = (event: MouseEvent<HTMLElement>) => {- if (event.currentTarget.matches(":focus-visible")) {- setIsActive(true);- }+ const handleMouseMove = () => {+ handleActiveChange(false);+ isMouseInsideRef.current = true;+ };++ const handleMouseLeave = () => {+ isMouseInsideRef.current = false;};const handleKeyDown = (event: KeyboardEvent<HTMLElement>) => {@@ -63,12 +78,6 @@ export const useKeyboardNavigation = ({return;}- // 1. handle transition from :focus to :focus-visible- // 2. switch from mouse navigation back to keyboard navigation if user starts to use keyboard- if (event.currentTarget.matches(":focus-visible")) {- setIsActive(true);- }-if (event.key === "ArrowUp" ||event.key === "ArrowRight" ||@@ -77,12 +86,18 @@ export const useKeyboardNavigation = ({) {event.preventDefault(); // prevent scrollingconst key = event.key;- setActiveProperty(- (property) => movementMap[property][movementKeys.indexOf(key)]- );++ handleActiveChange(true);++ if (isActive || isMouseInsideRef.current) {+ setActiveProperty(+ (property) => movementMap[property][movementKeys.indexOf(key)]+ );+ }}if (event.key === "Enter") {+ handleActiveChange(true);event.preventDefault(); // not sure we need this, but just in caseonOpen(activeProperty);}@@ -92,11 +107,11 @@ export const useKeyboardNavigation = ({activeProperty,isActive,handleHover,-// these are supposed to be put on the root element of the control- hadnleFocus,+ handleMouseMove,+ handleMouseLeave,+ handleFocus,handleBlur,handleKeyDown,- handleMouseLeave,};};diff --git a/apps/builder/app/builder/features/style-panel/sections/space/layout.tsx b/apps/builder/app/builder/features/style-panel/sections/space/layout.tsxindex a96bb75aa0..9c00aee87c 100644--- a/apps/builder/app/builder/features/style-panel/sections/space/layout.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/space/layout.tsx@@ -236,6 +236,7 @@ type LayoutProps = {onKeyDown?: ComponentProps<"div">["onKeyDown"];onClick?: ComponentProps<"div">["onClick"];onMouseLeave?: ComponentProps<"div">["onMouseLeave"];+ onMouseMove?: ComponentProps<"div">["onMouseMove"];onHover: (hoverTarget: HoverTagret | undefined) => void;activeProperties?: ReadonlyArray<SpaceStyleProperty>;renderCell: (args: { property: SpaceStyleProperty }) => React.ReactNode;@@ -250,6 +251,7 @@ export const SpaceLayout = forwardRef(onClick,onHover,onMouseLeave,+ onMouseMove,activeProperties,renderCell,}: LayoutProps,@@ -272,11 +274,12 @@ export const SpaceLayout = forwardRef(return (<Container- onClick={onClick}onFocus={onFocus}onBlur={onBlur}+ onClick={onClick}onKeyDown={onKeyDown}onMouseLeave={onMouseLeave}+ onMouseMove={onMouseMove}tabIndex={0}ref={ref}>diff --git a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx b/apps/builder/app/builder/features/style-panel/sections/space/space.tsxindex 3ffa9d45e8..cbf85d7271 100644--- a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/space/space.tsx@@ -1,4 +1,10 @@-import { type ComponentProps, useState, useRef } from "react";+import {+ type ComponentProps,+ type FocusEvent,+ useState,+ useRef,+ type FocusEventHandler,+} from "react";import type { RenderCategoryProps } from "../../style-sections";import { SpaceLayout } from "./layout";import { ValueText } from "./value-text";@@ -10,6 +16,7 @@ import { SpaceTooltip } from "./tooltip";import { getStyleSource } from "../../shared/style-info";import { CollapsibleSection } from "../../shared/collapsible-section";import { useKeyboardNavigation } from "./keyboard";+import { useDebouncedCallback } from "use-debounce";const Cell = ({isPopoverOpen,@@ -75,6 +82,48 @@ const Cell = ({);};+/**+ * useFocusWithin does't work with popovers, implement it using debounce+ */+const useFocusWithinDebounce = (+ props: {+ onFocusWithin: FocusEventHandler<Element>;+ onBlurWithin: FocusEventHandler<Element>;+ },+ timeout: number+) => {+ const isFocusedRef = useRef(false);++ const handleFocusBlur = useDebouncedCallback(+ (isFocus: boolean, event: FocusEvent<Element>) => {+ if (isFocus && isFocusedRef.current === false) {+ isFocusedRef.current = true;+ props.onFocusWithin(event);+ return;+ }+ if (isFocus === false && isFocusedRef.current === true) {+ isFocusedRef.current = false;+ props.onBlurWithin(event);+ }+ },+ timeout+ );++ const handleFocus = (event: FocusEvent<Element>) => {+ // ...event because we debounce handleFocusBlur, and react reuses events+ handleFocusBlur(true, { ...event });+ };++ const handleBlur = (event: FocusEvent<Element>) => {+ handleFocusBlur(false, event);+ };++ return {+ onFocus: handleFocus,+ onBlur: handleBlur,+ };+};+export const SpaceSection = ({setProperty,deleteProperty,@@ -109,6 +158,14 @@ export const SpaceSection = ({onOpen: setOpenProperty,});+ const focusProps = useFocusWithinDebounce(+ {+ onFocusWithin: keyboardNavigation.handleFocus,+ onBlurWithin: keyboardNavigation.handleBlur,+ },+ 100+ );+// by deafult highlight hovered or scrubbed propertieslet activeProperties = scrubStatus.properties;@@ -127,6 +184,10 @@ export const SpaceSection = ({keyboardNavigation.handleHover(target?.property);};+ const activeProperty = keyboardNavigation.isActive+ ? keyboardNavigation.activeProperty+ : hoverTarget?.property;+return (<CollapsibleSectionlabel="Space"@@ -144,9 +205,10 @@ export const SpaceSection = ({setOpenProperty(property);}}onHover={handleHover}- onFocus={keyboardNavigation.hadnleFocus}- onBlur={keyboardNavigation.handleBlur}+ onFocus={focusProps.onFocus}+ onBlur={focusProps.onBlur}onKeyDown={keyboardNavigation.handleKeyDown}+ onMouseMove={keyboardNavigation.handleMouseMove}onMouseLeave={keyboardNavigation.handleMouseLeave}activeProperties={activeProperties}renderCell={({ property }) => (@@ -168,7 +230,7 @@ export const SpaceSection = ({onHover={handleHover}property={property}scrubStatus={scrubStatus}- isActive={activeProperties.includes(property)}+ isActive={activeProperty === property}currentStyle={currentStyle}/>)}