Skip to content

The open alternative to Salesforce, designed for AI.

Float the mobile bottom bar over content with a glass style (#24229)

Félix Malfaitca5302441e65
diff --git a/packages/twenty-front/src/modules/navigation/components/MobileNavigationBar.tsx b/packages/twenty-front/src/modules/navigation/components/MobileNavigationBar.tsx
index 86b30c33de6..8a67a141e05 100644
--- a/packages/twenty-front/src/modules/navigation/components/MobileNavigationBar.tsx
+++ b/packages/twenty-front/src/modules/navigation/components/MobileNavigationBar.tsx
@@ -3,18 +3,22 @@ import { useLingui } from '@lingui/react/macro';
import { useSwitchToNewAiChat } from '@/ai/hooks/useSwitchToNewAiChat';
import { MAIN_CONTEXT_STORE_INSTANCE_ID } from '@/context-store/constants/MainContextStoreInstanceId';
import { contextStoreCurrentObjectMetadataItemIdComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataItemIdComponentState';
+import { MobileNavigationBarScrollEffect } from '@/navigation/components/MobileNavigationBarScrollEffect';
import { useIsSettingsDrawer } from '@/navigation/hooks/useIsSettingsDrawer';
import { useIsSettingsPage } from '@/navigation/hooks/useIsSettingsPage';
+import { isMobileNavigationBarVisibleState } from '@/navigation/states/isMobileNavigationBarVisibleState';
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { useHasPermissionFlag } from '@/settings/roles/hooks/useHasPermissionFlag';
import { useOpenRecordsSearchPageInSidePanel } from '@/side-panel/hooks/useOpenRecordsSearchPageInSidePanel';
import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
+import { RootStackingContextZIndices } from '@/ui/layout/constants/RootStackingContextZIndices';
import { currentMobileNavigationDrawerState } from '@/navigation/states/currentMobileNavigationDrawerState';
import { isNavigationDrawerExpandedState } from '@/ui/navigation/states/isNavigationDrawerExpanded';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
+import { styled } from '@linaria/react';
import { useLocation, useNavigate } from 'react-router-dom';
import { AppPath } from 'twenty-shared/types';
import {
@@ -24,10 +28,37 @@ import {
IconSearch,
} from 'twenty-ui/icon';
import { NavigationBar } from 'twenty-ui/navigation';
+import { themeCssVariables } from 'twenty-ui/theme-constants';
import { PermissionFlagType } from '~/generated-metadata/graphql';
type NavigationBarItemName = 'home' | 'search' | 'newAiChat';
+// The bar floats over the page, so the container has to let taps through to
+// whatever is scrolling underneath it. flex-start rather than left so the bar
+// follows the writing direction in RTL locales.
+const StyledFloatingContainer = styled.div`
+ bottom: 0;
+ display: flex;
+ justify-content: flex-start;
+ left: 0;
+ padding: ${themeCssVariables.spacing[3]};
+ padding-bottom: calc(
+ ${themeCssVariables.spacing[3]} + env(safe-area-inset-bottom, 0px)
+ );
+ pointer-events: none;
+ position: absolute;
+ right: 0;
+ z-index: ${RootStackingContextZIndices.MobileNavigationBar};
+
+ > * {
+ pointer-events: auto;
+ }
+
+ @media print {
+ display: none;
+ }
+`;
+
export const MobileNavigationBar = () => {
const { t } = useLingui();
const navigate = useNavigate();
@@ -41,6 +72,9 @@ export const MobileNavigationBar = () => {
const { alphaSortedActiveNonSystemObjectMetadataItems } =
useFilteredObjectMetadataItems();
const hasAiPermission = useHasPermissionFlag(PermissionFlagType.AI);
+ const isMobileNavigationBarVisible = useAtomStateValue(
+ isMobileNavigationBarVisibleState,
+ );
const setContextStoreCurrentObjectMetadataItemId = useSetAtomComponentState(
contextStoreCurrentObjectMetadataItemIdComponentState,
@@ -68,11 +102,13 @@ export const MobileNavigationBar = () => {
const isHomePage = pathname === AppPath.Home;
- const activeItemName: NavigationBarItemName | undefined = isSidePanelOpened
- ? 'search'
- : isHomePage
- ? 'home'
- : undefined;
+ // The side panel is full screen on mobile, so the bar would be floating over
+ // a view it does not navigate. It comes back when the panel closes.
+ const isHidden = isSidePanelOpened || !isMobileNavigationBarVisible;
+
+ const activeItemName: NavigationBarItemName | undefined = isHomePage
+ ? 'home'
+ : undefined;
const items: {
name: NavigationBarItemName;
@@ -127,5 +163,16 @@ export const MobileNavigationBar = () => {
: []),
];
- return <NavigationBar activeItemName={activeItemName ?? ''} items={items} />;
+ return (
+ <>
+ <MobileNavigationBarScrollEffect />
+ <StyledFloatingContainer>
+ <NavigationBar
+ activeItemName={activeItemName ?? ''}
+ isHidden={isHidden}
+ items={items}
+ />
+ </StyledFloatingContainer>
+ </>
+ );
};
diff --git a/packages/twenty-front/src/modules/navigation/components/MobileNavigationBarScrollEffect.tsx b/packages/twenty-front/src/modules/navigation/components/MobileNavigationBarScrollEffect.tsx
new file mode 100644
index 00000000000..085e76e127f
--- /dev/null
+++ b/packages/twenty-front/src/modules/navigation/components/MobileNavigationBarScrollEffect.tsx
@@ -0,0 +1,65 @@
+import { isMobileNavigationBarVisibleState } from '@/navigation/states/isMobileNavigationBarVisibleState';
+import {
+ getNextNavigationBarScrollState,
+ INITIAL_NAVIGATION_BAR_SCROLL_STATE,
+ type NavigationBarScrollState,
+} from '@/navigation/utils/getNextNavigationBarScrollState';
+import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
+import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
+import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
+import { useEffect } from 'react';
+import { useLocation } from 'react-router-dom';
+
+export const MobileNavigationBarScrollEffect = () => {
+ const { pathname } = useLocation();
+ const isSidePanelOpened = useAtomStateValue(isSidePanelOpenedState);
+ const setIsMobileNavigationBarVisible = useSetAtomState(
+ isMobileNavigationBarVisibleState,
+ );
+
+ // The app has many independent scroll containers, so rather than wiring each
+ // of them up we listen on the capture phase, where every scroll event passes
+ // through the document on its way down.
+ //
+ // Re-subscribing on navigation and on side panel transitions is what resets
+ // the tracking. The accumulated direction of the previous view must not carry
+ // over, or the next small scroll would cross the threshold on its own.
+ useEffect(() => {
+ setIsMobileNavigationBarVisible(true);
+
+ const lastScrollTopByElement = new WeakMap<Element, number>();
+ let scrollState: NavigationBarScrollState =
+ INITIAL_NAVIGATION_BAR_SCROLL_STATE;
+
+ const handleScroll = (event: Event) => {
+ const scrolledElement = event.target;
+
+ if (!(scrolledElement instanceof Element)) {
+ return;
+ }
+
+ const scrollTopInPx = scrolledElement.scrollTop;
+ const lastScrollTopInPx = lastScrollTopByElement.get(scrolledElement);
+ lastScrollTopByElement.set(scrolledElement, scrollTopInPx);
+
+ scrollState = getNextNavigationBarScrollState({
+ scrollTopInPx,
+ lastScrollTopInPx,
+ currentState: scrollState,
+ });
+
+ setIsMobileNavigationBarVisible(scrollState.isVisible);
+ };
+
+ document.addEventListener('scroll', handleScroll, {
+ capture: true,
+ passive: true,
+ });
+
+ return () => {
+ document.removeEventListener('scroll', handleScroll, { capture: true });
+ };
+ }, [pathname, isSidePanelOpened, setIsMobileNavigationBarVisible]);
+
+ return <></>;
+};
diff --git a/packages/twenty-front/src/modules/navigation/states/isMobileNavigationBarVisibleState.ts b/packages/twenty-front/src/modules/navigation/states/isMobileNavigationBarVisibleState.ts
new file mode 100644
index 00000000000..d5ca8c5d01f
--- /dev/null
+++ b/packages/twenty-front/src/modules/navigation/states/isMobileNavigationBarVisibleState.ts
@@ -0,0 +1,6 @@
+import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';
+
+export const isMobileNavigationBarVisibleState = createAtomState<boolean>({
+ key: 'navigation/isMobileNavigationBarVisibleState',
+ defaultValue: true,
+});
diff --git a/packages/twenty-front/src/modules/navigation/utils/__tests__/getNextNavigationBarScrollState.test.ts b/packages/twenty-front/src/modules/navigation/utils/__tests__/getNextNavigationBarScrollState.test.ts
new file mode 100644
index 00000000000..46888e47d67
--- /dev/null
+++ b/packages/twenty-front/src/modules/navigation/utils/__tests__/getNextNavigationBarScrollState.test.ts
@@ -0,0 +1,124 @@
+import {
+ getNextNavigationBarScrollState,
+ INITIAL_NAVIGATION_BAR_SCROLL_STATE,
+ type NavigationBarScrollState,
+} from '@/navigation/utils/getNextNavigationBarScrollState';
+
+const visibleAt = (accumulatedDeltaInPx: number): NavigationBarScrollState => ({
+ accumulatedDeltaInPx,
+ isVisible: true,
+});
+
+const hiddenAt = (accumulatedDeltaInPx: number): NavigationBarScrollState => ({
+ accumulatedDeltaInPx,
+ isVisible: false,
+});
+
+describe('getNextNavigationBarScrollState', () => {
+ it('should keep the current state when the container is seen for the first time', () => {
+ const currentState = visibleAt(0);
+
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 800,
+ lastScrollTopInPx: undefined,
+ currentState,
+ }),
+ ).toEqual(currentState);
+ });
+
+ it('should keep the current state when the container did not move vertically', () => {
+ const currentState = visibleAt(20);
+
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 200,
+ lastScrollTopInPx: 200,
+ currentState,
+ }),
+ ).toEqual(currentState);
+ });
+
+ it('should hide once the accumulated downward scroll passes the threshold', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 200,
+ lastScrollTopInPx: 100,
+ currentState: visibleAt(0),
+ }),
+ ).toEqual({ accumulatedDeltaInPx: 100, isVisible: false });
+ });
+
+ it('should stay visible while downward scrolls stay under the threshold', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 110,
+ lastScrollTopInPx: 100,
+ currentState: visibleAt(0),
+ }),
+ ).toEqual({ accumulatedDeltaInPx: 10, isVisible: true });
+ });
+
+ it('should accumulate successive scrolls in the same direction', () => {
+ const afterFirstScroll = getNextNavigationBarScrollState({
+ scrollTopInPx: 115,
+ lastScrollTopInPx: 100,
+ currentState: visibleAt(0),
+ });
+
+ expect(afterFirstScroll).toEqual({
+ accumulatedDeltaInPx: 15,
+ isVisible: true,
+ });
+
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 130,
+ lastScrollTopInPx: 115,
+ currentState: afterFirstScroll,
+ }),
+ ).toEqual({ accumulatedDeltaInPx: 30, isVisible: false });
+ });
+
+ it('should restart the accumulator when the scroll direction changes', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 195,
+ lastScrollTopInPx: 200,
+ currentState: hiddenAt(100),
+ }),
+ ).toEqual({ accumulatedDeltaInPx: -5, isVisible: false });
+ });
+
+ it('should show again once the accumulated upward scroll passes the threshold', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 100,
+ lastScrollTopInPx: 200,
+ currentState: hiddenAt(100),
+ }),
+ ).toEqual({ accumulatedDeltaInPx: -100, isVisible: true });
+ });
+
+ it('should reset to the initial state near the top of the container', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 0,
+ lastScrollTopInPx: 200,
+ currentState: hiddenAt(100),
+ }),
+ ).toEqual(INITIAL_NAVIGATION_BAR_SCROLL_STATE);
+ });
+
+ // The accumulator is what a route change or a side panel transition resets,
+ // so a small scroll on the next view must not inherit the previous direction.
+ it('should require the full threshold again from the initial state', () => {
+ expect(
+ getNextNavigationBarScrollState({
+ scrollTopInPx: 210,
+ lastScrollTopInPx: 200,
+ currentState: INITIAL_NAVIGATION_BAR_SCROLL_STATE,
+ }),
+ ).toEqual({ accumulatedDeltaInPx: 10, isVisible: true });
+ });
+});
diff --git a/packages/twenty-front/src/modules/navigation/utils/getNextNavigationBarScrollState.ts b/packages/twenty-front/src/modules/navigation/utils/getNextNavigationBarScrollState.ts
new file mode 100644
index 00000000000..47170fd5858
--- /dev/null
+++ b/packages/twenty-front/src/modules/navigation/utils/getNextNavigationBarScrollState.ts
@@ -0,0 +1,61 @@
+import { isDefined } from 'twenty-shared/utils';
+
+const SCROLL_DELTA_TO_TOGGLE_IN_PX = 24;
+const SCROLL_TOP_ALWAYS_VISIBLE_IN_PX = 8;
+
+export type NavigationBarScrollState = {
+ accumulatedDeltaInPx: number;
+ isVisible: boolean;
+};
+
+type GetNextNavigationBarScrollStateParams = {
+ scrollTopInPx: number;
+ lastScrollTopInPx: number | undefined;
+ currentState: NavigationBarScrollState;
+};
+
+export const INITIAL_NAVIGATION_BAR_SCROLL_STATE: NavigationBarScrollState = {
+ accumulatedDeltaInPx: 0,
+ isVisible: true,
+};
+
+export const getNextNavigationBarScrollState = ({
+ scrollTopInPx,
+ lastScrollTopInPx,
+ currentState,
+}: GetNextNavigationBarScrollStateParams): NavigationBarScrollState => {
+ // A container restoring its scroll position would otherwise read as a jump
+ // down from 0 the first time it is seen.
+ if (!isDefined(lastScrollTopInPx)) {
+ return currentState;
+ }
+
+ const deltaInPx = scrollTopInPx - lastScrollTopInPx;
+
+ // A horizontal scroll fires the same event without moving the container
+ // vertically, so it has to leave the accumulated direction alone.
+ if (deltaInPx === 0) {
+ return currentState;
+ }
+
+ if (scrollTopInPx <= SCROLL_TOP_ALWAYS_VISIBLE_IN_PX) {
+ return INITIAL_NAVIGATION_BAR_SCROLL_STATE;
+ }
+
+ const hasDirectionChanged =
+ Math.sign(currentState.accumulatedDeltaInPx) !== Math.sign(deltaInPx);
+
+ const accumulatedDeltaInPx = hasDirectionChanged
+ ? deltaInPx
+ : currentState.accumulatedDeltaInPx + deltaInPx;
+
+ if (accumulatedDeltaInPx > SCROLL_DELTA_TO_TOGGLE_IN_PX) {
+ return { accumulatedDeltaInPx, isVisible: false };
+ }
+
+ if (accumulatedDeltaInPx < -SCROLL_DELTA_TO_TOGGLE_IN_PX) {
+ return { accumulatedDeltaInPx, isVisible: true };
+ }
+
+ return { accumulatedDeltaInPx, isVisible: currentState.isVisible };
+};
diff --git a/packages/twenty-front/src/modules/ui/layout/constants/RootStackingContextZIndices.ts b/packages/twenty-front/src/modules/ui/layout/constants/RootStackingContextZIndices.ts
index cee3dff1aea..dc42481a4f2 100644
--- a/packages/twenty-front/src/modules/ui/layout/constants/RootStackingContextZIndices.ts
+++ b/packages/twenty-front/src/modules/ui/layout/constants/RootStackingContextZIndices.ts
@@ -15,6 +15,7 @@
export enum RootStackingContextZIndices {
SidePanel = 21,
SidePanelButton = 22,
+ MobileNavigationBar = 23,
DropdownPortalBelowModal = 38,
RootModalBackDrop = 39,
RootModal = 40,
diff --git a/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.module.scss b/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.module.scss
index 9a49c014f9d..21fdfeb3f4d 100644
--- a/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.module.scss
+++ b/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.module.scss
@@ -1,7 +1,30 @@
.container {
+ align-items: center;
+ backdrop-filter: var(--t-blur-strong);
+ background: var(--t-background-transparent-primary);
+ border: 1px solid var(--t-border-color-transparent-strong);
+ border-radius: var(--t-border-radius-pill);
+ box-shadow: var(--t-box-shadow-strong);
+ box-sizing: border-box;
+ corner-shape: round;
display: flex;
- gap: var(--t-spacing-4);
+ gap: var(--t-spacing-1);
justify-content: center;
- padding: var(--t-spacing-3);
- z-index: 1001;
+ padding: var(--t-spacing-1);
+ transition:
+ opacity duration(normal) ease,
+ transform duration(normal) ease,
+ visibility duration(normal);
+ width: max-content;
+
+ &[data-hidden] {
+ opacity: 0;
+ pointer-events: none;
+ transform: translateY(calc(100% + var(--t-spacing-4)));
+ visibility: hidden;
+ }
+
+ @media (prefers-reduced-motion: reduce) {
+ transition: none;
+ }
}
diff --git a/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.tsx b/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.tsx
index bc4fbb968df..717b63b249a 100644
--- a/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.tsx
+++ b/packages/twenty-ui/src/navigation/NavigationBar/NavigationBar.tsx
@@ -6,6 +6,7 @@ import styles from './NavigationBar.module.scss';
type NavigationBarProps = {
activeItemName: string;
+ isHidden?: boolean;
items: {
name: string;
label: string;
@@ -16,10 +17,15 @@ type NavigationBarProps = {
export const NavigationBar = ({
activeItemName,
+ isHidden = false,
items,
}: NavigationBarProps) => {
return (
- <div className={styles.container}>
+ <nav
+ className={styles.container}
+ data-hidden={isHidden ? '' : undefined}
+ aria-hidden={isHidden}
+ >
{items.map(({ Icon, name, label, onClick }) => (
<NavigationBarItem
key={name}
@@ -29,6 +35,6 @@ export const NavigationBar = ({
ariaLabel={label}
/>
))}
- </div>
+ </nav>
);
};
diff --git a/packages/twenty-ui/src/navigation/NavigationBar/__stories__/NavigationBar.stories.tsx b/packages/twenty-ui/src/navigation/NavigationBar/__stories__/NavigationBar.stories.tsx
index 5bd03bee8e1..28e06a3425e 100644
--- a/packages/twenty-ui/src/navigation/NavigationBar/__stories__/NavigationBar.stories.tsx
+++ b/packages/twenty-ui/src/navigation/NavigationBar/__stories__/NavigationBar.stories.tsx
@@ -1,6 +1,7 @@
import { type Meta, type StoryObj } from '@storybook/react-vite';
import { IconHome, IconSearch, IconSettings } from '@ui/icon';
import { ComponentDecorator } from '@ui/testing';
+import { expect, fn, userEvent, within } from 'storybook/test';
import { NavigationBar } from '@ui/navigation/NavigationBar/NavigationBar';
@@ -13,18 +14,47 @@ const meta: Meta<typeof NavigationBar> = {
export default meta;
type Story = StoryObj<typeof NavigationBar>;
+const getItems = (onSearchClick: () => void) => [
+ { name: 'Home', label: 'Home', Icon: IconHome, onClick: () => {} },
+ { name: 'Search', label: 'Search', Icon: IconSearch, onClick: onSearchClick },
+ {
+ name: 'Settings',
+ label: 'Settings',
+ Icon: IconSettings,
+ onClick: () => {},
+ },
+];
+
export const Default: Story = {
args: {
activeItemName: 'Home',
- items: [
- { name: 'Home', label: 'Home', Icon: IconHome, onClick: () => {} },
- { name: 'Search', label: 'Search', Icon: IconSearch, onClick: () => {} },
- {
- name: 'Settings',
- label: 'Settings',
- Icon: IconSettings,
- onClick: () => {},
- },
- ],
+ items: getItems(fn()),
+ },
+ play: async ({ canvasElement, args }) => {
+ const canvas = within(canvasElement);
+
+ const home = canvas.getByRole('button', { name: 'Home' });
+ expect(home).toHaveAttribute('aria-pressed', 'true');
+
+ const search = canvas.getByRole('button', { name: 'Search' });
+ expect(search).toHaveAttribute('aria-pressed', 'false');
+
+ await userEvent.click(search);
+ expect(args.items[1].onClick).toHaveBeenCalled();
+ },
+};
+
+export const Hidden: Story = {
+ args: {
+ activeItemName: 'Home',
+ isHidden: true,
+ items: getItems(fn()),
+ },
+ play: async ({ canvasElement }) => {
+ const navigationBar = canvasElement.querySelector('nav');
+
+ expect(navigationBar).toHaveAttribute('aria-hidden', 'true');
+ expect(navigationBar).toHaveAttribute('data-hidden');
+ expect(navigationBar).not.toBeVisible();
},
};
diff --git a/packages/twenty-ui/src/navigation/NavigationBarItem/NavigationBarItem.module.scss b/packages/twenty-ui/src/navigation/NavigationBarItem/NavigationBarItem.module.scss
index 69fd450c300..a431472394e 100644
--- a/packages/twenty-ui/src/navigation/NavigationBarItem/NavigationBarItem.module.scss
+++ b/packages/twenty-ui/src/navigation/NavigationBarItem/NavigationBarItem.module.scss
@@ -3,10 +3,12 @@
appearance: none;
background: none;
border: none;
- border-radius: var(--t-spacing-1);
+ border-radius: var(--t-border-radius-pill);
color: inherit;
+ corner-shape: round;
cursor: pointer;
display: flex;
+ flex-shrink: 0;
font: inherit;
height: var(--t-spacing-10);
justify-content: center;
@@ -15,11 +17,13 @@
width: var(--t-spacing-10);
&[data-active] {
- background-color: var(--t-background-transparent-light);
+ background-color: var(--t-background-transparent-medium);
}
- &:hover {
- background-color: var(--t-background-transparent-light);
+ @include hover-capable {
+ &:hover {
+ background-color: var(--t-background-transparent-light);
+ }
}
@include focus-ring;