Skip to content

Commit

Permalink
add example of drag handle on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthkp committed May 8, 2024
1 parent a8a3069 commit 1f63a8b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
72 changes: 72 additions & 0 deletions packages/react/src/TreeView/TreeView.examples.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {GrabberIcon} from '@primer/octicons-react'
import type {Meta, Story} from '@storybook/react'
import React from 'react'
import Box from '../Box'
import {TreeView} from './TreeView'

const meta: Meta = {
title: 'Components/TreeView/Examples',
component: TreeView,
decorators: [
Story => {
return (
// Prevent TreeView from expanding to the full width of the screen
<Box sx={{maxWidth: 400}}>
<Story />
</Box>
)
},
],
}

export const DraggableListItem: Story = () => {
return (
<Box
sx={{
// using Box for css, this could be in a css file as well
'.treeview-item': {
'.treeview-leading-action': {visibility: 'hidden'},
'&:hover, &:focus': {
'.treeview-leading-action': {visibility: 'visible'},
},
},
}}
>
<TreeView aria-label="Issues">
<ControlledDraggableItem id="item-1">Item 1</ControlledDraggableItem>
<ControlledDraggableItem 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>
</ControlledDraggableItem>
<ControlledDraggableItem id="item-3">Item 3</ControlledDraggableItem>
</TreeView>
</Box>
)
}

const ControlledDraggableItem: React.FC<{id: string; children: React.ReactNode}> = ({id, children}) => {
const [expanded, setExpanded] = React.useState(false)

return (
<>
<TreeView.Item id={id} className="treeview-item" expanded={expanded} onExpandedChange={setExpanded}>
<TreeView.LeadingAction
icon={GrabberIcon}
aria-label="Reorder item"
className="treeview-leading-action"
draggable="true"
onDragStart={() => {
setExpanded(false)
// other drag logic to follow
}}
/>
{children}
</TreeView.Item>
</>
)
}

export default meta
4 changes: 0 additions & 4 deletions packages/react/src/TreeView/TreeView.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,6 @@ export const WithoutIndentation: Story = () => (
)

export const LeadingAction: Story = () => {
// todo: implement fold on click
// todo: implement hide until hovered
// todo: check if draggy boi should be aria-hidden

return (
<TreeView aria-label="Issues">
<TreeView.Item id="item-0">
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const UlBox = styled.ul<SxProp>`
.PRIVATE_TreeView-item-leading-action {
display: flex;
color: ${get('colors.fg.muted')};
grid-area: 'leadingAction';
grid-area: leadingAction;
}
.PRIVATE_TreeView-item-level-line {
Expand Down Expand Up @@ -851,7 +851,11 @@ TrailingVisual.displayName = 'TreeView.TrailingVisual'
// TreeView.LeadingAction

const LeadingAction: React.FC<IconButtonProps> = props => {
return <IconButton variant="invisible" className="PRIVATE_TreeView-item-leading-action" {...props} />
return (
<div className="PRIVATE_TreeView-item-leading-action">
<IconButton variant="invisible" {...props} />
</div>
)
}

LeadingAction.displayName = 'TreeView.LeadingAction'
Expand Down

0 comments on commit 1f63a8b

Please sign in to comment.