|
| 1 | +import React, { |
| 2 | + HTMLAttributes, |
| 3 | + HTMLProps, |
| 4 | + PropsWithChildren, |
| 5 | + ReactElement, |
| 6 | + ReactNode, |
| 7 | + cloneElement, |
| 8 | + forwardRef, |
| 9 | + isValidElement |
| 10 | +} from "react"; |
| 11 | +import { Slot } from "@radix-ui/react-slot"; |
| 12 | +import { cn } from "../../utilities/index"; |
| 13 | + |
| 14 | +export const Sidebar = forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>( |
| 15 | + ({ className, children, ...rest }, ref) => { |
| 16 | + return ( |
| 17 | + <nav |
| 18 | + ref={ref} |
| 19 | + className={cn( |
| 20 | + "group flex-1 h-full flex w-9 hover:w-[220px] bg-neutral-100 transition-all overflow-x-hidden duration-300 flex-col items-center border-r border-theme-border-moderate", |
| 21 | + className |
| 22 | + )} |
| 23 | + {...rest} |
| 24 | + > |
| 25 | + <div className="flex flex-col h-full flex-1 w-full">{children}</div> |
| 26 | + </nav> |
| 27 | + ); |
| 28 | + } |
| 29 | +); |
| 30 | + |
| 31 | +Sidebar.displayName = "Sidebar"; |
| 32 | + |
| 33 | +export function SidebarHeaderImage({ children }: PropsWithChildren) { |
| 34 | + return <Slot className="w-6 h-6 rounded-sm shrink-0">{children}</Slot>; |
| 35 | +} |
| 36 | + |
| 37 | +export const SidebarHeaderTitle = forwardRef< |
| 38 | + HTMLParagraphElement, |
| 39 | + HTMLAttributes<HTMLParagraphElement> |
| 40 | +>(({ children, className, ...rest }, ref) => ( |
| 41 | + <p |
| 42 | + ref={ref} |
| 43 | + {...rest} |
| 44 | + className={cn( |
| 45 | + "opacity-0 group-hover:opacity-100 ml-1 truncate duration-300 transition-all", |
| 46 | + className |
| 47 | + )} |
| 48 | + > |
| 49 | + {children} |
| 50 | + </p> |
| 51 | +)); |
| 52 | + |
| 53 | +SidebarHeaderTitle.displayName = "SidebarHeaderTitle"; |
| 54 | + |
| 55 | +export type SidebarHeaderProps = HTMLAttributes<HTMLDivElement> & { |
| 56 | + title: string; |
| 57 | + icon?: ReactNode; |
| 58 | +}; |
| 59 | + |
| 60 | +export const SidebarHeader = forwardRef<HTMLDivElement, SidebarHeaderProps>( |
| 61 | + ({ title, icon, children, className, ...rest }, ref) => { |
| 62 | + const existingIconClasses = isValidElement(icon) ? icon.props.className || "" : ""; |
| 63 | + |
| 64 | + const iconClasses = cn( |
| 65 | + existingIconClasses, |
| 66 | + "w-6 ml-auto shrink-0 h-6 opacity-0 group-hover:opacity-100 duration-300 transition-all" |
| 67 | + ); |
| 68 | + |
| 69 | + return ( |
| 70 | + <div |
| 71 | + ref={ref} |
| 72 | + {...rest} |
| 73 | + className={cn( |
| 74 | + "bg-theme-surface-primary flex items-center whitespace-nowrap p-3 border-b border-theme-border-moderate", |
| 75 | + className |
| 76 | + )} |
| 77 | + > |
| 78 | + {children} |
| 79 | + |
| 80 | + {icon && cloneElement(icon as ReactElement, { className: iconClasses })} |
| 81 | + </div> |
| 82 | + ); |
| 83 | + } |
| 84 | +); |
| 85 | + |
| 86 | +SidebarHeader.displayName = "SidebarHeader"; |
| 87 | + |
| 88 | +export const SidebarBody = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>( |
| 89 | + ({ className, ...rest }, ref) => { |
| 90 | + return ( |
| 91 | + <div ref={ref} className={cn(`flex-1 flex px-1 py-2 flex-col w-full`, className)} {...rest} /> |
| 92 | + ); |
| 93 | + } |
| 94 | +); |
| 95 | +export const SidebarList = forwardRef<HTMLUListElement, HTMLAttributes<HTMLUListElement>>( |
| 96 | + ({ className, ...rest }, ref) => { |
| 97 | + return ( |
| 98 | + <ul |
| 99 | + ref={ref} |
| 100 | + className={cn("flex gap-0.5 w-full flex-col items-center", className)} |
| 101 | + {...rest} |
| 102 | + /> |
| 103 | + ); |
| 104 | + } |
| 105 | +); |
| 106 | + |
| 107 | +SidebarList.displayName = "SidebarList"; |
| 108 | + |
| 109 | +export type SidebarItemProps = HTMLAttributes<HTMLLIElement> & { |
| 110 | + isActive?: boolean; |
| 111 | +}; |
| 112 | + |
| 113 | +export function SidebarItem({ |
| 114 | + isActive = false, |
| 115 | + ...rest |
| 116 | +}: PropsWithChildren<{ isActive?: boolean }>) { |
| 117 | + return ( |
| 118 | + <Slot |
| 119 | + className={`flex p-2 items-center gap-2 rounded-md w-full ${ |
| 120 | + isActive ? "bg-theme-surface-primary" : "hover:bg-neutral-200 active:bg-neutral-300" |
| 121 | + }`} |
| 122 | + {...rest} |
| 123 | + ></Slot> |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +type SidebarItemContentProps = { |
| 128 | + icon: ReactNode; |
| 129 | + label: string; |
| 130 | + isActive?: boolean; |
| 131 | +}; |
| 132 | + |
| 133 | +export function SidebarItemContent({ icon, label, isActive }: SidebarItemContentProps) { |
| 134 | + const existingIconClasses = isValidElement(icon) ? icon.props.className || "" : ""; |
| 135 | + |
| 136 | + const iconClasses = cn( |
| 137 | + existingIconClasses, |
| 138 | + `w-5 h-5 shrink-0 ${isActive ? "stroke-primary-400" : "stroke-theme-text-tertiary"} ` |
| 139 | + ); |
| 140 | + return ( |
| 141 | + <> |
| 142 | + {cloneElement(icon as ReactElement, { className: iconClasses })} |
| 143 | + <div className="opacity-0 group-hover:opacity-100 duration-300 transition-all">{label}</div> |
| 144 | + </> |
| 145 | + ); |
| 146 | +} |
0 commit comments