Skip to content

Commit

Permalink
feat(sidebar): optimize mobile performance with CSS transitions
Browse files Browse the repository at this point in the history
- Refactored mobile sidebar implementation to use direct CSS transforms instead of Sheet component
- Added static overlay mask with opacity transition for mobile experience
- Implemented custom close button with X icon to replace Sheet's default
- Improved z-index handling for sidebar elements (chat-bubble z-index reduced to 30)
- Preserved DOM structure during sidebar toggle to prevent unnecessary remounting
- Unified PC/mobile behavior using CSS animation rather than dynamic mounting
- Removed dependency on radix-ui Dialog components for mobile sidebar
  • Loading branch information
heimoshuiyu committed Feb 7, 2025
1 parent 4ca40a7 commit 5107fc6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/ui/chat/chat-bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const ChatBubbleActionWrapper = React.forwardRef<
<div
ref={ref}
className={cn(
"absolute z-50 translate-y-full flex opacity-0 group-hover:opacity-100 transition-opacity duration-200",
"absolute z-30 translate-y-full flex opacity-0 group-hover:opacity-100 transition-opacity duration-200",
variant === "sent" ? "flex-row-reverse" : "",
className
)}
Expand Down
43 changes: 35 additions & 8 deletions src/components/ui/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { VariantProps, cva } from "class-variance-authority";
import { PanelLeft } from "lucide-react";
import { PanelLeft, X } from "lucide-react";

import { useIsMobile } from "@/hooks/use-mobile";
import { cn } from "@/lib/utils";
Expand Down Expand Up @@ -192,21 +192,48 @@ const Sidebar = React.forwardRef<

if (isMobile) {
return (
<Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
<SheetContent
<div className="md:hidden">
{/* 遮罩层 */}
<div
className={cn(
"fixed inset-0 z-40 bg-black/80 transition-opacity duration-500",
openMobile ? "opacity-100" : "opacity-0 pointer-events-none"
)}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
setOpenMobile(false);
}}
/>

{/* 侧边栏 */}
<div
data-sidebar="sidebar"
data-mobile="true"
className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
className={cn(
"fixed inset-y-0 z-50 h-svh w-[--sidebar-width-mobile] bg-sidebar p-0 text-sidebar-foreground transition-transform duration-500 ease-in-out",
side === "left"
? "-translate-x-full left-0"
: "translate-x-full right-0",
openMobile && "translate-x-0"
)}
style={
{
"--sidebar-width": SIDEBAR_WIDTH_MOBILE,
"--sidebar-width-mobile": SIDEBAR_WIDTH_MOBILE,
} as React.CSSProperties
}
side={side}
{...props}
>
{/* 关闭按钮 */}
<button
className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2"
onClick={() => setOpenMobile(false)}
>
<X className="h-4 w-4" />
</button>
<div className="flex h-full w-full flex-col">{children}</div>
</SheetContent>
</Sheet>
</div>
</div>
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Chatbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export default function ChatBOX() {
token: data.usage?.completion_tokens_details
? data.usage.completion_tokens -
data.usage.completion_tokens_details.reasoning_tokens
: data.usage.completion_tokens ?? calculate_token_length(msg.content),
: (data.usage.completion_tokens ?? calculate_token_length(msg.content)),
example: false,
audio: null,
logprobs: data.choices[0]?.logprobs,
Expand Down

0 comments on commit 5107fc6

Please sign in to comment.