Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refactor(sidebar): express mobile drawer duration in CSS (#18805)

Engel Nyst3083e1aa1660
diff --git a/__tests__/components/features/sidebar/sidebar.test.tsx b/__tests__/components/features/sidebar/sidebar.test.tsx
index 8099b2c896..1b211e4af3 100644
--- a/__tests__/components/features/sidebar/sidebar.test.tsx
+++ b/__tests__/components/features/sidebar/sidebar.test.tsx
@@ -1,3 +1,6 @@
+import { readFile } from "node:fs/promises";
+import { resolve } from "node:path";
+import { compile } from "@tailwindcss/node";
import {
act,
render,
@@ -273,6 +276,66 @@ describe("Sidebar", () => {
});
});
+ it("keeps the CSS drawer and backdrop timing in step with removal", async () => {
+ // Arrange: compile the production stylesheet, then drive the real drawer.
+ const compiler = await compile(
+ await readFile(resolve("src/tailwind.css"), "utf8"),
+ { base: resolve("src"), onDependency: () => {} },
+ );
+ vi.useFakeTimers();
+ try {
+ renderSidebar("/conversations");
+ fireEvent.click(screen.getByTestId("sidebar-mobile-menu-toggle"));
+ act(() => vi.advanceTimersToNextFrame());
+ const drawer = screen.getByTestId("sidebar-mobile-drawer");
+ const backdrop = drawer.previousElementSibling as HTMLElement;
+ expect(drawer).toHaveAttribute("aria-hidden", "false");
+ expect(backdrop).toHaveAttribute("aria-hidden", "false");
+
+ // Act: use only duration classes actually emitted by each element.
+ const durations = [drawer, backdrop].map((element) => {
+ const css = compiler.build(
+ [...element.classList].filter((candidate) =>
+ candidate.startsWith("duration-"),
+ ),
+ );
+ const declarations = [
+ ...css.matchAll(/\.((?:\\.|[\w-])+)\s*\{([^}]+)\}/g),
+ ]
+ .filter(([, selector]) =>
+ element.classList.contains(selector.replace(/\\(.)/g, "$1")),
+ )
+ .map(([, , body]) => body)
+ .join("\n");
+ const property = declarations.match(
+ /transition-duration:\s*var\((--[\w-]+)\);/,
+ )?.[1];
+ expect(property).toBeDefined();
+ const value = element.style.getPropertyValue(property!);
+ expect(value).toMatch(/^\d+ms$/);
+ return Number.parseFloat(value);
+ });
+
+ // Assert: the visible CSS duration and the lifecycle timer agree.
+ const [duration] = durations;
+ expect(duration).toBeGreaterThan(0);
+ expect(durations[1]).toBe(duration);
+ fireEvent.click(
+ within(drawer).getByTestId("sidebar-mobile-drawer-close"),
+ );
+ expect(drawer).toHaveAttribute("aria-hidden", "true");
+ expect(backdrop).toHaveAttribute("aria-hidden", "true");
+ act(() => vi.advanceTimersByTime(duration - 1));
+ expect(drawer).toBeInTheDocument();
+ expect(backdrop).toBeInTheDocument();
+ act(() => vi.advanceTimersByTime(1));
+ expect(drawer).not.toBeInTheDocument();
+ expect(backdrop).not.toBeInTheDocument();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("leaves the mobile drawer open when a menu inside it already handled Escape", () => {
renderSidebar("/conversations");
const toggle = screen.getByTestId("sidebar-mobile-menu-toggle");
diff --git a/src/components/features/sidebar/sidebar.tsx b/src/components/features/sidebar/sidebar.tsx
index 7c0c898f58..bff50db280 100644
--- a/src/components/features/sidebar/sidebar.tsx
+++ b/src/components/features/sidebar/sidebar.tsx
@@ -36,6 +36,9 @@ const ManageBackendsModal = React.lazy(() =>
);
const MOBILE_DRAWER_TRANSITION_MS = 250;
+const MOBILE_DRAWER_TRANSITION_STYLE = {
+ "--mobile-drawer-transition-duration": `${MOBILE_DRAWER_TRANSITION_MS}ms`,
+} as React.CSSProperties;
export function Sidebar() {
const { t } = useTranslation("openhands");
@@ -227,12 +230,12 @@ export function Sidebar() {
<div
className={cn(
"fixed inset-0 z-40 bg-black/50 md:hidden",
- "transition-opacity ease-in-out motion-reduce:transition-none",
+ "transition-opacity duration-(--mobile-drawer-transition-duration) ease-in-out motion-reduce:transition-none",
mobileDrawerVisible
? "opacity-100"
: "pointer-events-none opacity-0",
)}
- style={{ transitionDuration: `${MOBILE_DRAWER_TRANSITION_MS}ms` }}
+ style={MOBILE_DRAWER_TRANSITION_STYLE}
onClick={closeMobileNav}
aria-hidden={!mobileDrawerVisible}
/>
@@ -243,10 +246,10 @@ export function Sidebar() {
className={cn(
"fixed inset-y-0 left-0 z-50 flex min-h-0 w-[min(300px,85vw)] flex-col bg-canvas-base",
"border-r border-border pb-2 pl-2.5 pr-0 md:hidden",
- "transition-transform ease-in-out motion-reduce:transition-none",
+ "transition-transform duration-(--mobile-drawer-transition-duration) ease-in-out motion-reduce:transition-none",
mobileDrawerVisible ? "translate-x-0" : "-translate-x-full",
)}
- style={{ transitionDuration: `${MOBILE_DRAWER_TRANSITION_MS}ms` }}
+ style={MOBILE_DRAWER_TRANSITION_STYLE}
>
<SidebarRailBody
collapsed={false}