-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
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,106 @@ | ||
@use '../../helpers'; | ||
@use '../../mixins'; | ||
|
||
@mixin Accordion() { | ||
@if not mixins.includes('Accordion') { | ||
@include _Accordion(); | ||
} | ||
} | ||
|
||
@mixin _Accordion() { | ||
.ods-accordion-wrapper:not(.separated) { | ||
background-color: helpers.color('background-surface'); | ||
border: 1px solid helpers.color('border-separator'); | ||
border-radius: helpers.border-radius('large'); | ||
overflow: hidden; | ||
} | ||
|
||
.ods-accordion { | ||
overflow: hidden; | ||
|
||
&:not(:last-child) { | ||
border-bottom: 1px solid helpers.color('border-separator'); | ||
} | ||
} | ||
|
||
.separated .ods-accordion { | ||
background-color: helpers.color('background-surface'); | ||
border: 1px solid helpers.color('border-separator'); | ||
border-radius: helpers.border-radius('large'); | ||
|
||
&:not(:last-child) { | ||
margin-bottom: helpers.space(2); | ||
} | ||
} | ||
|
||
.ods-accordion-toggler { | ||
@include helpers.font('400-regular'); | ||
align-items: center; | ||
background-color: transparent; | ||
border: 0; | ||
cursor: pointer; | ||
display: flex; | ||
gap: helpers.space(1); | ||
justify-content: flex-start; | ||
padding: var(--padding); | ||
transition: background-color linear 0.2s; | ||
width: 100%; | ||
|
||
&:hover { | ||
background-color: helpers.color('background-action-subtle'); | ||
} | ||
} | ||
|
||
.ods-accordion-secondary-content > * { | ||
margin: 0; | ||
} | ||
|
||
.ods-accordion-chevron { | ||
transition: transform linear 0.2s; | ||
} | ||
|
||
.ods-accordion-title { | ||
margin-right: auto; | ||
} | ||
|
||
.ods-accordion-content-wrapper { | ||
max-height: 0; | ||
overflow: hidden; | ||
padding: 0 var(--padding); | ||
transition: padding ease-in-out 0.3s, max-height ease-in-out 0.3s; | ||
} | ||
|
||
.separator { | ||
position: relative; | ||
|
||
&::before { | ||
border-bottom: 1px solid helpers.color('border-separator'); | ||
content: ''; | ||
left: var(--padding); | ||
position: absolute; | ||
right: var(--padding); | ||
top: 0; | ||
} | ||
} | ||
|
||
.open { | ||
.ods-accordion-content-wrapper { | ||
max-height: 100vh; | ||
padding-bottom: var(--padding); | ||
padding-top: var(--padding); | ||
} | ||
|
||
.ods-accordion-content { | ||
opacity: 1; | ||
} | ||
|
||
.ods-accordion-chevron { | ||
transform: rotateZ(180deg); | ||
} | ||
} | ||
|
||
.ods-accordion-content { | ||
opacity: 0; | ||
transition: opacity linear 0.4s; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
packages/react/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,24 @@ | ||
import { Accordion, type AccordionProps } from '@onfido/castor-react'; | ||
import React from 'react'; | ||
import { Meta, Story } from '../../../../../docs'; | ||
|
||
export default { | ||
title: 'React/Accordion', | ||
component: Accordion, | ||
args: { | ||
list: [ | ||
{ | ||
title: 'A title with an icon', | ||
iconName: 'bolt', | ||
content: <div>Content example.</div>, | ||
}, | ||
{ | ||
title: 'Another title without icon', | ||
content: <div>Another content example.</div>, | ||
}, | ||
], | ||
}, | ||
parameters: {}, | ||
} as Meta<AccordionProps>; | ||
|
||
export const Playground: Story<AccordionProps> = {}; |
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,112 @@ | ||
import { c, classy, cssVars, space } from '@onfido/castor'; | ||
import type { IconName } from '@onfido/castor-icons'; | ||
import { Icon } from '@onfido/castor-react'; | ||
import React, { useEffect, useState, type FC } from 'react'; | ||
|
||
export type AccordionItem = { | ||
isOpen?: boolean; | ||
title: string; | ||
iconName?: IconName; | ||
content: React.ReactElement; | ||
secondaryContent?: React.ReactElement; | ||
}; | ||
|
||
export type AccordionStyles = { | ||
padding?: number; | ||
}; | ||
|
||
export interface AccordionProps { | ||
list: AccordionItem[]; | ||
onChange?: (newList: AccordionItem[]) => void; | ||
onlyOneOpen: boolean; | ||
separated: boolean; | ||
withSeparator: boolean; | ||
styles?: AccordionStyles; | ||
} | ||
|
||
const defaultStyles = { | ||
padding: 3, | ||
}; | ||
|
||
export const Accordion: FC<AccordionProps> = ({ | ||
list: listProps, | ||
onChange, | ||
onlyOneOpen = false, | ||
separated = false, | ||
withSeparator = false, | ||
styles: stylesProps = defaultStyles, | ||
}) => { | ||
const [list, setList] = useState<AccordionItem[]>(listProps); | ||
|
||
const _styles = { | ||
...defaultStyles, | ||
...stylesProps, | ||
}; | ||
|
||
useEffect(() => { | ||
let newList = [...list]; | ||
|
||
newList = newList.map((item) => ({ | ||
...item, | ||
isOpen: false, | ||
})); | ||
|
||
if (onChange) onChange(newList); | ||
}, [onlyOneOpen]); | ||
|
||
const handleClick = (index: number) => () => { | ||
let newList = [...list]; | ||
|
||
if (onlyOneOpen) { | ||
newList = newList.map((item, newIndex) => ({ | ||
...item, | ||
isOpen: newIndex !== index ? false : !item.isOpen, | ||
})); | ||
} else { | ||
newList[index].isOpen = !list[index].isOpen; | ||
} | ||
setList(newList); | ||
if (onChange) onChange(newList); | ||
}; | ||
|
||
const style = cssVars({ padding: space(_styles.padding) }); | ||
|
||
return ( | ||
<div | ||
style={style} | ||
className={classy(c('accordion-wrapper'), { separated })} | ||
> | ||
{list.map((item, index) => ( | ||
<div | ||
key={index} | ||
className={classy(c('accordion'), { open: item.isOpen })} | ||
> | ||
<button | ||
className={classy(c('accordion-toggler'))} | ||
onClick={handleClick(index)} | ||
> | ||
{item.iconName && <Icon name={item.iconName} aria-hidden="true" />} | ||
<span className={classy(c('accordion-title'))}>{item.title}</span> | ||
{item.secondaryContent && ( | ||
<span className={classy(c('accordion-secondary-content'))}> | ||
{item.secondaryContent} | ||
</span> | ||
)} | ||
<Icon | ||
className={classy(c('accordion-chevron'))} | ||
name="chevron-down" | ||
aria-hidden="true" | ||
/> | ||
</button> | ||
<div | ||
className={classy(c('accordion-content-wrapper'), { | ||
separator: withSeparator, | ||
})} | ||
> | ||
<div className={classy(c('accordion-content'))}>{item.content}</div> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; |
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
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