Skip to content
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

Sid/treeview leading action (merged to another branch) #4569

Merged
Merged
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
20 changes: 10 additions & 10 deletions packages/react/src/TreeView/TreeView.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
"name": "flat",
"type": "boolean",
"description": "Prevents the tree from indenting items. This should only be used when the tree is used to display a flat list of items."
},
{
"name": "dragAndDrop",
"type": "boolean",
"description": "Updates the css grid of the tree to have space to render the drag handle. This is currently only used for drag and drop in subissues."
}
],
"subcomponents": [
Expand Down Expand Up @@ -77,11 +72,6 @@
{
"name": "ref",
"type": "React.Ref<HTMLElement>"
},
{
"name": "dragHandle",
"type": "ReactElement",
"description": "The trigger button that activates the drag and drop of the TreeView.Item. Consumer also needs to pass `dragAndDrop` prop to TreeView so dragHandle is rendered in the correct position."
}
]
},
Expand Down Expand Up @@ -115,6 +105,16 @@
}
]
},
{
"name": "TreeView.LeadingAction",
"props": [
{
"name": "children",
"required": true,
"type": "React.ReactNode"
}
]
},
{
"name": "TreeView.DirectoryIcon",
"props": []
Expand Down
132 changes: 49 additions & 83 deletions packages/react/src/TreeView/TreeView.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
FileIcon,
GrabberIcon,
KebabHorizontalIcon,
IssueClosedIcon,
IssueOpenedIcon,
} from '@primer/octicons-react'
import type {Meta, Story} from '@storybook/react'
import React from 'react'
Expand Down Expand Up @@ -696,89 +698,6 @@ export const NestedTrees: Story = () => {
)
}

export const WithDragHandle: Story = () => {
const dragHandle = (
<IconButton sx={{p: 1, cursor: 'grab', mr: 1}} aria-label={`Move`} variant="invisible" icon={GrabberIcon} />
)

const [isLoading, setIsLoading] = React.useState(false)
const [asyncItems, setAsyncItems] = React.useState<string[]>([])

let state: SubTreeState = 'initial'

if (isLoading) {
state = 'loading'
} else if (asyncItems.length > 0) {
state = 'done'
}

return (
<nav aria-label="Files">
<TreeView aria-label="Files" dragAndDrop>
<TreeView.Item id="file-1" dragHandle={dragHandle}>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Some file
</TreeView.Item>
<TreeView.Item
id="async-directory"
dragHandle={dragHandle}
onExpandedChange={async isExpanded => {
if (asyncItems.length === 0 && isExpanded) {
setIsLoading(true)

// Load items
const items = await loadItems(1000)

setIsLoading(false)
setAsyncItems(items)
}
}}
>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
Directory with async items
<TreeView.SubTree state={state}>
{asyncItems.map(item => (
<TreeView.Item id={`item-${item}`} key={item}>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
{item}
</TreeView.Item>
))}
<TreeView.Item id="nested-directory">
Nested Sub-tree
<TreeView.SubTree state="done">
<TreeView.Item id="nested-directory/file-1">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Some file
</TreeView.Item>
<TreeView.Item id="nested-directory/another-file">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Another file
</TreeView.Item>
</TreeView.SubTree>
</TreeView.Item>
</TreeView.SubTree>
</TreeView.Item>
<TreeView.Item id="another-file" dragHandle={dragHandle}>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
Another file
</TreeView.Item>
</TreeView>
</nav>
)
}

