-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee25dcf
commit 7bc24ee
Showing
26 changed files
with
2,085 additions
and
1,096 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,3 @@ | ||
export type { | ||
SidebarOptions, | ||
SidebarOverlayProps, | ||
SidebarProps, | ||
SidebarSectionProps, | ||
SidebarToggleButtonProps, | ||
} from './sidebar-types' | ||
export { | ||
Sidebar, | ||
SidebarOverlay, | ||
SidebarSection, | ||
SidebarToggleButton, | ||
} from './sidebar' | ||
export { Nav } from './nav' | ||
export type { NavProps } from './nav' | ||
export { NavGroup, NavGroupContent, NavGroupTitle } from './nav-group' | ||
export type { NavGroupProps, NavGroupTitleProps } from './nav-group' | ||
export { NavItem, NavItemLabel } from './nav-item' | ||
export type { NavItemLabelProps, NavItemProps } from './nav-item' | ||
export { | ||
NavGroupStylesProvider, | ||
NavItemStylesProvider, | ||
useNavGroupStyles, | ||
useNavItemStyles, | ||
} from './nav-context' | ||
export { SidebarStylesProvider, useSidebarStyles } from './sidebar-context' | ||
export { | ||
SidebarProvider, | ||
useSidebarContext, | ||
useSidebarToggleButton, | ||
} from './use-sidebar' | ||
export type { UseSidebarReturn } from './use-sidebar' | ||
export * as Sidebar from './namespace' | ||
|
||
export { useSidebar } from './sidebar.context' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export { | ||
SidebarProvider as Provider, | ||
SidebarRoot as Root, | ||
SidebarTrigger as Trigger, | ||
SidebarBackdrop as Backdrop, | ||
SidebarSection as Section, | ||
SidebarHeader as Header, | ||
SidebarBody as Body, | ||
SidebarFooter as Footer, | ||
SidebarTrack as Track, | ||
} from './sidebar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useCallback, useMemo, useState } from 'react' | ||
|
||
import { | ||
UseDisclosureReturn, | ||
createContext, | ||
useBreakpointValue, | ||
useDisclosure, | ||
} from '@chakra-ui/react' | ||
|
||
import type { HTMLSystemProps } from '#system' | ||
|
||
import { callAll } from '../utils/call-all.ts' | ||
import { getBreakpoints } from './sidebar-utils.ts' | ||
import type { SidebarProps } from './sidebar.types.ts' | ||
|
||
export interface UseSidebarReturn { | ||
open: boolean | ||
setOpen: (open: boolean) => void | ||
toggle: () => void | ||
isMobile?: boolean | ||
openMobile?: boolean | ||
setOpenMobile: (open: boolean) => void | ||
breakpoints?: boolean | ||
} | ||
|
||
const [SidebarContextProvider, useSidebar] = createContext<UseSidebarReturn>({ | ||
name: 'SidebarProvider', | ||
strict: true, | ||
}) | ||
|
||
export interface SidebarProviderProps extends SidebarProps { | ||
/** | ||
* Define the breakpoint for the mobile nav. Use `false` to disable the mobile nav. | ||
* | ||
* @default "lg" | ||
*/ | ||
toggleBreakpoint?: false | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | ||
/** | ||
* Control the default visibility of the sidebar. | ||
*/ | ||
defaultOpen?: boolean | ||
/** | ||
* Control the visibility of the sidebar. | ||
*/ | ||
open?: boolean | ||
/** | ||
* Callback invoked when the sidebar is opened. | ||
*/ | ||
onOpenChange?: (open: boolean) => void | ||
/** | ||
* The content of the sidebar. | ||
*/ | ||
children: React.ReactNode | ||
} | ||
|
||
export function SidebarProvider(props: SidebarProviderProps) { | ||
const { children, toggleBreakpoint, defaultOpen, open, onOpenChange } = props | ||
|
||
const isMobile = useBreakpointValue(getBreakpoints(toggleBreakpoint), { | ||
fallback: undefined, | ||
}) | ||
|
||
const [openMobile, setOpenMobile] = useState(false) | ||
|
||
const disclosure = useDisclosure({ | ||
defaultOpen: isMobile ? openMobile : defaultOpen, | ||
open: isMobile ? openMobile : open, | ||
onOpen: () => { | ||
isMobile ? setOpenMobile(true) : onOpenChange?.(true) | ||
}, | ||
onClose: () => (isMobile ? setOpenMobile(false) : onOpenChange?.(false)), | ||
}) | ||
|
||
const context = useMemo(() => { | ||
return { | ||
open: disclosure.open, | ||
setOpen: (open: boolean = true) => | ||
open ? disclosure.onOpen() : disclosure.onClose(), | ||
toggle: disclosure.onToggle, | ||
isMobile, | ||
openMobile, | ||
setOpenMobile, | ||
} | ||
}, [disclosure, toggleBreakpoint, isMobile, openMobile]) | ||
|
||
return ( | ||
<SidebarContextProvider value={context}>{children}</SidebarContextProvider> | ||
) | ||
} | ||
|
||
export { useSidebar } | ||
|
||
export const useSidebarTrigger = () => { | ||
const context = useSidebar() | ||
|
||
const getButtonProps = (props: HTMLSystemProps<'button'>) => { | ||
return { | ||
onClick: callAll(context?.toggle, props?.onClick), | ||
'aria-label': context.open ? 'Close sidebar' : 'Open sidebar', | ||
'data-state': context.open ? 'open' : 'closed', | ||
} | ||
} | ||
|
||
return { | ||
open: context?.open, | ||
isMobile: context?.isMobile, | ||
getButtonProps, | ||
} | ||
} |
Oops, something went wrong.