Last active
December 13, 2022 20:16
-
-
Save nireno/e172bba838169028d5f3eed1aa65bc6d to your computer and use it in GitHub Desktop.
Rescript Animated Accordion Item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Belt | |
@get | |
external getClientHeight: Dom.element => int = "clientHeight" | |
let useClientHeight = () => { | |
let ref: React.ref<Js.Nullable.t<Dom.element>> = React.useRef(Js.Nullable.null) | |
(ref, ref.current->Js.Nullable.toOption->Option.map(el => el->getClientHeight)) | |
} | |
@react.component | |
let make = (~children, ~isOpen) => { | |
let (ref, clientHeight) = useClientHeight() | |
<div | |
style={ReactDOM.Style.make( | |
~overflowY="hidden", | |
~maxHeight=isOpen ? `${clientHeight->Option.getWithDefault(0)->Int.toString}px` : "0", | |
~transition="max-height 225ms ease-in-out", | |
(), | |
)}> | |
{React.cloneElement(children, {"ref": ref})} | |
</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment