Spaces:
Build error
Build error
File size: 6,562 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
'use client';
import { cva, VariantProps } from 'class-variance-authority';
import * as React from 'react';
import { AppLayoutAnatomy } from '.';
import { cn, ComponentAnatomy, defineStyleAnatomy } from '../core/styling';
import { Drawer, DrawerProps } from '../drawer';
/* -------------------------------------------------------------------------------------------------
* Context
* -----------------------------------------------------------------------------------------------*/
export const __AppSidebarContext = React.createContext<{
open: boolean;
setOpen: (open: boolean) => void;
size: VariantProps<typeof AppLayoutAnatomy.root>['sidebarSize'];
setSize: (
size: VariantProps<typeof AppLayoutAnatomy.root>['sidebarSize']
) => void;
isBelowBreakpoint: boolean;
}>({
open: false,
setOpen: () => {},
setSize: () => {},
size: 'md',
isBelowBreakpoint: false,
});
export function useAppSidebarContext() {
const ctx = React.useContext(__AppSidebarContext);
if (!ctx)
throw new Error(
'useAppSidebarContext must be used within a AppSidebarProvider'
);
return ctx;
}
/* -------------------------------------------------------------------------------------------------
* Anatomy
* -----------------------------------------------------------------------------------------------*/
export const AppSidebarAnatomy = defineStyleAnatomy({
sidebar: cva([
'UI-AppSidebar__sidebar',
'flex flex-grow flex-col overflow-y-auto border-r border-transparent bg-[--background]',
]),
});
export const AppSidebarTriggerAnatomy = defineStyleAnatomy({
trigger: cva([
'UI-AppSidebarTrigger__trigger',
'block lg:hidden',
'items-center justify-center rounded-[--radius] p-2 text-[--muted] hover:bg-[--subtle] hover:text-[--foreground] transition-colors',
'focus:outline-none focus:ring-2 focus:ring-inset focus:ring-[--ring]',
]),
});
/* -------------------------------------------------------------------------------------------------
* AppSidebar
* -----------------------------------------------------------------------------------------------*/
export type AppSidebarProps = React.ComponentPropsWithoutRef<'div'> &
ComponentAnatomy<typeof AppSidebarAnatomy> & {
mobileDrawerProps?: Partial<DrawerProps>;
};
export const AppSidebar = React.forwardRef<HTMLDivElement, AppSidebarProps>(
(props, ref) => {
const { children, className, ...rest } = props;
const ctx = React.useContext(__AppSidebarContext);
return (
<>
<div
ref={ref}
className={cn(
AppSidebarAnatomy.sidebar(),
// process.env.NEXT_PUBLIC_PLATFORM === "desktop" && "pt-4",
className
)}
{...rest}
>
{children}
</div>
<Drawer
open={ctx.open}
onOpenChange={(v) => ctx.setOpen(v)}
side="left"
>
{children}
</Drawer>
</>
);
}
);
AppSidebar.displayName = 'AppSidebar';
/* -------------------------------------------------------------------------------------------------
* AppSidebarTrigger
* -----------------------------------------------------------------------------------------------*/
export type AppSidebarTriggerProps = React.ComponentPropsWithoutRef<'button'> &
ComponentAnatomy<typeof AppSidebarTriggerAnatomy>;
export const AppSidebarTrigger = React.forwardRef<
HTMLButtonElement,
AppSidebarTriggerProps
>((props, ref) => {
const { children, className, ...rest } = props;
const ctx = React.useContext(__AppSidebarContext);
return (
<button
ref={ref}
className={cn(AppSidebarTriggerAnatomy.trigger(), className)}
onClick={() => ctx.setOpen(!ctx.open)}
{...rest}
>
<span className="sr-only">Open main menu</span>
{ctx.open ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="block h-6 w-6"
>
<line x1="18" x2="6" y1="6" y2="18"></line>
<line x1="6" x2="18" y1="6" y2="18"></line>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="block h-6 w-6"
>
<line x1="4" x2="20" y1="12" y2="12"></line>
<line x1="4" x2="20" y1="6" y2="6"></line>
<line x1="4" x2="20" y1="18" y2="18"></line>
</svg>
)}
</button>
);
});
AppSidebarTrigger.displayName = 'AppSidebarTrigger';
/* -------------------------------------------------------------------------------------------------
* AppSidebarProvider
* -----------------------------------------------------------------------------------------------*/
export type AppSidebarProviderProps = {
children?: React.ReactNode;
open?: boolean;
onOpenChange?: (open: boolean) => void;
onSizeChange?: (
size: VariantProps<typeof AppLayoutAnatomy.root>['sidebarSize']
) => void;
};
export const AppSidebarProvider: React.FC<AppSidebarProviderProps> = ({
children,
onOpenChange,
onSizeChange,
}) => {
const [open, setOpen] = React.useState(false);
const [size, setSize] =
React.useState<VariantProps<typeof AppLayoutAnatomy.root>['sidebarSize']>(
undefined
);
const [isBelowBreakpoint, setIsBelowBreakpoint] =
React.useState<boolean>(false);
React.useEffect(() => {
const handleResize = () => setIsBelowBreakpoint(window.innerWidth < 1024); // lg breakpoint
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, [isBelowBreakpoint]);
return (
<__AppSidebarContext.Provider
value={{
open,
setOpen: (open: boolean) => {
onOpenChange?.(open);
setOpen(open);
},
setSize: (
size: VariantProps<typeof AppLayoutAnatomy.root>['sidebarSize']
) => {
onSizeChange?.(size);
setSize(size);
},
size: size,
isBelowBreakpoint,
}}
>
{children}
</__AppSidebarContext.Provider>
);
};
AppSidebarProvider.displayName = 'AppSidebarProvider';
|