mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import React, { useContext, useState, useCallback } from 'react';
|
|
import _noop from 'lodash/noop';
|
|
|
|
interface SidebarContextProps {
|
|
showSidebar: boolean;
|
|
switchSidebar: () => void;
|
|
setShowSidebar: React.Dispatch<React.SetStateAction<boolean>>;
|
|
}
|
|
const SidebarContext = React.createContext<SidebarContextProps>({
|
|
showSidebar: true,
|
|
switchSidebar: _noop,
|
|
setShowSidebar: _noop,
|
|
});
|
|
SidebarContext.displayName = 'SidebarContext';
|
|
|
|
export const SidebarContextProvider: React.FC = React.memo((props) => {
|
|
const [showSidebar, setShowSidebar] = useState(true);
|
|
|
|
// 切换
|
|
const switchSidebar = useCallback(() => {
|
|
setShowSidebar(!showSidebar);
|
|
}, [showSidebar]);
|
|
|
|
return (
|
|
<SidebarContext.Provider
|
|
value={{ showSidebar, switchSidebar, setShowSidebar }}
|
|
>
|
|
{props.children}
|
|
</SidebarContext.Provider>
|
|
);
|
|
});
|
|
SidebarContextProvider.displayName = 'SidebarContextProvider';
|
|
|
|
export function useSidebarContext(): SidebarContextProps {
|
|
const context = useContext(SidebarContext);
|
|
|
|
return context;
|
|
}
|