-
Notifications
You must be signed in to change notification settings - Fork 74
[LG-844] feat(collection-toolbar): CollectionToolbar Title SubComponent #3420
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
Merged
adamrasheed
merged 10 commits into
ar/LG-5830-collection-toolbar
from
ar/LG-5844-collection-toolbar-title
Jan 7, 2026
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
98da991
[LG-844] feat(collection-toolbar): Title component
adamrasheed 9279070
updated lintin
adamrasheed d84c9e2
updated tests, linitng cleanup
adamrasheed 526097c
feedback updates
adamrasheed a20fa54
fixed type exports
adamrasheed 517b874
updated collectionToolbar ref, updated added types docs
adamrasheed 9e650ea
added context provider
adamrasheed ae26238
fixed storybook darkmode, updated defaul theading element
adamrasheed b4711fc
reverted heading
adamrasheed 9010485
added refs
adamrasheed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
34 changes: 32 additions & 2 deletions
34
packages/collection-toolbar/src/CollectionToolbar/CollectionToolbar.spec.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,11 +1,41 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { Variant } from '../shared.types'; | ||
| import { getTestUtils } from '../testing/getTestUtils'; | ||
|
|
||
| import { CollectionToolbar } from '.'; | ||
|
|
||
| describe('packages/collection-toolbar', () => { | ||
| test('renders correctly', () => { | ||
| render(<CollectionToolbar>Collection Toolbar</CollectionToolbar>); | ||
| expect(screen.getByText('Collection Toolbar')).toBeInTheDocument(); | ||
| render(<CollectionToolbar />); | ||
| const utils = getTestUtils(); | ||
| expect(utils.getCollectionToolbar()).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('applies className to the root element', () => { | ||
| render(<CollectionToolbar className="test-class" />); | ||
| const utils = getTestUtils(); | ||
| expect(utils.getCollectionToolbar()?.className).toContain('test-class'); | ||
| }); | ||
|
|
||
| describe('variant: collapsible', () => { | ||
| test('renders title when variant is collapsible', () => { | ||
| render( | ||
| <CollectionToolbar variant={Variant.Collapsible}> | ||
| <CollectionToolbar.Title>Test Title</CollectionToolbar.Title> | ||
| </CollectionToolbar>, | ||
| ); | ||
| expect(screen.getByText('Test Title')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('does not render title when variant is not collapsible', () => { | ||
| render( | ||
| <CollectionToolbar variant={Variant.Default}> | ||
| <CollectionToolbar.Title>Test Title</CollectionToolbar.Title> | ||
| </CollectionToolbar>, | ||
| ); | ||
| expect(screen.queryByText('Test Title')).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
4 changes: 1 addition & 3 deletions
4
packages/collection-toolbar/src/CollectionToolbar/CollectionToolbar.styles.ts
This file contains hidden or 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
72 changes: 55 additions & 17 deletions
72
packages/collection-toolbar/src/CollectionToolbar/CollectionToolbar.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,23 +1,61 @@ | ||
| import React from 'react'; | ||
| import React, { forwardRef } from 'react'; | ||
|
|
||
| import { getCollectionToolbarStyles } from './CollectionToolbar.styles'; | ||
| import { | ||
| CollectionToolbarProps, | ||
| CompoundComponent, | ||
| findChild, | ||
| } from '@leafygreen-ui/compound-component'; | ||
|
|
||
| import { Title } from '../components'; | ||
| import { CollectionToolbarProvider } from '../Context/CollectionToolbarProvider'; | ||
| import { | ||
| CollectionToolbarSubComponentProperty, | ||
| Size, | ||
| Variant, | ||
| } from './CollectionToolbar.types'; | ||
| } from '../shared.types'; | ||
| import { getLgIds } from '../utils'; | ||
|
|
||
| import { getCollectionToolbarStyles } from './CollectionToolbar.styles'; | ||
| import { CollectionToolbarProps } from './CollectionToolbar.types'; | ||
|
|
||
| export const CollectionToolbar = CompoundComponent( | ||
| // eslint-disable-next-line react/display-name | ||
| forwardRef<HTMLDivElement, CollectionToolbarProps>( | ||
| ({ | ||
| size = Size.Default, | ||
| variant = Variant.Default, | ||
| className, | ||
| children, | ||
| 'data-lgid': dataLgId, | ||
| darkMode, | ||
| ...rest | ||
| }) => { | ||
| const lgIds = getLgIds(dataLgId); | ||
| const title = findChild( | ||
| children, | ||
| CollectionToolbarSubComponentProperty.Title, | ||
| ); | ||
|
|
||
| export function CollectionToolbar({ | ||
| size = Size.Default, | ||
| variant = Variant.Default, | ||
| className, | ||
| children, | ||
| }: CollectionToolbarProps) { | ||
| return ( | ||
| <div className={getCollectionToolbarStyles({ size, variant, className })}> | ||
| {children} | ||
| </div> | ||
| ); | ||
| } | ||
| const showTitle = title && variant === Variant.Collapsible; | ||
|
|
||
| CollectionToolbar.displayName = 'CollectionToolbar'; | ||
| return ( | ||
| <CollectionToolbarProvider | ||
| darkMode={darkMode} | ||
| size={size} | ||
| lgIds={lgIds} | ||
| > | ||
| <div | ||
| data-lgid={lgIds.root} | ||
| className={getCollectionToolbarStyles({ size, variant, className })} | ||
| {...rest} | ||
| > | ||
| {showTitle && title} | ||
| </div> | ||
| </CollectionToolbarProvider> | ||
| ); | ||
| }, | ||
| ), | ||
| { | ||
| displayName: 'CollectionToolbar', | ||
| Title, | ||
| }, | ||
| ); | ||
36 changes: 18 additions & 18 deletions
36
packages/collection-toolbar/src/CollectionToolbar/CollectionToolbar.types.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,23 +1,23 @@ | ||
| import React from 'react'; | ||
| import { ComponentPropsWithRef } from 'react'; | ||
|
|
||
| import { Size as ImportedSize } from '@leafygreen-ui/tokens'; | ||
| import { DarkModeProps, LgIdProps } from '@leafygreen-ui/lib'; | ||
|
|
||
| export const Variant = { | ||
| Compact: 'compact', | ||
| Default: 'default', | ||
| Collapsible: 'collapsible', | ||
| } as const; | ||
| export type Variant = (typeof Variant)[keyof typeof Variant]; | ||
| import { Size, Variant } from '../shared.types'; | ||
|
|
||
| export const Size = { | ||
| Default: ImportedSize.Default, | ||
| Small: ImportedSize.Small, | ||
| } as const; | ||
| export type Size = (typeof Size)[keyof typeof Size]; | ||
|
|
||
| export interface CollectionToolbarProps { | ||
| size?: typeof ImportedSize.Default | typeof ImportedSize.Small; | ||
| export interface CollectionToolbarProps | ||
| extends ComponentPropsWithRef<'div'>, | ||
| DarkModeProps, | ||
| LgIdProps { | ||
| /** | ||
| * The size of the CollectionToolbar and it's sub-components. | ||
| * | ||
| * @default `'default'` | ||
| */ | ||
| size?: typeof Size.Default | typeof Size.Small; | ||
| /** | ||
| * The variant of the CollectionToolbar. Determines the layout of the CollectionToolbar. | ||
| * | ||
| * @default `'default'` | ||
| */ | ||
| variant?: Variant; | ||
| className?: string; | ||
| children?: React.ReactNode; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,2 @@ | ||
| export { CollectionToolbar } from './CollectionToolbar'; | ||
| export { | ||
| type CollectionToolbarProps, | ||
| Size, | ||
| Variant, | ||
| } from './CollectionToolbar.types'; | ||
| export { type CollectionToolbarProps } from './CollectionToolbar.types'; |
62 changes: 62 additions & 0 deletions
62
packages/collection-toolbar/src/Context/CollectionToolbarProvider.tsx
This file contains hidden or 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,62 @@ | ||
| import React, { | ||
| createContext, | ||
| PropsWithChildren, | ||
| useContext, | ||
| useMemo, | ||
| } from 'react'; | ||
|
|
||
| import LeafyGreenProvider from '@leafygreen-ui/leafygreen-provider'; | ||
| import { DarkModeProps } from '@leafygreen-ui/lib'; | ||
|
|
||
| import { Size } from '../shared.types'; | ||
| import { getLgIds, type GetLgIdsReturnType } from '../utils'; | ||
|
|
||
| export interface CollectionToolbarContextProps extends DarkModeProps { | ||
| /** | ||
| * The size of the CollectionToolbar and it's sub-components. | ||
| * | ||
| * @default `'default'` | ||
| */ | ||
| size?: Size; | ||
| /** | ||
| * LGIDs for CollectionToolbar components | ||
| */ | ||
| lgIds: GetLgIdsReturnType; | ||
| } | ||
|
|
||
| export const CollectionToolbarContext = | ||
| createContext<CollectionToolbarContextProps>({ | ||
| size: Size.Default, | ||
| lgIds: getLgIds(), | ||
| }); | ||
|
|
||
| export const useCollectionToolbarContext = () => { | ||
| const context = useContext(CollectionToolbarContext); | ||
| if (!context) | ||
| throw new Error( | ||
| 'useCollectionToolbarContext must be used within a CollectionToolbarProvider', | ||
| ); | ||
| return useContext(CollectionToolbarContext); | ||
| }; | ||
|
|
||
| export const CollectionToolbarProvider = ({ | ||
| children, | ||
| darkMode, | ||
| size, | ||
| lgIds, | ||
| }: PropsWithChildren<CollectionToolbarContextProps>) => { | ||
| const collectionToolbarProviderData = useMemo(() => { | ||
| return { | ||
| size, | ||
| lgIds, | ||
| }; | ||
| }, [size, lgIds]); | ||
|
|
||
| return ( | ||
| <LeafyGreenProvider darkMode={darkMode}> | ||
| <CollectionToolbarContext.Provider value={collectionToolbarProviderData}> | ||
| {children} | ||
| </CollectionToolbarContext.Provider> | ||
| </LeafyGreenProvider> | ||
| ); | ||
| }; |
42 changes: 42 additions & 0 deletions
42
packages/collection-toolbar/src/components/Title/Title.spec.tsx
This file contains hidden or 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 React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { CollectionToolbarSubComponentProperty } from '../../shared.types'; | ||
| import { getTestUtils } from '../../testing/getTestUtils'; | ||
|
|
||
| import Title from './Title'; | ||
|
|
||
| describe('packages/collection-toolbar/CollectionToolbar/components/Title', () => { | ||
| test('renders children correctly', () => { | ||
| render(<Title>Test Title</Title>); | ||
| const utils = getTestUtils(); | ||
| expect(screen.getByText('Test Title')).toBeInTheDocument(); | ||
| expect(utils.getTitle()).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('renders as an h3 element as default', () => { | ||
| render(<Title>Test Title</Title>); | ||
| expect(screen.getByRole('heading', { level: 3 })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('renders as a p element when using as prop', () => { | ||
| render(<Title as="p">Test Title</Title>); | ||
| expect(screen.getByText('Test Title').tagName).toBe('P'); | ||
| }); | ||
|
|
||
| test('applies className to the rendered element', () => { | ||
| render(<Title className="custom-class">Test Title</Title>); | ||
| expect(screen.getByText('Test Title')).toHaveClass('custom-class'); | ||
| }); | ||
|
|
||
| test('does not allow darkMode prop', () => { | ||
| // @ts-expect-error: darkMode prop is not allowed | ||
| render(<Title darkMode={true}>Test Title</Title>); | ||
| const utils = getTestUtils(); | ||
| expect(utils.getTitle()).not.toHaveClass('dark-mode'); | ||
| }); | ||
|
|
||
| test('has the correct static property for compound component identification', () => { | ||
| expect(Title[CollectionToolbarSubComponentProperty.Title]).toBe(true); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
packages/collection-toolbar/src/components/Title/Title.tsx
This file contains hidden or 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,30 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { CompoundSubComponent } from '@leafygreen-ui/compound-component'; | ||
| import { H3 } from '@leafygreen-ui/typography'; | ||
|
|
||
| import { useCollectionToolbarContext } from '../../Context/CollectionToolbarProvider'; | ||
| import { CollectionToolbarSubComponentProperty } from '../../shared.types'; | ||
|
|
||
| import { TitleProps } from './Title.types'; | ||
|
|
||
| /** | ||
| * Title is a compound component that renders a title for CollectionToolbar. | ||
| * It will only render if the CollectionToolbar variant is set to Collapsible. | ||
| */ | ||
| const Title = CompoundSubComponent( | ||
| ({ className, children, ...rest }: TitleProps) => { | ||
| const { lgIds } = useCollectionToolbarContext(); | ||
| return ( | ||
| <H3 data-lgid={lgIds.title} className={className} {...rest}> | ||
adamrasheed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
adamrasheed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {children} | ||
| </H3> | ||
| ); | ||
| }, | ||
| { | ||
| displayName: 'Title', | ||
| key: CollectionToolbarSubComponentProperty.Title, | ||
| }, | ||
| ); | ||
|
|
||
| export default Title; | ||
4 changes: 4 additions & 0 deletions
4
packages/collection-toolbar/src/components/Title/Title.types.ts
This file contains hidden or 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,4 @@ | ||
| import { DarkModeProps } from '@leafygreen-ui/lib'; | ||
| import { H3Props } from '@leafygreen-ui/typography'; | ||
|
|
||
| export interface TitleProps extends Omit<H3Props, keyof DarkModeProps> {} |
This file contains hidden or 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,2 @@ | ||
| export { default as Title } from './Title'; | ||
| export type { TitleProps } from './Title.types'; |
This file contains hidden or 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 @@ | ||
| export { Title, type TitleProps } from './Title'; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| export { | ||
| CollectionToolbar, | ||
| type CollectionToolbarProps, | ||
| Size, | ||
| Variant, | ||
| } from './CollectionToolbar'; | ||
| export { type TitleProps as CollectionToolbarTitleProps } from './components'; | ||
adamrasheed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export { Size, Variant } from './shared.types'; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to include a
forwardReffor this component?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrapped in
forwardRefnow, but we're still missing a ref being passed to the root<div>