From 46c431b1e1c519e566bb98d8b1b351355f67549b Mon Sep 17 00:00:00 2001 From: MrTRyy Date: Sun, 2 Jun 2024 19:07:48 +0200 Subject: [PATCH 01/59] refactor: Update design functions and components for theme color generation --- lib/index.ts | 3 ++- .../generateThemeColorSteps.ts} | 2 +- .../designFunctions/generateThemeColorSteps/index.ts | 1 + .../validateThemeColorSteps.ts} | 2 +- .../theme/FancyThemeProvider/FancyThemeProvider.tsx | 6 +++--- .../TgenerateObjectToCssVariables.model.ts} | 0 .../generateObjectToCssVariables.mdx} | 10 +++++----- .../generateObjectToCssVariables.ts} | 6 +++--- .../theme/generateObjectToCssVariables/index.ts | 3 +++ .../theme/generateThemeColor/generateThemeColor.ts | 12 ++++++------ src/design/theme/generateThemeToCssVars/index.ts | 3 --- src/stories/Setup/SetupNext.mdx | 4 ++-- 12 files changed, 27 insertions(+), 25 deletions(-) rename src/design/{theme/generateThemeColor/utils/generateColorSteps.ts => designFunctions/generateThemeColorSteps/generateThemeColorSteps.ts} (95%) create mode 100644 src/design/designFunctions/generateThemeColorSteps/index.ts rename src/design/{theme/generateThemeColor/utils/validateColorSteps.ts => designFunctions/generateThemeColorSteps/validateThemeColorSteps.ts} (83%) rename src/design/theme/{generateThemeToCssVars/TgenerateThemeToCssVars.model.ts => generateObjectToCssVariables/TgenerateObjectToCssVariables.model.ts} (100%) rename src/design/theme/{generateThemeToCssVars/generateThemeToCssVars.mdx => generateObjectToCssVariables/generateObjectToCssVariables.mdx} (55%) rename src/design/theme/{generateThemeToCssVars/generateThemeToCssVars.ts => generateObjectToCssVariables/generateObjectToCssVariables.ts} (65%) create mode 100644 src/design/theme/generateObjectToCssVariables/index.ts delete mode 100644 src/design/theme/generateThemeToCssVars/index.ts diff --git a/lib/index.ts b/lib/index.ts index 73abd36dc..93e4cad01 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -182,6 +182,7 @@ export * from '@/design/designFunctions/leftRightCenterToFlexJustify'; export * from '@/design/designFunctions/getThemeOrValueAsCss'; export * from '@/design/designFunctions/calcBorderRadiusWithPadding'; export * from '@/design/theme/theme'; +export * from '@/design/designFunctions/generateThemeColorSteps'; // ---------- Interfaces ------- // export * from '@/types'; @@ -198,7 +199,7 @@ export * from '@/components/atoms/RawSlider/TRawSlider.model'; // ---------- Store ------- // export * from '@/design/theme/themeStore'; export * from '@/design/theme/globalStyle/DefaultStyle'; -export * from '@/design/theme/generateThemeToCssVars'; +export * from '@/design/theme/generateObjectToCssVariables'; export * from '@/design/theme/applyThemeToDomStyleSheet'; // ---------- SVGs ------- // diff --git a/src/design/theme/generateThemeColor/utils/generateColorSteps.ts b/src/design/designFunctions/generateThemeColorSteps/generateThemeColorSteps.ts similarity index 95% rename from src/design/theme/generateThemeColor/utils/generateColorSteps.ts rename to src/design/designFunctions/generateThemeColorSteps/generateThemeColorSteps.ts index ca0c7a208..36b102f3d 100644 --- a/src/design/theme/generateThemeColor/utils/generateColorSteps.ts +++ b/src/design/designFunctions/generateThemeColorSteps/generateThemeColorSteps.ts @@ -59,7 +59,7 @@ interface IGenerateColorSteps { color: string; pimaryColor: string; } -export default function generateColorSteps({ themeType, color, pimaryColor }: IGenerateColorSteps): ColorSteps { +export default function generateThemeColorSteps({ themeType, color, pimaryColor }: IGenerateColorSteps): ColorSteps { const lightColors = lightenColors({ color, themeType, pimaryColor }); //generate the colors const obj: ColorSteps = {} as ColorSteps; diff --git a/src/design/designFunctions/generateThemeColorSteps/index.ts b/src/design/designFunctions/generateThemeColorSteps/index.ts new file mode 100644 index 000000000..fe11be7b3 --- /dev/null +++ b/src/design/designFunctions/generateThemeColorSteps/index.ts @@ -0,0 +1 @@ +export { default as generateThemeColorSteps } from './generateThemeColorSteps'; diff --git a/src/design/theme/generateThemeColor/utils/validateColorSteps.ts b/src/design/designFunctions/generateThemeColorSteps/validateThemeColorSteps.ts similarity index 83% rename from src/design/theme/generateThemeColor/utils/validateColorSteps.ts rename to src/design/designFunctions/generateThemeColorSteps/validateThemeColorSteps.ts index 2e8e0530c..fde5d0169 100644 --- a/src/design/theme/generateThemeColor/utils/validateColorSteps.ts +++ b/src/design/designFunctions/generateThemeColorSteps/validateThemeColorSteps.ts @@ -1,4 +1,4 @@ -export default function validateColorSteps(colorSteps: object) { +export default function validateThemeColorSteps(colorSteps: object) { const colorStepsKeys = Object.keys(colorSteps); if (colorStepsKeys.length !== 10) return false; diff --git a/src/design/theme/FancyThemeProvider/FancyThemeProvider.tsx b/src/design/theme/FancyThemeProvider/FancyThemeProvider.tsx index 3e7f7f427..596aa1961 100644 --- a/src/design/theme/FancyThemeProvider/FancyThemeProvider.tsx +++ b/src/design/theme/FancyThemeProvider/FancyThemeProvider.tsx @@ -4,7 +4,7 @@ import { ThemeProvider } from 'styled-components'; import { themeStore } from '@/design/theme/themeStore'; import { TFancyThemeProvider } from './TFancyThemeProvider.model'; -import { generateCssVariables } from '@/design/theme/generateThemeToCssVars'; +import { generateObjectToCssVariables } from '@/design/theme/generateObjectToCssVariables'; import { applyThemeToDomStyleSheet } from '@/design/theme/applyThemeToDomStyleSheet'; // --------------------------------------------------------------------------- // @@ -27,7 +27,7 @@ export default function FancyThemeProvider(props: TFancyThemeProvider) { useEffect(() => { // if the theme is not initialized we don't need to apply the css vars if (!applyCssVars && !isInitialized) return; - const cssVars = generateCssVariables(themeState); + const cssVars = generateObjectToCssVariables(themeState); applyThemeToDomStyleSheet(cssVars); }, [theme, themeState, applyCssVars, isInitialized]); @@ -37,7 +37,7 @@ export default function FancyThemeProvider(props: TFancyThemeProvider) { const getStyle = document.getElementById('fui-theme'); if (!getStyle) return; getStyle.innerHTML = `:root { - ${generateCssVariables(themeState)} + ${generateObjectToCssVariables(themeState)} }`; }, [themeState, isDarkMode, applyCssVars]); diff --git a/src/design/theme/generateThemeToCssVars/TgenerateThemeToCssVars.model.ts b/src/design/theme/generateObjectToCssVariables/TgenerateObjectToCssVariables.model.ts similarity index 100% rename from src/design/theme/generateThemeToCssVars/TgenerateThemeToCssVars.model.ts rename to src/design/theme/generateObjectToCssVariables/TgenerateObjectToCssVariables.model.ts diff --git a/src/design/theme/generateThemeToCssVars/generateThemeToCssVars.mdx b/src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.mdx similarity index 55% rename from src/design/theme/generateThemeToCssVars/generateThemeToCssVars.mdx rename to src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.mdx index ac229fca0..793a911f5 100644 --- a/src/design/theme/generateThemeToCssVars/generateThemeToCssVars.mdx +++ b/src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.mdx @@ -1,15 +1,15 @@ ### Component Title -`generateCssVariables` Function +`generateObjectToCssVariables` Function ### Brief Description -The `generateCssVariables` function dynamically generates CSS variables from a given theme object. It's designed to work recursively, allowing nested theme objects. This function facilitates the application of theming in CSS by converting theme properties into CSS custom properties (variables) with an optional prefix to namespace them, making it easier to maintain and switch themes dynamically in web projects. +The `generateObjectToCssVariables` function dynamically generates CSS variables from a given theme object. It's designed to work recursively, allowing nested theme objects. This function facilitates the application of theming in CSS by converting theme properties into CSS custom properties (variables) with an optional prefix to namespace them, making it easier to maintain and switch themes dynamically in web projects. ### Setup Instructions 1. Ensure you have a theme object that conforms to the `TThemeObject` type, which can include nested objects for structured themes. -2. Import the `generateCssVariables` function into your project file where you wish to generate CSS variables. +2. Import the `generateObjectToCssVariables` function into your project file where you wish to generate CSS variables. ```javascript import { fui-fancyui } from 'fui-fancyui'; @@ -31,7 +31,7 @@ const theme = { }, }; -const cssVariablesString = generateCssVariables(theme); +const cssVariablesString = generateObjectToCssVariables(theme); // Use the `cssVariablesString` to set CSS custom properties in your application ``` @@ -42,4 +42,4 @@ This example demonstrates how to convert a theme object with nested color and ba - **theme (`TThemeObject`)**: The theme object to convert into CSS variables. It must follow the `TThemeObject` structure, allowing nested objects for hierarchical themes. This is a required property. - **prefix (`string`)**: An optional prefix string to prepend to each CSS variable name, used for namespacing. Default is an empty string (`''`). -This documentation outlines the `generateCssVariables` function's purpose, setup, usage, and its components, providing a clear overview of how to dynamically generate CSS variables from a theme object for web development projects. +This documentation outlines the `generateObjectToCssVariables` function's purpose, setup, usage, and its components, providing a clear overview of how to dynamically generate CSS variables from a theme object for web development projects. diff --git a/src/design/theme/generateThemeToCssVars/generateThemeToCssVars.ts b/src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.ts similarity index 65% rename from src/design/theme/generateThemeToCssVars/generateThemeToCssVars.ts rename to src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.ts index 66a4dc381..d544d5f87 100644 --- a/src/design/theme/generateThemeToCssVars/generateThemeToCssVars.ts +++ b/src/design/theme/generateObjectToCssVariables/generateObjectToCssVariables.ts @@ -1,14 +1,14 @@ -import { TThemeObject } from './TgenerateThemeToCssVars.model'; +import { TThemeObject } from './TgenerateObjectToCssVariables.model'; // this function generates css variables from a theme object -export default function generateCssVariables(theme: TThemeObject, prefix = '') { +export default function generateObjectToCssVariables(theme: TThemeObject, prefix = '') { let cssVars = ''; // iterate over the theme object Object.entries(theme).forEach(([key, value]) => { if (typeof value === 'object' && value !== null) { // Recursive case: value is an object - cssVars += generateCssVariables(value, `${prefix}${key}-`); + cssVars += generateObjectToCssVariables(value, `${prefix}${key}-`); } else if (typeof value === 'string') { // Base case: value is a string cssVars += `--${prefix}${key}: ${value};`; diff --git a/src/design/theme/generateObjectToCssVariables/index.ts b/src/design/theme/generateObjectToCssVariables/index.ts new file mode 100644 index 000000000..ba141a5bd --- /dev/null +++ b/src/design/theme/generateObjectToCssVariables/index.ts @@ -0,0 +1,3 @@ +export { default as generateObjectToCssVariables } from './generateObjectToCssVariables'; + +export type { TThemeValue, TThemeObject } from './TgenerateObjectToCssVariables.model'; diff --git a/src/design/theme/generateThemeColor/generateThemeColor.ts b/src/design/theme/generateThemeColor/generateThemeColor.ts index 7063de045..729571999 100644 --- a/src/design/theme/generateThemeColor/generateThemeColor.ts +++ b/src/design/theme/generateThemeColor/generateThemeColor.ts @@ -1,8 +1,8 @@ import { TUiColorsTypeObject } from '@/types/TUiColorsTypeObject'; -import generateColorSteps from './utils/generateColorSteps'; +import generateThemeColorSteps from '../../designFunctions/generateThemeColorSteps/generateThemeColorSteps'; import { isColorValid } from '@/utils//validations/isColorValid'; import { TUiColorTypes } from '@/types/TUiColorTypes'; -import validateColorSteps from '@/design/theme/generateThemeColor/utils/validateColorSteps'; +import validateThemeColorSteps from '@/design/designFunctions/generateThemeColorSteps/validateThemeColorSteps'; import { TUiColorsNotTransparent } from '@/types/TUiColorsNotTransparent'; const themeColors = { @@ -28,7 +28,7 @@ export function initialGenerateUiColors() { // generate for each theme color the color steps for (const color in themeColors) { // generate the color steps for the color - const generatedColor = generateColorSteps({ + const generatedColor = generateThemeColorSteps({ themeType: color as TUiColorTypes, color: themeColors[color as TUiColorsNotTransparent], pimaryColor: themeColors.secondary, @@ -46,7 +46,7 @@ initialGenerateUiColors(); export const regenerateUiColors = (isDarkTheme: boolean) => { for (const color in uiColors) { // generate the color steps for the color - const generatedColor = generateColorSteps({ + const generatedColor = generateThemeColorSteps({ themeType: color as TUiColorTypes, color: uiColors[color as TUiColorsNotTransparent]['0'], pimaryColor: isDarkTheme ? themeColors.primary : themeColors.secondary, // if the theme is dark, the primary color is the secondary color @@ -85,7 +85,7 @@ export const updateThemeColors = (colorObject: IUiColorPops) => { // accentDarken should not provided by the user% if (colorType !== 'accentDarken' && colorObject[colorTypedkey] !== undefined) { // update the theme color with the new color - const generatedColorSteps = generateColorSteps({ + const generatedColorSteps = generateThemeColorSteps({ themeType: colorTypedkey, color: colorObject[colorTypedkey]! as string, pimaryColor: themeColors.secondary, @@ -101,7 +101,7 @@ export const updateThemeColors = (colorObject: IUiColorPops) => { //Validate ColorSteps object (check if the object has the correct keys) // and then update the themeColor with the new color steps - if (validateColorSteps(colorSteps)) { + if (validateThemeColorSteps(colorSteps)) { uiColors[colorTypedkey] = colorSteps as TthemeColorGroup; } } diff --git a/src/design/theme/generateThemeToCssVars/index.ts b/src/design/theme/generateThemeToCssVars/index.ts deleted file mode 100644 index 09fb65cd5..000000000 --- a/src/design/theme/generateThemeToCssVars/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { default as generateCssVariables } from './generateThemeToCssVars'; - -export type { TThemeValue, TThemeObject } from './TgenerateThemeToCssVars.model'; diff --git a/src/stories/Setup/SetupNext.mdx b/src/stories/Setup/SetupNext.mdx index 84bc03e3d..291c0400f 100644 --- a/src/stories/Setup/SetupNext.mdx +++ b/src/stories/Setup/SetupNext.mdx @@ -97,7 +97,7 @@ This is needed because the CSS variables are not applied on the server-side rend 'use client'; import React, { useRef } from 'react'; -import { generateCssVariables, themeStore } from 'fui-fancyui'; +import { generateObjectToCssVariables, themeStore } from 'fui-fancyui'; import { useServerInsertedHTML } from 'next/navigation'; export default function ThemeObjectToCSSVars({ children }: { children: React.ReactNode }) { @@ -110,7 +110,7 @@ export default function ThemeObjectToCSSVars({ children }: { children: React.Rea useServerInsertedHTML(() => { if (!isStyleInserted.current) { isStyleInserted.current = true; - const cssVars = generateCssVariables(themeState); + const cssVars = generateObjectToCssVariables(themeState); return