-
Notifications
You must be signed in to change notification settings - Fork 73
[LG-5646] feat(chat-layout): implement ChatSideNav component with Header and Content subcomponents #3271
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
Merged
[LG-5646] feat(chat-layout): implement ChatSideNav component with Header and Content subcomponents #3271
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d3ea7e
chore(chat-layout): add deps for ChatSideNav
stephl3 2e45ef0
feat(chat-layout): implement ChatSideNav and subcomponents
stephl3 1b31595
test(chat-layout): update stories
stephl3 eac26f8
docs(chat-layout): README
stephl3 941bc5f
fix(chat-layout): apply focus styles on :focus-visible
stephl3 d34eb2e
refactor(chat-layout): PR feedback
stephl3 6363f63
refactor(chat-layout): ChatSideNav only renders header and content
stephl3 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import React from 'react'; | ||
| import { | ||
| LeafyGreenChatProvider, | ||
| Variant, | ||
| } from '@lg-chat/leafygreen-chat-provider'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| import { ChatSideNav } from '.'; | ||
|
|
||
| const Providers = ({ children }: { children: React.ReactNode }) => ( | ||
| <LeafyGreenChatProvider variant={Variant.Compact}> | ||
| {children} | ||
| </LeafyGreenChatProvider> | ||
| ); | ||
|
|
||
| describe('ChatSideNav', () => { | ||
| beforeAll(() => { | ||
| global.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
| observe: jest.fn(), | ||
| unobserve: jest.fn(), | ||
| disconnect: jest.fn(), | ||
| })); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('renders Header and Content in enforced order', () => { | ||
| render( | ||
| <Providers> | ||
| <ChatSideNav> | ||
| {/* Intentionally reverse order in JSX */} | ||
| <ChatSideNav.Content> | ||
| <div data-testid="content">Content</div> | ||
| </ChatSideNav.Content> | ||
| <ChatSideNav.Header /> | ||
| </ChatSideNav> | ||
| </Providers>, | ||
| ); | ||
|
|
||
| const content = screen.getByTestId('content'); | ||
| expect(content).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('Header shows "New Chat" button when onClickNewChat provided', async () => { | ||
| const onClickNewChat = jest.fn(); | ||
|
|
||
| render( | ||
| <Providers> | ||
| <ChatSideNav> | ||
| <ChatSideNav.Header onClickNewChat={onClickNewChat} /> | ||
| <ChatSideNav.Content /> | ||
| </ChatSideNav> | ||
| </Providers>, | ||
| ); | ||
|
|
||
| const button = screen.getByRole('button', { name: /new chat/i }); | ||
| expect(button).toBeInTheDocument(); | ||
|
|
||
| await userEvent.click(button); | ||
| expect(onClickNewChat).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('Header does not render "New Chat" button when onClickNewChat is absent', () => { | ||
| render( | ||
| <Providers> | ||
| <ChatSideNav> | ||
| <ChatSideNav.Header /> | ||
| <ChatSideNav.Content /> | ||
| </ChatSideNav> | ||
| </Providers>, | ||
| ); | ||
|
|
||
| expect( | ||
| screen.queryByRole('button', { name: /new chat/i }), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
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,26 @@ | ||
| import { css, cx } from '@leafygreen-ui/emotion'; | ||
| import { Theme } from '@leafygreen-ui/lib'; | ||
| import { color, InteractionState, Variant } from '@leafygreen-ui/tokens'; | ||
|
|
||
| import { gridAreas } from '../constants'; | ||
|
|
||
| const getBaseContainerStyles = (theme: Theme) => css` | ||
| grid-area: ${gridAreas.sideNav}; | ||
| background: ${color[theme].background[Variant.Secondary][ | ||
| InteractionState.Default | ||
| ]}; | ||
| border-right: 1px solid | ||
| ${color[theme].border[Variant.Secondary][InteractionState.Default]}; | ||
| display: flex; | ||
| flex-direction: column; | ||
| height: 100%; | ||
| overflow: hidden; | ||
| `; | ||
|
|
||
| export const getContainerStyles = ({ | ||
| className, | ||
| theme, | ||
| }: { | ||
| className?: string; | ||
| theme: Theme; | ||
| }) => cx(getBaseContainerStyles(theme), className); |
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,64 @@ | ||
| import React, { forwardRef } from 'react'; | ||
|
|
||
| import { | ||
| CompoundComponent, | ||
| filterChildren, | ||
| findChild, | ||
| } from '@leafygreen-ui/compound-component'; | ||
| import LeafyGreenProvider, { | ||
| useDarkMode, | ||
| } from '@leafygreen-ui/leafygreen-provider'; | ||
|
|
||
| import { getContainerStyles } from './ChatSideNav.styles'; | ||
| import { | ||
| type ChatSideNavProps, | ||
| ChatSideNavSubcomponentProperty, | ||
| } from './ChatSideNav.types'; | ||
| import { ChatSideNavContent } from './ChatSideNavContent'; | ||
| import { ChatSideNavFooter } from './ChatSideNavFooter'; | ||
| import { ChatSideNavHeader } from './ChatSideNavHeader'; | ||
|
|
||
| export const ChatSideNav = CompoundComponent( | ||
| // eslint-disable-next-line react/display-name | ||
| forwardRef<HTMLElement, ChatSideNavProps>( | ||
| ({ children, className, darkMode: darkModeProp, ...rest }, ref) => { | ||
| const { darkMode, theme } = useDarkMode(darkModeProp); | ||
| // Find subcomponents | ||
| const header = findChild( | ||
| children, | ||
| ChatSideNavSubcomponentProperty.Header, | ||
| ); | ||
| const content = findChild( | ||
| children, | ||
| ChatSideNavSubcomponentProperty.Content, | ||
| ); | ||
|
|
||
| // Filter out subcomponents from remaining children | ||
| const remainingChildren = filterChildren( | ||
| children, | ||
| Object.values(ChatSideNavSubcomponentProperty), | ||
| ); | ||
|
|
||
| return ( | ||
| <LeafyGreenProvider darkMode={darkMode}> | ||
| <nav | ||
| ref={ref} | ||
| className={getContainerStyles({ className, theme })} | ||
| aria-label="Side navigation" | ||
| {...rest} | ||
| > | ||
| {header} | ||
| {content} | ||
| {remainingChildren} | ||
shaneeza marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <ChatSideNavFooter /> | ||
| </nav> | ||
| </LeafyGreenProvider> | ||
| ); | ||
| }, | ||
| ), | ||
| { | ||
| displayName: 'ChatSideNav', | ||
| Header: ChatSideNavHeader, | ||
| Content: ChatSideNavContent, | ||
| }, | ||
| ); | ||
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,37 @@ | ||
| import { ComponentPropsWithRef, MouseEventHandler } from 'react'; | ||
|
|
||
| import { DarkModeProps } from '@leafygreen-ui/lib'; | ||
|
|
||
| export interface ChatSideNavProps | ||
| extends ComponentPropsWithRef<'nav'>, | ||
| DarkModeProps {} | ||
|
|
||
| export interface ChatSideNavHeaderProps | ||
| extends ComponentPropsWithRef<'div'>, | ||
| DarkModeProps { | ||
| /** | ||
| * Optional callback fired when the "New Chat" button is clicked. | ||
| */ | ||
| onClickNewChat?: MouseEventHandler<HTMLButtonElement>; | ||
| } | ||
|
|
||
| export interface ChatSideNavContentProps | ||
| extends ComponentPropsWithRef<'div'>, | ||
| DarkModeProps {} | ||
|
|
||
| export interface ChatSideNavFooterProps extends ComponentPropsWithRef<'div'> {} | ||
|
|
||
| /** | ||
| * Static property names used to identify ChatSideNav compound components. | ||
| * These are implementation details for the compound component pattern and should not be exported. | ||
| */ | ||
| export const ChatSideNavSubcomponentProperty = { | ||
| Header: 'isChatSideNavHeader', | ||
| Content: 'isChatSideNavContent', | ||
| } as const; | ||
|
|
||
| /** | ||
| * Type representing the possible static property names for ChatSideNav subcomponents. | ||
| */ | ||
| export type ChatSideNavSubcomponentProperty = | ||
| (typeof ChatSideNavSubcomponentProperty)[keyof typeof ChatSideNavSubcomponentProperty]; |
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.
Uh oh!
There was an error while loading. Please reload this page.