Skip to content

Commit

Permalink
chore: Add transform to handle opacity as percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
RayRedGoose committed Apr 1, 2024
1 parent 0829677 commit 76bb212
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/canvas-tokens/utils/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ export const isComposite: Matcher = ({type}) => {
export const isNotComposite: Matcher = token => {
return !isComposite(token);
};

export const isBaseOpacity: Matcher = token => {
const [level, category] = token.path;
return level === 'base' && category === 'opacity' && parseFloat(token.value) > 1;
};
10 changes: 10 additions & 0 deletions packages/canvas-tokens/utils/spec/transforms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,14 @@ describe('transforms', () => {

expect(result).toBe(expected);
});

it('should convert opacity to number below 1', () => {
const result = transforms['value/opacity'].transformer(
{...defaultToken, value: '40'},
defaultOptions
);
const expected = '0.4';

expect(result).toBe(expected);
});
});
6 changes: 6 additions & 0 deletions packages/canvas-tokens/utils/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export const transforms: Record<string, Transform> = {
matcher: filter.isSysColor,
transformer: flatRGBAColor,
},
'value/opacity': {
type: 'value',
transitive: true,
matcher: filter.isBaseOpacity,
transformer: ({value}) => `${value / 100}`,
},
// transform function that changes a value to its CSS var name
'value/variables': {
type: 'value',
Expand Down

0 comments on commit 76bb212

Please sign in to comment.