-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rd-375 customize theme via typed provider
- Loading branch information
Showing
7 changed files
with
136 additions
and
57 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/robindoc/src/components/blocks/theme-detector/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from "react"; | ||
|
||
const clientLogic = () => { | ||
const userTheme = localStorage.getItem("theme"); | ||
if (userTheme && ["light", "dark"].includes(userTheme)) { | ||
document.documentElement.classList.add(`theme-${userTheme}`); | ||
} else { | ||
document.documentElement.classList.add(`theme-system`); | ||
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
document.documentElement.classList.add("theme-dark"); | ||
} else { | ||
document.documentElement.classList.add("theme-light"); | ||
} | ||
} | ||
|
||
const store = localStorage.getItem("r-tabs"); | ||
const items = store?.split(";").filter((item) => item && /[\w-]+=[\w]+/.test(item)) || []; | ||
const classNames = Array.from(document.documentElement.classList); | ||
classNames.forEach((className) => { | ||
if (className.startsWith(`r-tabs-global`)) { | ||
document.documentElement.classList.remove(className); | ||
} | ||
}); | ||
items.forEach((item) => { | ||
const [tabsKey, tab] = item.split("="); | ||
document.documentElement.classList.add(`r-tabs-global__${tabsKey}`, `r-tabs-global__${tabsKey}_${tab}`); | ||
}); | ||
}; | ||
|
||
export const ThemeDetector: React.FC = () => ( | ||
<script | ||
id="detect-theme" | ||
dangerouslySetInnerHTML={{ | ||
__html: `(${clientLogic})()`, | ||
}} | ||
async | ||
/> | ||
); |
26 changes: 26 additions & 0 deletions
26
packages/robindoc/src/components/blocks/theme-styles/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react"; | ||
|
||
import { type Theme } from "@src/core/types/theme"; | ||
|
||
export interface ThemeStylesProps { | ||
theme: Theme; | ||
} | ||
|
||
export const ThemeStyles: React.FC<ThemeStylesProps> = ({ theme }) => { | ||
const { colors = {} } = theme; | ||
|
||
const colorsStyles = Object.entries(colors).reduce<string[]>((acc, [type, variants]) => { | ||
Object.entries(variants).forEach(([key, color]) => { | ||
acc.push(`--${type}-${key}:${color};`); | ||
}); | ||
return acc; | ||
}, []); | ||
|
||
return ( | ||
<style | ||
dangerouslySetInnerHTML={{ | ||
__html: `:root{${colorsStyles.join("")}}`, | ||
}} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 13 additions & 39 deletions
52
packages/robindoc/src/components/elements/robin-provider/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,18 @@ | ||
import React from "react"; | ||
|
||
import { type Theme } from "@src/core/types/theme"; | ||
import { NavigateProvider } from "@src/components/contexts/navigate/provider"; | ||
import { ThemeStyles } from "@src/components/blocks/theme-styles"; | ||
import { ThemeDetector } from "@src/components/blocks/theme-detector"; | ||
|
||
const clientLogic = () => { | ||
const userTheme = localStorage.getItem("theme"); | ||
if (userTheme && ["light", "dark"].includes(userTheme)) { | ||
document.documentElement.classList.add(`theme-${userTheme}`); | ||
} else { | ||
document.documentElement.classList.add(`theme-system`); | ||
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
document.documentElement.classList.add("theme-dark"); | ||
} else { | ||
document.documentElement.classList.add("theme-light"); | ||
} | ||
} | ||
interface RobinProviderProps { | ||
theme?: Theme; | ||
} | ||
|
||
const store = localStorage.getItem("r-tabs"); | ||
const items = store?.split(";").filter((item) => item && /[\w-]+=[\w]+/.test(item)) || []; | ||
const classNames = Array.from(document.documentElement.classList); | ||
classNames.forEach((className) => { | ||
if (className.startsWith(`r-tabs-global`)) { | ||
document.documentElement.classList.remove(className); | ||
} | ||
}); | ||
items.forEach((item) => { | ||
const [tabsKey, tab] = item.split("="); | ||
document.documentElement.classList.add(`r-tabs-global__${tabsKey}`, `r-tabs-global__${tabsKey}_${tab}`); | ||
}); | ||
}; | ||
|
||
export const RobinProvider: React.FC<React.PropsWithChildren> = ({ children }) => { | ||
return ( | ||
<> | ||
<script | ||
id="detect-theme" | ||
dangerouslySetInnerHTML={{ | ||
__html: `(${clientLogic})()`, | ||
}} | ||
/> | ||
<NavigateProvider>{children}</NavigateProvider> | ||
</> | ||
); | ||
}; | ||
export const RobinProvider: React.FC<React.PropsWithChildren<RobinProviderProps>> = ({ children, theme }) => ( | ||
<> | ||
{theme && <ThemeStyles theme={theme} />} | ||
<ThemeDetector /> | ||
<NavigateProvider>{children}</NavigateProvider> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type ThemeColors = { | ||
accent?: { | ||
"50": string; | ||
"100": string; | ||
"200": string; | ||
"300": string; | ||
"400": string; | ||
"500": string; | ||
"600": string; | ||
"700": string; | ||
"800": string; | ||
"900": string; | ||
"950": string; | ||
}; | ||
link?: { | ||
base: string; | ||
"base-hovered": string; | ||
visited: string; | ||
"visited-hovered": string; | ||
active: string; | ||
}; | ||
}; | ||
|
||
export type Theme = { | ||
colors?: ThemeColors; | ||
}; |