-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { colord, extend } from 'colord'; | ||
import a11yPlugin from 'colord/plugins/a11y'; | ||
import namesPlugin from 'colord/plugins/names'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { ThemeInputValues, ThemeOutputValues } from './types'; | ||
import { COLORS } from '../utils'; | ||
|
||
extend( [ namesPlugin, a11yPlugin ] ); | ||
|
||
export function generateThemeVariables( | ||
inputs: ThemeInputValues | ||
): ThemeOutputValues { | ||
validateInputs( inputs ); | ||
|
||
return { | ||
colors: { | ||
...generateAccentDependentColors( inputs.accent ), | ||
...generateBackgroundDependentColors( inputs.background ), | ||
}, | ||
}; | ||
} | ||
|
||
// TODO: Add unit tests | ||
function validateInputs( inputs: ThemeInputValues ) { | ||
for ( const [ key, value ] of Object.entries( inputs ) ) { | ||
if ( ! colord( value ).isValid() ) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`wp.components.Theme: "${ value }" is not a valid color value for the '${ key }' prop.` | ||
); | ||
} | ||
} | ||
|
||
if ( | ||
inputs.background && | ||
! colord( inputs.background ).isReadable( | ||
inputs.accent || COLORS.ui.theme | ||
) | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`wp.components.Theme: The background color provided ("${ inputs.background }") does not have sufficient contrast against the accent color ("${ inputs.accent }").` | ||
); | ||
} | ||
} | ||
|
||
function generateAccentDependentColors( accent?: string ) { | ||
if ( ! accent ) return {}; | ||
|
||
return { | ||
accent, | ||
accentDarker10: colord( accent ).darken( 0.1 ).toHex(), | ||
accentDarker20: colord( accent ).darken( 0.2 ).toHex(), | ||
accentInverted: getForegroundForColor( accent ), | ||
}; | ||
} | ||
|
||
function generateBackgroundDependentColors( background?: string ) { | ||
if ( ! background ) return {}; | ||
|
||
const foreground = getForegroundForColor( background ); | ||
|
||
return { | ||
background, | ||
foreground, | ||
foregroundInverted: getForegroundForColor( foreground ), | ||
gray: generateShades( background, foreground ), | ||
}; | ||
} | ||
|
||
function getForegroundForColor( color: string ) { | ||
return colord( color ).isDark() ? COLORS.white : COLORS.gray[ 900 ]; | ||
} | ||
|
||
// TODO: Add unit test so the result of this matches the default case (#fff to #1e1e1e) | ||
function generateShades( background: string, foreground: string ) { | ||
// How much darkness you need to add to #fff to get the COLORS.gray[n] color | ||
const SHADES = { | ||
100: 0.06, | ||
200: 0.121, | ||
300: 0.132, | ||
400: 0.2, | ||
600: 0.42, | ||
700: 0.543, | ||
800: 0.821, | ||
}; | ||
|
||
// Darkness of COLORS.gray[ 900 ], relative to #fff | ||
const limit = 0.884; | ||
|
||
const direction = colord( background ).isDark() ? 'lighten' : 'darken'; | ||
|
||
// Lightness delta between the background and foreground colors | ||
const range = | ||
Math.abs( | ||
colord( background ).toHsl().l - colord( foreground ).toHsl().l | ||
) / 100; | ||
|
||
const result: Record< number, string > = {}; | ||
|
||
Object.entries( SHADES ).forEach( ( [ key, value ] ) => { | ||
result[ parseInt( key ) ] = colord( background ) | ||
[ direction ]( ( value / limit ) * range ) | ||
.toHex(); | ||
} ); | ||
|
||
return result as ThemeOutputValues[ 'colors' ][ 'gray' ]; | ||
} |
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,97 +1,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { colord } from 'colord'; | ||
import styled from '@emotion/styled'; | ||
import { css } from '@emotion/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { ThemeProps } from './types'; | ||
import { COLORS } from '../utils'; | ||
import type { ThemeOutputValues } from './types'; | ||
|
||
const getForegroundForColor = ( color: string ) => | ||
colord( color ).isDark() ? COLORS.white : COLORS.gray[ 900 ]; | ||
|
||
const accentColor = ( { accent }: ThemeProps ) => | ||
accent | ||
? css` | ||
--wp-components-color-accent: ${ accent }; | ||
--wp-components-color-accent-darker-10: ${ colord( accent ) | ||
.darken( 0.1 ) | ||
.toHex() }; | ||
--wp-components-color-accent-darker-20: ${ colord( accent ) | ||
.darken( 0.2 ) | ||
.toHex() }; | ||
--wp-components-color-accent-inverted: ${ getForegroundForColor( | ||
accent | ||
) }; | ||
` | ||
: undefined; | ||
|
||
// TODO: Add unit test so the result of this matches the default case (#fff to #1e1e1e) | ||
function generateShades( background: string, foreground: string ) { | ||
// Start from #fff (background) | ||
const shades = { | ||
100: 0.06, | ||
200: 0.121, | ||
300: 0.132, | ||
400: 0.2, | ||
600: 0.42, | ||
700: 0.543, | ||
800: 0.821, | ||
}; | ||
|
||
// Darkness of COLORS.gray[ 900 ], relative to #fff | ||
const limit = 0.884; | ||
|
||
const direction = colord( background ).isDark() ? 'lighten' : 'darken'; | ||
const range = | ||
Math.abs( | ||
colord( background ).toHsl().l - colord( foreground ).toHsl().l | ||
) / 100; | ||
|
||
const result: Record< string, string > = {}; | ||
|
||
Object.entries( shades ).forEach( ( [ k, v ] ) => { | ||
result[ k ] = colord( background ) | ||
[ direction ]( ( v / limit ) * range ) | ||
.toHex(); | ||
} ); | ||
|
||
return result; | ||
} | ||
|
||
const backgroundColor = ( { accent, background }: ThemeProps ) => { | ||
if ( ! background ) return; | ||
|
||
const foreground = getForegroundForColor( background ); | ||
|
||
// TODO: Add unit test | ||
if ( ! colord( background ).isReadable( accent || COLORS.ui.theme ) ) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`wp.components.Theme: The background color provided ("${ background }") does not have sufficient contrast against the accent color ("${ accent }").` | ||
); | ||
} | ||
|
||
const shades = Object.entries( generateShades( background, foreground ) ) | ||
const colorVariables = ( { colors }: ThemeOutputValues ) => { | ||
const shades = Object.entries( colors.gray || {} ) | ||
.map( ( [ k, v ] ) => `--wp-components-color-gray-${ k }: ${ v };` ) | ||
.join( '' ); | ||
|
||
return css` | ||
--wp-components-color-background: ${ background }; | ||
--wp-components-color-foreground: ${ foreground }; | ||
--wp-components-color-foreground-inverted: ${ getForegroundForColor( | ||
foreground | ||
) }; | ||
return [ | ||
css` | ||
--wp-components-color-accent: ${ colors.accent }; | ||
--wp-components-color-accent-darker-10: ${ colors.accentDarker10 }; | ||
--wp-components-color-accent-darker-20: ${ colors.accentDarker20 }; | ||
--wp-components-color-accent-inverted: ${ colors.accentInverted }; | ||
--wp-components-color-background: ${ colors.background }; | ||
--wp-components-color-foreground: ${ colors.foreground }; | ||
--wp-components-color-foreground-inverted: ${ colors.foregroundInverted }; | ||
${ shades } | ||
`; | ||
${ shades } | ||
`, | ||
]; | ||
}; | ||
|
||
export const Wrapper = styled.div< ThemeProps >` | ||
${ accentColor } | ||
${ backgroundColor } | ||
export const Wrapper = styled.div< ThemeOutputValues >` | ||
${ colorVariables } | ||
`; |
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