26 lines
805 B
TypeScript
26 lines
805 B
TypeScript
import { createContext, useContext, useLayoutEffect } from "react";
|
|
|
|
export type AppShellSidebarControls = {
|
|
collapseSidebar: () => void;
|
|
collapseSidebarIfPreferred: () => void;
|
|
};
|
|
|
|
const NOOP_CONTROLS: AppShellSidebarControls = {
|
|
collapseSidebar: () => undefined,
|
|
collapseSidebarIfPreferred: () => undefined,
|
|
};
|
|
|
|
export const AppShellSidebarContext = createContext<AppShellSidebarControls>(NOOP_CONTROLS);
|
|
|
|
export function useAppShellSidebar(): AppShellSidebarControls {
|
|
return useContext(AppShellSidebarContext);
|
|
}
|
|
|
|
export function useRecommendedSidebarCollapse(workspaceKey: string | null): void {
|
|
const { collapseSidebarIfPreferred } = useAppShellSidebar();
|
|
|
|
useLayoutEffect(() => {
|
|
if (workspaceKey) collapseSidebarIfPreferred();
|
|
}, [collapseSidebarIfPreferred, workspaceKey]);
|
|
}
|