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
Background list focus management (#1356)
Ivan Starkovd58c9aa73b18
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsxindex 58b48ba9ec..33476f4114 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx@@ -2,6 +2,8 @@ import type { RenderCategoryProps } from "../../style-sections";import { styleConfigByName } from "../../shared/configs";import { FloatingPanel } from "~/builder/shared/floating-panel";import {+ ArrowFocus,+ Box,CssValueListItem,Flex,Grid,@@ -39,7 +41,7 @@ import {import { BackgroundContent } from "./background-content";import { getLayerName, LayerThumbnail } from "./background-thumbnail";import { useSortable } from "./use-sortable";-import { useMemo } from "react";+import { useMemo, type ReactNode } from "react";import type { RgbValue, StyleProperty } from "@webstudio-is/css-data";import {CollapsibleSectionBase,@@ -47,8 +49,13 @@ import {} from "~/builder/shared/collapsible-section";import { getDots } from "../../shared/collapsible-section";+const LIST_ITEM_ATTRIBUTE = "data-list-item";++const listItemAttributes = { [LIST_ITEM_ATTRIBUTE]: true };+const Layer = (props: {id: string;+ tabIndex: -1 | 0;isHighlighted: boolean;layerStyle: StyleInfo;setProperty: SetBackgroundProperty;@@ -94,6 +101,7 @@ const Layer = (props: {<CssValueListItemactive={props.isHighlighted}data-id={props.id}+ tabIndex={props.tabIndex}label={<Label truncate onReset={props.deleteLayer}>{getLayerName(props.layerStyle)}@@ -108,18 +116,19 @@ const Layer = (props: {pressed={isHidden}onPressedChange={handleHiddenChange}variant="normal"- tabIndex={0}+ tabIndex={-1}icon={isHidden ? <EyeconClosedIcon /> : <EyeconOpenIcon />}/><SmallIconButtonvariant="destructive"- tabIndex={0}+ tabIndex={-1}icon={<SubtractIcon />}onClick={props.deleteLayer}/></>}+ {...listItemAttributes}/></FloatingPanel>);@@ -187,6 +196,28 @@ const BackgroundsCollapsibleSection = ({);};+const ListItemsFocusWrap = (props: { children: ReactNode }) => {+ return (+ <ArrowFocus+ render={({ handleKeyDown }) => (+ <Box+ css={{ display: "contents" }}+ onKeyDown={(event) => {+ if (event.key === "ArrowUp" || event.key === "ArrowDown") {+ handleKeyDown(event, {+ accept: (element) =>+ element.getAttribute(LIST_ITEM_ATTRIBUTE) === "true",+ });+ }+ }}+ >+ {props.children}+ </Box>+ )}+ />+ );+};+export const BackgroundsSection = (props: RenderCategoryProps) => {const { setProperty, deleteProperty, currentStyle, createBatchUpdate } =props;@@ -219,47 +250,50 @@ export const BackgroundsSection = (props: RenderCategoryProps) => {category={props.category}><Flex gap={1} direction="column">- <Flex- gap={1}- direction="column"- ref={sortableRefCallback}- css={{- pointerEvents: dragItemId ? "none" : "auto",- // to make DnD work we have to disable scrolling using touch- touchAction: "none",- }}- >- {layers.map((layer) => (- <Layer- id={layer.id}- key={layer.id}- isHighlighted={dragItemId === layer.id}- layerStyle={getLayerBackgroundStyleInfo(- layer.index,- currentStyle- )}- deleteLayer={deleteLayer(- layer.index,- currentStyle,- createBatchUpdate- )}- setProperty={setLayerProperty(- layer.index,- currentStyle,- createBatchUpdate- )}- deleteProperty={deleteLayerProperty(- layer.index,- currentStyle,- deleteProperty,- createBatchUpdate- )}- setBackgroundColor={setProperty("backgroundColor")}- />- ))}+ <ListItemsFocusWrap>+ <Flex+ gap={1}+ direction="column"+ ref={sortableRefCallback}+ css={{+ pointerEvents: dragItemId ? "none" : "auto",+ // to make DnD work we have to disable scrolling using touch+ touchAction: "none",+ }}+ >+ {layers.map((layer, index) => (+ <Layer+ id={layer.id}+ tabIndex={index === 0 ? 0 : -1}+ key={layer.id}+ isHighlighted={dragItemId === layer.id}+ layerStyle={getLayerBackgroundStyleInfo(+ layer.index,+ currentStyle+ )}+ deleteLayer={deleteLayer(+ layer.index,+ currentStyle,+ createBatchUpdate+ )}+ setProperty={setLayerProperty(+ layer.index,+ currentStyle,+ createBatchUpdate+ )}+ deleteProperty={deleteLayerProperty(+ layer.index,+ currentStyle,+ deleteProperty,+ createBatchUpdate+ )}+ setBackgroundColor={setProperty("backgroundColor")}+ />+ ))}- {placementIndicator}- </Flex>+ {placementIndicator}+ </Flex>+ </ListItemsFocusWrap><Gridcss={{diff --git a/packages/design-system/src/components/css-value-list-item.stories.tsx b/packages/design-system/src/components/css-value-list-item.stories.tsxindex 93135e1e42..4d7a0e4118 100644--- a/packages/design-system/src/components/css-value-list-item.stories.tsx+++ b/packages/design-system/src/components/css-value-list-item.stories.tsx@@ -15,6 +15,9 @@ import {FloatingPanelPopoverTrigger,} from "./floating-panel-popover";import { StorySection, StoryGrid } from "./storybook";+import { ArrowFocus } from "./primitives/arrow-focus";+import { Box } from "./box";+import type { ReactNode } from "react";export default {component: CssValueListItem,@@ -30,6 +33,10 @@ export default {title: "Library/CSS Value List Item",};+const LIST_ITEM_ATTRIBUTE = "data-list-item";++const listItemAttributes = { [LIST_ITEM_ATTRIBUTE]: true };+const Thumbnail = styled("div", {width: theme.spacing[10],height: theme.spacing[10],@@ -47,6 +54,7 @@ const ListItem = (props: {active: boolean;focused: undefined | boolean;label?: React.ReactNode;+ tabIndex?: -1 | 0;}) => {const [pressed, onPressedChange] = React.useState(false);@@ -62,27 +70,51 @@ const ListItem = (props: {state={props.state}focused={props.focused}active={props.active}+ tabIndex={props.tabIndex ?? 0}buttons={<><SmallToggleButtonpressed={pressed}onPressedChange={onPressedChange}variant="normal"- tabIndex={0}+ tabIndex={-1}icon={pressed ? <EyeconClosedIcon /> : <EyeconOpenIcon />}/><SmallIconButtonvariant="destructive"- tabIndex={0}+ tabIndex={-1}icon={<SubtractIcon />}/></>}+ {...listItemAttributes}/>);};+const ListItemsFocusWrap = (props: { children: ReactNode }) => {+ return (+ <ArrowFocus+ render={({ handleKeyDown }) => (+ <Box+ css={{ display: "contents" }}+ onKeyDown={(event) => {+ if (event.key === "ArrowUp" || event.key === "ArrowDown") {+ handleKeyDown(event, {+ accept: (element) => {+ return element.getAttribute(LIST_ITEM_ATTRIBUTE) === "true";+ },+ });+ }+ }}+ >+ {props.children}+ </Box>+ )}+ />+ );+};export const Declarative = (props: {hidden: boolean;focused: boolean;@@ -110,13 +142,13 @@ export const Declarative = (props: {pressed={pressed}onPressedChange={onPressedChange}variant="normal"- tabIndex={0}+ tabIndex={-1}icon={pressed ? <EyeconClosedIcon /> : <EyeconOpenIcon />}/><SmallIconButtonvariant="destructive"- tabIndex={0}+ tabIndex={-1}icon={<SubtractIcon />}/></>@@ -131,105 +163,118 @@ export const Declarative = (props: {<StorySection title="Overflows"><StoryGrid>- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={false}- active={false}- labelColor={labelColor}- state={undefined}- focused={false}- label="Very long text, very long text"- />- )- )}+ <ListItemsFocusWrap>+ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor, index) => (+ <ListItem+ key={labelColor}+ tabIndex={index === 0 ? 0 : -1}+ hidden={false}+ active={false}+ labelColor={labelColor}+ state={undefined}+ focused={false}+ label="Very long text, very long text"+ />+ )+ )}+ </ListItemsFocusWrap></StoryGrid></StorySection><StorySection title="Variants"><StoryGrid>- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={false}- active={false}- labelColor={labelColor}- state={undefined}- focused={false}- />- )- )}-- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={true}- active={false}- labelColor={labelColor}- state={undefined}- focused={false}- />- )- )}-- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={false}- active={false}- labelColor={labelColor}- state={"open"}- focused={false}- />- )- )}+ <ListItemsFocusWrap>+ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor, index) => (+ <ListItem+ key={labelColor}+ tabIndex={index === 0 ? 0 : -1}+ hidden={false}+ active={false}+ labelColor={labelColor}+ state={undefined}+ focused={false}+ />+ )+ )}++ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor) => (+ <ListItem+ key={labelColor}+ tabIndex={-1}+ hidden={true}+ active={false}+ labelColor={labelColor}+ state={undefined}+ focused={false}+ />+ )+ )}++ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor) => (+ <ListItem+ key={labelColor}+ tabIndex={-1}+ hidden={false}+ active={false}+ labelColor={labelColor}+ state={"open"}+ focused={false}+ />+ )+ )}+ </ListItemsFocusWrap></StoryGrid></StorySection><StorySection title="Active"><StoryGrid>- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={false}- active={true}- labelColor={labelColor}- state={undefined}- focused={false}- />- )- )}-- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={true}- active={true}- labelColor={labelColor}- state={undefined}- focused={false}- />- )- )}-- {(["default", "preset", "local", "remote"] as const).map(- (labelColor) => (- <ListItem- key={labelColor}- hidden={false}- active={true}- labelColor={labelColor}- state={"open"}- focused={false}- />- )- )}+ <ListItemsFocusWrap>+ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor, index) => (+ <ListItem+ key={labelColor}+ tabIndex={index === 0 ? 0 : -1}+ hidden={false}+ active={true}+ labelColor={labelColor}+ state={undefined}+ focused={false}+ />+ )+ )}++ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor, index) => (+ <ListItem+ key={labelColor}+ tabIndex={-1}+ hidden={true}+ active={true}+ labelColor={labelColor}+ state={undefined}+ focused={false}+ />+ )+ )}++ {(["default", "preset", "local", "remote"] as const).map(+ (labelColor, index) => (+ <ListItem+ key={labelColor}+ tabIndex={-1}+ hidden={false}+ active={true}+ labelColor={labelColor}+ state={"open"}+ focused={false}+ />+ )+ )}+ </ListItemsFocusWrap></StoryGrid></StorySection></Panel>diff --git a/packages/design-system/src/components/css-value-list-item.tsx b/packages/design-system/src/components/css-value-list-item.tsxindex 5d0c11a5d1..f03f7e64fb 100644--- a/packages/design-system/src/components/css-value-list-item.tsx+++ b/packages/design-system/src/components/css-value-list-item.tsx@@ -9,6 +9,7 @@ import { styled } from "../stitches.config";import { Flex } from "./flex";import { theme } from "../stitches.config";import { DragHandleIcon } from "@webstudio-is/icons";+import { ArrowFocus } from "./primitives/arrow-focus";const DragHandleIconStyled = styled(DragHandleIcon, {visibility: "hidden",@@ -137,41 +138,54 @@ export const CssValueListItem = forwardRef();return (- <ItemWrapper>- <ItemButton- ref={ref}- data-focused={focused}- data-state={state ?? dataState}- data-active={active}- {...rest}- tabIndex={0}- >- <DragHandleIconStyled />-- <Flex gap={2} shrink>- <ThumbHolder>{thumbnail}</ThumbHolder>- {label}- </Flex>-- <Flex grow={true} />-- {/*+ <ArrowFocus+ render={({ handleKeyDown }) => (+ <ItemWrapper+ onKeyDown={(event) => {+ if (event.key === "ArrowLeft" || event.key === "ArrowRight") {+ handleKeyDown(event);+ }+ }}+ >+ <ItemButton+ ref={ref}+ data-focused={focused}+ data-state={state ?? dataState}+ data-active={active}+ {...rest}+ >+ <DragHandleIconStyled />++ <Flex gap={2} shrink>+ <ThumbHolder>{thumbnail}</ThumbHolder>+ {label}+ </Flex>++ <Flex grow={true} />++ {/*We place fake divs with same dimensions as small buttons here to avoid following warning:Warning: validateDOMNesting(...): <button> cannot appear as a descendant of <button>Real buttons will be placed on top of fake buttons*/}- <Flex shrink={false} css={{ paddingLeft: theme.spacing[5] }} gap={2}>- {fakeButtons}- </Flex>- </ItemButton>-- {/*+ <Flex+ shrink={false}+ css={{ paddingLeft: theme.spacing[5] }}+ gap={2}+ >+ {fakeButtons}+ </Flex>+ </ItemButton>++ {/*Real buttons are placed above ItemButton to avoid <button> cannot appear as a descendant of <button> warning*/}- <IconButtonsWrapper gap={2} align="center">- {buttons}- </IconButtonsWrapper>- </ItemWrapper>+ <IconButtonsWrapper gap={2} align="center">+ {buttons}+ </IconButtonsWrapper>+ </ItemWrapper>+ )}+ />);});diff --git a/packages/design-system/src/components/primitives/arrow-focus.tsx b/packages/design-system/src/components/primitives/arrow-focus.tsxindex a8a48740db..ea0ce08e93 100644--- a/packages/design-system/src/components/primitives/arrow-focus.tsx+++ b/packages/design-system/src/components/primitives/arrow-focus.tsx@@ -1,8 +1,15 @@-import { FocusScope, useFocusManager } from "@react-aria/focus";+import {+ FocusScope,+ useFocusManager,+ type FocusManagerOptions,+} from "@react-aria/focus";import type { KeyboardEvent } from "react";type Render = (props: {- handleKeyDown: (event: KeyboardEvent) => void;+ handleKeyDown: (+ event: KeyboardEvent,+ focusManagerOptions?: FocusManagerOptions+ ) => void;focusManager: ReturnType<typeof useFocusManager>;}) => JSX.Element;@@ -12,12 +19,17 @@ const ContextHelper = ({ render }: { render: Render }) => {const focusManager = useFocusManager();return render({- handleKeyDown: (event: KeyboardEvent) => {+ handleKeyDown: (+ event: KeyboardEvent,+ focusManagerOptions?: FocusManagerOptions+ ) => {if (event.key === "ArrowRight" || event.key === "ArrowDown") {- focusManager.focusNext({ wrap: true });+ focusManager.focusNext({ wrap: true, ...focusManagerOptions });+ event.preventDefault(); // Prevents the page from scrolling}if (event.key === "ArrowLeft" || event.key === "ArrowUp") {- focusManager.focusPrevious({ wrap: true });+ focusManager.focusPrevious({ wrap: true, ...focusManagerOptions });+ event.preventDefault(); // Prevents the page from scrolling}},focusManager,diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex 55bb84963c..f49ace956e 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -105,7 +105,12 @@ const getInitialState = <Data>() => {*/export const defaultGetValidChildren = (parent: Element) =>Array.from(parent.children).filter(- (element) => element.hasAttribute("data-placement-indicator") === false+ (element) =>+ // The 'hidden' attribute is utilized by the react-arial FocusScope+ // as helper elements to enable focus lock, focus wrap, and other features.+ // We should exclude them from the list of children.+ element.hasAttribute("hidden") === false &&+ element.hasAttribute("data-placement-indicator") === false);export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {