1
0
Fork 0
docsearch/packages/docsearch-react/src/useTheme.tsx
NiteshSingh17 85568ce095
feat: add theme prop (#2651)
Co-authored-by: Dylan Tientcheu <dylantientcheu@gmail.com>
2025-07-08 20:05:25 +02:00

23 lines
661 B
TypeScript

import { useEffect } from 'react';
import type { DocSearchTheme } from './types';
export interface UseThemeProps {
theme?: DocSearchTheme;
}
export const useTheme = ({ theme }: UseThemeProps): void => {
useEffect(() => {
if (theme) {
const previousTheme = document.documentElement.dataset.theme;
if (theme !== previousTheme) {
document.documentElement.dataset.theme = theme;
return (): void => {
if (previousTheme === undefined) delete document.documentElement.dataset.theme;
else document.documentElement.dataset.theme = previousTheme;
};
}
}
return undefined;
}, [theme]);
};