Skip to content

Commit

Permalink
Merge pull request #1 from x86chi/fix/use-styled-props
Browse files Browse the repository at this point in the history
fix: Use styled props in Collapse component
  • Loading branch information
shiftpsh committed May 10, 2022
2 parents 392d9c1 + f946f76 commit 609190f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 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 CollapseContainer = styled.div<RenderComponentProps>`
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,19 +53,13 @@ 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',
}}
<CollapseContainer
as={as}
height={renderHeight}
opacity={prevShown || shown ? 1 : 0}
>
{mountChild ? <div ref={contentsRef}>{children}</div> : null}
</RenderComponent>
</CollapseContainer>
)
}

0 comments on commit 609190f

Please sign in to comment.