Skip to content

Commit

Permalink
Merge pull request #454 from ably/web-3878-expander-resizing
Browse files Browse the repository at this point in the history
[WEB-3878] Handle window resizing in Expander
  • Loading branch information
jamiehenson authored Aug 21, 2024
2 parents 270e586 + 7d50a3a commit 64b3854
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ably/ui",
"version": "14.3.2",
"version": "14.3.3",
"description": "Home of the Ably design system library ([design.ably.com](https://design.ably.com)). It provides a showcase, development/test environment and a publishing pipeline for different distributables.",
"repository": {
"type": "git",
Expand Down
21 changes: 19 additions & 2 deletions src/core/Expander.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PropsWithChildren, useEffect, useRef, useState } from "react";
import throttle from "lodash.throttle";

type ExpanderProps = {
heightThreshold?: number;
Expand All @@ -20,11 +21,14 @@ const Expander = ({
}: PropsWithChildren<ExpanderProps>) => {
const innerRef = useRef<HTMLDivElement>(null);
const [showControls, setShowControls] = useState(false);
const [contentHeight, setContentHeight] = useState<number>(heightThreshold);
const [height, setHeight] = useState<number | "auto">(heightThreshold);
const [expanded, setExpanded] = useState(false);

useEffect(() => {
const contentHeight = innerRef.current?.clientHeight ?? heightThreshold;
if (innerRef.current) {
setContentHeight(innerRef.current.clientHeight);
}

if (contentHeight < heightThreshold) {
setHeight("auto");
Expand All @@ -35,7 +39,20 @@ const Expander = ({
}

setShowControls(contentHeight >= heightThreshold);
}, [heightThreshold, expanded]);
}, [contentHeight, heightThreshold, expanded]);

useEffect(() => {
const onResize = throttle(() => {
if (innerRef.current) {
setContentHeight(innerRef.current.clientHeight);
}
}, 250);

window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
};
}, []);

return (
<>
Expand Down

0 comments on commit 64b3854

Please sign in to comment.