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

chore: Add transform to handle opacity as percentage #100

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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;
Copy link
Member

@alanbsmith alanbsmith Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> 1 is a very smart safeguard. 👍🏼

};
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}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always forget that JS is cool with doing math on string numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's only numbers, no letters lol

},
// transform function that changes a value to its CSS var name
'value/variables': {
type: 'value',
Expand Down
Loading