-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-context.tsx
More file actions
57 lines (49 loc) · 1.34 KB
/
theme-context.tsx
File metadata and controls
57 lines (49 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use client";
import React, { createContext, useContext } from "react";
import type { DocsJsonConfig } from "./util";
export interface ThemeColors {
primary: string;
light?: string;
dark?: string;
}
export interface DocsTheme {
colors: ThemeColors;
name: string;
theme: string;
}
const DocsThemeContext = createContext<DocsTheme | null>(null);
export interface DocsThemeProviderProps {
config: DocsJsonConfig;
children: React.ReactNode;
}
export function DocsThemeProvider({ config, children }: DocsThemeProviderProps) {
const theme: DocsTheme = {
colors: config.colors,
name: config.name,
theme: config.theme,
};
return (
<DocsThemeContext.Provider value={theme}>
<div
style={{
'--docs-primary': theme.colors.primary,
'--docs-primary-light': theme.colors.light || theme.colors.primary,
'--docs-primary-dark': theme.colors.dark || theme.colors.primary,
} as React.CSSProperties}
>
{children}
</div>
</DocsThemeContext.Provider>
);
}
export function useDocsTheme(): DocsTheme {
const context = useContext(DocsThemeContext);
if (!context) {
throw new Error('useDocsTheme must be used within a DocsThemeProvider');
}
return context;
}
export function useDocsColors(): ThemeColors {
const theme = useDocsTheme();
return theme.colors;
}