Skip to content

Commit

Permalink
Dynamically calculate heights of panel option containers
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Nov 15, 2023
1 parent 92d66ea commit 1c26491
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/components/controls/panelSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export const PanelSection = ({ panel, title, tooltip, options=undefined }: Props
setOptionsAreVisible(panelIsVisible)
}, [panelIsVisible])

const [contentHeight, setContentHeight] = React.useState(1000);
const optionsContainer = React.useRef<HTMLDivElement>(null);

// FIXME: useLayoutEffect?
React.useEffect(() => {
if (optionsContainer.current) {
setContentHeight(optionsContainer.current.scrollHeight); // FIXME: offsetHeight?
}
}, [options]);

return (
<PanelSectionContainer>
<PanelHeader
Expand All @@ -42,7 +52,7 @@ export const PanelSection = ({ panel, title, tooltip, options=undefined }: Props
optionsAreVisible={optionsAreVisible}
setOptionsAreVisible={setOptionsAreVisible}
/>
<PanelOptionsContainer className={`${optionsAreVisible ? "open" : ""}`}>
<PanelOptionsContainer ref={optionsContainer} className={`${optionsAreVisible ? "open" : ""}`} contentHeight={contentHeight}>
{options}
</PanelOptionsContainer>
</PanelSectionContainer>
Expand Down
2 changes: 1 addition & 1 deletion src/components/controls/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const PanelOptionsContainer = styled.div`
&.open {
visibility: visible;
max-height: 100vh;
max-height: ${({ contentHeight }) => `${contentHeight}px`};
}
`

Expand Down

0 comments on commit 1c26491

Please sign in to comment.