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

feat(internals): add rules for border-top/right/bottom/left-color classes #78

Merged
merged 2 commits into from Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/_rules/internal.js
@@ -1,7 +1,14 @@
import { handler as h } from '#utils';
import { handler as h, directionMap } from '#utils';

export const internalRules = [
[/^i-bg-(.+)$/, ([, cssvar]) => ({ 'background-color': h.warpToken(cssvar) })],
[/^i-text-(.+)$/, ([, cssvar]) => ({ color: h.warpToken(cssvar) })],
[/^i-border-(.+)$/, ([, cssvar]) => ({ 'border-color': h.warpToken(cssvar) })],
[/^i-border-([rltb])-(.+)$/, ([_, direction, cssvar]) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

No big thing 😄 but we don't use underscore variables for non-needed matches in the other regex callbacks.

Copy link
Contributor

Choose a reason for hiding this comment

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

And another thing... I am not quite sure how this works, but will it be a problem that for example i-border-r-$color-alert-warning-border will be matched by both /^i-border-(.+)$ and /^i-border-([rltb])-(.+)$/ ? 🤔 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently it works, according to the test output... but I can't understand how. 🤷 🙈 😅 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed underscore in 20824bf

As for why the /^i-border-(.+)$/ rule didn't work for i-border-l-$color-alert-warning-border, it was because the second argument of the callback was then"l-$color-alert-warning-border", and that when passed to the warpToken handler didn't match str.match(/^\$\S/) check. As a result the CSS property was assigned undefined as value, which makes unocss not generate any CSS for that selector. Does that make sense? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok I think I get it... it DOES match the first regex and tries to create the border-color rule, but since h.warpToken() does not return anything the whole rule gets disposed. It then also matches the next rule, which creates the correct rule using the variable. Did I get that correct? 😄 ...Reading your comment again, I see that was about exactly what you said. 😄 Thanks. 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I checked, the cssvar string needs to start with a $ to be matched with the regex (it's because we say so with ^ at the beginning of the group). See here:
Screenshot 2023-03-28 at 14 37 26

Copy link
Contributor

Choose a reason for hiding this comment

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

Just trying to understand, and I am NOT saying that you should do this (😅)... but the best way would have been to write the first regex as to match anything but[rltb]- so that it wouldn't even try to create the rules? Is that correct? 🤷 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I understand now!
I think the reason for this check to be in the warpToken handler was that we'd keep the logic for ensuring correct prefixing in one place. So if we were to change how we separate prefixes from actual tokens, we wouldn't need to do it wherever that token could be used.
There might also come a time when we rewrite how the color tokens are translated into CSS in our preset, so I'm not sure if it's worth rewriting too much at this point. But that topic is still up for discussion at some point in the future (related potential task)

Copy link
Contributor

Choose a reason for hiding this comment

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

No, nothing that needs to be changed. 🙂 This just made me understand more about how this whole thing is working. 😄 Thanks for taking your time explaining. 🙂 🙏

if (direction in directionMap && cssvar != null) {
return directionMap[direction].map(
(dir) => [`border${dir}-color`, h.warpToken(cssvar)],
);
}
}],
];
6 changes: 5 additions & 1 deletion test/__snapshots__/internal.js.snap
Expand Up @@ -4,5 +4,9 @@ exports[`it generates css based on warp tokens 1`] = `
"/* layer: default */
.i-bg-\\\\$foo-bar{background-color:var(--w-foo-bar);}
.i-text-\\\\$biz-baz{color:var(--w-biz-baz);}
.i-border-\\\\$wombat-llama{border-color:var(--w-wombat-llama);}"
.i-border-\\\\$wombat-llama{border-color:var(--w-wombat-llama);}
.i-border-b-\\\\$color-alert-info-border{border-bottom-color:var(--w-color-alert-info-border);}
.i-border-l-\\\\$color-alert-info-border{border-left-color:var(--w-color-alert-info-border);}
.i-border-r-\\\\$color-alert-info-border{border-right-color:var(--w-color-alert-info-border);}
.i-border-t-\\\\$color-alert-info-border{border-top-color:var(--w-color-alert-info-border);}"
`;
22 changes: 20 additions & 2 deletions test/internal.js
Expand Up @@ -4,8 +4,26 @@ import { expect, test } from 'vitest';
setup();

test('it generates css based on warp tokens', async ({ uno }) => {
const classes = ['i-bg-$foo-bar', 'i-text-$biz-baz', 'i-border-$wombat-llama'];
const anticlasses = ['i-bg-foos-bars', 'i-text-bizs-bazs', 'i-border-wombats-llamas', 'i-magnuseses'];
const classes = [
'i-bg-$foo-bar',
'i-text-$biz-baz',
'i-border-$wombat-llama',
'i-border-l-$color-alert-info-border',
'i-border-r-$color-alert-info-border',
'i-border-t-$color-alert-info-border',
'i-border-b-$color-alert-info-border',
];
const anticlasses = [
'i-bg-foos-bars',
'i-text-bizs-bazs',
'i-border-wombats-llamas',
'i-magnuseses',
'i-border-tr-$color-alert-info-border',
'i-border-br-$color-alert-info-border',
'i-border-tl-$color-alert-info-border',
'i-border-bl-$color-alert-info-border',
'i-border-tbl-$color-alert-info-border',
];
const { css } = await uno.generate([...classes, ...anticlasses]);
expect(css).toMatchSnapshot();
});