|
| 1 | +'use client'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { |
| 4 | + useEnhancedEffect, |
| 5 | + useTransitionStatus, |
| 6 | +} from '@base-ui-components/react/utils'; |
| 7 | +import classes from './animation.module.css'; |
| 8 | + |
| 9 | +import { useAnimationsFinished } from '../../../../../../packages/react/src/utils/useAnimationsFinished'; |
| 10 | +import { useEventCallback } from '../../../../../../packages/react/src/utils/useEventCallback'; |
| 11 | +import { useForkRef } from '../../../../../../packages/react/src/utils/useForkRef'; |
| 12 | +import { warn } from '../../../../../../packages/react/src/utils/warn'; |
| 13 | + |
| 14 | +type AnimationType = 'css-transition' | 'css-animation' | 'none' | null; |
| 15 | + |
| 16 | +function Collapsible(props: { |
| 17 | + defaultOpen?: boolean; |
| 18 | + keepMounted?: boolean; |
| 19 | + id?: string; |
| 20 | +}) { |
| 21 | + const { keepMounted = true, defaultOpen = false, id } = props; |
| 22 | + |
| 23 | + const [open, setOpen] = React.useState(defaultOpen); |
| 24 | + |
| 25 | + const { mounted, setMounted } = useTransitionStatus(open); |
| 26 | + |
| 27 | + // keyframe animations doesn't need data-starting/ending-style |
| 28 | + |
| 29 | + const [height, setHeight] = React.useState<number | undefined>(undefined); |
| 30 | + |
| 31 | + // const shouldCancelInitialOpenTransitionRef = React.useRef(open); |
| 32 | + |
| 33 | + const isHidden = React.useMemo(() => { |
| 34 | + if (keepMounted) { |
| 35 | + return !open; |
| 36 | + } |
| 37 | + |
| 38 | + return !open && !mounted; |
| 39 | + }, [keepMounted, open, mounted]); |
| 40 | + |
| 41 | + const animationTypeRef = React.useRef<AnimationType>(null); |
| 42 | + const panelRef: React.RefObject<HTMLElement | null> = React.useRef(null); |
| 43 | + |
| 44 | + const handlePanelRef = useEventCallback((element: HTMLElement) => { |
| 45 | + if (!element) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + element.style.animationDuration = '0s'; |
| 50 | + |
| 51 | + // if (height === undefined) { |
| 52 | + // // set` display: block !important` here to force layout |
| 53 | + // element.style.setProperty('display', 'block', 'important'); |
| 54 | + // // measure the height |
| 55 | + // setHeight(element.scrollHeight); |
| 56 | + // element.style.removeProperty('display'); |
| 57 | + |
| 58 | + // if (shouldCancelInitialOpenTransitionRef.current) { |
| 59 | + // element.style.animationDuration = '0s'; |
| 60 | + // } |
| 61 | + // } |
| 62 | + |
| 63 | + // requestAnimationFrame(() => { |
| 64 | + // shouldCancelInitialOpenTransitionRef.current = false; |
| 65 | + // }); |
| 66 | + }); |
| 67 | + |
| 68 | + const mergedRef = useForkRef(panelRef, handlePanelRef); |
| 69 | + |
| 70 | + // const runOnceAnimationsFinish = useAnimationsFinished(panelRef, false); |
| 71 | + |
| 72 | + const handleTrigger = useEventCallback(() => { |
| 73 | + const nextOpen = !open; |
| 74 | + |
| 75 | + if (!keepMounted) { |
| 76 | + if (!mounted && nextOpen) { |
| 77 | + setMounted(true); |
| 78 | + } |
| 79 | + } |
| 80 | + setOpen(nextOpen); |
| 81 | + |
| 82 | + const panel = panelRef.current; |
| 83 | + if (!panel) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (nextOpen) { |
| 88 | + /* opening */ |
| 89 | + } else { |
| 90 | + /* closing */ |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + return ( |
| 95 | + <div |
| 96 | + className={classes.Root} |
| 97 | + style={{ |
| 98 | + // animationDuration: '0s', |
| 99 | + // @ts-ignore |
| 100 | + '--collapsible-panel-height': height !== undefined ? `${height}px` : 'auto', |
| 101 | + }} |
| 102 | + > |
| 103 | + <button |
| 104 | + type="button" |
| 105 | + className={classes.Trigger} |
| 106 | + data-panel-open={open || undefined} |
| 107 | + onClick={handleTrigger} |
| 108 | + > |
| 109 | + <ExpandMoreIcon className={classes.Icon} /> |
| 110 | + Trigger {id} |
| 111 | + </button> |
| 112 | + |
| 113 | + {(keepMounted || (!keepMounted && mounted)) && ( |
| 114 | + <div |
| 115 | + // @ts-ignore |
| 116 | + ref={mergedRef} |
| 117 | + className={classes.Panel} |
| 118 | + // {...{ [open ? 'data-open' : 'data-closed']: '' }} |
| 119 | + data-open="" |
| 120 | + // {...styleHooks} |
| 121 | + hidden={isHidden} |
| 122 | + id={id} |
| 123 | + > |
| 124 | + <div className={classes.Content}> |
| 125 | + <p> |
| 126 | + He rubbed his eyes, and came close to the picture, and examined it |
| 127 | + again. There were no signs of any change when he looked into the actual |
| 128 | + painting, and yet there was no doubt that the whole expression had |
| 129 | + altered. It was not a mere fancy of his own. The thing was horribly |
| 130 | + apparent. |
| 131 | + </p> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +export default function App() { |
| 140 | + return ( |
| 141 | + <div className={classes.grid}> |
| 142 | + <div className={classes.wrapper}> |
| 143 | + <Collapsible keepMounted defaultOpen id="1" /> |
| 144 | + |
| 145 | + {/*<Collapsible keepMounted defaultOpen={false} id="2" />*/} |
| 146 | + |
| 147 | + <small>———</small> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +function ExpandMoreIcon(props: React.SVGProps<SVGSVGElement>) { |
| 154 | + return ( |
| 155 | + <svg |
| 156 | + xmlns="http://www.w3.org/2000/svg" |
| 157 | + {...props} |
| 158 | + width="24" |
| 159 | + height="24" |
| 160 | + viewBox="0 0 24 24" |
| 161 | + fill="none" |
| 162 | + > |
| 163 | + <path d="M16.59 8.59 12 13.17 7.41 8.59 6 10l6 6 6-6z" fill="currentColor" /> |
| 164 | + </svg> |
| 165 | + ); |
| 166 | +} |
0 commit comments