The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Animate the sidebar when switching between Home, AI and Settings (#26905)
Thomas des Francsce5ee93ea097
diff --git a/packages/twenty-front/src/modules/navigation/components/AppNavigationDrawer.tsx b/packages/twenty-front/src/modules/navigation/components/AppNavigationDrawer.tsxindex dcdb7e9825a..3aa299f3826 100644--- a/packages/twenty-front/src/modules/navigation/components/AppNavigationDrawer.tsx+++ b/packages/twenty-front/src/modules/navigation/components/AppNavigationDrawer.tsx@@ -2,6 +2,7 @@ import { useIsSettingsDrawer } from '@/navigation/hooks/useIsSettingsDrawer';import { MainNavigationDrawerContent } from '@/navigation/components/MainNavigationDrawerContent';import { MainNavigationDrawerModeSwitcher } from '@/navigation/components/MainNavigationDrawerModeSwitcher';+import { NavigationDrawerModeTransition } from '@/navigation/components/NavigationDrawerModeTransition';import { SettingsNavigationDrawerContent } from '@/navigation/components/SettingsNavigationDrawerContent';import { NavigationDrawer } from '@/ui/navigation/navigation-drawer/components/NavigationDrawer';import { NavigationDrawerFixedContent } from '@/ui/navigation/navigation-drawer/components/NavigationDrawerFixedContent';@@ -32,11 +33,13 @@ export const AppNavigationDrawer = ({</NavigationDrawerFixedContent>)}- {isSettingsDrawer ? (- <SettingsNavigationDrawerContent />- ) : (- <MainNavigationDrawerContent />- )}+ <NavigationDrawerModeTransition>+ {isSettingsDrawer ? (+ <SettingsNavigationDrawerContent />+ ) : (+ <MainNavigationDrawerContent />+ )}+ </NavigationDrawerModeTransition></NavigationDrawer>);};diff --git a/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransition.tsx b/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransition.tsxnew file mode 100644index 00000000000..d3dfc4c25a8--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransition.tsx@@ -0,0 +1,29 @@+import { styled } from '@linaria/react';+import { type ReactNode, useRef } from 'react';++import { NavigationDrawerModeTransitionEffect } from '@/navigation/components/NavigationDrawerModeTransitionEffect';++const StyledContainer = styled.div`+ display: flex;+ flex: 1;+ flex-direction: column;+ gap: inherit;+ min-height: 0;+`;++type NavigationDrawerModeTransitionProps = {+ children: ReactNode;+};++export const NavigationDrawerModeTransition = ({+ children,+}: NavigationDrawerModeTransitionProps) => {+ const containerRef = useRef<HTMLDivElement>(null);++ return (+ <StyledContainer ref={containerRef}>+ <NavigationDrawerModeTransitionEffect containerRef={containerRef} />+ {children}+ </StyledContainer>+ );+};diff --git a/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransitionEffect.tsx b/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransitionEffect.tsxnew file mode 100644index 00000000000..117c37459ea--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/components/NavigationDrawerModeTransitionEffect.tsx@@ -0,0 +1,69 @@+import { useLingui } from '@lingui/react';+import { useReducedMotion } from 'framer-motion';+import { type RefObject, useLayoutEffect, useState } from 'react';+import { getLocaleTextDirection } from 'twenty-shared/translations';+import { isDefined } from 'twenty-shared/utils';++import { NAVIGATION_DRAWER_MODE_TRANSITION } from '@/navigation/constants/NavigationDrawerModeTransition';+import { useActiveNavigationDrawerMode } from '@/navigation/hooks/useActiveNavigationDrawerMode';+import { type NavigationDrawerModeTransitionDirection } from '@/navigation/types/NavigationDrawerModeTransitionDirection';+import { getNavigationDrawerModeTransitionDirection } from '@/navigation/utils/getNavigationDrawerModeTransitionDirection';++type NavigationDrawerModeTransitionEffectProps = {+ containerRef: RefObject<HTMLDivElement | null>;+};++export const NavigationDrawerModeTransitionEffect = ({+ containerRef,+}: NavigationDrawerModeTransitionEffectProps) => {+ const activeNavigationDrawerMode = useActiveNavigationDrawerMode();+ const { i18n } = useLingui();+ const shouldReduceMotion = useReducedMotion();++ const [previousNavigationDrawerMode, setPreviousNavigationDrawerMode] =+ useState(activeNavigationDrawerMode);+ const [modeTransition, setModeTransition] = useState<{+ direction: NavigationDrawerModeTransitionDirection;+ } | null>(null);++ if (activeNavigationDrawerMode !== previousNavigationDrawerMode) {+ setPreviousNavigationDrawerMode(activeNavigationDrawerMode);+ setModeTransition(+ shouldReduceMotion+ ? null+ : {+ direction: getNavigationDrawerModeTransitionDirection({+ previousNavigationDrawerMode,+ nextNavigationDrawerMode: activeNavigationDrawerMode,+ textDirection: getLocaleTextDirection(i18n.locale),+ }),+ },+ );+ }++ useLayoutEffect(() => {+ const container = containerRef.current;++ if (!isDefined(modeTransition) || !isDefined(container)) {+ return;+ }++ const { durationInMs, easing, offsetInPx } =+ NAVIGATION_DRAWER_MODE_TRANSITION;++ const animation = container.animate(+ [+ {+ opacity: 0,+ transform: `translateX(${modeTransition.direction * offsetInPx}px)`,+ },+ { opacity: 1, transform: 'translateX(0)' },+ ],+ { duration: durationInMs, easing },+ );++ return () => animation.cancel();+ }, [containerRef, modeTransition]);++ return null;+};diff --git a/packages/twenty-front/src/modules/navigation/components/__tests__/AppNavigationDrawer.test.tsx b/packages/twenty-front/src/modules/navigation/components/__tests__/AppNavigationDrawer.test.tsxindex efc91eeec0a..708ac01db96 100644--- a/packages/twenty-front/src/modules/navigation/components/__tests__/AppNavigationDrawer.test.tsx+++ b/packages/twenty-front/src/modules/navigation/components/__tests__/AppNavigationDrawer.test.tsx@@ -24,6 +24,12 @@ jest.mock('@/navigation/components/SettingsNavigationDrawerContent', () => ({SettingsNavigationDrawerContent: () => <div>Settings content</div>,}));+jest.mock('@/navigation/components/NavigationDrawerModeTransition', () => ({+ NavigationDrawerModeTransition: ({ children }: { children: ReactNode }) => (+ <>{children}</>+ ),+}));+jest.mock('@/ui/navigation/navigation-drawer/components/NavigationDrawer',() => ({diff --git a/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeOrder.ts b/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeOrder.tsnew file mode 100644index 00000000000..ccef8016a4c--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeOrder.ts@@ -0,0 +1,10 @@+import {+ NAVIGATION_DRAWER_TABS,+ type NavigationDrawerActiveTab,+} from '@/ui/navigation/states/navigationDrawerTabs';++export const NAVIGATION_DRAWER_MODE_ORDER: NavigationDrawerActiveTab[] = [+ NAVIGATION_DRAWER_TABS.NAVIGATION_MENU,+ NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY,+ NAVIGATION_DRAWER_TABS.SETTINGS,+];diff --git a/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeTransition.ts b/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeTransition.tsnew file mode 100644index 00000000000..e3460ff8f6e--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/constants/NavigationDrawerModeTransition.ts@@ -0,0 +1,5 @@+export const NAVIGATION_DRAWER_MODE_TRANSITION = {+ durationInMs: 180,+ easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)',+ offsetInPx: 8,+} as const;diff --git a/packages/twenty-front/src/modules/navigation/hooks/useNavigationDrawerModes.ts b/packages/twenty-front/src/modules/navigation/hooks/useNavigationDrawerModes.tsindex e869119677a..7fda625bcae 100644--- a/packages/twenty-front/src/modules/navigation/hooks/useNavigationDrawerModes.ts+++ b/packages/twenty-front/src/modules/navigation/hooks/useNavigationDrawerModes.ts@@ -7,6 +7,7 @@ import {IconSettings,} from 'twenty-ui/icon';+import { NAVIGATION_DRAWER_MODE_ORDER } from '@/navigation/constants/NavigationDrawerModeOrder';import { useHasPermissionFlag } from '@/settings/roles/hooks/useHasPermissionFlag';import {type NavigationDrawerActiveTab,@@ -36,25 +37,26 @@ export const useNavigationDrawerModes = (): NavigationDrawerMode[] => {return [];}- return [- {+ const navigationDrawerModeDisplays: Record<+ NavigationDrawerActiveTab,+ Omit<NavigationDrawerMode, 'mode'>+ > = {+ [NAVIGATION_DRAWER_TABS.NAVIGATION_MENU]: {Icon: IconHome,label: t`Home`,- mode: NAVIGATION_DRAWER_TABS.NAVIGATION_MENU,},- ...(hasAiPermission- ? [- {- Icon: IconComment,- label: t`AI`,- mode: NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY,- },- ]- : []),- {+ [NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY]: {+ Icon: IconComment,+ label: t`AI`,+ },+ [NAVIGATION_DRAWER_TABS.SETTINGS]: {Icon: IconSettings,label: t`Settings`,- mode: NAVIGATION_DRAWER_TABS.SETTINGS,},- ];+ };++ return NAVIGATION_DRAWER_MODE_ORDER.filter(+ (mode) =>+ mode !== NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY || hasAiPermission,+ ).map((mode) => ({ ...navigationDrawerModeDisplays[mode], mode }));};diff --git a/packages/twenty-front/src/modules/navigation/types/NavigationDrawerModeTransitionDirection.ts b/packages/twenty-front/src/modules/navigation/types/NavigationDrawerModeTransitionDirection.tsnew file mode 100644index 00000000000..1992ba480be--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/types/NavigationDrawerModeTransitionDirection.ts@@ -0,0 +1 @@+export type NavigationDrawerModeTransitionDirection = 1 | -1;diff --git a/packages/twenty-front/src/modules/navigation/utils/__tests__/getNavigationDrawerModeTransitionDirection.test.ts b/packages/twenty-front/src/modules/navigation/utils/__tests__/getNavigationDrawerModeTransitionDirection.test.tsnew file mode 100644index 00000000000..891b5abbbcb--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/utils/__tests__/getNavigationDrawerModeTransitionDirection.test.ts@@ -0,0 +1,34 @@+import { getNavigationDrawerModeTransitionDirection } from '@/navigation/utils/getNavigationDrawerModeTransitionDirection';+import { NAVIGATION_DRAWER_TABS } from '@/ui/navigation/states/navigationDrawerTabs';++describe('getNavigationDrawerModeTransitionDirection', () => {+ it('slides in from the right when moving to a later mode', () => {+ expect(+ getNavigationDrawerModeTransitionDirection({+ previousNavigationDrawerMode: NAVIGATION_DRAWER_TABS.NAVIGATION_MENU,+ nextNavigationDrawerMode: NAVIGATION_DRAWER_TABS.SETTINGS,+ textDirection: 'ltr',+ }),+ ).toBe(1);+ });++ it('slides in from the left when moving to an earlier mode', () => {+ expect(+ getNavigationDrawerModeTransitionDirection({+ previousNavigationDrawerMode: NAVIGATION_DRAWER_TABS.SETTINGS,+ nextNavigationDrawerMode: NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY,+ textDirection: 'ltr',+ }),+ ).toBe(-1);+ });++ it('mirrors the slide when the switcher is laid out right to left', () => {+ expect(+ getNavigationDrawerModeTransitionDirection({+ previousNavigationDrawerMode: NAVIGATION_DRAWER_TABS.NAVIGATION_MENU,+ nextNavigationDrawerMode: NAVIGATION_DRAWER_TABS.SETTINGS,+ textDirection: 'rtl',+ }),+ ).toBe(-1);+ });+});diff --git a/packages/twenty-front/src/modules/navigation/utils/getNavigationDrawerModeTransitionDirection.ts b/packages/twenty-front/src/modules/navigation/utils/getNavigationDrawerModeTransitionDirection.tsnew file mode 100644index 00000000000..0b4d0af2cd5--- /dev/null+++ b/packages/twenty-front/src/modules/navigation/utils/getNavigationDrawerModeTransitionDirection.ts@@ -0,0 +1,26 @@+import { type TextDirection } from 'twenty-shared/translations';++import { NAVIGATION_DRAWER_MODE_ORDER } from '@/navigation/constants/NavigationDrawerModeOrder';+import { type NavigationDrawerModeTransitionDirection } from '@/navigation/types/NavigationDrawerModeTransitionDirection';+import { type NavigationDrawerActiveTab } from '@/ui/navigation/states/navigationDrawerTabs';++type GetNavigationDrawerModeTransitionDirectionParams = {+ previousNavigationDrawerMode: NavigationDrawerActiveTab;+ nextNavigationDrawerMode: NavigationDrawerActiveTab;+ textDirection: TextDirection;+};++export const getNavigationDrawerModeTransitionDirection = ({+ previousNavigationDrawerMode,+ nextNavigationDrawerMode,+ textDirection,+}: GetNavigationDrawerModeTransitionDirectionParams): NavigationDrawerModeTransitionDirection => {+ const isMovingForward =+ NAVIGATION_DRAWER_MODE_ORDER.indexOf(nextNavigationDrawerMode) >+ NAVIGATION_DRAWER_MODE_ORDER.indexOf(previousNavigationDrawerMode);++ const isEnteringFromTheRight =+ textDirection === 'rtl' ? !isMovingForward : isMovingForward;++ return isEnteringFromTheRight ? 1 : -1;+};