|
| 1 | +import * as React from 'react' |
| 2 | +import { Transition } from 'react-transition-group' |
| 3 | + |
| 4 | +import { Box } from '../box' |
| 5 | + |
| 6 | +import styles from './animated-expansion-panel-content.module.css' |
| 7 | + |
| 8 | +const HEIGHT_TRANSITION_DURATION_MS = 200 |
| 9 | + |
| 10 | +/** Vertical offset (px) the content drops in from as it opens. Negative = starts |
| 11 | + * slightly above its resting place and settles down, matching the direction the |
| 12 | + * height reveals (top-down), so the two motions read as one. */ |
| 13 | +const CONTENT_ENTER_OFFSET = -12 |
| 14 | + |
| 15 | +/* On open the content drops in with a gentle overshoot, and its fade is delayed |
| 16 | + * slightly so it trails the reveal/drop rather than fading in from the first frame. |
| 17 | + * On close it just fades out promptly (no delay, no spring). */ |
| 18 | +const OPEN_TRANSITION = |
| 19 | + 'opacity 150ms ease-out 50ms, transform 190ms cubic-bezier(0.34, 1.3, 0.64, 1)' |
| 20 | +const CLOSE_TRANSITION = 'opacity 140ms ease-in, transform 190ms cubic-bezier(0.4, 0, 0.2, 1)' |
| 21 | + |
| 22 | +function setElementHeight(element: HTMLElement, height: number | 'auto') { |
| 23 | + element.style.transitionDuration = `${HEIGHT_TRANSITION_DURATION_MS}ms` |
| 24 | + element.style.height = height === 'auto' ? height : `${height}px` |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Drives the content's own motion (opacity + vertical offset). The bounce lives |
| 29 | + * here — on the content itself — rather than on the container height, so it reads |
| 30 | + * the same regardless of what sits below it. (A height-only overshoot is only |
| 31 | + * visible by displacing following content.) Spring easing lives in the inline |
| 32 | + * transition above. |
| 33 | + */ |
| 34 | +function setContentMotion( |
| 35 | + element: HTMLElement | null, |
| 36 | + opacity: 0 | 1, |
| 37 | + offset: number, |
| 38 | + transition?: string, |
| 39 | +) { |
| 40 | + if (!element) { |
| 41 | + return |
| 42 | + } |
| 43 | + if (transition !== undefined) { |
| 44 | + element.style.transition = transition |
| 45 | + } |
| 46 | + element.style.opacity = String(opacity) |
| 47 | + element.style.transform = offset === 0 ? 'none' : `translateY(${offset}px)` |
| 48 | +} |
| 49 | + |
| 50 | +type Props = { |
| 51 | + /** The content to be collapsed */ |
| 52 | + children: React.ReactNode |
| 53 | + |
| 54 | + /** The expanded/collapse state of the panel */ |
| 55 | + isExpanded: boolean |
| 56 | + |
| 57 | + /** Callback fired when the expansion animation completes */ |
| 58 | + onEntered?: () => void |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Internal wrapper used by `ExpansionPanelContent`. Animates the container's |
| 63 | + * height to reveal/hide the space, while the content cross-fades and springs |
| 64 | + * into place (driven imperatively so the motion still runs when entering from |
| 65 | + * `display: none`). |
| 66 | + */ |
| 67 | +function AnimatedExpansionPanelContent({ isExpanded, children, onEntered }: Props) { |
| 68 | + const transitionElementRef = React.useRef<HTMLDivElement>(null) |
| 69 | + const wrapperRef = React.useRef<HTMLDivElement>(null) |
| 70 | + const contentRef = React.useRef<HTMLDivElement>(null) |
| 71 | + |
| 72 | + const handleEnter = React.useCallback(() => { |
| 73 | + if (!transitionElementRef.current) { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + setElementHeight(transitionElementRef.current, 0) |
| 78 | + setContentMotion(contentRef.current, 0, CONTENT_ENTER_OFFSET, OPEN_TRANSITION) |
| 79 | + }, []) |
| 80 | + |
| 81 | + const handleEntering = React.useCallback(() => { |
| 82 | + if (!transitionElementRef.current) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + setElementHeight(transitionElementRef.current, wrapperRef.current?.clientHeight ?? 0) |
| 87 | + setContentMotion(contentRef.current, 1, 0) |
| 88 | + }, []) |
| 89 | + |
| 90 | + const handleEntered = React.useCallback(() => { |
| 91 | + if (!transitionElementRef.current) { |
| 92 | + return |
| 93 | + } |
| 94 | + setElementHeight(transitionElementRef.current, 'auto') |
| 95 | + onEntered?.() |
| 96 | + }, [onEntered]) |
| 97 | + |
| 98 | + const handleExit = React.useCallback(() => { |
| 99 | + if (!transitionElementRef.current) { |
| 100 | + return |
| 101 | + } |
| 102 | + setElementHeight(transitionElementRef.current, wrapperRef.current?.clientHeight ?? 0) |
| 103 | + setContentMotion(contentRef.current, 1, 0, CLOSE_TRANSITION) |
| 104 | + }, []) |
| 105 | + |
| 106 | + const handleExiting = React.useCallback(() => { |
| 107 | + if (!transitionElementRef.current) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + // Reading this property is important, even if we do not consume the value. |
| 112 | + // Without this, the expanded -> collapsed animation will not work. |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 114 | + wrapperRef.current?.clientHeight |
| 115 | + setElementHeight(transitionElementRef.current, 0) |
| 116 | + setContentMotion(contentRef.current, 0, 0) |
| 117 | + }, []) |
| 118 | + |
| 119 | + return ( |
| 120 | + <Transition |
| 121 | + nodeRef={transitionElementRef} |
| 122 | + onEntering={handleEntering} |
| 123 | + onEnter={handleEnter} |
| 124 | + onEntered={handleEntered} |
| 125 | + onExiting={handleExiting} |
| 126 | + onExit={handleExit} |
| 127 | + timeout={HEIGHT_TRANSITION_DURATION_MS} |
| 128 | + in={isExpanded} |
| 129 | + > |
| 130 | + {(state) => ( |
| 131 | + <Box |
| 132 | + ref={transitionElementRef} |
| 133 | + overflow={state === 'entered' ? 'visible' : 'hidden'} |
| 134 | + display={state === 'exited' ? 'none' : 'block'} |
| 135 | + className={[styles.container, state === 'entered' ? styles.entered : null]} |
| 136 | + > |
| 137 | + <Box display="flex" ref={wrapperRef}> |
| 138 | + <Box width="full" ref={contentRef}> |
| 139 | + {children} |
| 140 | + </Box> |
| 141 | + </Box> |
| 142 | + </Box> |
| 143 | + )} |
| 144 | + </Transition> |
| 145 | + ) |
| 146 | +} |
| 147 | + |
| 148 | +export { AnimatedExpansionPanelContent } |
0 commit comments