-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat(Accordion): new component #1310
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ff56e6a
feat(Accordion): new component
marcinsawicki 8ab71a8
feat(Accordion): styles and keyboard controls
marcinsawicki aa80829
feat(Accordion): animated label with two types
marcinsawicki c3790ff
feat(Accordion): rendering hook
marcinsawicki a160df7
feat(Accordion): multiline component
marcinsawicki d114608
feat(Accordion): update
marcinsawicki 3dfba2d
feat(Accordion): docs tests and updates
marcinsawicki 1ea5294
feat(Accordion): styles update
marcinsawicki 429a03e
feat(Accordion): changes after review
marcinsawicki 4285969
feat(Accordion): styles update
marcinsawicki c0695bf
feat(Accordion): new resize observer implementation
marcinsawicki 58ef00f
feat(Accordion): check if resize observer is available
marcinsawicki 528591f
feat(Accordion): update
marcinsawicki ab478b7
Merge branch 'main' into feature/accordion-new-component
marcinsawicki 12d26af
feat(Accordion): small code refactor
marcinsawicki 919bf13
Merge branch 'main' into feature/accordion-new-component
marcinsawicki 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
29 changes: 29 additions & 0 deletions
29
packages/react-components/src/components/Accordion/Accordion.mdx
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,29 @@ | ||
import { Canvas, ArgTypes, Meta, Title } from '@storybook/blocks'; | ||
|
||
import * as AccordionStories from './Accordion.stories'; | ||
|
||
<Meta of={AccordionStories} /> | ||
|
||
<Title>Accordion</Title> | ||
|
||
[Intro](#Intro) | [Component API](#ComponentAPI) | ||
|
||
## Intro <a id="Intro" /> | ||
|
||
Accordion is a simple component for building expandable tiles that display provided content when opened. | ||
|
||
<Canvas of={AccordionStories.Examples} sourceState="none" /> | ||
|
||
#### Example implementation | ||
|
||
```jsx | ||
import { Accordion } from '@livechat/design-system-react-components'; | ||
|
||
<Accordion label="Default"> | ||
Default accordion content | ||
</Accordion> | ||
``` | ||
|
||
## Component API <a id="ComponentAPI" /> | ||
|
||
<ArgTypes of={AccordionStories.Default} sort="requiredFirst" /> |
89 changes: 89 additions & 0 deletions
89
packages/react-components/src/components/Accordion/Accordion.module.scss
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,89 @@ | ||
$base-class: 'accordion'; | ||
|
||
.#{$base-class} { | ||
display: flex; | ||
position: relative; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
transition: all var(--transition-duration-moderate-1); | ||
border: 1px solid transparent; | ||
border-radius: var(--radius-4); | ||
box-shadow: unset; | ||
background-color: var(--surface-secondary-default); | ||
width: 100%; | ||
min-height: 24px; | ||
|
||
&:focus-visible { | ||
outline: 0; | ||
box-shadow: var(--shadow-focus); | ||
} | ||
|
||
&:hover { | ||
border-color: var(--border-basic-hover); | ||
box-shadow: var(--shadow-float); | ||
} | ||
|
||
&--warning { | ||
background-color: var(--surface-accent-emphasis-min-warning); | ||
|
||
&:hover { | ||
border-color: var(--border-basic-warning); | ||
} | ||
} | ||
|
||
&--error { | ||
background-color: var(--surface-accent-emphasis-min-negative); | ||
|
||
&:hover { | ||
border-color: var(--content-basic-negative); | ||
} | ||
} | ||
|
||
&--open { | ||
border: 1px solid var(--action-primary-default); | ||
box-shadow: var(--shadow-float); | ||
background-color: var(--surface-primary-default); | ||
|
||
&:hover { | ||
border-color: var(--action-primary-default); | ||
} | ||
} | ||
|
||
&__chevron { | ||
position: absolute; | ||
top: 20px; | ||
right: 20px; | ||
transition: inherit; | ||
pointer-events: none; | ||
|
||
&--open { | ||
transform: rotate(180deg); | ||
} | ||
} | ||
|
||
&__label { | ||
margin: 0; | ||
padding: var(--spacing-5) var(--spacing-12) var(--spacing-5) | ||
var(--spacing-5); | ||
|
||
&:hover { | ||
cursor: pointer; | ||
} | ||
} | ||
|
||
&__content { | ||
transition: inherit; | ||
height: 100%; | ||
overflow: hidden; | ||
|
||
&__inner { | ||
transition: all var(--transition-duration-moderate-1); | ||
opacity: 0; | ||
padding: 0 var(--spacing-12) var(--spacing-5) var(--spacing-5); | ||
|
||
&--open { | ||
opacity: 1; | ||
} | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/react-components/src/components/Accordion/Accordion.spec.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,83 @@ | ||
import * as React from 'react'; | ||
|
||
import { vi } from 'vitest'; | ||
|
||
import { render, userEvent, waitFor } from 'test-utils'; | ||
|
||
import { Accordion } from './Accordion'; | ||
import { IAccordionProps } from './types'; | ||
|
||
const DEFAULT_PROPS = { | ||
label: 'Label', | ||
children: <div>Content</div>, | ||
}; | ||
|
||
const renderComponent = (props: IAccordionProps) => { | ||
return render(<Accordion {...props} />); | ||
}; | ||
|
||
describe('<Accordion> component', () => { | ||
it('should allow for custom class', () => { | ||
const { container } = renderComponent({ | ||
...DEFAULT_PROPS, | ||
className: 'my-class', | ||
}); | ||
|
||
expect(container.firstChild).toHaveClass('my-class'); | ||
}); | ||
|
||
it('should render as closed by default', () => { | ||
const { getByText, queryByText } = renderComponent(DEFAULT_PROPS); | ||
|
||
expect(getByText('Label')).toBeInTheDocument(); | ||
expect(queryByText('Content')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render as open if openOnInit is set true', () => { | ||
const { getByText } = renderComponent({ | ||
...DEFAULT_PROPS, | ||
openOnInit: true, | ||
}); | ||
|
||
expect(getByText('Label')).toBeInTheDocument(); | ||
expect(getByText('Content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onClose and onOpen handlers on label click', () => { | ||
const onClose = vi.fn(); | ||
const onOpen = vi.fn(); | ||
const { getByRole } = renderComponent({ | ||
...DEFAULT_PROPS, | ||
onClose, | ||
onOpen, | ||
}); | ||
|
||
userEvent.click(getByRole('button')); | ||
expect(onOpen).toHaveBeenCalledTimes(1); | ||
userEvent.click(getByRole('button')); | ||
expect(onClose).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should show different label content when open and closed', async () => { | ||
const { getByText, getByRole } = renderComponent({ | ||
...DEFAULT_PROPS, | ||
label: { | ||
open: <div>Open label</div>, | ||
closed: <div>Closed label</div>, | ||
}, | ||
}); | ||
|
||
expect(getByText('Closed label')).toBeInTheDocument(); | ||
userEvent.click(getByRole('button')); | ||
await waitFor(() => expect(getByText('Open label')).toBeInTheDocument()); | ||
}); | ||
|
||
it('should show multiline element if closed', () => { | ||
const { getByText } = renderComponent({ | ||
...DEFAULT_PROPS, | ||
multilineElement: <div>Multi</div>, | ||
}); | ||
|
||
expect(getByText('Multi')).toBeInTheDocument(); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/react-components/src/components/Accordion/Accordion.stories.css
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,31 @@ | ||
.accordion-content-container { | ||
border-left: 1px dashed var(--border-basic-primary); | ||
padding-left: var(--spacing-6); | ||
} | ||
|
||
.rule-picker { | ||
margin-bottom: var(--spacing-4); | ||
max-width: 50%; | ||
} | ||
|
||
.label { | ||
display: flex; | ||
flex-direction: row; | ||
gap: 5px; | ||
align-items: center; | ||
} | ||
|
||
.multiline { | ||
border-radius: var(--radius-3); | ||
background-color: var(--surface-tertiary-default); | ||
padding: var(--spacing-3); | ||
white-space: pre-wrap; | ||
|
||
p { | ||
margin-top: 0; | ||
|
||
&:last-child { | ||
margin-bottom: 0; | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
packages/react-components/src/components/Accordion/Accordion.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,113 @@ | ||
import * as React from 'react'; | ||
|
||
import { Tag as TagIcon } from '@livechat/design-system-icons'; | ||
import { Meta } from '@storybook/react'; | ||
|
||
import { StoryDescriptor } from '../../stories/components/StoryDescriptor'; | ||
import { Icon } from '../Icon'; | ||
import { IPickerListItem, Picker } from '../Picker'; | ||
import { Tag } from '../Tag'; | ||
|
||
import { Accordion } from './Accordion'; | ||
import { RULE_PICKER_OPTIONS, TAGS_PICKER_OPTIONS } from './stories-helpers'; | ||
|
||
import './Accordion.stories.css'; | ||
|
||
export default { | ||
title: 'Components/Accordion', | ||
component: Accordion, | ||
} as Meta<typeof Accordion>; | ||
|
||
export const Default = (): React.ReactElement => { | ||
return <Accordion label="Accordion label">Accordion content</Accordion>; | ||
}; | ||
|
||
export const Kinds = (): React.ReactElement => { | ||
return ( | ||
<div> | ||
<StoryDescriptor title="Default"> | ||
<Accordion label="Default" kind="default"> | ||
Default accordion content | ||
</Accordion> | ||
</StoryDescriptor> | ||
<StoryDescriptor title="Warning"> | ||
<Accordion label="Warning" kind="warning"> | ||
Warning accordion content | ||
</Accordion> | ||
</StoryDescriptor> | ||
<StoryDescriptor title="Error"> | ||
<Accordion label="Error" kind="error"> | ||
Error accordion content | ||
</Accordion> | ||
</StoryDescriptor> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Examples = (): React.ReactElement => { | ||
const [ruleSelected, setRuleSelected] = React.useState< | ||
IPickerListItem[] | null | ||
>([RULE_PICKER_OPTIONS[0]]); | ||
const [tagSelected, setTagSelected] = React.useState< | ||
IPickerListItem[] | null | ||
>([TAGS_PICKER_OPTIONS[0]]); | ||
|
||
return ( | ||
<div> | ||
<StoryDescriptor title="With changing label"> | ||
<Accordion | ||
label={{ | ||
closed: ( | ||
<div className="label"> | ||
<Icon source={TagIcon} /> | ||
Tag <Tag>{ruleSelected && ruleSelected[0].name}</Tag> | ||
and | ||
{tagSelected && | ||
tagSelected && | ||
tagSelected.map((tag) => <Tag key={tag.key}>{tag.name}</Tag>)} | ||
</div> | ||
), | ||
open: <div className="label">Edit your tags</div>, | ||
}} | ||
> | ||
<div className="accordion-content-container"> | ||
<div className="rule-picker"> | ||
<Picker | ||
id="rule-picker" | ||
options={RULE_PICKER_OPTIONS} | ||
selected={ruleSelected} | ||
hideClearButton | ||
searchDisabled | ||
onSelect={(selected) => setRuleSelected(selected)} | ||
/> | ||
</div> | ||
<Picker | ||
id="first-picker" | ||
type="multi" | ||
options={TAGS_PICKER_OPTIONS} | ||
selected={tagSelected} | ||
onSelect={(selected) => setTagSelected(selected)} | ||
/> | ||
</div> | ||
</Accordion> | ||
</StoryDescriptor> | ||
<StoryDescriptor title="With multiline element"> | ||
<Accordion | ||
label="Default" | ||
multilineElement={ | ||
<div className="multiline"> | ||
<p>{`Hello {{ticket.requesterName}},`}</p> | ||
<p> | ||
We haven't heard back from you for some time. If you need any | ||
further help, please follow up on this email. | ||
</p> | ||
<p>Thank you.</p> | ||
</div> | ||
} | ||
> | ||
Default accordion content | ||
</Accordion> | ||
</StoryDescriptor> | ||
</div> | ||
); | ||
}; |
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.
Please add an import path for this example