export const NestedScrollContainer: Story = () => {
return (
<Box sx={{maxHeight: '50vh', overflow: 'auto'}}>
Expand Down Expand Up @@ -1073,4 +992,51 @@ export const WithoutIndentation: Story = () => (
</nav>
)

export const WithLeadingAction: Story = () => {
// todo: implement fold on click
// todo: implement hide until hovered
// todo: check if draggy boi should be aria-hidden
const dragAction = <IconButton aria-label="Reorder item" variant="invisible" icon={GrabberIcon} />

return (
<TreeView aria-label="Issues">
<TreeView.Item id="item-0">
<TreeView.LeadingAction>{dragAction}</TreeView.LeadingAction>
<TreeView.LeadingVisual>
<Octicon icon={IssueClosedIcon} sx={{color: 'done.fg'}} />
</TreeView.LeadingVisual>
Item 1
</TreeView.Item>
<TreeView.Item id="item-2">
<TreeView.LeadingAction>{dragAction}</TreeView.LeadingAction>
<TreeView.LeadingVisual>
<Octicon icon={IssueOpenedIcon} sx={{color: 'open.fg'}} />
</TreeView.LeadingVisual>
Item 2
<TreeView.SubTree>
<TreeView.Item id="item-2-sub-task-1">
<TreeView.LeadingVisual>
<Octicon icon={IssueOpenedIcon} sx={{color: 'open.fg'}} />
</TreeView.LeadingVisual>
sub task 1
</TreeView.Item>
<TreeView.Item id="item-2-sub-task-2">
<TreeView.LeadingVisual>
<Octicon icon={IssueOpenedIcon} sx={{color: 'open.fg'}} />
</TreeView.LeadingVisual>
sub task 2
</TreeView.Item>
</TreeView.SubTree>
</TreeView.Item>
<TreeView.Item id="item-3">
<TreeView.LeadingAction>{dragAction}</TreeView.LeadingAction>
<TreeView.LeadingVisual>
<Octicon icon={IssueOpenedIcon} sx={{color: 'open.fg'}} />
</TreeView.LeadingVisual>
Item 3
</TreeView.Item>
</TreeView>
)
}

export default meta
13 changes: 0 additions & 13 deletions packages/react/src/TreeView/TreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,6 @@ describe('Markup', () => {
const item1 = getByRole('treeitem', {name: /Item 1/})
expect(item1).toHaveFocus()
})

it("should have 'data-drag-and-drop' attribute when dragAndDrop is passed to TreeView", () => {
const {queryByRole} = renderWithTheme(
<TreeView aria-label="Test tree" dragAndDrop>
<TreeView.Item id="item-1">Item 1</TreeView.Item>
<TreeView.Item id="item-2">Item 2</TreeView.Item>
<TreeView.Item id="item-3">Item 3</TreeView.Item>
</TreeView>,
)

const root = queryByRole('tree')
expect(root).toHaveAttribute('data-drag-and-drop')
})
})

describe('Keyboard interactions', () => {
Expand Down
66 changes: 32 additions & 34 deletions packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FileDirectoryOpenFillIcon,
} from '@primer/octicons-react'
import clsx from 'clsx'
import React, {useCallback, useEffect, type ReactElement} from 'react'
import React, {useCallback, useEffect} from 'react'
import styled, {keyframes} from 'styled-components'
import {ConfirmationDialog} from '../ConfirmationDialog/ConfirmationDialog'
import Spinner from '../Spinner'
Expand Down Expand Up @@ -65,7 +65,6 @@ export type TreeViewProps = {
children: React.ReactNode
flat?: boolean
className?: string
dragAndDrop?: boolean
}

const UlBox = styled.ul<SxProp>`
Expand Down Expand Up @@ -98,15 +97,20 @@ const UlBox = styled.ul<SxProp>`
outline-offset: -2;
}
}
&[data-has-leading-action] {
--has-leading-action: 1;
}
}

.PRIVATE_TreeView-item-container {
--level: 1; /* default level */
--toggle-width: 1rem; /* 16px */
position: relative;
display: grid;
grid-template-columns: calc(calc(var(--level) - 1) * (var(--toggle-width) / 2)) var(--toggle-width) 1fr;
grid-template-areas: 'spacer toggle content';
--leading-action-width: calc(var(--has-leading-action, 0) * 1.5rem);
--spacer-width: calc(calc(var(--level) - 1) * (var(--toggle-width) / 2));
grid-template-columns: var(--spacer-width) var(--leading-action-width) var(--toggle-width) 1fr;
grid-template-areas: 'spacer leadingAction toggle content';
width: 100%;
min-height: 2rem; /* 32px */
font-size: ${get('fontSizes.1')};
Expand All @@ -121,10 +125,6 @@ const UlBox = styled.ul<SxProp>`
outline: 2px solid transparent;
outline-offset: -2px;
}

.PRIVATE_TreeView-item-drag-handle {
visibility: visible;
}
}

@media (pointer: coarse) {
Expand All @@ -143,21 +143,7 @@ const UlBox = styled.ul<SxProp>`
}

&[data-omit-spacer='true'] .PRIVATE_TreeView-item-container {
grid-template-columns: 0 0 1fr;
}

