Skip to content

TreeView: add trailing actions [WIP] #6156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion packages/react/src/TreeView/TreeView.examples.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {GrabberIcon} from '@primer/octicons-react'
import {GearIcon, GrabberIcon} from '@primer/octicons-react'
import type {Meta, StoryFn} from '@storybook/react'
import React from 'react'
import Box from '../Box'
import {TreeView} from './TreeView'
import {IconButton} from '../Button'
import {Dialog} from '../Dialog/Dialog'

const meta: Meta = {
title: 'Components/TreeView/Examples',
Expand Down Expand Up @@ -73,4 +74,91 @@
)
}

export const TrailingActions: StoryFn = () => {
return (
<Box>
<h2>Trailing Actions: Example with direct focus</h2>
<p>Press (Command + Shift + U) to focus the trailing action button</p>
<TreeView aria-label="Issues">
<TreeView.Item id="item-1">Item 1</TreeView.Item>
<TrailingAction id="item-2">
Item 2
<TreeView.SubTree>
<TreeView.Item id="item-2-sub-task-1">sub task 1</TreeView.Item>
<TreeView.Item id="item-2-sub-task-2">sub task 2</TreeView.Item>
</TreeView.SubTree>
</TrailingAction>
<TreeView.Item id="item-3">Item 3</TreeView.Item>
</TreeView>

<h2>Trailing Actions: Example with dialog</h2>
<p> Press (Command + Shift + U) to interact with the trailing action</p>
<TreeView aria-label="Issues">
<TreeView.Item id="item-1">Item 1</TreeView.Item>
<TrailingAction id="item-2" dialogOnOpen={true}>
Item 2
<TreeView.SubTree>
<TreeView.Item id="item-2-sub-task-1">sub task 1</TreeView.Item>
<TreeView.Item id="item-2-sub-task-2">sub task 2</TreeView.Item>
</TreeView.SubTree>
</TrailingAction>
<TreeView.Item id="item-3">Item 3</TreeView.Item>
</TreeView>
</Box>
)
}

const TrailingAction: React.FC<{id: string; children: React.ReactNode; dialogOnOpen?: boolean}> = ({
id,
dialogOnOpen,
children,
}) => {
const [expanded, setExpanded] = React.useState(false)
const [dialogOpen, setDialogOpen] = React.useState(false)

const btnRef = React.useRef<HTMLButtonElement>(null)

const mockKeyboardShortcut = (event: React.KeyboardEvent) => {

Check failure on line 121 in packages/react/src/TreeView/TreeView.examples.stories.tsx

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used. Allowed unused args must match /^_/u
if (!dialogOnOpen) btnRef.current?.focus()
if (dialogOnOpen) setDialogOpen(true)
}

return (
<>
<TreeView.Item
id={id}
className="treeview-item"
expanded={expanded}
onExpandedChange={setExpanded}
onKeyDown={mockKeyboardShortcut}
>
{children}
<TreeView.TrailingAction visible>
<IconButton
icon={GearIcon}
variant="invisible"
aria-label="Item settings"
className="treeview-leading-action"
draggable="true"
onDragStart={() => {
setExpanded(false)
// other drag logic to follow
}}
onClick={() => {
setDialogOpen(true)
}}
ref={btnRef}
/>
</TreeView.TrailingAction>
</TreeView.Item>

{dialogOpen ? (
<Dialog title="My Dialog" onClose={() => setDialogOpen(false)}>
Dialog that opens when the trailing action is clicked.
</Dialog>
) : null}
</>
)
}

export default meta
12 changes: 11 additions & 1 deletion packages/react/src/TreeView/TreeView.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
cursor: pointer;
border-radius: var(--borderRadius-medium);
grid-template-columns: var(--spacer-width) var(--leading-action-width) var(--toggle-width) 1fr;
grid-template-areas: 'spacer leadingAction toggle content';
grid-template-areas: 'spacer leadingAction toggle content trailingAction';

--leading-action-width: calc(var(--has-leading-action, 0) * 1.5rem);
--spacer-width: calc(calc(var(--level) - 1) * (var(--toggle-width) / 2));
Expand Down Expand Up @@ -180,6 +180,16 @@
}
}

.TreeViewItemTrailingAction {
display: flex;
color: var(--fgColor-muted);
grid-area: trailingAction;

&>button {
flex-shrink: 1;
}
}

.TreeViewItemLevelLine {
width: 100%;
height: 100%;
Expand Down
46 changes: 46 additions & 0 deletions packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
expanded?: boolean | null
onExpandedChange?: (expanded: boolean) => void
onSelect?: (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void
onKeyDown?: (event: React.KeyboardEvent<HTMLElement>) => void
className?: string
}

Expand All @@ -179,13 +180,15 @@
className,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
onKeyDown,
},
ref,
) => {
const [slots, rest] = useSlots(children, {
leadingAction: LeadingAction,
leadingVisual: LeadingVisual,
trailingVisual: TrailingVisual,
trailingAction: TrailingAction,
})
const {expandedStateCache} = React.useContext(RootContext)
const labelId = useId()
Expand Down Expand Up @@ -251,9 +254,14 @@
event.stopPropagation()
setIsExpandedWithCache(false)
break
case 'u':
if (!event.metaKey || !event.shiftKey || !onKeyDown) return
// If the user presses `Shift + Meta + U`
onKeyDown(event)
break
}
},
[onSelect, setIsExpandedWithCache, toggle],

Check failure on line 264 in packages/react/src/TreeView/TreeView.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook React.useCallback has a missing dependency: 'onKeyDown'. Either include it or remove the dependency array. If 'onKeyDown' changes too often, find the parent component that defines it and wrap that definition in useCallback
)

const ariaDescribedByIds = [
Expand Down Expand Up @@ -364,6 +372,7 @@
</span>
{slots.trailingVisual}
</div>
{slots.trailingAction}
</div>
{subTree}
</li>
Expand Down Expand Up @@ -671,6 +680,42 @@

LeadingAction.displayName = 'TreeView.LeadingAction'
// ----------------------------------------------------------------------------
// TreeView.TrailingAction

export type TreeViewTrailingAction = {
children: React.ReactNode | ((props: {isExpanded: boolean}) => React.ReactNode)
// Provide an accessible name for the visual. This should provide information
// about what the visual indicates or represents
label?: string
visible?: boolean
}

const TrailingAction: React.FC<TreeViewTrailingAction> = props => {
const {isExpanded} = React.useContext(ItemContext)
const children = typeof props.children === 'function' ? props.children({isExpanded}) : props.children
return (
<>
<div className={clsx('PRIVATE_VisuallyHidden', classes.TreeViewVisuallyHidden)} aria-hidden={true}>
{props.label}
</div>
<div
className={clsx('PRIVATE_TreeView-item-trailing-action', classes.TreeViewItemTrailingAction)}
aria-hidden={true}
onClick={event =>
// Prevent focus event from bubbling up to parent items
// This is needed to prevent the TreeView from interfering with trailing actions
event.stopPropagation()
}
onKeyDown={event => event.stopPropagation()}
>
{children}
</div>
</>
)
}

TrailingAction.displayName = 'TreeView.TrailingAction'
// ----------------------------------------------------------------------------
// TreeView.DirectoryIcon

const DirectoryIcon = () => {
Expand Down Expand Up @@ -740,6 +785,7 @@
Item,
SubTree,
LeadingAction,
TrailingAction,
LeadingVisual,
TrailingVisual,
DirectoryIcon,
Expand Down
Loading