-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graph): update tags to render on a single line by default with e…
…xpand option (nrwl#27829)
- Loading branch information
Showing
3 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
graph/ui-project-details/src/lib/tag-list/tag-list.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { TagList } from './tag-list'; | ||
|
||
const meta: Meta<typeof TagList> = { | ||
component: TagList, | ||
title: 'TagList', | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof TagList>; | ||
|
||
export const FewTags: Story = { | ||
args: { | ||
tags: ['tag1', 'tag2', 'tag3'], | ||
}, | ||
}; | ||
export const ManyTags: Story = { | ||
args: { | ||
tags: [ | ||
'tag1', | ||
'tag2', | ||
'tag3', | ||
'tag4', | ||
'tag5', | ||
'tag6', | ||
'tag7', | ||
'tag8', | ||
'tag9', | ||
'tag10', | ||
'tag11', | ||
'tag12', | ||
'tag13', | ||
'tag14', | ||
'tag15', | ||
'tag16', | ||
'tag17', | ||
'tag18', | ||
'tag19', | ||
'tag20', | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { Pill } from '../pill'; | ||
|
||
interface TagListProps { | ||
tags: string[]; | ||
} | ||
|
||
export function TagList({ tags }: TagListProps) { | ||
const [isExpanded, _setIsExpanded] = useState(false); | ||
const [isOverflowing, setIsOverflowing] = useState(false); | ||
const tagsContainerRef = useRef<HTMLSpanElement>(null); | ||
|
||
const checkOverflow = () => { | ||
requestAnimationFrame(() => { | ||
if (tagsContainerRef.current) { | ||
setIsOverflowing( | ||
tagsContainerRef.current.scrollWidth > | ||
tagsContainerRef.current.offsetWidth | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
const setExpanded = (value: boolean) => { | ||
_setIsExpanded(value); | ||
checkOverflow(); | ||
}; | ||
|
||
useEffect(() => { | ||
checkOverflow(); | ||
|
||
window.addEventListener('resize', checkOverflow); | ||
return () => window.removeEventListener('resize', checkOverflow); | ||
}, [tagsContainerRef]); | ||
|
||
return ( | ||
<div className="relative max-w-full"> | ||
<p className="flex min-w-0 font-medium leading-loose"> | ||
<span className="inline-block">Tags:</span> | ||
|
||
<span | ||
className={`inline-block ${ | ||
isExpanded ? 'whitespace-normal' : 'whitespace-nowrap' | ||
} w-full max-w-full overflow-hidden transition-all duration-300`} | ||
style={{ | ||
maskImage: | ||
isOverflowing && !isExpanded | ||
? 'linear-gradient(to right, black 80%, transparent 100%)' | ||
: 'none', | ||
}} | ||
ref={tagsContainerRef} | ||
> | ||
{tags.map((tag) => ( | ||
<span key={tag} className="ml-2 font-mono lowercase"> | ||
<Pill text={tag} /> | ||
</span> | ||
))} | ||
{isExpanded && ( | ||
<button | ||
onClick={() => setExpanded(false)} | ||
className="inline-block px-2 align-middle" | ||
> | ||
Show less | ||
</button> | ||
)} | ||
</span> | ||
{isOverflowing && !isExpanded && ( | ||
<button | ||
onClick={() => setExpanded(true)} | ||
className="ml-1 inline-block whitespace-nowrap" | ||
> | ||
Show more | ||
</button> | ||
)} | ||
</p> | ||
</div> | ||
); | ||
} |