&[data-drag-and-drop='true'] .PRIVATE_TreeView-item-container {
grid-template-columns: 1.5rem calc(calc(var(--level) - 1) * (var(--toggle-width) / 2)) var(--toggle-width) 1fr;
grid-template-areas: 'drag spacer toggle content';
}

.PRIVATE_TreeView-item-drag-handle {
grid-area: drag;
height: 100%;
visibility: hidden;
display: flex;
align-items: center;
justify-content: center;
grid-template-columns: 0 0 0 1fr;
}

.PRIVATE_TreeView-item[aria-current='true'] > .PRIVATE_TreeView-item-container {
Expand Down Expand Up @@ -221,6 +207,12 @@ const UlBox = styled.ul<SxProp>`
color: ${get('colors.fg.muted')};
}

.PRIVATE_TreeView-item-leading-action {
display: flex;
color: ${get('colors.fg.muted')};
grid-area: 'leadingAction';
}

.PRIVATE_TreeView-item-level-line {
width: 100%;
height: 100%;
Expand Down Expand Up @@ -276,7 +268,6 @@ const Root: React.FC<TreeViewProps> = ({
children,
flat,
className,
dragAndDrop,
}) => {
const containerRef = React.useRef<HTMLUListElement>(null)
const mouseDownRef = React.useRef<boolean>(false)
Expand Down Expand Up @@ -332,7 +323,6 @@ const Root: React.FC<TreeViewProps> = ({
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
data-omit-spacer={flat}
data-drag-and-drop={dragAndDrop}
onMouseDown={onMouseDown}
className={className}
>
Expand All @@ -358,7 +348,6 @@ export type TreeViewItemProps = {
onExpandedChange?: (expanded: boolean) => void
onSelect?: (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void
className?: string
dragHandle?: ReactElement
}

const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
Expand All @@ -373,11 +362,14 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
onSelect,
children,
className,
dragHandle,
},
ref,
) => {
const [slots, rest] = useSlots(children, {leadingVisual: LeadingVisual, trailingVisual: TrailingVisual})
const [slots, rest] = useSlots(children, {
leadingAction: LeadingAction,
leadingVisual: LeadingVisual,
trailingVisual: TrailingVisual,
})
const {expandedStateCache} = React.useContext(RootContext)
const labelId = useId()
const leadingVisualId = useId()
Expand Down Expand Up @@ -472,6 +464,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
aria-expanded={isSubTreeEmpty ? undefined : isExpanded}
aria-current={isCurrentItem ? 'true' : undefined}
aria-selected={isFocused ? 'true' : 'false'}
data-has-leading-action={slots.leadingAction ? true : undefined}
onKeyDown={handleKeyDown}
onFocus={event => {
// Scroll the first child into view when the item receives focus
Expand Down Expand Up @@ -511,12 +504,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
<div style={{gridArea: 'spacer', display: 'flex'}}>
<LevelIndicatorLines level={level} />
</div>
{dragHandle ? (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div className="PRIVATE_TreeView-item-drag-handle" onMouseDown={() => setIsExpanded(false)}>
{dragHandle}
</div>
) : null}
{slots.leadingAction}
{hasSubTree ? (
// This lint rule is disabled due to the guidelines in the `TreeView` api docs.
// https://github.com/github/primer/blob/main/apis/tree-view-api.md#the-expandcollapse-chevron-toggle
Expand Down Expand Up @@ -858,6 +846,15 @@ const TrailingVisual: React.FC<TreeViewVisualProps> = props => {

TrailingVisual.displayName = 'TreeView.TrailingVisual'

// ----------------------------------------------------------------------------
// TreeView.LeadingAction

const LeadingAction: React.FC<{children: React.ReactElement}> = props => {
return <div className="PRIVATE_TreeView-item-leading-action" {...props} />
}

LeadingAction.displayName = 'TreeView.LeadingAction'

// ----------------------------------------------------------------------------
// TreeView.DirectoryIcon

Expand Down Expand Up @@ -927,6 +924,7 @@ ErrorDialog.displayName = 'TreeView.ErrorDialog'
export const TreeView = Object.assign(Root, {
Item,
SubTree,
LeadingAction,
LeadingVisual,
TrailingVisual,
DirectoryIcon,
Expand Down
Loading