Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hsl and use Primitive v8 colors #442

Merged
merged 20 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/cool-comics-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@primer/brand-primitives": minor
"@primer/react-brand": minor
---

- Change base color scales to hex instead of hsl
- Remove the hsl logic that handles opacity
- Use a raw hex8 value where alpha was used temporarily until we have the new build process in place from Primitives
- Adds new tokens for videoPlayer to help with this transition
- Use two files for light/dark vs. one in the base tokens
langermank marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions apps/docs/scripts/components-with-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const supportedComponents = [
'Pillar',
'SectionIntro',
'Stack',
'Pillar',
'Testimonial',
'Text',
'Timeline',
Expand Down
23 changes: 12 additions & 11 deletions apps/docs/src/components/content/ColorScales.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import colors from '@primer/brand-primitives/lib/design-tokens/js/module/tokens/base/colors/color-scales'
import colors from '@primer/brand-primitives/lib/design-tokens/js/module/tokens/base/colors/light'
import React from 'react'
import {
Box as PRCBox,
Expand All @@ -14,16 +14,16 @@ import {useColorTheme} from './ColorThemeContext'

const availableColorModes = Object.values(ColorModesEnum)

const convertToHsl = (rawHSL) => `hsl(${rawHSL.split(' ').join(', ')})`

export function ColorScales() {
const [colorTheme, setCurrentMode] = useColorTheme()

const predicateFn = (colorEntry) => ['black', 'white'].includes(colorEntry[0])

const rgbScales = Object.entries(colors.base.color.scale).filter(
(color) => !predicateFn(color),
)
const rgbScales = Object.entries(colors.base.color.scale)
.filter(([scaleName]) => {
return scaleName !== 'transparent'
})
.filter((color) => !predicateFn(color))

const blackWhiteScales = Object.entries(colors.base.color.scale).filter(
(color) => predicateFn(color),
Expand All @@ -35,14 +35,15 @@ export function ColorScales() {
<PRCBox key={name}>
{Object.entries(colorScale).map(([key, obj]) => {
const value = colorTheme === 'dark' ? obj.dark : obj.value
const hslValue = convertToHsl(value)

if (!value) return null

return (
<PRCBox
key={`${key}-${hslValue}`}
key={`${key}-${value}`}
sx={{
color: readableColor(hslValue),
bg: hslValue,
color: readableColor(value),
bg: value,
p: 2,
display: 'flex',
justifyContent: 'space-between',
Expand All @@ -53,7 +54,7 @@ export function ColorScales() {
<Text>
{name}.{key}
</Text>
<Text>{hslValue}</Text>
<Text>{value}</Text>
</PRCBox>
)
})}
Expand Down
7 changes: 2 additions & 5 deletions apps/docs/src/components/content/ColorThemePicker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Box as PRCBox} from '@primer/react'
import React from 'react'
import {useColorTheme, availableModes} from './ColorThemeContext'
import globalFunctionalTokens from '@primer/brand-primitives/lib/design-tokens/js/module/tokens/base/colors/color-scales'
import baseColorScales from '@primer/brand-primitives/lib/design-tokens/js/module/tokens/base/colors/light'

export function ColorThemePicker() {
const [colorTheme, setColorTheme] = useColorTheme()
Expand Down Expand Up @@ -67,10 +67,7 @@ function ColorThemePreview() {
sx={{
width: 20,
height: 20,
bg: `hsl(${globalFunctionalTokens.base.color.scale[name][5].value
.split(' ')
.join(', ')})`,
margin: '2px',
bg: baseColorScales.base.color.scale[name][5].value,
borderRadius: 999,
}}
/>
Expand Down
23 changes: 13 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions packages/design-tokens/scripts/build-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {buildPrimitives, StyleDictionary} = require('@primer/primitives/build')
const mediaQueryFormat = require('../src/formats/responsive-media-query')
const colorModeFormat = require('../src/formats/color-mode-attributes')

const lightJson = require('../src/tokens/base/colors/light')
const darkJson = require('../src/tokens/base/colors/dark')

;(function () {
const namespace = 'brand'
const outputPath = './lib/design-tokens'
Expand All @@ -14,7 +17,6 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')
* Step 1:
* Create a temporary directory with JSON files to convert into tokens
*/
// move over base configs

fs.cpSync('../../node_modules/@primer/primitives/tokens', dest, {recursive: true})

Expand All @@ -24,6 +26,30 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')

/**
* Step 2:
* Produce a color-scales.json src file temporarily
*/

const mergeLightAndDark = (light, dark) => {
const merged = {}

for (const key in dark) {
if (typeof dark[key] === 'object' && dark[key] !== null) {
merged[key] = mergeLightAndDark(light[key] || {}, dark[key])
} else if (key === 'value' && light) {
merged[key] = light[key]
merged['dark'] = dark[key]
}
}

return merged
}

const mergedColorScales = mergeLightAndDark(lightJson, darkJson)

fs.writeFileSync(`${dest}/base/colors/color-scales.json`, JSON.stringify(mergedColorScales))

/**
* Step 3:
* Build tokens by running function against the temporary directory
*/

Expand Down Expand Up @@ -178,7 +204,6 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')
format: `css/color-mode-attributes`,
options: {
outputReferences: false,
containsRawHSL: true,
},
},
],
Expand Down Expand Up @@ -206,6 +231,7 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')
`tokens/functional/components/grid/colors.json`,
`tokens/functional/components/logosuite/colors.json`,
`tokens/functional/components/timeline/colors.json`,
`tokens/functional/components/video-player/colors.js`,
`tokens/functional/components/prose/colors.js`,
]

Expand All @@ -225,7 +251,6 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')
format: `css/color-mode-attributes`,
options: {
outputReferences: false,
containsRawHSL: false,
},
},
],
Expand All @@ -235,7 +260,7 @@ const colorModeFormat = require('../src/formats/color-mode-attributes')
}

/**
* Step 3:
* Step 4:
* Clean up the temporary directory
*/
fs.rmdirSync(dest, {recursive: true})
Expand Down
39 changes: 1 addition & 38 deletions packages/design-tokens/src/formats/color-mode-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const prettier = require('prettier')
* Custom style dictionary formatter for outputting CSS variables that are
* compatible with the ThemeProvider component.
*/
function colorModeAttributes({dictionary: origDictionary, file, options}) {
function colorModeAttributes({dictionary, file, options}) {
/**
* Need to add a new color mode? Add it here!
*/
Expand All @@ -24,42 +24,6 @@ function colorModeAttributes({dictionary: origDictionary, file, options}) {
]
const defaultMode = supportedModes[0]

const convertToHSL = value => `hsl(${value.split(' ').join(', ')})`

const dictionary = options.containsRawHSL ? JSON.parse(JSON.stringify(origDictionary)) : origDictionary

if (options.containsRawHSL) {
const reduceProperties = (acc, token) => {
acc.push({
...token,
name: `${token.name}-hsl`,
path: [...token.path, 'hsl'],
})

/**
* Creates a new token object adjacent to the original
* This takes the raw HSL values and wraps it in a HSL css function
*/
const tokenWithHSL = {...token, value: convertToHSL(token.value)}

/**
* Checks if the token object contains one of the supported modes
* If yes; mutate the HSL-specific token object with the converted HSL value for the alternate mode
*/
for (const mode of supportedModes) {
if (token.hasOwnProperty(mode)) {
tokenWithHSL[mode] = convertToHSL(token[mode])
}
}

acc.push(tokenWithHSL)
return acc
}

dictionary.allTokens = dictionary.allTokens.reduce(reduceProperties, [])
dictionary.allProperties = dictionary.allProperties.reduce(reduceProperties, [])
}

const {outputReferences} = options
let {allTokens} = dictionary

Expand Down Expand Up @@ -123,7 +87,6 @@ function colorModeAttributes({dictionary: origDictionary, file, options}) {

}\n`
}

const template = `
${fileHeader({file})}

Expand Down
Loading
Loading