This repository was archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Add accordion component #343
Open
rose-liang
wants to merge
6
commits into
main
Choose a base branch
from
DSYS-26-accordion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da73fec
feature: create accordion component and story
rose-liang de1e01f
feat: add test, aria labels, update storybook
rose-liang 3700fb8
feat: add animation and update styles
rose-liang 5c94258
chore(baseline): update lost-pixel baselines (#344)
juliewongbandue 713402c
feat: add prop comments, focus component, optional props
rose-liang e58dcaa
Merge branch 'DSYS-26-accordion' of https://github.com/vimeo/iris int…
rose-liang 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,93 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { | ||
Content, | ||
ChevronUp, | ||
CircleWarningIcon, | ||
HeaderContainer, | ||
StyledChevronDown, | ||
Subcopy, | ||
Title, | ||
TitleContainer, | ||
TriggerContainer, | ||
Wrapper, | ||
} from './Accordion.style'; | ||
import { MinorComponent } from '../../utils'; | ||
import { AccordionItemProps } from './Accordion.types'; | ||
|
||
export interface Minors { | ||
Item: MinorComponent<any>; | ||
} | ||
|
||
export function Item({ | ||
children, | ||
title, | ||
format, | ||
index, | ||
allowMultiple, | ||
defaultActive, | ||
setActiveIndex, | ||
itemActive, | ||
subcopy = '', | ||
icon, | ||
hasError = false, | ||
disabled = false, | ||
}: AccordionItemProps) { | ||
const [active, setActive] = useState<boolean>(defaultActive); | ||
const isActive = allowMultiple | ||
? active && !disabled | ||
: itemActive && !disabled; | ||
|
||
return ( | ||
<Wrapper | ||
active={isActive} | ||
disabled={disabled} | ||
format={format} | ||
key={index} | ||
> | ||
<TriggerContainer | ||
onClick={() => { | ||
if (allowMultiple) { | ||
setActive(!active); | ||
} else { | ||
setActiveIndex({ index }); | ||
} | ||
}} | ||
tabIndex={0} | ||
format={format} | ||
active={isActive} | ||
aria-expanded={isActive} | ||
aria-controls={`accordion-${index}-content`} | ||
id={`accordion-${index}-trigger`} | ||
> | ||
<HeaderContainer> | ||
{hasError && <CircleWarningIcon />} | ||
{!hasError && icon && icon} | ||
<TitleContainer> | ||
<Title size="4">{title}</Title> | ||
{subcopy && ( | ||
<Subcopy size="6" variant="thin"> | ||
{subcopy} | ||
</Subcopy> | ||
)} | ||
</TitleContainer> | ||
</HeaderContainer> | ||
{isActive ? ( | ||
<ChevronUp width="24" /> | ||
) : ( | ||
<StyledChevronDown width="24" /> | ||
)} | ||
</TriggerContainer> | ||
{isActive && ( | ||
<Content | ||
active={isActive} | ||
aria-labelledby={`accordion-${index}-trigger`} | ||
id={`accordion-${index}-content`} | ||
role="region" | ||
> | ||
{children} | ||
</Content> | ||
)} | ||
</Wrapper> | ||
); | ||
} |
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,59 @@ | ||
import React from 'react'; | ||
import { Story } from '@storybook/react'; | ||
|
||
import { Accordion } from './Accordion'; | ||
import { Props } from './Accordion.types'; | ||
import styled from 'styled-components'; | ||
import { Gear } from '../../icons'; | ||
import { rem } from 'polished'; | ||
import { core } from '../../tokens'; | ||
import { Layout } from '../../storybook'; | ||
|
||
export default { | ||
title: 'components/Accordion', | ||
component: Accordion, | ||
}; | ||
|
||
const Template: Story<Props> = (args) => { | ||
return ( | ||
<Layout.StoryVertical center> | ||
<Accordion {...args}> | ||
<Accordion.Item | ||
title="Accordion item title" | ||
subcopy="Subcopy text" | ||
> | ||
Accordion content | ||
</Accordion.Item> | ||
<Accordion.Item | ||
title="Accordion item with icon" | ||
icon={<GearIcon />} | ||
> | ||
Accordion content | ||
</Accordion.Item> | ||
<Accordion.Item | ||
title="Disabled accordion item" | ||
disabled={true} | ||
> | ||
Accordion content | ||
</Accordion.Item> | ||
<Accordion.Item | ||
title="Accordion item with error" | ||
hasError={true} | ||
> | ||
Accordion content | ||
</Accordion.Item> | ||
</Accordion> | ||
</Layout.StoryVertical> | ||
); | ||
}; | ||
|
||
const GearIcon = styled(Gear)` | ||
width: ${rem(22)}; | ||
margin-right: ${rem(10)}; | ||
path { | ||
fill: ${core.color.text.primary}; | ||
} | ||
`; | ||
|
||
export const Controls = Template.bind({}); | ||
Controls.storyName = 'Accordion'; |
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,155 @@ | ||
import { rem } from 'polished'; | ||
import styled, { css, keyframes } from 'styled-components'; | ||
|
||
import { ChevronDown, CircleWarning } from '../../icons'; | ||
import { Header } from '../../typography'; | ||
|
||
import { grayscale } from '../../color'; | ||
import { core } from '../../tokens'; | ||
|
||
export const AccordionStyled = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: ${rem(8)}; | ||
`; | ||
|
||
export const Wrapper = styled.div<{ | ||
format: 'basic' | 'secondary'; | ||
active: boolean; | ||
disabled: boolean; | ||
}>` | ||
${({ disabled }) => | ||
disabled && | ||
css` | ||
pointer-events: none; | ||
opacity: 0.4; | ||
`} | ||
${({ active, theme, format }) => | ||
active && | ||
css` | ||
background-color: ${format === 'basic' | ||
? theme.name === 'dark' | ||
? grayscale(800) | ||
: grayscale(50) | ||
juliewongbandue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: 'none'}; | ||
`} | ||
border-radius: ${rem(10)}; | ||
color: ${core.color.text.primary}; | ||
${({ active, theme, format }) => | ||
active && | ||
css` | ||
border: ${format === 'secondary' | ||
? theme.name === 'dark' | ||
? `${rem(1)} solid ${grayscale(800)}` | ||
: `${rem(1)} solid ${grayscale(50)}` | ||
Comment on lines
+43
to
+44
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. If the border color is based on the surface token, you could hold that value in a const (or css property!). You should be able to use the |
||
: 'none'}; | ||
`} | ||
`; | ||
|
||
export const TriggerContainer = styled.button<{ | ||
format: 'basic' | 'secondary'; | ||
active: boolean; | ||
}>` | ||
display: flex; | ||
justify-content: space-between; | ||
cursor: pointer; | ||
padding: ${rem(12)} ${rem(15)}; | ||
border-radius: ${rem(10)}; | ||
width: 100%; | ||
&:hover { | ||
background-color: ${({ theme, format }) => | ||
format === 'basic' | ||
? theme.name === 'dark' | ||
? grayscale(800) | ||
: grayscale(50) | ||
juliewongbandue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: 'none'}; | ||
outline: ${({ active, theme, format }) => | ||
!active && format === 'secondary' | ||
? theme.name === 'dark' | ||
? `${rem(1)} solid ${grayscale(800)}` | ||
: `${rem(1)} solid ${grayscale(50)}` | ||
: 'none'}; | ||
} | ||
`; | ||
|
||
export const HeaderContainer = styled.div` | ||
display: flex; | ||
margin-right: ${rem(10)}; | ||
`; | ||
|
||
export const TitleContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
text-align: left; | ||
gap: ${rem(4)}; | ||
`; | ||
|
||
export const Title = styled(Header)` | ||
margin-bottom: ${rem(0)}; | ||
`; | ||
|
||
export const Subcopy = styled(Header)` | ||
margin-bottom: -${rem(0.2)}; | ||
`; | ||
|
||
export const CircleWarningIcon = styled(CircleWarning)` | ||
width: ${rem(22)}; | ||
margin-right: ${rem(10)}; | ||
path { | ||
fill: ${core.color.status.negative}; | ||
} | ||
`; | ||
|
||
const rotateUp = keyframes` | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(-180deg); | ||
} | ||
`; | ||
|
||
const rotateDown = keyframes` | ||
from { | ||
transform: rotate(-180deg); | ||
} | ||
to { | ||
transform: rotate(0deg); | ||
} | ||
`; | ||
|
||
export const StyledChevronDown = styled(ChevronDown)` | ||
animation: ${rotateDown} 120ms ease-in-out both; | ||
path { | ||
juliewongbandue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fill: ${core.color.text.primary}; | ||
} | ||
`; | ||
|
||
export const ChevronUp = styled(StyledChevronDown)` | ||
animation: ${rotateUp} 120ms ease-in-out both; | ||
`; | ||
|
||
const fadeAndExpand = keyframes` | ||
from { | ||
transform: translateY(-50%); | ||
opacity: 0; | ||
} | ||
to { | ||
transform: translateY(0); | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
export const Content = styled.div<{ active: boolean }>` | ||
padding: ${rem(0)} ${rem(15)} ${rem(20)} ${rem(20)}; | ||
max-height: ${({ active }) => (active ? '100%' : '0')}; | ||
overflow: hidden; | ||
transform: translateY(-50%); | ||
opacity: 0; | ||
${({ active }) => | ||
active && | ||
css` | ||
animation: ${fadeAndExpand} 150ms ease-in-out both; | ||
opacity: 1; | ||
`}; | ||
`; |
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.
My concern about this here is what if we want want a different composition of of where the chevron is? What if we just want to render a different kind of warning icon? We should allow devs to compose the contents here however they might need to.