-
Notifications
You must be signed in to change notification settings - Fork 71
[LG-5645] feat(chat-layout): implement ChatMain component #3252
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,19 @@ | ||
| import { ComponentPropsWithRef, PropsWithChildren } from 'react'; | ||
| import { ComponentPropsWithRef } from 'react'; | ||
|
|
||
| import { DarkModeProps } from '@leafygreen-ui/lib'; | ||
|
|
||
| export type ChatLayoutProps = ComponentPropsWithRef<'div'> & | ||
| DarkModeProps & | ||
| PropsWithChildren<{ | ||
| /** | ||
| * Initial state for whether the side nav is pinned (expanded). | ||
| * @default true | ||
| */ | ||
| initialIsPinned?: boolean; | ||
| export interface ChatLayoutProps | ||
| extends ComponentPropsWithRef<'div'>, | ||
| DarkModeProps { | ||
| /** | ||
| * Initial state for whether the side nav is pinned (expanded). | ||
| * @default true | ||
| */ | ||
| initialIsPinned?: boolean; | ||
|
|
||
| /** | ||
| * Callback fired when the side nav is toggled (pinned/unpinned). | ||
| * Receives the new `isPinned` state as an argument. | ||
| */ | ||
| onTogglePinned?: (isPinned: boolean) => void; | ||
| }>; | ||
| /** | ||
| * Callback fired when the side nav is toggled (pinned/unpinned). | ||
| * Receives the new `isPinned` state as an argument. | ||
| */ | ||
| onTogglePinned?: (isPinned: boolean) => void; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { ChatLayout } from '../ChatLayout'; | ||
|
|
||
| import { ChatMain } from '.'; | ||
|
|
||
| describe('packages/chat-layout/ChatMain', () => { | ||
| describe('ChatMain', () => { | ||
| test('renders children', () => { | ||
| render( | ||
| <ChatLayout> | ||
| <ChatMain> | ||
| <div>Main Content</div> | ||
| </ChatMain> | ||
| </ChatLayout>, | ||
| ); | ||
| expect(screen.getByText('Main Content')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('forwards HTML attributes to the div element', () => { | ||
| render( | ||
| <ChatLayout> | ||
| <ChatMain data-testid="chat-main" aria-label="Chat content"> | ||
| Content | ||
| </ChatMain> | ||
| </ChatLayout>, | ||
| ); | ||
| const element = screen.getByTestId('chat-main'); | ||
| expect(element).toHaveAttribute('aria-label', 'Chat content'); | ||
| }); | ||
|
|
||
| test('forwards ref to the div element', () => { | ||
| const ref = React.createRef<HTMLDivElement>(); | ||
| render( | ||
| <ChatLayout> | ||
| <ChatMain ref={ref}>Content</ChatMain> | ||
| </ChatLayout>, | ||
| ); | ||
| expect(ref.current).toBeInstanceOf(HTMLDivElement); | ||
| expect(ref.current?.tagName).toBe('DIV'); | ||
| }); | ||
|
|
||
| test('applies custom className', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this test. Do we need to test this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'm planning to test this moving forward as well. It helps document to make sure |
||
| render( | ||
| <ChatLayout> | ||
| <ChatMain data-testid="chat-main" className="custom-class"> | ||
| Content | ||
| </ChatMain> | ||
| </ChatLayout>, | ||
| ); | ||
| const element = screen.getByTestId('chat-main'); | ||
| expect(element).toHaveClass('custom-class'); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { css, cx } from '@leafygreen-ui/emotion'; | ||
|
|
||
| import { gridAreas } from '../constants'; | ||
|
|
||
| const baseContainerStyles = css` | ||
| grid-area: ${gridAreas.main}; | ||
| display: flex; | ||
| flex-direction: column; | ||
| min-width: 0; | ||
| height: 100%; | ||
| `; | ||
|
|
||
| export const getContainerStyles = ({ className }: { className?: string }) => | ||
| cx(baseContainerStyles, className); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import React, { forwardRef } from 'react'; | ||
|
|
||
| import { getContainerStyles } from './ChatMain.styles'; | ||
| import { ChatMainProps } from './ChatMain.types'; | ||
|
|
||
| /** | ||
| * ChatMain represents the main content area of the chat layout. | ||
| * It automatically positions itself in the second column of the parent | ||
| * ChatLayout's CSS Grid, allowing the layout to control spacing for the sidebar. | ||
| */ | ||
| export const ChatMain = forwardRef<HTMLDivElement, ChatMainProps>( | ||
| ({ children, className, ...rest }, ref) => { | ||
| return ( | ||
| <div ref={ref} className={getContainerStyles({ className })} {...rest}> | ||
| {children} | ||
| </div> | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| ChatMain.displayName = 'ChatMain'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { ComponentPropsWithRef } from 'react'; | ||
|
|
||
| import { DarkModeProps } from '@leafygreen-ui/lib'; | ||
|
|
||
| export interface ChatMainProps | ||
| extends ComponentPropsWithRef<'div'>, | ||
| DarkModeProps {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { ChatMain } from './ChatMain'; | ||
| export { type ChatMainProps } from './ChatMain.types'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 need to test this?
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.
Yes, I'm planning to include these in tests from now on to test if HTML attributes are settable. In some cases, our components intentionally don't offer this. It felt worth testing to more explicitly document that
ComponentPropsWith*Refis part of the type interface, and it's a low effort inclusion