Skip to content

The open alternative to Salesforce, designed for AI.

Scroll the tab row on mobile instead of hiding tabs behind a dropdown (#24184)

Félix Malfait0f96703c5c61
diff --git a/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabList.tsx b/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabList.tsx
index b8420b6e554..b89e47ef481 100644
--- a/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabList.tsx
+++ b/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabList.tsx
@@ -17,6 +17,7 @@ import { activeTabIdComponentState } from '@/ui/layout/tab-list/states/activeTab
import { TabListComponentInstanceContext } from '@/ui/layout/tab-list/states/contexts/TabListComponentInstanceContext';
import { type TabListProps } from '@/ui/layout/tab-list/types/TabListProps';
import { NodeDimension } from '@/ui/utilities/dimensions/components/NodeDimension';
+import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { useClickOutsideListener } from '@/ui/utilities/pointer-event/hooks/useClickOutsideListener';
import { useAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentState';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
@@ -110,6 +111,7 @@ export const PageLayoutTabList = ({
}));
const navigate = useNavigate();
+ const isMobile = useIsMobile();
const [activeTabId, setActiveTabId] = useAtomComponentState(
activeTabIdComponentState,
@@ -335,9 +337,12 @@ export const PageLayoutTabList = ({
const canReorderTabs = isReorderEnabled;
+ const shouldScrollTabs = isMobile && !canReorderTabs;
+
const shouldRenderReorderableDropdown = hasHiddenTabs && canReorderTabs;
- const shouldRenderStaticDropdown = hasHiddenTabs && !canReorderTabs;
+ const shouldRenderStaticDropdown =
+ hasHiddenTabs && !canReorderTabs && !shouldScrollTabs;
// Record pages accept widget drops on vertical-list tabs (dnd-kit drags);
// dashboards accept them on grid tabs (react-grid-layout drags bridged by
@@ -367,7 +372,7 @@ export const PageLayoutTabList = ({
tabListIds={tabsWithIcons.map((tab) => tab.id)}
/>
- {tabsWithIcons.length > 1 && (
+ {tabsWithIcons.length > 1 && !shouldScrollTabs && (
<TabListHiddenMeasurements
visibleTabs={tabsWithIcons}
activeTabId={activeTabId}
@@ -396,7 +401,10 @@ export const PageLayoutTabList = ({
<StyledContainer className={className}>
<PageLayoutTabListVisibleTabs
visibleTabs={tabsWithIcons}
- visibleTabCount={visibleTabCount}
+ visibleTabCount={
+ shouldScrollTabs ? tabsWithIcons.length : visibleTabCount
+ }
+ isScrollable={shouldScrollTabs}
activeTabId={activeTabId}
behaveAsLinks={behaveAsLinks}
loading={loading}
diff --git a/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabListVisibleTabs.tsx b/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabListVisibleTabs.tsx
index dec933d8df8..02dc8725928 100644
--- a/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabListVisibleTabs.tsx
+++ b/packages/twenty-front/src/modules/page-layout/components/PageLayoutTabListVisibleTabs.tsx
@@ -2,6 +2,8 @@ import { styled } from '@linaria/react';
import { TabButton } from 'twenty-ui/input';
import { TAB_LIST_GAP } from '@/ui/layout/tab-list/constants/TabListGap';
+import { useScrollActiveTabIntoView } from '@/ui/layout/tab-list/hooks/useScrollActiveTabIntoView';
+import { SCROLLABLE_TAB_ROW_CSS } from '@/ui/layout/tab-list/styles/ScrollableTabRowCSS';
import { type SingleTabProps } from '@/ui/layout/tab-list/types/SingleTabProps';
import { PAGE_LAYOUT_TAB_LIST_DROPPABLE_IDS } from '@/page-layout/components/PageLayoutTabListDroppableIds';
@@ -19,13 +21,15 @@ type PageLayoutTabListVisibleTabsProps = {
canReorder: boolean;
widgetDropTargetTabIds: Set<string>;
firstHiddenTabId: string | null;
+ isScrollable: boolean;
};
-const StyledTabContainer = styled.div`
+const StyledTabContainer = styled.div<{ isScrollable: boolean }>`
display: flex;
max-width: 100%;
- overflow: hidden;
+ overflow-x: ${({ isScrollable }) => (isScrollable ? 'auto' : 'hidden')};
position: relative;
+ ${SCROLLABLE_TAB_ROW_CSS}
> *:not(:last-child) {
margin-right: ${TAB_LIST_GAP}px;
@@ -53,12 +57,18 @@ export const PageLayoutTabListVisibleTabs = ({
canReorder,
widgetDropTargetTabIds,
firstHiddenTabId,
+ isScrollable,
}: PageLayoutTabListVisibleTabsProps) => {
+ const { tabRowRef } = useScrollActiveTabIntoView({
+ activeTabId,
+ isScrollable,
+ });
+
if (canReorder) {
const shownTabs = visibleTabs.slice(0, visibleTabCount);
return (
- <StyledTabContainer>
+ <StyledTabContainer ref={tabRowRef} isScrollable={isScrollable}>
{shownTabs.map((tab, index) => (
<StyledTabSlot key={tab.id}>
<StyledLeadingDropTarget>
@@ -94,7 +104,7 @@ export const PageLayoutTabListVisibleTabs = ({
}
return (
- <StyledTabContainer>
+ <StyledTabContainer ref={tabRowRef} isScrollable={isScrollable}>
{visibleTabs.slice(0, visibleTabCount).map((tab) => (
<TabButton
key={tab.id}
diff --git a/packages/twenty-front/src/modules/ui/layout/tab-list/components/TabList.tsx b/packages/twenty-front/src/modules/ui/layout/tab-list/components/TabList.tsx
index ebac87c2e11..4ce90115be4 100644
--- a/packages/twenty-front/src/modules/ui/layout/tab-list/components/TabList.tsx
+++ b/packages/twenty-front/src/modules/ui/layout/tab-list/components/TabList.tsx
@@ -2,11 +2,14 @@ import { useCloseDropdown } from '@/ui/layout/dropdown/hooks/useCloseDropdown';
import { TabListHiddenMeasurements } from '@/ui/layout/tab-list/components/TabListHiddenMeasurements';
import { TAB_LIST_GAP } from '@/ui/layout/tab-list/constants/TabListGap';
import { TAB_LIST_HEIGHT } from '@/ui/layout/tab-list/constants/TabListHeight';
+import { useScrollActiveTabIntoView } from '@/ui/layout/tab-list/hooks/useScrollActiveTabIntoView';
import { useTabListMeasurements } from '@/ui/layout/tab-list/hooks/useTabListMeasurements';
+import { SCROLLABLE_TAB_ROW_CSS } from '@/ui/layout/tab-list/styles/ScrollableTabRowCSS';
import { activeTabIdComponentState } from '@/ui/layout/tab-list/states/activeTabIdComponentState';
import { TabListComponentInstanceContext } from '@/ui/layout/tab-list/states/contexts/TabListComponentInstanceContext';
import { type TabListProps } from '@/ui/layout/tab-list/types/TabListProps';
import { NodeDimension } from '@/ui/utilities/dimensions/components/NodeDimension';
+import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { useAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentState';
import { styled } from '@linaria/react';
import { useCallback, useEffect, useMemo } from 'react';
@@ -49,12 +52,13 @@ const StyledDropdownContainer = styled.div`
display: flex;
`;
-const StyledTabContainer = styled.div`
+const StyledTabContainer = styled.div<{ isScrollable: boolean }>`
display: flex;
gap: ${TAB_LIST_GAP}px;
max-width: 100%;
- overflow: hidden;
+ overflow-x: ${({ isScrollable }) => (isScrollable ? 'auto' : 'hidden')};
position: relative;
+ ${SCROLLABLE_TAB_ROW_CSS}
`;
const StyledNodeDimension = styled(NodeDimension)`
@@ -82,6 +86,7 @@ export const TabList = ({
}: TabListProps) => {
const visibleTabs = tabs.filter((tab) => !tab.hide);
const navigate = useNavigate();
+ const isMobile = useIsMobile();
const [activeTabId, setActiveTabId] = useAtomComponentState(
activeTabIdComponentState,
@@ -104,6 +109,17 @@ export const TabList = ({
hasAddButton: false,
});
+ const shouldScrollTabs = isMobile;
+ const renderedTabs = shouldScrollTabs
+ ? visibleTabs
+ : visibleTabs.slice(0, visibleTabCount);
+ const shouldShowOverflowDropdown = hasHiddenTabs && !shouldScrollTabs;
+
+ const { tabRowRef } = useScrollActiveTabIntoView({
+ activeTabId,
+ isScrollable: shouldScrollTabs,
+ });
+
const dropdownId = `tab-overflow-${componentInstanceId}`;
const { closeDropdown } = useCloseDropdown();
@@ -151,7 +167,7 @@ export const TabList = ({
tabListIds={tabs.map((tab) => tab.id)}
/>
- {visibleTabs.length > 1 && (
+ {visibleTabs.length > 1 && !shouldScrollTabs && (
<TabListHiddenMeasurements
visibleTabs={visibleTabs}
activeTabId={activeTabId}
@@ -163,9 +179,12 @@ export const TabList = ({
<StyledContainer className={className}>
<StyledNodeDimension onDimensionChange={onContainerWidthChange}>
- <StyledInnerContainer $centerTabs={centerTabs}>
- <StyledTabContainer>
- {visibleTabs.slice(0, visibleTabCount).map((tab) => (
+ <StyledInnerContainer $centerTabs={centerTabs && !shouldScrollTabs}>
+ <StyledTabContainer
+ ref={tabRowRef}
+ isScrollable={shouldScrollTabs}
+ >
+ {renderedTabs.map((tab) => (
<TabButton
key={tab.id}
id={tab.id}
@@ -186,7 +205,7 @@ export const TabList = ({
))}
</StyledTabContainer>
- {hasHiddenTabs && (
+ {shouldShowOverflowDropdown && (
<StyledDropdownContainer>
<TabListDropdown
dropdownId={dropdownId}
diff --git a/packages/twenty-front/src/modules/ui/layout/tab-list/hooks/useScrollActiveTabIntoView.ts b/packages/twenty-front/src/modules/ui/layout/tab-list/hooks/useScrollActiveTabIntoView.ts
new file mode 100644
index 00000000000..1daca934a23
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/layout/tab-list/hooks/useScrollActiveTabIntoView.ts
@@ -0,0 +1,55 @@
+import { useEffect, useRef } from 'react';
+
+type UseScrollActiveTabIntoViewParams = {
+ activeTabId: string | null;
+ isScrollable: boolean;
+};
+
+// A tab selected from the URL or from initial state can sit outside the
+// scrolled region, and a rotation can push it back out, which would leave the
+// row looking like nothing is selected.
+export const useScrollActiveTabIntoView = ({
+ activeTabId,
+ isScrollable,
+}: UseScrollActiveTabIntoViewParams) => {
+ const tabRowRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ const tabRow = tabRowRef.current;
+
+ if (!isScrollable || !tabRow) {
+ return;
+ }
+
+ const scrollActiveTabIntoView = () => {
+ const activeTab = tabRow.querySelector('[data-active]');
+
+ if (!(activeTab instanceof HTMLElement)) {
+ return;
+ }
+
+ const activeTabLeft = activeTab.offsetLeft;
+ const activeTabRight = activeTabLeft + activeTab.offsetWidth;
+
+ if (activeTabLeft < tabRow.scrollLeft) {
+ tabRow.scrollLeft = activeTabLeft;
+ return;
+ }
+
+ if (activeTabRight > tabRow.scrollLeft + tabRow.clientWidth) {
+ tabRow.scrollLeft = activeTabRight - tabRow.clientWidth;
+ }
+ };
+
+ scrollActiveTabIntoView();
+
+ const tabRowResizeObserver = new ResizeObserver(scrollActiveTabIntoView);
+ tabRowResizeObserver.observe(tabRow);
+
+ return () => {
+ tabRowResizeObserver.disconnect();
+ };
+ }, [activeTabId, isScrollable]);
+
+ return { tabRowRef };
+};
diff --git a/packages/twenty-front/src/modules/ui/layout/tab-list/styles/ScrollableTabRowCSS.ts b/packages/twenty-front/src/modules/ui/layout/tab-list/styles/ScrollableTabRowCSS.ts
new file mode 100644
index 00000000000..3dc0a424255
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/layout/tab-list/styles/ScrollableTabRowCSS.ts
@@ -0,0 +1,14 @@
+// The breakpoint is width-based, so a narrow desktop window matches it too. A
+// mouse there has no swipe to fall back on, so only coarse pointers lose the
+// scrollbar.
+export const SCROLLABLE_TAB_ROW_CSS = `
+ overflow-y: hidden;
+
+ @media (pointer: coarse) {
+ scrollbar-width: none;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+ }
+`;