Merge pull request #12 from acid-info/topic-implement-collapse

Implement collapse component
This commit is contained in:
jeangovil 2023-02-23 15:26:22 +03:30 committed by GitHub
commit 5a8c503477
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 330 additions and 0 deletions

View File

@ -12,6 +12,8 @@ import { BreadcrumbStyles } from '../Breadcrumb/Breadcrumb.styles'
import { BreadcrumbItemStyles } from '../BreadcrumbItem/BreadcrumbItem.styles'
import { defaultThemes, Theme, withTheme } from '../Theme'
import { TypographyStyles } from '../Typography/Typography.styles'
import { CollapseHeaderStyles } from '../CollapseHeader/CollapseHeader.styles'
import { CollapseStyles } from '../Collapse/Collapse.styles'
const componentStyles: Array<ReturnType<typeof withTheme> | SerializedStyles> =
[
@ -26,6 +28,8 @@ const componentStyles: Array<ReturnType<typeof withTheme> | SerializedStyles> =
IconTagStyles,
BreadcrumbStyles,
BreadcrumbItemStyles,
CollapseStyles,
CollapseHeaderStyles,
]
export const CSSBaseline: React.FC<{ theme?: Theme }> = ({

View File

@ -0,0 +1,8 @@
export const collapseClasses = {
root: `lsd-collapse`,
content: `lsd-collapse-content`,
open: 'lsd-collapse--open-item',
disabled: 'lsd-collapse--disabled',
}

View File

@ -0,0 +1,34 @@
import { Meta, Story } from '@storybook/react'
import { Typography } from '../Typography'
import { Collapse, CollapseProps } from './Collapse'
export default {
title: 'Collapse',
component: Collapse,
argTypes: {
size: {
type: {
name: 'enum',
value: ['small', 'medium', 'large'],
},
},
},
} as Meta
export const Root: Story<CollapseProps> = (args) => (
<div style={{ width: 'fit-content' }}>
<Collapse {...args}>
<div style={{ padding: '10px 18px' }}>
<Typography color="primary" component="label">
Slot component
</Typography>
</div>
</Collapse>
</div>
)
Root.args = {
size: 'large',
label: 'Title',
disabled: false,
}

View File

@ -0,0 +1,20 @@
import { css } from '@emotion/react'
import { collapseClasses } from './Collapse.classes'
export const CollapseStyles = css`
.${collapseClasses.root} {
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.${collapseClasses.open} {
.${collapseClasses.content} {
border-top: 1px solid transparent;
}
}
.${collapseClasses.content} {
border: 1px solid rgb(var(--lsd-border-primary));
}
`

View File

@ -0,0 +1,58 @@
import clsx from 'clsx'
import React, { useEffect, useRef, useState } from 'react'
import { CollapseHeader } from '../CollapseHeader'
import { collapseClasses } from './Collapse.classes'
export type CollapseProps = Omit<
React.HTMLAttributes<HTMLDivElement>,
'label'
> & {
label: string
disabled?: boolean
size?: 'small' | 'medium' | 'large'
open?: boolean
onChange?: (open: boolean) => void
}
export const Collapse: React.FC<CollapseProps> & {
classes: typeof collapseClasses
} = ({ label, disabled = false, size = 'large', children, ...props }) => {
const ref = useRef<HTMLDivElement>(null)
const [open, setOpen] = useState(props.open ?? false)
const handleChange = (value: boolean) => {
if (typeof props.open === 'undefined') return setOpen(value)
props.onChange && props.onChange(value)
}
const onTrigger = () => !disabled && handleChange(!open)
useEffect(() => {
disabled && open && handleChange(false)
}, [disabled, open, handleChange])
return (
<div
{...props}
ref={ref}
className={clsx(
props.className,
collapseClasses.root,
disabled && collapseClasses.disabled,
open && collapseClasses.open,
)}
>
<CollapseHeader
label={label}
open={open}
setOpen={setOpen}
size={size}
onTrigger={onTrigger}
disabled={disabled}
/>
{open && <div className={collapseClasses.content}>{children}</div>}
</div>
)
}
Collapse.classes = collapseClasses

View File

@ -0,0 +1 @@
export * from './Collapse'

View File

@ -0,0 +1,16 @@
export const collapseHeaderClasses = {
root: `lsd-collapse-header`,
trigger: `lsd-collapse-header-trigger`,
triggerLabel: `lsd-collapse-header-trigger__label`,
triggerIcons: `lsd-collapse-header-trigger-icons`,
triggerIcon: `lsd-collapse-header-trigger-icons__icon`,
triggerMenuIcon: `lsd-collapse-header-trigger-icons__menu-icon`,
open: 'lsd-collapse-header--open',
disabled: 'lsd-collapse-header--disabled',
small: `lsd-collapse-header--small`,
medium: `lsd-collapse-header--medium`,
large: `lsd-collapse-header--large`,
}

View File

@ -0,0 +1,27 @@
import { Meta, Story } from '@storybook/react'
import { CollapseHeader, CollapseHeaderProps } from './CollapseHeader'
export default {
title: 'CollapseHeader',
component: CollapseHeader,
argTypes: {
size: {
type: {
name: 'enum',
value: ['small', 'medium', 'large'],
},
},
},
} as Meta
export const Root: Story<CollapseHeaderProps> = (args) => (
<div style={{ width: 'fit-content' }}>
<CollapseHeader {...args}></CollapseHeader>
</div>
)
Root.args = {
size: 'large',
label: 'title',
disabled: false,
}

View File

@ -0,0 +1,88 @@
import { css } from '@emotion/react'
import { collapseHeaderClasses } from './CollapseHeader.classes'
export const CollapseHeaderStyles = css`
.${collapseHeaderClasses.root} {
box-sizing: border-box;
}
.${collapseHeaderClasses.root}:not(.${collapseHeaderClasses.disabled}) {
.${collapseHeaderClasses.trigger} {
&:hover {
.${collapseHeaderClasses.triggerLabel} {
text-decoration: underline;
}
}
}
}
.${collapseHeaderClasses.trigger} {
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 10px 14px 10px 18px;
cursor: pointer;
background: none;
border: 1px solid rgb(var(--lsd-border-primary));
&:focus {
outline: none;
}
}
.${collapseHeaderClasses.triggerLabel} {
cursor: inherit;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin: auto;
}
.${collapseHeaderClasses.triggerIcons} {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-end;
}
.${collapseHeaderClasses.triggerIcon} {
margin-right: 8px;
}
.${collapseHeaderClasses.triggerMenuIcon} {
}
.${collapseHeaderClasses.disabled} {
.${collapseHeaderClasses.trigger} {
opacity: 0.34;
cursor: initial;
}
}
.${collapseHeaderClasses.large} {
.${collapseHeaderClasses.trigger} {
width: 299px;
height: 40px;
padding: 10px 18px;
}
}
.${collapseHeaderClasses.medium} {
.${collapseHeaderClasses.trigger} {
width: 270px;
height: 32px;
padding: 6px 14px;
}
}
.${collapseHeaderClasses.small} {
.${collapseHeaderClasses.trigger} {
width: 235px;
height: 28px;
padding: 6px 12px;
}
}
`

View File

@ -0,0 +1,71 @@
import clsx from 'clsx'
import React, { useEffect, useState } from 'react'
import { ArrowDownIcon, ArrowUpIcon } from '../Icons'
import { Typography } from '../Typography'
import { collapseHeaderClasses } from './CollapseHeader.classes'
export type CollapseHeaderProps = Omit<
React.HTMLAttributes<HTMLDivElement>,
'label' | 'disabled'
> & {
label: string
open: boolean
setOpen: React.Dispatch<React.SetStateAction<boolean>>
disabled?: boolean
onTrigger: () => void
size?: 'small' | 'medium' | 'large'
}
export const CollapseHeader: React.FC<CollapseHeaderProps> & {
classes: typeof collapseHeaderClasses
} = ({
label,
disabled = false,
open,
setOpen,
size = 'large',
onTrigger,
...props
}) => {
return (
<div
{...props}
className={clsx(
props.className,
collapseHeaderClasses.root,
collapseHeaderClasses[size],
disabled && collapseHeaderClasses.disabled,
open && collapseHeaderClasses.open,
)}
>
<button
className={clsx(collapseHeaderClasses.trigger)}
onClick={onTrigger}
>
<Typography
color="primary"
component="label"
variant={size === 'small' ? 'label2' : 'label1'}
className={collapseHeaderClasses.triggerLabel}
>
{label}
</Typography>
<div className={collapseHeaderClasses.triggerIcons}>
{open ? (
<ArrowUpIcon
color="primary"
className={collapseHeaderClasses.triggerMenuIcon}
/>
) : (
<ArrowDownIcon
color="primary"
className={collapseHeaderClasses.triggerMenuIcon}
/>
)}
</div>
</button>
</div>
)
}
CollapseHeader.classes = collapseHeaderClasses

View File

@ -0,0 +1 @@
export * from './CollapseHeader'

View File

@ -9,3 +9,5 @@ export * from './components/Tabs'
export * from './components/Theme'
export * from './components/Breadcrumb'
export * from './components/BreadcrumbItem'
export * from './components/Collapse'
export * from './components/CollapseHeader'