-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from dolthub/taylor/t-context
components: Add tailwind provider, better types
- Loading branch information
Showing
16 changed files
with
217 additions
and
111 deletions.
There are no files selected for viewing
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 { IThemeRGB, IThemeVariables } from "../types"; | ||
|
||
// Should be used to apply the theme to the root element of the app. Will use | ||
// base colors in `theme/base/colors.ts` if no overrides are provided. | ||
export default function applyTheme(themeRGB: IThemeRGB) { | ||
const themeObject: IThemeVariables = mapTheme(themeRGB); | ||
const root = document.documentElement; | ||
|
||
Object.keys(themeObject).forEach(v => { | ||
const propertyVal = themeObject[v as keyof IThemeVariables]; | ||
const validation = validateRGB(propertyVal); | ||
if (!validation) { | ||
throw new Error(`Invalid RGB value for ${v}: ${propertyVal}`); | ||
} | ||
|
||
root.style.setProperty(v, propertyVal); | ||
}); | ||
} | ||
|
||
function mapTheme(rgb: IThemeRGB): IThemeVariables { | ||
return { | ||
"--color-primary": rgb["rgb-primary"] ?? "", | ||
"--color-acc-1": rgb["rgb-acc-1"] ?? "", | ||
"--color-background-acc-1": rgb["rgb-background-acc-1"] ?? "", | ||
"--color-background-acc-start": rgb["rgb-background-acc-start"] ?? "", | ||
"--color-button-1": rgb["rgb-button-1"] ?? "", | ||
"--color-link-1": rgb["rgb-link-1"] ?? "", | ||
"--color-button-2": rgb["rgb-button-2"] ?? "", | ||
"--color-link-2": rgb["rgb-link-2"] ?? "", | ||
"--color-link-light": rgb["rgb-link-light"] ?? "", | ||
}; | ||
} | ||
|
||
function validateRGB(rgb: string): boolean { | ||
if (!rgb) return true; | ||
const rgbRegex = /^(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})$/; | ||
return rgbRegex.test(rgb); | ||
} |
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,58 @@ | ||
import React, { createContext, useContext, useEffect } from "react"; | ||
import { baseColorVariableValues } from "../theme/base/colors"; | ||
import { IThemeColors, IThemeRGB } from "../types"; | ||
import applyTheme from "./applyTheme"; | ||
import { rgbToHex } from "./utils"; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
themeRGBOverrides?: IThemeRGB; | ||
}; | ||
|
||
type ThemeContextType = { | ||
themeRGB: IThemeRGB; | ||
convertThemeRGBToHex: () => IThemeColors; | ||
}; | ||
|
||
const ThemeContext = createContext<ThemeContextType>({ | ||
themeRGB: baseColorVariableValues, | ||
convertThemeRGBToHex: () => { | ||
return {}; | ||
}, | ||
}); | ||
|
||
// ThemedProvider applies tailwind theme default, potentially with provided | ||
// overrides. Must wrap your app root in this provider to use this library. | ||
export default function ThemeProvider(props: Props) { | ||
const themeRGB: IThemeRGB = { | ||
...baseColorVariableValues, | ||
...(props.themeRGBOverrides ?? {}), | ||
}; | ||
|
||
useEffect(() => { | ||
applyTheme(themeRGB); | ||
}, []); | ||
|
||
// Converts the theme from RGB to Hex | ||
function convertThemeRGBToHex(): IThemeColors { | ||
const hexThemeRGB: IThemeColors = {}; | ||
Object.keys(themeRGB).forEach(key => { | ||
const rgb = themeRGB[key as keyof IThemeRGB]; | ||
if (rgb) { | ||
const color = key.replace("rgb-", ""); | ||
hexThemeRGB[color as keyof IThemeColors] = rgbToHex(rgb); | ||
} | ||
}); | ||
return hexThemeRGB; | ||
} | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ themeRGB, convertThemeRGBToHex }}> | ||
{props.children} | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
|
||
export function useThemeContext(): ThemeContextType { | ||
return useContext(ThemeContext); | ||
} |
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,9 @@ | ||
export function rgbToHex(rgb: string): string { | ||
const [r, g, b] = rgb.split(", ").map(Number); | ||
return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`; | ||
} | ||
|
||
function componentToHex(c: number): string { | ||
const hex = c.toString(16); | ||
return hex.length === 1 ? `0${hex}` : hex; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export const colors = { | ||
"ld-pink": "#fc42c9", | ||
"ld-darkblue": "#010a40", | ||
"ld-pink": "#fc42c9", | ||
|
||
"acc-altlightgreen": "#defbe4", | ||
"acc-lightred": "#ffeaec", | ||
"acc-darkblue": "#183362", | ||
"acc-light-text": "#999db3", | ||
"acc-lightred": "#ffeaec", | ||
}; |
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
Oops, something went wrong.