Skip to content

Commit

Permalink
TreeView: Add truncate prop (#5122)
Browse files Browse the repository at this point in the history
* Add "opt in" prop for `TreeView` to opt in to truncation of text

* Make non-truncate opt-in

* Add extra CSS, stories

* Add changeset

* Cleanup stories

* Remove `truncate` from story

* Add back prop to story

* test(vrt): update snapshots

* Fix story typos

* test(vrt): update snapshots

---------

Co-authored-by: TylerJDev <TylerJDev@users.noreply.github.com>
  • Loading branch information
TylerJDev and TylerJDev authored Nov 6, 2024
1 parent 688ed1e commit e021a8f
Show file tree
Hide file tree
Showing 22 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-shoes-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

TreeView: Adds prop `truncate`, keeps default behavior of truncation but allows for text to wrap when turned off.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/react/src/TreeView/TreeView.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
"name": "onSelect",
"type": "(event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void"
},
{
"name": "truncate",
"type": "boolean",
"defaultValue": "true",
"description": "Truncate the item's text content with an ellipsis."
},
{
"name": "ref",
"type": "React.Ref<HTMLElement>"
Expand Down
22 changes: 9 additions & 13 deletions packages/react/src/TreeView/TreeView.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const meta: Meta = {

export const Files: StoryFn = () => (
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView aria-label="Files" truncate={false}>
<TreeView.Item id="src" defaultExpanded>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
Expand Down Expand Up @@ -112,7 +112,7 @@ export const Files: StoryFn = () => (
export const FilesChanged: StoryFn = () => {
return (
<nav aria-label="Files">
<TreeView aria-label="Files">
<TreeView aria-label="Files" truncate={false}>
<TreeView.Item id="src" defaultExpanded>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
Expand Down Expand Up @@ -786,7 +786,7 @@ export const ContainIntrinsicSize: StoryFn = () => {
export const InitialFocus: StoryFn = () => (
<div>
<Button>Focusable element before TreeView</Button>
<TreeView aria-label="Test tree">
<TreeView aria-label="Test tree" truncate={false}>
<TreeView.Item id="src" defaultExpanded>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
Expand Down Expand Up @@ -891,11 +891,11 @@ export const InitialFocus: StoryFn = () => (
</TreeView.Item>
</TreeView.SubTree>
</TreeView.Item>
<TreeView.Item id="src/ReallyLongFileNameThatShouldBeTruncated.tsx">
<TreeView.Item id="src/ReallyLongFileNameThatShouldNotBeTruncated.tsx">
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
ReallyLongFileNameThatShouldBeTruncated.tsx
ReallyLongFileNameThatShouldNotBeTruncated.tsx
</TreeView.Item>
</TreeView.SubTree>
</TreeView.Item>
Expand Down Expand Up @@ -1047,15 +1047,13 @@ export const LeadingAction: StoryFn = () => {

export const MultilineItems: StoryFn = () => (
<nav aria-label="Files changed">
<TreeView aria-label="Files changed">
<TreeView aria-label="Files changed" truncate={false}>
<TreeView.Item id="src" defaultExpanded>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
<div style={{whiteSpace: 'wrap'}}>
this is a very long directory name that we have intentionally allowed to wrap over multiple lines to
demonstrate alignment
</div>
this is a very long directory name that we have intentionally allowed to wrap over multiple lines to demonstrate
alignment
<TreeView.SubTree>
<TreeView.Item id="src/Avatar.tsx">
<TreeView.LeadingVisual>
Expand All @@ -1072,9 +1070,7 @@ export const MultilineItems: StoryFn = () => (
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
<div style={{whiteSpace: 'wrap'}}>
this is a medium directory name that we wrap over 2 lines to demonstrate alignment
</div>
this is a medium directory name that we wrap over 2 lines to demonstrate alignment
<TreeView.TrailingVisual label="Added">
<Octicon icon={DiffAddedIcon} color="success.fg" />
</TreeView.TrailingVisual>
Expand Down
11 changes: 10 additions & 1 deletion packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type TreeViewProps = {
'aria-labelledby'?: React.AriaAttributes['aria-labelledby']
children: React.ReactNode
flat?: boolean
truncate?: boolean
className?: string
}

Expand Down Expand Up @@ -205,14 +206,20 @@ const UlBox = styled.ul<SxProp>`
}
.PRIVATE_TreeView-item-content-text {
/* Truncate text label */
flex: 1 1 auto;
width: 0;
}
&[data-truncate-text='true'] .PRIVATE_TreeView-item-content-text {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
&[data-truncate-text='false'] .PRIVATE_TreeView-item-content-text {
word-break: break-word;
}
.PRIVATE_TreeView-item-visual {
display: flex;
align-items: center;
Expand Down Expand Up @@ -282,6 +289,7 @@ const Root: React.FC<TreeViewProps> = ({
'aria-labelledby': ariaLabelledby,
children,
flat,
truncate = true,
className,
}) => {
const containerRef = React.useRef<HTMLUListElement>(null)
Expand Down Expand Up @@ -338,6 +346,7 @@ const Root: React.FC<TreeViewProps> = ({
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
data-omit-spacer={flat}
data-truncate-text={truncate || false}
onMouseDown={onMouseDown}
className={className}
>
Expand Down

0 comments on commit e021a8f

Please sign in to comment.