Skip to content

Commit

Permalink
replace hexAlpha with hex (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Sep 11, 2024
1 parent a38ee38 commit 60dcf94
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/consumer_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Re-enable this workflow when primer/brand has been updated to usev8 tokens"
echo "Re-enable this workflow when primer/brand has been updated to latest tokens"
# - name: Checkout current repo
# uses: actions/checkout@v4

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/visual_regression_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
fail-fast: false
matrix:
shard: [1, 2, 3, 4, 5]

steps:
- uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v4
with:
Expand All @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Build v8 tokens
- name: Build tokens
run: npm run build

- name: Build storybook
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
if: ${{ always() }}
runs-on: ubuntu-latest
needs: vrt-runner

steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -99,4 +99,4 @@ jobs:

- name: Check vrt-runner job status
if: ${{ needs.vrt-runner.result == 'failure' }}
run: exit 1
run: exit 1
5 changes: 0 additions & 5 deletions integration/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('PrimerStyleDictionary', () => {
transforms: [
'name/pathToKebabCase',
'color/hex',
'color/hexAlpha',
'color/hexMix',
'dimension/rem',
'duration/css',
Expand All @@ -37,7 +36,6 @@ describe('PrimerStyleDictionary', () => {
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand All @@ -62,7 +60,6 @@ describe('PrimerStyleDictionary', () => {
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down Expand Up @@ -108,7 +105,6 @@ describe('PrimerStyleDictionary', () => {
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand All @@ -131,7 +127,6 @@ describe('PrimerStyleDictionary', () => {
buildPath: `${basePath}/build/js/`,
transforms: [
'color/hex',
'color/hexAlpha',
'shadow/css',
'border/css',
'dimension/rem',
Expand Down
16 changes: 6 additions & 10 deletions package-lock.json

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

6 changes: 0 additions & 6 deletions src/PrimerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import StyleDictionary from 'style-dictionary'
import {w3cJsonParser} from './parsers'
import {
borderToCss,
colorToHexAlpha,
colorToRgbAlpha,
colorToHex,
colorToHexMix,
Expand Down Expand Up @@ -108,11 +107,6 @@ StyleDictionary.registerTransform({
...colorToRgbaFloat,
})

StyleDictionary.registerTransform({
name: 'color/hexAlpha',
...colorToHexAlpha,
})

StyleDictionary.registerTransform({
name: 'color/hexMix',
...colorToHexMix,
Expand Down
1 change: 0 additions & 1 deletion src/platforms/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
transforms: [
'name/pathToKebabCase',
'color/hex',
'color/hexAlpha',
'color/hexMix',
'dimension/rem',
'duration/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/docJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const docJson: PlatformInitializer = (outputFile, prefix, buildPath): Sty
'name/pathToKebabCase',
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/fallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const fallbacks: PlatformInitializer = (outputFile, prefix, buildPath): S
'name/pathToKebabCase',
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const javascript: PlatformInitializer = (outputFile, prefix, buildPath):
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const json: PlatformInitializer = (outputFile, prefix, buildPath): StyleD
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/styleLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const styleLint: PlatformInitializer = (outputFile, prefix, buildPath): S
'name/pathToKebabCase',
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/remPxArray',
'shadow/css',
'border/css',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/typeDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const typeDefinitions: PlatformInitializer = (outputFile, prefix, buildPa
buildPath,
transforms: [
'color/hex',
'color/hexAlpha',
'shadow/css',
'border/css',
'dimension/rem',
Expand Down
1 change: 0 additions & 1 deletion src/platforms/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const typescript: PlatformInitializer = (outputFile, prefix, buildPath):
transforms: [
'color/hex',
'color/hexMix',
'color/hexAlpha',
'dimension/rem',
'shadow/css',
'border/css',
Expand Down
33 changes: 24 additions & 9 deletions src/transformers/colorToHex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ describe('Transformer: colorToHex', () => {
expect(input.map(item => colorToHex.transformer(item, {}))).toStrictEqual(expectedOutput)
})

it('transforms `color` tokens and ignores alpha value', () => {
expect(
[
getMockToken({value: '#343434', alpha: 0.4}),
getMockToken({value: '#34343466', alpha: 0.9}),
getMockToken({value: 'rgb(100,200,255)', alpha: 0.4}),
getMockToken({value: 'rgba(100,200,255,0.8)', alpha: 0.4}),
].map(item => colorToHex.transformer(item, {})),
).toStrictEqual(['#343434', '#34343466', '#64c8ff', '#64c8ffcc'])
it('transforms hex3, hex6 `color` tokens with alpha value', () => {
const input = [getMockToken({value: '#123', alpha: 0.2}), getMockToken({value: '#343434', alpha: 0.4})]
const expectedOutput = ['#11223333', '#34343466']
expect(input.map(item => colorToHex.transformer(item, {}))).toStrictEqual(expectedOutput)
})

it('transforms hex8 `color` tokens with alpha value, ignoring the initial alpha from the hex8', () => {
const input = getMockToken({value: '#34343466', alpha: 0.6})
expect(colorToHex.transformer(input, {})).toStrictEqual('#34343499')
})

it('transforms rgb `color` tokens with alpha value', () => {
const input = getMockToken({value: 'rgb(100,200,255)', alpha: 0.6})
expect(colorToHex.transformer(input, {})).toStrictEqual('#64c8ff99')
})

it('transforms rgba `color` tokens with alpha value, ignoring the initial alpha from the hex8', () => {
const input = getMockToken({value: 'rgba(100,200,255, 0.2)', alpha: 0.6})
expect(colorToHex.transformer(input, {})).toStrictEqual('#64c8ff99')
})

it('transforms rgba `color` tokens with alpha null, ignoring alpha', () => {
const input = getMockToken({value: 'rgba(100,200,255, 0.5)', alpha: null})
expect(colorToHex.transformer(input, {})).toStrictEqual('#64c8ff80')
})
})
9 changes: 8 additions & 1 deletion src/transformers/colorToHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {toHex} from 'color2k'
import {isColor} from '../filters'
import type StyleDictionary from 'style-dictionary'
import {getTokenValue} from './utilities/getTokenValue'
import {alpha} from './utilities/alpha'
/**
* @description converts color tokens value to `hex6` or `hex8`
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
Expand All @@ -12,5 +13,11 @@ export const colorToHex: StyleDictionary.Transform = {
type: `value`,
transitive: true,
matcher: isColor,
transformer: (token: StyleDictionary.TransformedToken) => toHex(getTokenValue(token)),
transformer: (token: StyleDictionary.TransformedToken) => {
const alphaValue = token.alpha ?? token.$extensions?.alpha
if (alphaValue === null || alphaValue === undefined) {
return toHex(getTokenValue(token))
}
return toHex(alpha(getTokenValue(token), alphaValue, token))
},
}
30 changes: 0 additions & 30 deletions src/transformers/colorToHexAlpha.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/transformers/colorToHexAlpha.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {borderToCss} from './borderToCss'
export {colorToHex} from './colorToHex'
export {colorToHexAlpha} from './colorToHexAlpha'
export {colorToHexMix} from './colorToHexMix'
export {colorToRgbAlpha} from './colorToRgbAlpha'
export {colorToRgbaFloat} from './colorToRgbaFloat'
Expand Down

0 comments on commit 60dcf94

Please sign in to comment.