Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed that on mobile menu is closed if item is clicked in menu #6578

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions apps/www/registry/default/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,17 +553,29 @@ const SidebarMenuButton = React.forwardRef<
},
ref
) => {
const { isMobile, setOpenMobile, state } = useSidebar()
const Comp = asChild ? Slot : "button"
const { isMobile, state } = useSidebar()
// Destructure onClick so we can wrap it.
const { onClick, ...rest } = props
const handleClick = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => {
onClick?.(event)
if (isMobile) {
// When on mobile, clicking a menu item will close the sidebar.
setOpenMobile(false)
}
}

const button = (
<Comp
ref={ref}
data-sidebar="menu-button"
data-size={size}
data-active={isActive}
onClick={handleClick}
className={cn(sidebarMenuButtonVariants({ variant, size }), className)}
{...props}
{...rest}
/>
)

Expand Down Expand Up @@ -713,14 +725,27 @@ const SidebarMenuSubButton = React.forwardRef<
isActive?: boolean
}
>(({ asChild = false, size = "md", isActive, className, ...props }, ref) => {
const { isMobile, setOpenMobile } = useSidebar()
const Comp = asChild ? Slot : "a"
// Destructure onClick so we can wrap it.
const { onClick, ...rest } = props
const handleClick = (
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
) => {
onClick?.(event)
if (isMobile) {
// When on mobile, clicking a submenu item will also close the sidebar.
setOpenMobile(false)
}
}

return (
<Comp
ref={ref}
data-sidebar="menu-sub-button"
data-size={size}
data-active={isActive}
onClick={handleClick}
className={cn(
"flex h-7 min-w-0 -translate-x-px items-center gap-2 overflow-hidden rounded-md px-2 text-sidebar-foreground outline-none ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-sidebar-accent-foreground",
"data-[active=true]:bg-sidebar-accent data-[active=true]:text-sidebar-accent-foreground",
Expand All @@ -729,7 +754,7 @@ const SidebarMenuSubButton = React.forwardRef<
"group-data-[collapsible=icon]:hidden",
className
)}
{...props}
{...rest}
/>
)
})
Expand Down
31 changes: 28 additions & 3 deletions apps/www/registry/new-york/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,17 +553,29 @@ const SidebarMenuButton = React.forwardRef<
},
ref
) => {
const { isMobile, setOpenMobile, state } = useSidebar()
const Comp = asChild ? Slot : "button"
const { isMobile, state } = useSidebar()
// Destructure onClick so we can wrap it.
const { onClick, ...rest } = props
const handleClick = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => {
onClick?.(event)
if (isMobile) {
// When on mobile, clicking a menu item will close the sidebar.
setOpenMobile(false)
}
}

const button = (
<Comp
ref={ref}
data-sidebar="menu-button"
data-size={size}
data-active={isActive}
onClick={handleClick}
className={cn(sidebarMenuButtonVariants({ variant, size }), className)}
{...props}
{...rest}
/>
)

Expand Down Expand Up @@ -713,14 +725,27 @@ const SidebarMenuSubButton = React.forwardRef<
isActive?: boolean
}
>(({ asChild = false, size = "md", isActive, className, ...props }, ref) => {
const { isMobile, setOpenMobile } = useSidebar()
const Comp = asChild ? Slot : "a"
// Destructure onClick so we can wrap it.
const { onClick, ...rest } = props
const handleClick = (
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
) => {
onClick?.(event)
if (isMobile) {
// When on mobile, clicking a submenu item will also close the sidebar.
setOpenMobile(false)
}
}

return (
<Comp
ref={ref}
data-sidebar="menu-sub-button"
data-size={size}
data-active={isActive}
onClick={handleClick}
className={cn(
"flex h-7 min-w-0 -translate-x-px items-center gap-2 overflow-hidden rounded-md px-2 text-sidebar-foreground outline-none ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-sidebar-accent-foreground",
"data-[active=true]:bg-sidebar-accent data-[active=true]:text-sidebar-accent-foreground",
Expand All @@ -729,7 +754,7 @@ const SidebarMenuSubButton = React.forwardRef<
"group-data-[collapsible=icon]:hidden",
className
)}
{...props}
{...rest}
/>
)
})
Expand Down