Skip to content
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

Add accordion component #343

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions src/components/Accordion/Accordion.minors.tsx
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" />
)}
Copy link
Collaborator

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.

</TriggerContainer>
{isActive && (
<Content
active={isActive}
aria-labelledby={`accordion-${index}-trigger`}
id={`accordion-${index}-content`}
role="region"
>
{children}
</Content>
)}
</Wrapper>
);
}
59 changes: 59 additions & 0 deletions src/components/Accordion/Accordion.story.tsx
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';
155 changes: 155 additions & 0 deletions src/components/Accordion/Accordion.style.ts
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 -- css custom property syntax (see example from https://styled-components.com/)

: '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;
`};
`;