Skip to content

Commit

Permalink
feat(HeightAnimation): add onInit to get animation instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Oct 3, 2022
1 parent dc14a79 commit 2b906d1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ showTabs: true

## Events

| Events | Description |
| -------- | ----------------------------------------------------------------------------------------------------- |
| `onOpen` | _(optional)_ Is called when fully opened or closed. Returns `true` or `false` depending on the state. |
| `onOpen` | _(optional)_ Is called when fully opened or closed. Returns `true` or `false` depending on the state. |
| Events | Description |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `onOpen` | _(optional)_ Is called when fully opened or closed. Returns `true` or `false` depending on the state. |
| `onAnimationEnd` | _(optional)_ Is called when aanimation is done and the full height has been reached.. |
| `onInit` | _(optional)_ Is called once before mounting the component (useLayoutEffect). Returns the instance of the internal animation class. |
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function HeightAnimation({
className,
innerRef,
children,
onInit = null,
onOpen = null,
onAnimationEnd = null,
...props
Expand All @@ -55,6 +56,7 @@ export default function HeightAnimation({
open,
animate,
children,
onInit,
onOpen,
onAnimationEnd,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { render, act, fireEvent } from '@testing-library/react'
import ToggleButton from '../../ToggleButton'
import { wait } from '@testing-library/user-event/dist/utils'
import HeightAnimation, { HeightAnimationProps } from '../HeightAnimation'
import AnimateHeight from '../../../shared/AnimateHeight'

beforeEach(() => {
global.IS_TEST = false
Expand Down Expand Up @@ -160,6 +161,75 @@ describe('HeightAnimation', () => {
})
})

it('should call onOpen', () => {
const onOpen = jest.fn()
const { rerender } = render(<Component onOpen={onOpen} />)

expect(document.querySelector('.dnb-height-animation')).toBeFalsy()

rerender(<Component open />)

act(() => {
simulateAnimationEnd()
expect(onOpen).toHaveBeenCalledTimes(1)
expect(onOpen).toHaveBeenCalledWith(true)
})

rerender(<Component open={false} />)

act(() => {
simulateAnimationEnd()
expect(onOpen).toHaveBeenCalledTimes(2)
expect(onOpen).toHaveBeenCalledWith(false)
})
})

it('should call onAnimationEnd', () => {
const onAnimationEnd = jest.fn()
const { rerender } = render(
<Component onAnimationEnd={onAnimationEnd} />
)

expect(document.querySelector('.dnb-height-animation')).toBeFalsy()

rerender(<Component open />)

act(() => {
simulateAnimationEnd()
expect(onAnimationEnd).toHaveBeenCalledTimes(1)
expect(onAnimationEnd).toHaveBeenCalledWith('opened')
})

rerender(<Component open={false} />)

act(() => {
simulateAnimationEnd()
expect(onAnimationEnd).toHaveBeenCalledWith('closed')
})
})

it('should call onInit', () => {
const onInit = jest.fn()
const { rerender } = render(<Component onInit={onInit} />)

expect(document.querySelector('.dnb-height-animation')).toBeFalsy()

rerender(<Component open />)

act(() => {
simulateAnimationEnd()
expect(onInit).toHaveBeenCalledTimes(1)
expect(onInit).toHaveBeenCalledWith(expect.any(AnimateHeight))
})

rerender(<Component open={false} />)

act(() => {
simulateAnimationEnd()
expect(onInit).toHaveBeenCalledTimes(1)
})
})

it('should have content in DOM when keepInDOM is true', async () => {
const { rerender } = render(<Component keepInDOM />)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ export type useHeightAnimationOptions = {
*/
children?: React.ReactNode | HTMLElement

/**
* Is called once before mounting the component (useLayoutEffect)
*/
onInit?: (instance: AnimateHeight) => void

/**
* Is called when fully opened or closed
*/
onOpen?: (isOpen: boolean) => void

/**
* Is called when animation is done and the full height has reached
* Is called when aanimation is done and the full height has been reached.
*/
onAnimationEnd?: (state: HeightAnimationOnEndTypes) => void
}
Expand All @@ -43,11 +48,12 @@ export function useHeightAnimation(
open = null,
animate = true,
children = null,
onInit = null,
onOpen = null,
onAnimationEnd = null,
}: useHeightAnimationOptions = {}
) {
const animRef = React.useRef(null)
const animRef = React.useRef<AnimateHeight>(null)
const [isOpen, setIsOpen] = React.useState(open)
const [isVisible, setIsVisible] = React.useState(false)
const [isAnimating, setIsAnimating] = React.useState(false)
Expand All @@ -63,6 +69,10 @@ export function useHeightAnimation(
React.useLayoutEffect(() => {
animRef.current = new AnimateHeight({ animate })

if (isInitialRender && onInit) {
onInit(animRef.current)
}

if (isInitialRender && isOpen) {
onOpen?.(true)
}
Expand Down

0 comments on commit 2b906d1

Please sign in to comment.