forked from hatchet-dev/hatchet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(dashboard): collapsible sidebar
Showing
5 changed files
with
112 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
type SidebarState = 'open' | 'closed'; | ||
|
||
type SidebarProviderProps = PropsWithChildren & { | ||
defaultSidebarOpen?: SidebarState; | ||
}; | ||
|
||
type SidebarProviderState = { | ||
sidebarOpen: SidebarState; | ||
setSidebarOpen: (open: SidebarState) => void; | ||
toggleSidebarOpen: () => void; | ||
}; | ||
|
||
const initialState: SidebarProviderState = { | ||
sidebarOpen: 'closed', | ||
setSidebarOpen: () => null, | ||
toggleSidebarOpen: () => null, | ||
}; | ||
|
||
const SidebarProviderContext = | ||
createContext<SidebarProviderState>(initialState); | ||
|
||
export function SidebarProvider({ | ||
children, | ||
defaultSidebarOpen = 'closed', | ||
|
||
...props | ||
}: SidebarProviderProps) { | ||
const [sidebarOpen, setSidebarOpen] = useState<SidebarState>( | ||
() => defaultSidebarOpen, | ||
); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
if (window.innerWidth >= 768) { | ||
console.log('open'); | ||
setSidebarOpen('open'); | ||
} else { | ||
console.log('closed'); | ||
setSidebarOpen('closed'); | ||
} | ||
}; | ||
|
||
handleResize(); | ||
|
||
window.addEventListener('resize', handleResize); | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<SidebarProviderContext.Provider | ||
{...props} | ||
value={{ | ||
sidebarOpen, | ||
setSidebarOpen: (open: SidebarState) => { | ||
setSidebarOpen(open); | ||
}, | ||
toggleSidebarOpen: () => { | ||
setSidebarOpen((state) => (state === 'open' ? 'closed' : 'open')); | ||
}, | ||
}} | ||
> | ||
{children} | ||
</SidebarProviderContext.Provider> | ||
); | ||
} | ||
|
||
export const useSidebar = () => { | ||
const context = useContext(SidebarProviderContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('useSidebar must be used within a SidebarProvider'); | ||
} | ||
|
||
return 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
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