Hooks
Functions
useTheme
A hook for managing and accessing theme-related states and actions.
Example
An example showcasing how to use the useTheme hook to toggle modes, change directions, and adjust layout settings.
'use client'
import useTheme from '@/hooks/useTheme'
import { MODE_DARK, MODE_LIGHT } from '@/constants/theme.constant'
import { Direction, LayoutType } from '@/@types/theme'
const Component = () => {
const {
mode,
direction,
layout,
setMode,
setDirection,
setLayout,
setSideNavCollapse,
setPanelExpand,
} = useTheme((state) => ({
mode: state.mode,
direction: state.direction,
layout: state.layout.type,
setMode: state.setMode,
setDirection: state.setDirection,
setLayout: state.setLayout,
setSideNavCollapse: state.setSideNavCollapse,
setPanelExpand: state.setPanelExpand,
}));
const toggleThemeMode = () => {
setMode(mode === MODE_LIGHT ? MODE_DARK : MODE_LIGHT);
};
const toggleDirection = () => {
setDirection(direction === 'ltr' ? 'rtl' : 'ltr');
};
const changeLayout = (layoutType: LayoutType) => {
setLayout(layoutType);
};
const toggleSideNav = () => {
setSideNavCollapse(true); // Collapse the side navigation
};
const togglePanelExpand = () => {
setPanelExpand(true); // Expand the panel
};
return (...)
}
| return | Description | Type | Default |
|---|---|---|---|
| setSchema | Updates the current theme schema and applies the new schema. | (schema: string) => void | - |
| setMode | Sets the theme mode (e.g., light or dark) and applies it to the document. | (mode: Mode) => void | - |
| setSideNavCollapse | Toggles the collapse state of the side navigation. | (sideNavCollapse: boolean) => void | - |
| setDirection | Sets the text direction of the document (e.g., ltr or rtl). | (direction: Direction) => void | - |
| setPanelExpand | Expands or collapses a panel based on the provided value. | (panelExpand: boolean) => void | - |
| setLayout | Sets the layout type of the application (e.g., vertical, horizontal). | (layout: LayoutType) => void | - |
| mode | The current theme mode (e.g., light or dark). | Mode | - |
| direction | The current text direction (e.g., ltr or rtl). | Direction | - |
| layout | The current layout type of the application. | LayoutType | - |