1- import { Fragment , useEffect } from 'react' ;
1+ import { useEffect } from 'react' ;
2+ import { usePathname } from 'next/navigation' ;
23
34import { Button } from '@keystar/ui/button' ;
45import { menuIcon } from '@keystar/ui/icon/icons/menuIcon' ;
@@ -15,67 +16,55 @@ import { ColorSchemeMenu } from '../theme-switcher';
1516
1617/** Responsively render sidebar navigation items. */
1718export const Sidebar = ( { items } : { items : SidebarItem [ ] } ) => {
18- // const [headerHeight, setHeaderHeight] = useState(NaN);
1919 const { sidebarIsOpen, closeSidebar, toggleSidebar } = useSidebarContext ( ) ;
20+ const pathname = usePathname ( ) ;
2021
2122 useEffect ( ( ) => {
22- addEventListener ( 'popstate' , closeSidebar ) ;
23- return ( ) => {
24- removeEventListener ( 'popstate' , closeSidebar ) ;
25- } ;
26- } , [ closeSidebar ] ) ;
23+ // Close the sidebar when the pathname changes
24+ closeSidebar ( ) ;
25+ } , [ closeSidebar , pathname ] ) ;
2726
2827 return (
29- < Fragment >
30- < div
31- data-open = { sidebarIsOpen }
32- className = { css ( {
33- backgroundColor : tokenSchema . color . background . canvas ,
34- display : 'flex' ,
35- flexDirection : 'column' ,
36- position : 'fixed' ,
37- zIndex : 1 ,
28+ < div
29+ data-open = { sidebarIsOpen }
30+ className = { css ( {
31+ display : 'flex' ,
32+ flexDirection : 'column' ,
33+ position : 'fixed' ,
34+ width : '100%' ,
35+ zIndex : 1 ,
36+ '&[data-open=true]' : {
37+ height : '100%' ,
38+ } ,
39+ [ breakpointQueries . above . mobile ] : {
40+ height : '100%' ,
41+ width : SIDEBAR_WIDTH ,
42+ } ,
43+ } ) }
44+ >
45+ < SidebarHeader menuIsOpen = { sidebarIsOpen } onMenuPress = { toggleSidebar } />
3846
47+ < Box
48+ flex
49+ minHeight = { 0 }
50+ backgroundColor = "surface"
51+ borderTopEndRadius = { { tablet : 'medium' } }
52+ overflow = "hidden auto"
53+ paddingBottom = "xlarge"
54+ paddingTop = { { mobile : 'large' , tablet : 'xlarge' } }
55+ paddingEnd = { { mobile : 'large' , tablet : 'xlarge' } }
56+ data-open = { sidebarIsOpen }
57+ UNSAFE_className = { css ( {
3958 [ breakpointQueries . below . tablet ] : {
40- width : '100vw' ,
41- '[data-open=true]' : {
42- height : '100%' ,
59+ '&[data-open=false]' : {
60+ display : 'none' ,
4361 } ,
4462 } ,
45- [ breakpointQueries . above . mobile ] : {
46- height : '100%' ,
47- width : SIDEBAR_WIDTH ,
48- } ,
4963 } ) }
5064 >
51- < SidebarHeader
52- menuIsOpen = { sidebarIsOpen }
53- onMenuPress = { toggleSidebar }
54- // onLayout={rect => setHeaderHeight(rect.height)}
55- />
56-
57- < Box
58- flex
59- backgroundColor = "surface"
60- borderTopEndRadius = { { tablet : 'medium' } }
61- overflow = "hidden auto"
62- paddingBottom = "xlarge"
63- paddingTop = { { mobile : 'large' , tablet : 'xlarge' } }
64- paddingEnd = { { mobile : 'large' , tablet : 'xlarge' } }
65- data-open = { sidebarIsOpen }
66- UNSAFE_className = { css ( {
67- [ breakpointQueries . below . tablet ] : {
68- '&[data-open=false]' : {
69- display : 'none' ,
70- } ,
71- } ,
72- } ) }
73- >
74- < NavItems items = { items } />
75- </ Box >
76- </ div >
77- { /* <Box height={headerHeight} isHidden={{ above: 'mobile' }} /> */ }
78- </ Fragment >
65+ < NavItems items = { items } />
66+ </ Box >
67+ </ div >
7968 ) ;
8069} ;
8170
@@ -84,11 +73,9 @@ export const Sidebar = ({ items }: { items: SidebarItem[] }) => {
8473
8574function SidebarHeader ( {
8675 menuIsOpen,
87- // onLayout,
8876 onMenuPress,
8977} : {
9078 menuIsOpen : boolean ;
91- // onLayout: (rect: DOMRect) => void;
9279 onMenuPress : ( ) => void ;
9380} ) {
9481 const menuLabel = 'Open navigation panel' ;
0 commit comments