From aace2580da9811a9f4e459a67be43b80f59c5ae6 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 1 Mar 2023 10:19:21 -0800 Subject: [PATCH] Tweaks --- src-web/components/Dropdown.tsx | 42 +++++++++++++++-------------- src-web/components/ResponsePane.tsx | 2 +- src-web/components/Sidebar.tsx | 15 ++++++----- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src-web/components/Dropdown.tsx b/src-web/components/Dropdown.tsx index e07f375c..fdd654dd 100644 --- a/src-web/components/Dropdown.tsx +++ b/src-web/components/Dropdown.tsx @@ -1,18 +1,10 @@ import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; import { DropdownMenuRadioGroup } from '@radix-ui/react-dropdown-menu'; -import { motion } from 'framer-motion'; import { CheckIcon } from '@radix-ui/react-icons'; -import { - ForwardedRef, - forwardRef, - HTMLAttributes, - ReactNode, - useEffect, - useImperativeHandle, - useMemo, - useRef, -} from 'react'; import classnames from 'classnames'; +import { motion } from 'framer-motion'; +import type { ForwardedRef, HTMLAttributes, ReactNode } from 'react'; +import { forwardRef, useImperativeHandle, useLayoutEffect, useState } from 'react'; interface DropdownMenuRadioProps { children: ReactNode; @@ -120,20 +112,30 @@ const DropdownMenuContent = forwardRef, ) { - const divRef = useRef(null); - useImperativeHandle(ref, () => divRef.current); + const [styles, setStyles] = useState<{ maxHeight: number }>(); + const [divRef, setDivRef] = useState(null); + useImperativeHandle(ref, () => divRef); + + const initDivRef = (ref: HTMLDivElement | null) => { + setDivRef(ref); + }; // Calculate the max height so we can scroll - const styles = useMemo(() => { - if (divRef.current === null) return; - const windowBox = document.documentElement.getBoundingClientRect(); - const menuBox = divRef.current.getBoundingClientRect(); - return { maxHeight: windowBox.height - menuBox.top - 5 }; - }, [divRef.current]); + useLayoutEffect(() => { + if (divRef === null) return; + // Needs to be in a setTimeout because the ref is not positioned yet + // TODO: Make this better? + setTimeout(() => { + const windowBox = document.documentElement.getBoundingClientRect(); + const menuBox = divRef.getBoundingClientRect(); + const styles = { maxHeight: windowBox.height - menuBox.top - 5 - 45 }; + setStyles(styles); + }); + }, [divRef]); return ( ({ + ...responses.data.slice(0, 10).map((r) => ({ label: r.status + ' - ' + r.elapsed, leftSlot: response?.id === r.id ? : <>, onSelect: () => setActiveResponseId(r.id), diff --git a/src-web/components/Sidebar.tsx b/src-web/components/Sidebar.tsx index c41abea0..dfc5036d 100644 --- a/src-web/components/Sidebar.tsx +++ b/src-web/components/Sidebar.tsx @@ -1,13 +1,14 @@ -import React, { HTMLAttributes } from 'react'; import classnames from 'classnames'; -import { IconButton } from './IconButton'; -import { Button } from './Button'; -import useTheme from '../hooks/useTheme'; -import { HStack, VStack } from './Stacks'; -import { WindowDragRegion } from './WindowDragRegion'; -import { HttpRequest } from '../lib/models'; +import type { HTMLAttributes } from 'react'; +import React from 'react'; import { Link } from 'react-router-dom'; import { useRequestCreate } from '../hooks/useRequest'; +import useTheme from '../hooks/useTheme'; +import type { HttpRequest } from '../lib/models'; +import { Button } from './Button'; +import { IconButton } from './IconButton'; +import { HStack, VStack } from './Stacks'; +import { WindowDragRegion } from './WindowDragRegion'; interface Props extends Omit, 'children'> { workspaceId: string;