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

fix: Use styled props in Collapse component #1

Merged
merged 2 commits into from
May 10, 2022
Merged
Changes from 1 commit
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
29 changes: 19 additions & 10 deletions src/components/Collapse.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import React, { ReactHTML, useLayoutEffect, useRef, useState } from 'react'
import styled from 'styled-components'

export interface CollapseProps {
shown: boolean
children?: React.ReactNode
as?: keyof ReactHTML
}

interface RenderComponentProps {
height: number | 'auto'
opacity: number
}

const RenderComponent = styled.div<RenderComponentProps>`
mu-hun marked this conversation as resolved.
Show resolved Hide resolved
height: ${({ height }) => height};
transform-origin: top;
opacity: ${({ opacity }) => opacity};
transition: height 0.3s ease, opacity 0.3s ease;
pointer-events: ${({ opacity }) => (opacity ? 'all' : 'none')};
overflow: 'hidden';
`

export const Collapse: React.FC<CollapseProps> = (props) => {
const { as = 'div', shown, children } = props
const { as, shown, children } = props

const contentsRef = useRef<HTMLDivElement>(null)
const [contentHeight, setContentHeight] = useState<number>(0)
Expand Down Expand Up @@ -38,17 +53,11 @@ export const Collapse: React.FC<CollapseProps> = (props) => {
}
}, [shown, contentHeight])

const RenderComponent = as
return (
<RenderComponent
style={{
height: renderHeight,
transformOrigin: 'top',
opacity: prevShown || shown ? 1 : 0,
transition: `height .3s ease, opacity .3s ease`,
pointerEvents: prevShown || shown ? 'all' : 'none',
overflow: 'hidden',
}}
as={as}
height={renderHeight}
opacity={prevShown || shown ? 1 : 0}
>
{mountChild ? <div ref={contentsRef}>{children}</div> : null}
</RenderComponent>
Expand Down