Improve keyboard navigation on sidebar tree items
This commit is contained in:
@@ -2,7 +2,7 @@ import * as React from "react";
|
|||||||
import Tooltip, { Props as TooltipProps } from "~/components/Tooltip";
|
import Tooltip, { Props as TooltipProps } from "~/components/Tooltip";
|
||||||
import { Action, ActionContext } from "~/types";
|
import { Action, ActionContext } from "~/types";
|
||||||
|
|
||||||
export type Props = {
|
export type Props = React.HTMLAttributes<HTMLButtonElement> & {
|
||||||
/** Show the button in a disabled state */
|
/** Show the button in a disabled state */
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
/** Hide the button entirely if action is not applicable */
|
/** Hide the button entirely if action is not applicable */
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import styled, { css } from "styled-components";
|
import styled, { css } from "styled-components";
|
||||||
import NudeButton from "~/components/NudeButton";
|
import NudeButton from "~/components/NudeButton";
|
||||||
|
|
||||||
type Props = {
|
type Props = React.ComponentProps<typeof Button> & {
|
||||||
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
||||||
expanded: boolean;
|
expanded: boolean;
|
||||||
root?: boolean;
|
root?: boolean;
|
||||||
|
|||||||
@@ -291,6 +291,21 @@ function InnerDocumentLink(
|
|||||||
const isExpanded = expanded && !isDragging;
|
const isExpanded = expanded && !isDragging;
|
||||||
const hasChildren = nodeChildren.length > 0;
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<Relative onDragLeave={resetHoverExpanding}>
|
<Relative onDragLeave={resetHoverExpanding}>
|
||||||
@@ -299,6 +314,7 @@ function InnerDocumentLink(
|
|||||||
ref={drag}
|
ref={drag}
|
||||||
$isDragging={isDragging}
|
$isDragging={isDragging}
|
||||||
$isMoving={isMoving}
|
$isMoving={isMoving}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
>
|
>
|
||||||
<div ref={dropToReparent}>
|
<div ref={dropToReparent}>
|
||||||
<DropToImport documentId={node.id} activeClassName="activeDropZone">
|
<DropToImport documentId={node.id} activeClassName="activeDropZone">
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ function SidebarLink(
|
|||||||
expanded={expanded}
|
expanded={expanded}
|
||||||
onClick={onDisclosureClick}
|
onClick={onDisclosureClick}
|
||||||
root={depth === 0}
|
root={depth === 0}
|
||||||
|
tabIndex={-1}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{icon && <IconWrapper>{icon}</IconWrapper>}
|
{icon && <IconWrapper>{icon}</IconWrapper>}
|
||||||
|
|||||||
Reference in New Issue
Block a user