diff --git a/frontend/web/components/navigation/TabMenu/Tabs/Tabs.scss b/frontend/web/components/navigation/TabMenu/Tabs/Tabs.scss new file mode 100644 index 000000000000..8780494aec9e --- /dev/null +++ b/frontend/web/components/navigation/TabMenu/Tabs/Tabs.scss @@ -0,0 +1,42 @@ +// Phones: tabs scroll sideways, and the edge with more tabs behind it fades +.tabs-nav__scroller { + --tabs-fade: 40px; + + position: relative; + overflow-x: auto; + scrollbar-width: none; + + &::-webkit-scrollbar { + display: none; + } + + > * { + flex-shrink: 0; + } + + &--fade-end { + mask-image: linear-gradient( + to right, + black calc(100% - var(--tabs-fade)), + transparent + ); + } + + &--fade-start { + mask-image: linear-gradient( + to left, + black calc(100% - var(--tabs-fade)), + transparent + ); + } + + &--fade-start.tabs-nav__scroller--fade-end { + mask-image: linear-gradient( + to right, + transparent, + black var(--tabs-fade), + black calc(100% - var(--tabs-fade)), + transparent + ); + } +} diff --git a/frontend/web/components/navigation/TabMenu/Tabs.tsx b/frontend/web/components/navigation/TabMenu/Tabs/Tabs.tsx similarity index 79% rename from frontend/web/components/navigation/TabMenu/Tabs.tsx rename to frontend/web/components/navigation/TabMenu/Tabs/Tabs.tsx index d3f509829ade..d0a3e2b56978 100644 --- a/frontend/web/components/navigation/TabMenu/Tabs.tsx +++ b/frontend/web/components/navigation/TabMenu/Tabs/Tabs.tsx @@ -10,15 +10,18 @@ import React, { import ModalHR from 'components/modals/ModalHR' import Utils from 'common/utils/utils' import { useHistory } from 'react-router-dom' -import TabButton from './TabButton' -import { TabItemProps } from './TabItem' -import TabLabel from './TabLabel' +import TabButton from 'components/navigation/TabMenu/TabButton' +import { TabItemProps } from 'components/navigation/TabMenu/TabItem' +import TabLabel from 'components/navigation/TabMenu/TabLabel' import classNames from 'classnames' import DropdownMenu, { DropdownMenuItem } from 'components/base/DropdownMenu' import IconButton from 'components/base/IconButton' import Icon from 'components/icons/Icon' import { useOverflowVisibleCount } from 'common/hooks/useOverflowVisibleCount' +import { useBreakpoint } from 'common/hooks/useBreakpoint' +import { useTabScroller } from 'components/navigation/TabMenu/useTabScroller' +import './Tabs.scss' // Room for the "More tabs" button, with some spare const OVERFLOW_BUTTON_WIDTH = 48 @@ -80,6 +83,7 @@ const Tabs: React.FC = ({ const outerContainerRef = useRef(null) const itemsContainerRef = useRef(null) + const isPhone = useBreakpoint('isPhone') const { isMeasuring, visibleCount } = useOverflowVisibleCount({ extraWidth: OVERFLOW_BUTTON_WIDTH, @@ -89,16 +93,31 @@ const Tabs: React.FC = ({ outerContainerRef, }) + // Phones scroll the tabs sideways instead of hiding some behind a menu + const scrollTabs = isPhone && !disableOverflow + const { canScrollEnd, canScrollStart } = useTabScroller( + itemsContainerRef, + scrollTabs, + value, + tabChildren.length, + ) + const visible = useMemo( // Disable overflow for pill tabs - () => (disableOverflow ? tabChildren : tabChildren.slice(0, visibleCount)), - [tabChildren, visibleCount, disableOverflow], + () => + disableOverflow || scrollTabs + ? tabChildren + : tabChildren.slice(0, visibleCount), + [tabChildren, visibleCount, disableOverflow, scrollTabs], ) const overflow = useMemo( - () => (disableOverflow ? [] : tabChildren.slice(visibleCount)), - [tabChildren, visibleCount, disableOverflow], + () => + disableOverflow || scrollTabs ? [] : tabChildren.slice(visibleCount), + [tabChildren, visibleCount, disableOverflow, scrollTabs], ) - const canGrow = !isMeasuring && visibleCount === tabChildren.length + const measuring = isMeasuring && !scrollTabs + const canGrow = + !scrollTabs && !isMeasuring && visibleCount === tabChildren.length const hasOverflow = overflow.length > 0 && !isMeasuring const tabLabelOf = (child: ReactElement) => String(child.props.tabLabelString || child.props.tabLabel) @@ -136,11 +155,14 @@ const Tabs: React.FC = ({
- {(isMeasuring ? tabChildren : visible).map((child, i) => { - const isSelected = !isMeasuring && value === i + {(measuring ? tabChildren : visible).map((child, i) => { + const isSelected = !measuring && value === i return ( , + enabled: boolean, + activeIndex: number, + tabCount: number, +): ScrollEdges => { + const [edges, setEdges] = useState({ + canScrollEnd: false, + canScrollStart: false, + }) + + useLayoutEffect(() => { + const el = ref.current + if (!enabled || !el) return + const update = () => + setEdges({ + canScrollEnd: el.scrollLeft + el.clientWidth < el.scrollWidth - 1, + canScrollStart: el.scrollLeft > 1, + }) + update() + el.addEventListener('scroll', update, { passive: true }) + const observer = + typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(update) + // The tabs too: a tab that grows, such as with the unsaved dot, moves the + // edges without resizing the row + const watched = [el, ...Array.from(el.children)] + watched.forEach((node) => observer?.observe(node)) + return () => { + el.removeEventListener('scroll', update) + observer?.disconnect() + } + }, [enabled, ref, tabCount]) + + useEffect(() => { + const el = ref.current + const tab = el?.children.item(activeIndex) + if (!enabled || !el || !(tab instanceof HTMLElement)) return + const reduceMotion = window.matchMedia?.( + '(prefers-reduced-motion: reduce)', + ).matches + // Next frame, so the new tab's content has rendered and can't cut it short + const frame = requestAnimationFrame(() => + el.scrollTo({ + behavior: reduceMotion ? 'auto' : 'smooth', + left: tab.offsetLeft - (el.clientWidth - tab.offsetWidth) / 2, + }), + ) + return () => cancelAnimationFrame(frame) + }, [enabled, activeIndex, ref]) + + return edges +}