From b68e58fad5df048f93fbcb9d5c2f3f3e7604b6a5 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 17 Sep 2022 19:47:16 -0400 Subject: [PATCH] Improve keyboard navigation on sidebar tree items --- app/components/ActionButton.tsx | 2 +- app/components/Sidebar/components/Disclosure.tsx | 2 +- .../Sidebar/components/DocumentLink.tsx | 16 ++++++++++++++++ .../Sidebar/components/SidebarLink.tsx | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/components/ActionButton.tsx b/app/components/ActionButton.tsx index 4472a0e6d..863eef5c8 100644 --- a/app/components/ActionButton.tsx +++ b/app/components/ActionButton.tsx @@ -2,7 +2,7 @@ import * as React from "react"; import Tooltip, { Props as TooltipProps } from "~/components/Tooltip"; import { Action, ActionContext } from "~/types"; -export type Props = { +export type Props = React.HTMLAttributes & { /** Show the button in a disabled state */ disabled?: boolean; /** Hide the button entirely if action is not applicable */ diff --git a/app/components/Sidebar/components/Disclosure.tsx b/app/components/Sidebar/components/Disclosure.tsx index 568f77d04..eb6236f5f 100644 --- a/app/components/Sidebar/components/Disclosure.tsx +++ b/app/components/Sidebar/components/Disclosure.tsx @@ -4,7 +4,7 @@ import { useTranslation } from "react-i18next"; import styled, { css } from "styled-components"; import NudeButton from "~/components/NudeButton"; -type Props = { +type Props = React.ComponentProps & { onClick?: React.MouseEventHandler; expanded: boolean; root?: boolean; diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index c0519f293..62bc8887f 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -291,6 +291,21 @@ function InnerDocumentLink( const isExpanded = expanded && !isDragging; const hasChildren = nodeChildren.length > 0; + const handleKeyDown = React.useCallback( + (ev: React.KeyboardEvent) => { + if (!hasChildren) { + return; + } + if (ev.key === "ArrowRight" && !expanded) { + setExpanded(true); + } + if (ev.key === "ArrowLeft" && expanded) { + setExpanded(false); + } + }, + [hasChildren, expanded] + ); + return ( <> @@ -299,6 +314,7 @@ function InnerDocumentLink( ref={drag} $isDragging={isDragging} $isMoving={isMoving} + onKeyDown={handleKeyDown} >
diff --git a/app/components/Sidebar/components/SidebarLink.tsx b/app/components/Sidebar/components/SidebarLink.tsx index 49e6041bd..1b4fc722f 100644 --- a/app/components/Sidebar/components/SidebarLink.tsx +++ b/app/components/Sidebar/components/SidebarLink.tsx @@ -104,6 +104,7 @@ function SidebarLink( expanded={expanded} onClick={onDisclosureClick} root={depth === 0} + tabIndex={-1} /> )} {icon && {icon}}