Skip to content

Instantly share code, notes, and snippets.

@nireno
Last active December 13, 2022 20:16
Show Gist options
  • Save nireno/e172bba838169028d5f3eed1aa65bc6d to your computer and use it in GitHub Desktop.
Save nireno/e172bba838169028d5f3eed1aa65bc6d to your computer and use it in GitHub Desktop.
Rescript Animated Accordion Item
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