Created
May 5, 2024 03:24
-
-
Save romannurik/c7f4b1c1ed2d877fed76b6be9dc45525 to your computer and use it in GitHub Desktop.
CSS-only expando transition, as a React component
This file contains hidden or 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
:where(.container) { | |
--expando-duration: .25s; | |
--expando-easing: ease; | |
display: grid !important; | |
grid-template-rows: 0fr; | |
--expando-transition: | |
grid-template-rows var(--expando-duration) var(--expando-easing), | |
visibility var(--expando-duration); | |
transition: var(--expando-transition); | |
overflow: hidden; | |
visibility: hidden; | |
&.isExpanded { | |
grid-template-rows: 1fr; | |
visibility: visible; | |
} | |
} | |
.inner { | |
min-height: 0; | |
box-sizing: border-box; | |
} |
This file contains hidden or 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
import cn from 'classnames'; | |
import { PropsWithChildren } from 'react'; | |
import styles from './Expando.module.scss'; | |
interface Props extends PropsWithChildren { | |
className?: string; | |
classNameExpanded?: string; | |
classNameContent?: string; | |
expanded?: boolean; | |
} | |
export function Expando({ | |
className, | |
classNameContent, | |
classNameExpanded, | |
expanded, | |
children | |
}: Props) { | |
return <div className={cn( | |
className, | |
styles.container, | |
{ [styles.isExpanded]: expanded }, | |
classNameExpanded && { [classNameExpanded]: expanded }, | |
)}> | |
<div className={styles.inner}> | |
<div className={classNameContent}> | |
{children} | |
</div> | |
</div> | |
</div>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment