Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions frontend/web/components/navigation/TabMenu/Tabs/Tabs.scss
Original file line number Diff line number Diff line change
@@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +83,7 @@ const Tabs: React.FC<TabsProps> = ({

const outerContainerRef = useRef<HTMLDivElement>(null)
const itemsContainerRef = useRef<HTMLDivElement>(null)
const isPhone = useBreakpoint('isPhone')

const { isMeasuring, visibleCount } = useOverflowVisibleCount({
extraWidth: OVERFLOW_BUTTON_WIDTH,
Expand All @@ -89,16 +93,31 @@ const Tabs: React.FC<TabsProps> = ({
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<TabItemProps>) =>
String(child.props.tabLabelString || child.props.tabLabel)
Expand Down Expand Up @@ -136,11 +155,14 @@ const Tabs: React.FC<TabsProps> = ({
<div
ref={itemsContainerRef}
className={classNames('d-flex align-items-center flex-1 gap-2', {
'opacity-0': isMeasuring,
'opacity-0': measuring,
'tabs-nav__scroller': scrollTabs,
'tabs-nav__scroller--fade-end': scrollTabs && canScrollEnd,
'tabs-nav__scroller--fade-start': scrollTabs && canScrollStart,
})}
>
{(isMeasuring ? tabChildren : visible).map((child, i) => {
const isSelected = !isMeasuring && value === i
{(measuring ? tabChildren : visible).map((child, i) => {
const isSelected = !measuring && value === i
return (
<TabButton
key={`button-${i}`}
Expand Down
1 change: 1 addition & 0 deletions frontend/web/components/navigation/TabMenu/Tabs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Tabs'
61 changes: 61 additions & 0 deletions frontend/web/components/navigation/TabMenu/useTabScroller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { RefObject, useEffect, useLayoutEffect, useState } from 'react'

type ScrollEdges = {
canScrollStart: boolean
canScrollEnd: boolean
}

// Horizontally scrolling tabs: reports which edges have more tabs behind them
// and keeps the active tab in view.
export const useTabScroller = (
ref: RefObject<HTMLDivElement | null>,
enabled: boolean,
activeIndex: number,
tabCount: number,
): ScrollEdges => {
const [edges, setEdges] = useState<ScrollEdges>({
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
}
Loading