-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(labs): Add color expansion support to theme object
- Loading branch information
Showing
5 changed files
with
96 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import chroma from 'chroma-js'; | ||
import deepmerge from 'deepmerge'; | ||
import colors from '@workday/canvas-colors-web'; | ||
import {defaultCanvasTheme} from './theme'; | ||
import {CanvasTheme, PartialCanvasTheme, PartialCanvasThemePalette} from './types'; | ||
import {CanvasColor} from '@workday/canvas-kit-react-core'; | ||
|
||
const {gradients, primary, ...allColors} = colors; | ||
|
||
// TODO: Make darken prop more readable. | ||
function shiftColor(hexColor: string, darken: boolean = true) { | ||
const canvasColor = Object.keys(allColors).find( | ||
key => allColors[key as CanvasColor] === hexColor | ||
); | ||
|
||
if (canvasColor) { | ||
const colorRegex = /([a-zAz]*)(\d{3})/g; | ||
const match = colorRegex.exec(canvasColor); | ||
|
||
if (match) { | ||
const baseColor = match[1]; | ||
const shadeNumber = parseInt(match[2], 10); | ||
|
||
const newShade = darken ? shadeNumber + 100 : shadeNumber - 100; | ||
|
||
if (newShade >= 100 && newShade <= 600) { | ||
return colors[(baseColor + newShade) as CanvasColor]; | ||
} | ||
} | ||
} | ||
|
||
const newColor = darken ? chroma(hexColor).darken() : chroma(hexColor).brighten(); | ||
|
||
return newColor.hex(); | ||
} | ||
|
||
function fillPalette(palette?: PartialCanvasThemePalette): PartialCanvasThemePalette { | ||
if (!palette) { | ||
return {}; | ||
} | ||
const shades = {...palette}; | ||
|
||
if (!shades.main) { | ||
console.warn( | ||
'The color provided to fillPalette(palette) is invalid. The palette object needs to have a `main` property' | ||
); | ||
} | ||
|
||
const dark = shiftColor(shades.main!); | ||
const darkest = shiftColor(dark); | ||
const light = shiftColor(shades.main!, false); | ||
const lightest = shiftColor(light, false); | ||
|
||
return { | ||
lightest, | ||
light, | ||
main: shades.main, | ||
dark, | ||
darkest, | ||
contrast: shades.contrast || colors.frenchVanilla100, | ||
}; | ||
} | ||
|
||
export default function createCanvasTheme(partialTheme: PartialCanvasTheme): CanvasTheme { | ||
const {palette, breakpoints = {}} = partialTheme; | ||
const {primary, alert, error, success, neutral, common = {}} = palette!; | ||
|
||
const mergable: PartialCanvasTheme = { | ||
palette: { | ||
common, | ||
primary: fillPalette(primary), | ||
alert: fillPalette(alert), | ||
error: fillPalette(error), | ||
success: fillPalette(success), | ||
neutral: fillPalette(neutral), | ||
}, | ||
breakpoints, | ||
}; | ||
|
||
return deepmerge(defaultCanvasTheme, mergable) as CanvasTheme; | ||
} |
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