mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 02:40:14 +01:00
Problem:
--------
Commit 5ed1f2991 introduced a layout shift regression when opening the
sidebar. The UI would visibly "jump" as elements shifted right before
the animation completed. Closing the sidebar worked correctly.
Root Cause Analysis:
--------------------
The accessibility PR added a redundant `{navVisible && ...}` conditional
wrapper around the `<nav>` content inside Nav.tsx's `motion.div`. This
caused a race condition:
1. User clicks "Open Sidebar" button
2. `navVisible` state becomes `true`
3. React renders the `motion.div` AND its children simultaneously
4. The inner `{navVisible && (<nav>...)}` renders content at full width
(320px/260px) BEFORE framer-motion applies `initial={{ width: 0 }}`
5. Brief flash of full-width content causes visible layout shift
6. Animation then starts from width: 0, but damage is done
The ref-based focus management (passing `openSidebarRef`/`closeSidebarRef`
through context) was suspected but was not the actual cause. However,
`requestAnimationFrame` focus calls during animation start could trigger
forced layout calculations, exacerbating the issue.
Solution:
---------
1. Remove redundant conditional rendering in Nav.tsx
- The outer `{navVisible && (<motion.div>...)}` already controls
visibility
- The `overflow-x-hidden` class on motion.div clips content during
animation
- Content should always exist inside motion.div for smooth clipping
2. Replace ref-based focus with ID-based focus management
- Refs passed through component tree can affect React's reconciliation
- Using `document.getElementById()` decouples focus from render cycle
- Exported `CLOSE_SIDEBAR_ID` and `OPEN_SIDEBAR_ID` constants for
consistency
3. Delay focus until after animation completes
- Changed from `requestAnimationFrame` to `setTimeout(..., 250)`
- Animation duration is 200ms; 250ms ensures completion
- Prevents layout thrashing during animation
4. Clean up prop drilling
- Removed `openSidebarRef`/`closeSidebarRef` from Root.tsx context
- Simplified Nav.tsx, Header.tsx, NewChat.tsx prop signatures
- Updated ContextType to remove ref properties
Files Changed:
--------------
- client/src/routes/Root.tsx
- client/src/components/Nav/Nav.tsx
- client/src/components/Nav/NewChat.tsx
- client/src/components/Chat/Header.tsx
- client/src/components/Chat/Menus/OpenSidebar.tsx
- client/src/common/types.ts
Accessibility Note:
-------------------
The original inner conditional was added to prevent keyboard navigation
to hidden sidebar content for screen readers. This is still handled by:
- AnimatePresence unmounting the motion.div after exit animation
- The motion.div having width: 0 during exit (content not reachable)
- Screen readers typically skip content being animated out
- Other: removed non-existant prop from BookmarkNav
Testing:
--------
- Verified smooth animation when opening sidebar (no layout shift)
- Verified smooth animation when closing sidebar (unchanged)
- Verified focus transfers correctly between open/close buttons
- Verified keyboard navigation works as expected
95 lines
3.5 KiB
TypeScript
95 lines
3.5 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useMediaQuery } from '@librechat/client';
|
|
import { useOutletContext } from 'react-router-dom';
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
|
import { getConfigDefaults, PermissionTypes, Permissions } from 'librechat-data-provider';
|
|
import type { ContextType } from '~/common';
|
|
import { PresetsMenu, HeaderNewChat, OpenSidebar } from './Menus';
|
|
import ModelSelector from './Menus/Endpoints/ModelSelector';
|
|
import { useGetStartupConfig } from '~/data-provider';
|
|
import ExportAndShareMenu from './ExportAndShareMenu';
|
|
import BookmarkMenu from './Menus/BookmarkMenu';
|
|
import { TemporaryChat } from './TemporaryChat';
|
|
import AddMultiConvo from './AddMultiConvo';
|
|
import { useHasAccess } from '~/hooks';
|
|
import { cn } from '~/utils';
|
|
|
|
const defaultInterface = getConfigDefaults().interface;
|
|
|
|
export default function Header() {
|
|
const { data: startupConfig } = useGetStartupConfig();
|
|
const { navVisible, setNavVisible } = useOutletContext<ContextType>();
|
|
|
|
const interfaceConfig = useMemo(
|
|
() => startupConfig?.interface ?? defaultInterface,
|
|
[startupConfig],
|
|
);
|
|
|
|
const hasAccessToBookmarks = useHasAccess({
|
|
permissionType: PermissionTypes.BOOKMARKS,
|
|
permission: Permissions.USE,
|
|
});
|
|
|
|
const hasAccessToMultiConvo = useHasAccess({
|
|
permissionType: PermissionTypes.MULTI_CONVO,
|
|
permission: Permissions.USE,
|
|
});
|
|
|
|
const isSmallScreen = useMediaQuery('(max-width: 768px)');
|
|
|
|
return (
|
|
<div className="sticky top-0 z-10 flex h-14 w-full items-center justify-between bg-white p-2 font-semibold text-text-primary dark:bg-gray-800">
|
|
<div className="hide-scrollbar flex w-full items-center justify-between gap-2 overflow-x-auto">
|
|
<div className="mx-1 flex items-center">
|
|
<AnimatePresence initial={false}>
|
|
{!navVisible && (
|
|
<motion.div
|
|
className="flex items-center gap-2"
|
|
initial={{ width: 0, opacity: 0 }}
|
|
animate={{ width: 'auto', opacity: 1 }}
|
|
exit={{ width: 0, opacity: 0 }}
|
|
transition={{ duration: 0.2 }}
|
|
key="header-buttons"
|
|
>
|
|
<OpenSidebar setNavVisible={setNavVisible} className="max-md:hidden" />
|
|
<HeaderNewChat />
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
{!(navVisible && isSmallScreen) && (
|
|
<div
|
|
className={cn(
|
|
'flex items-center gap-2',
|
|
!isSmallScreen ? 'transition-all duration-200 ease-in-out' : '',
|
|
)}
|
|
>
|
|
<ModelSelector startupConfig={startupConfig} />
|
|
{interfaceConfig.presets === true && interfaceConfig.modelSelect && <PresetsMenu />}
|
|
{hasAccessToBookmarks === true && <BookmarkMenu />}
|
|
{hasAccessToMultiConvo === true && <AddMultiConvo />}
|
|
{isSmallScreen && (
|
|
<>
|
|
<ExportAndShareMenu
|
|
isSharedButtonEnabled={startupConfig?.sharedLinksEnabled ?? false}
|
|
/>
|
|
<TemporaryChat />
|
|
</>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{!isSmallScreen && (
|
|
<div className="flex items-center gap-2">
|
|
<ExportAndShareMenu
|
|
isSharedButtonEnabled={startupConfig?.sharedLinksEnabled ?? false}
|
|
/>
|
|
<TemporaryChat />
|
|
</div>
|
|
)}
|
|
</div>
|
|
{/* Empty div for spacing */}
|
|
<div />
|
|
</div>
|
|
);
|
|
}
|