From 96e5f38644ea06e2d796870446b348f16b45e66c Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Mon, 31 Jul 2023 18:55:18 +0100 Subject: [PATCH] feat: allow tabs to wrap and apply bg color --- libs/ui-toolkit/src/components/tabs/tabs.tsx | 44 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/libs/ui-toolkit/src/components/tabs/tabs.tsx b/libs/ui-toolkit/src/components/tabs/tabs.tsx index 11827b703..d20720556 100644 --- a/libs/ui-toolkit/src/components/tabs/tabs.tsx +++ b/libs/ui-toolkit/src/components/tabs/tabs.tsx @@ -2,10 +2,11 @@ import * as TabsPrimitive from '@radix-ui/react-tabs'; import { useLocalStorageSnapshot, getValidItem, + useResizeObserver, } from '@vegaprotocol/react-helpers'; import classNames from 'classnames'; import type { ReactElement, ReactNode } from 'react'; -import { Children, isValidElement, useState } from 'react'; +import { Children, isValidElement, useRef, useState } from 'react'; export interface TabsProps extends TabsPrimitive.TabsProps { children: (ReactElement | null)[]; } @@ -24,6 +25,19 @@ export const Tabs = ({ return children.find((v) => v)?.props.id; }); + // Bunch of refs in order to detect wrapping in side the tabs so that we + // can apply a bg color + const wrapperRef = useRef(null); + const tabsRef = useRef(null); + const menuRef = useRef(null); + const [wrapped, setWrapped] = useState(() => + isWrapped(tabsRef.current, menuRef.current) + ); + + useResizeObserver(wrapperRef.current, () => { + setWrapped(isWrapped(tabsRef.current, menuRef.current)); + }); + return ( -
+
{Children.map(children, (child) => { if (!isValidElement(child) || child.props.hidden) return null; @@ -67,13 +85,21 @@ export const Tabs = ({ ); })} -
+
{Children.map(children, (child) => { if (!isValidElement(child) || child.props.hidden) return null; return ( {child.props.menu} @@ -136,3 +162,13 @@ export const LocalStoragePersistTabs = ({ /> ); }; + +const isWrapped = ( + tabs: HTMLDivElement | null, + menu: HTMLDivElement | null +) => { + if (!tabs || !menu) return; + const listRect = tabs.getBoundingClientRect(); + const menuRect = menu.getBoundingClientRect(); + return menuRect.y > listRect.y; +};