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.

finish keyboard navigation & minor fixes

Roman Pominov904e3864cad9
diff --git a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
index f8349bdaf1..3fa7af1dfc 100644
--- a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
@@ -23,10 +23,10 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {
<Tree
root={rootInstance}
selectedInstanceId={selectedInstanceData?.id}
- onSelect={(instance) => {
+ onSelect={(instanceId) => {
publish<"selectInstanceById", Instance["id"]>({
type: "selectInstanceById",
- payload: instance.id,
+ payload: instanceId,
});
}}
onDragEnd={(payload) => {
diff --git a/apps/designer/app/designer/shared/tree/tree-node.tsx b/apps/designer/app/designer/shared/tree/tree-node.tsx
index 8f15019fe2..189e342260 100644
--- a/apps/designer/app/designer/shared/tree/tree-node.tsx
+++ b/apps/designer/app/designer/shared/tree/tree-node.tsx
@@ -9,7 +9,6 @@ import {
Box,
} from "@webstudio-is/design-system";
import { TriangleRightIcon, TriangleDownIcon } from "@webstudio-is/icons";
-import noop from "lodash.noop";
export const INDENT = 16;
const ITEM_HEIGHT = 32;
@@ -177,7 +176,7 @@ type TreeNodeProps = {
selectedInstanceId?: Instance["id"];
parentIsSelected?: boolean;
level?: number;
- onSelect?: (instance: Instance) => void;
+ onSelect?: (instanceId: Instance["id"]) => void;
animate?: boolean;
getIsExpanded: (instance: Instance) => boolean;
setIsExpanded?: (instance: Instance, expanded: boolean) => void;
@@ -189,10 +188,10 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
const {
getIsExpanded,
animate = true,
- setIsExpanded = noop,
+ setIsExpanded,
selectedInstanceId,
- onSelect = noop,
- onExpandTransitionEnd = noop,
+ onSelect,
+ onExpandTransitionEnd,
disableHoverStates = false,
} = commonProps;
@@ -205,7 +204,7 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
const makeSelected = () => {
if (isSelected === false) {
- onSelect(instance);
+ onSelect?.(instance.id);
}
};
@@ -220,7 +219,7 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
const handleAnimationEnd = useCallback(
(event: React.AnimationEvent<HTMLDivElement>) => {
if (event.target === collapsibleContentRef.current) {
- onExpandTransitionEnd();
+ onExpandTransitionEnd?.();
}
},
[onExpandTransitionEnd]
@@ -230,7 +229,7 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
const prevIsExpanded = useRef(isExpanded);
useEffect(() => {
if (animate === false && isExpanded !== prevIsExpanded.current) {
- onExpandTransitionEnd();
+ onExpandTransitionEnd?.();
}
prevIsExpanded.current = isExpanded;
}, [animate, onExpandTransitionEnd, isExpanded]);
@@ -239,7 +238,7 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
<Collapsible.Root
ref={ref}
open={isExpanded}
- onOpenChange={(isOpen) => setIsExpanded(instance, isOpen)}
+ onOpenChange={(isOpen) => setIsExpanded?.(instance, isOpen)}
data-drop-target-id={instance.id}
>
<ItemWrapper
@@ -254,6 +253,8 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
{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 />}
@@ -262,9 +263,8 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(
<ItemButton
type="button"
data-drag-item-id={instance.id}
+ data-item-button-id={instance.id}
onFocus={makeSelected}
- // Focus on click works inconsistently
- onClick={(event) => event.currentTarget.focus()}
enableHoverState={disableHoverStates === false}
>
<Icon />
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index cd776bb5ab..8765c8119e 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -36,7 +36,7 @@ import { useHotkeys } from "react-hotkeys-hook";
type TreeProps = {
root: Instance;
selectedInstanceId?: Instance["id"];
- onSelect?: (instance: Instance) => void;
+ onSelect?: (instanceId: Instance["id"]) => void;
animate?: boolean;
onDragEnd: (event: {
instanceId: Instance["id"];
@@ -322,7 +322,7 @@ export const Tree = ({
return instance;
},
onStart: ({ data }) => {
- onSelect?.(data);
+ onSelect?.(data.id);
setDragItem(data);
dropHandlers.handleStart();
autoScrollHandlers.setEnabled(true);
@@ -362,6 +362,74 @@ export const Tree = ({
useDragCursor(dragItem !== undefined);
+ const keyboardNavigation = useKeyboardNavigation({
+ root,
+ selectedInstanceId,
+ getIsExpanded,
+ setIsExpanded,
+ });
+
+ return (
+ <Box
+ css={{
+ width: "100%",
+ overflowY: "auto",
+ overflowX: "hidden",
+ flexBasis: 0,
+ flexGrow: 1,
+ pt: 2,
+ pb: 2,
+ }}
+ ref={autoScrollHandlers.targetRef}
+ onScroll={dropHandlers.handleScroll}
+ >
+ <Box
+ ref={keyboardNavigation.rootRef}
+ onClick={keyboardNavigation.handleClick}
+ >
+ <TreeNode
+ animate={animate}
+ onSelect={onSelect}
+ selectedInstanceId={selectedInstanceId}
+ instance={root}
+ level={0}
+ getIsExpanded={getIsExpanded}
+ setIsExpanded={setIsExpanded}
+ onExpandTransitionEnd={dropHandlers.handleDomMutation}
+ disableHoverStates={dragItem !== undefined}
+ ref={(element) => {
+ rootNodeRef.current = element;
+ dragHandlers.rootRef(element);
+ dropHandlers.rootRef(element);
+ }}
+ />
+ </Box>
+ {finalDropTarget &&
+ createPortal(
+ finalDropTarget.placement.type === "rect" ? (
+ <PlacementIndicatorOutline rect={finalDropTarget.placement.rect} />
+ ) : (
+ <PlacementIndicatorLine
+ placement={finalDropTarget.placement.placement}
+ />
+ ),
+ document.body
+ )}
+ </Box>
+ );
+};
+
+const useKeyboardNavigation = ({
+ root,
+ selectedInstanceId,
+ getIsExpanded,
+ setIsExpanded,
+}: {
+ root: Instance;
+ selectedInstanceId: Instance["id"] | undefined;
+ getIsExpanded: (instance: Instance) => boolean;
+ setIsExpanded: (instance: Instance, isExpanded: boolean) => void;
+}) => {
const selectedInstance = useMemo(() => {
if (selectedInstanceId === undefined) {
return undefined;
@@ -369,9 +437,24 @@ export const Tree = ({
return findInstanceById(root, selectedInstanceId);
}, [root, selectedInstanceId]);
- const hotkeysRef = useHotkeys(
+ const flatCurrentlyExpandedTree = useMemo(() => {
+ const result = [] as Instance["id"][];
+ const traverse = (instance: Instance | string) => {
+ if (typeof instance === "string") {
+ return;
+ }
+ result.push(instance.id);
+ if (getIsExpanded(instance)) {
+ instance.children.forEach(traverse);
+ }
+ };
+ traverse(root);
+ return result;
+ }, [root, getIsExpanded]);
+
+ const rootRef = useHotkeys(
"up,down,right,left,space",
- (_, { shortcut }) => {
+ (event, { shortcut }) => {
if (selectedInstance === undefined) {
return;
}
@@ -385,61 +468,61 @@ export const Tree = ({
setIsExpanded(selectedInstance, !getIsExpanded(selectedInstance));
}
if (shortcut === "up") {
- // @todo
+ const index = flatCurrentlyExpandedTree.indexOf(selectedInstance.id);
+ if (index > 0) {
+ setFocus(flatCurrentlyExpandedTree[index - 1]);
+
+ // prevent scrolling
+ event.preventDefault();
+ }
}
if (shortcut === "down") {
- // @todo
+ const index = flatCurrentlyExpandedTree.indexOf(selectedInstance.id);
+ if (index < flatCurrentlyExpandedTree.length - 1) {
+ setFocus(flatCurrentlyExpandedTree[index + 1]);
+
+ // prevent scrolling
+ event.preventDefault();
+ }
}
},
- [selectedInstance, getIsExpanded, setIsExpanded]
+ [selectedInstance, flatCurrentlyExpandedTree, getIsExpanded, setIsExpanded]
);
- return (
- <Box
- css={{
- width: "100%",
- overflowY: "auto",
- overflowX: "hidden",
- flexBasis: 0,
- flexGrow: 1,
- pt: 2,
- pb: 2,
- }}
- ref={(element) => {
- autoScrollHandlers.targetRef(element);
- hotkeysRef.current = element;
- }}
- onScroll={dropHandlers.handleScroll}
- >
- <TreeNode
- animate={animate}
- onSelect={onSelect}
- selectedInstanceId={selectedInstanceId}
- instance={root}
- level={0}
- getIsExpanded={getIsExpanded}
- setIsExpanded={setIsExpanded}
- onExpandTransitionEnd={dropHandlers.handleDomMutation}
- disableHoverStates={dragItem !== undefined}
- ref={(element) => {
- rootNodeRef.current = element;
- dragHandlers.rootRef(element);
- dropHandlers.rootRef(element);
- }}
- />
- {finalDropTarget &&
- createPortal(
- finalDropTarget.placement.type === "rect" ? (
- <PlacementIndicatorOutline rect={finalDropTarget.placement.rect} />
- ) : (
- <PlacementIndicatorLine
- placement={finalDropTarget.placement.placement}
- />
- ),
- document.body
- )}
- </Box>
+ const setFocus = useCallback(
+ (instanceId: Instance["id"]) => {
+ const itemButton = rootRef.current?.querySelector(
+ `[data-item-button-id="${instanceId}"]`
+ );
+ if (itemButton instanceof HTMLElement) {
+ itemButton.focus();
+ }
+ },
+ [rootRef]
);
+
+ return {
+ rootRef(element: HTMLElement | null) {
+ rootRef.current = element;
+ },
+ handleClick(event: React.MouseEvent<Element>) {
+ // When clicking on an item button make sure it gets focused.
+ // (see https://zellwk.com/blog/inconsistent-button-behavior/)
+ const itemButton = (event.target as HTMLElement).closest(
+ "[data-item-button-id]"
+ );
+ if (itemButton instanceof HTMLElement) {
+ itemButton.focus();
+ return;
+ }
+
+ // When clicking anywhere else in the tree,
+ // make sure the selected item doesn't loose focus.
+ if (selectedInstanceId !== undefined) {
+ setFocus(selectedInstanceId);
+ }
+ },
+ };
};
const useExpandState = ({
@@ -481,9 +564,14 @@ const useExpandState = ({
const getIsExpanded = useCallback(
(instance: Instance) => {
+ // root is always expanded
+ if (instance.id === root.id) {
+ return true;
+ }
+
return getIsExpandable(instance) && record[instance.id] === true;
},
- [record]
+ [record, root]
);
const setIsExpanded = useCallback((instance: Instance, expanded: boolean) => {