|
1 | | -import React, { useCallback, useEffect, useLayoutEffect, useRef } from 'react'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
2 | 2 | import styled from 'styled-components'; |
3 | | -import { timeoutEffect, timeoutEffects } from '../helpers/timeoutEffect'; |
| 3 | +import { timeoutEffect } from '../helpers/timeoutEffect'; |
4 | 4 | import { animationDuration } from '../styling'; |
5 | 5 |
|
6 | | -export interface CollapseProps { |
7 | | - open: boolean; |
8 | | - /** Animation speed in ms, Defaults to the animation duration defined in the theme */ |
9 | | - duration?: number; |
| 6 | +interface CollapseProps { |
| 7 | + open?: boolean; |
10 | 8 | className?: string; |
11 | 9 | } |
12 | 10 |
|
13 | | -const SPEED_MODIFIER = 1.5; |
| 11 | +// the styling file is not loaded at boot so we have to use a function here |
| 12 | +const ANIMATION_DURATION = () => animationDuration * 1.5; |
14 | 13 |
|
15 | | -/** |
16 | | - * Collapsible Component that only shows `children` when `open={true}`. Animates |
17 | | - * the transition for `{duration}`ms. Designed to work in the sidebar with |
18 | | - * potentially deeply nested, recurring Collapse units. |
19 | | - */ |
20 | 14 | export function Collapse({ |
21 | 15 | open, |
22 | | - children, |
23 | | - duration, |
24 | 16 | className, |
| 17 | + children, |
25 | 18 | }: React.PropsWithChildren<CollapseProps>): JSX.Element { |
26 | | - const node = useRef<HTMLDivElement>(null); |
27 | | - const [initialHeight, setInitialHeight] = React.useState(0); |
28 | | - |
29 | | - // We can't use a `useState` here. |
30 | | - // When `measureAndSet` is used as a callback for the MutationObserver, |
31 | | - // the scope will be outdated when the props update. |
32 | | - // To work around this we have to use a ref in order to reference the most up to date value. |
33 | | - const openRef = useRef(open); |
34 | | - |
35 | | - const measureAndSet = () => { |
36 | | - const div = node.current; |
37 | | - |
38 | | - if (!div) { |
39 | | - return; |
40 | | - } |
41 | | - |
42 | | - div.style.height = 'inital'; |
43 | | - const height = div.scrollHeight; |
44 | | - |
45 | | - setInitialHeight(height); |
46 | | - |
47 | | - div.style.position = 'static'; |
48 | | - |
49 | | - if (!openRef.current) { |
50 | | - div.style.height = '0px'; |
51 | | - div.style.visibility = 'hidden'; |
52 | | - } else { |
53 | | - div.style.height = `${height}px`; |
54 | | - div.style.visibility = 'visible'; |
55 | | - } |
56 | | - }; |
57 | | - |
58 | | - const mutationObserver = useRef(new MutationObserver(measureAndSet)); |
| 19 | + const [mountChildren, setMountChildren] = useState(open); |
59 | 20 |
|
60 | | - const speed = duration ?? animationDuration * SPEED_MODIFIER; |
61 | | - |
62 | | - // Measure the height of the element when it is added to the DOM. |
63 | | - const onRefConnect = useCallback((div: HTMLDivElement) => { |
64 | | - if (!div) return; |
65 | | - |
66 | | - // @ts-ignore this works and is mutable, ts doesn't like it |
67 | | - node.current = div; |
68 | | - measureAndSet(); |
69 | | - }, []); |
70 | | - |
71 | | - // Measure the height again when a child is added or removed. |
72 | 21 | useEffect(() => { |
73 | | - node.current && |
74 | | - mutationObserver.current.observe(node.current, { childList: true }); |
75 | | - |
76 | | - return () => mutationObserver.current.disconnect(); |
77 | | - }, []); |
78 | | - |
79 | | - // Perform the animation when the `open` prop changes. |
80 | | - useLayoutEffect(() => { |
81 | | - openRef.current = open; |
82 | | - if (!node.current) return; |
83 | | - |
84 | | - const wrapper = node.current; |
85 | | - |
86 | | - if (open) { |
87 | | - wrapper.style.visibility = 'visible'; |
88 | | - wrapper.style.height = `${initialHeight}px`; |
89 | | - |
90 | | - // after the animation has finished, remove the set height so the element can grow if it needs to. |
| 22 | + if (!open) { |
91 | 23 | return timeoutEffect(() => { |
92 | | - wrapper.style.overflowY = 'visible'; |
93 | | - wrapper.style.height = 'initial'; |
94 | | - }, speed); |
95 | | - } else { |
96 | | - // recalculate the height as it might have changed, then set it so it can be animated from. |
97 | | - const newHeight = wrapper.scrollHeight; |
98 | | - setInitialHeight(newHeight); |
99 | | - wrapper.style.height = `${newHeight}px`; |
100 | | - wrapper.style.overflowY = 'hidden'; |
101 | | - |
102 | | - return timeoutEffects( |
103 | | - [ |
104 | | - () => { |
105 | | - wrapper.style.height = '0px'; |
106 | | - }, |
107 | | - 0, |
108 | | - ], |
109 | | - [ |
110 | | - () => { |
111 | | - wrapper.style.visibility = 'hidden'; |
112 | | - }, |
113 | | - speed, |
114 | | - ], |
115 | | - ); |
| 24 | + setMountChildren(false); |
| 25 | + }, ANIMATION_DURATION()); |
116 | 26 | } |
| 27 | + |
| 28 | + setMountChildren(true); |
117 | 29 | }, [open]); |
118 | 30 |
|
119 | 31 | return ( |
120 | | - <Wrapper ref={onRefConnect} duration={speed} className={className}> |
121 | | - {children} |
122 | | - </Wrapper> |
| 32 | + <GridCollapser open={open} className={className}> |
| 33 | + <CollapseInner>{mountChildren && children}</CollapseInner> |
| 34 | + </GridCollapser> |
123 | 35 | ); |
124 | 36 | } |
125 | 37 |
|
126 | | -interface WrapperProps { |
127 | | - duration: number; |
| 38 | +interface GridCollapserProps { |
| 39 | + open?: boolean; |
128 | 40 | } |
129 | 41 |
|
130 | | -const Wrapper = styled.div<WrapperProps>` |
131 | | - overflow-y: hidden; |
132 | | - transition: height ${p => p.duration}ms ease-in-out; |
| 42 | +const GridCollapser = styled.div<GridCollapserProps>` |
| 43 | + display: grid; |
| 44 | + grid-template-rows: ${({ open }) => (open ? '1fr' : '0fr')}; |
| 45 | + transition: grid-template-rows ${() => ANIMATION_DURATION()}ms ease-in-out; |
133 | 46 |
|
134 | 47 | @media (prefers-reduced-motion) { |
135 | 48 | transition: unset; |
136 | 49 | } |
137 | 50 | `; |
| 51 | + |
| 52 | +const CollapseInner = styled.div` |
| 53 | + overflow: hidden; |
| 54 | +`; |
0 commit comments