This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 16
chore: adding initial json generated from figma variables #673
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7fc644
chore: adding initial json generated from figma variables
georgewrmarshall b606e70
chore: adding docs to hook
georgewrmarshall e25353e
chore: blue 900 value fix
georgewrmarshall 9428072
chore: updating light and dark theme figma json
georgewrmarshall df18a2f
chore: updating figma json and hook
georgewrmarshall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { getCSSVariablesFromStylesheet } from './getCSSVariablesFromStylesheet'; | ||
export { useJsonColor } from './useJsonColor'; |
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,100 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import figmaBrandColors from '../../src/figma/brandColors.json'; | ||
import figmaDarkTheme from '../../src/figma/darkTheme.json'; | ||
import figmaLightTheme from '../../src/figma/lightTheme.json'; | ||
|
||
export type ColorDetails = { | ||
value: string; // Hex value or alias to another color | ||
type: string; // Type usually color | ||
parent: string; // Parent category or group of the color | ||
description: string; // Description or notes about the color | ||
}; | ||
|
||
export type ColorPalette = { | ||
[shade: string]: ColorDetails; | ||
}; | ||
|
||
export type Theme = { | ||
[colorName: string]: ColorPalette; | ||
}; | ||
|
||
type CompiledColors = { | ||
[themeName: string]: Theme; | ||
}; | ||
|
||
/** | ||
* Custom hook for compiling color themes from Figma JSON definitions. | ||
* Merges brand, light, and dark theme color settings into a single object. | ||
* | ||
* @returns Object containing compiled color themes. | ||
*/ | ||
export const useJsonColor = (): CompiledColors => { | ||
const [colors, setColors] = useState<CompiledColors>({}); | ||
|
||
useEffect(() => { | ||
/** | ||
* Parses a referenced color value and resolves it based on the current theme. | ||
* If the value is a reference (enclosed in curly braces), it navigates through the theme object. | ||
* | ||
* @param value - The color value or reference to resolve. | ||
* @param theme - The theme object to resolve references against. | ||
* @returns The resolved color value. | ||
*/ | ||
const parseColorValue = (value: string, theme: Theme): string => { | ||
if (value.startsWith('{') && value.endsWith('}')) { | ||
const path = value.slice(1, -1).split('.'); | ||
let current: any = theme; | ||
for (const segment of path) { | ||
current = current[segment]; | ||
if (!current) { | ||
return value; // Return original value if resolution fails. | ||
} | ||
} | ||
return current.value; // Return the resolved color value. | ||
} | ||
return value; // Return the direct value if not a reference. | ||
}; | ||
|
||
/** | ||
* Compiles color themes from provided JSON theme definitions. | ||
* Each color value is checked and potentially resolved using parseColorValue. | ||
* | ||
* @param themes - Object containing various theme definitions. | ||
* @returns Object containing compiled and resolved themes. | ||
*/ | ||
const compileColors = (themes: { | ||
[key: string]: Theme; | ||
}): CompiledColors => { | ||
const compiledColors: CompiledColors = {}; | ||
Object.entries(themes).forEach(([themeName, theme]) => { | ||
compiledColors[themeName] = {}; | ||
Object.entries(theme).forEach(([colorName, colorValues]) => { | ||
compiledColors[themeName][colorName] = {}; | ||
Object.entries(colorValues).forEach(([shade, details]) => { | ||
const { value, description } = details; | ||
const resolvedValue = parseColorValue(value, figmaBrandColors); | ||
compiledColors[themeName][colorName][shade] = { | ||
...details, | ||
value: resolvedValue, | ||
description: | ||
description + | ||
(value === resolvedValue ? '' : ` Alias: ${value}`), | ||
}; | ||
}); | ||
}); | ||
}); | ||
return compiledColors; | ||
}; | ||
|
||
// Compile all color themes into a single object and update the state | ||
const allColors = compileColors({ | ||
brandColor: { ...figmaBrandColors }, | ||
lightTheme: figmaLightTheme, | ||
darkTheme: figmaDarkTheme, | ||
}); | ||
setColors(allColors); | ||
}, []); | ||
|
||
return colors; | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is going to be a fun merge conflict for me... 🙃