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: support dynamic values (JIT) & add tests #60

Merged
merged 3 commits into from
Oct 31, 2022
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
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"repository": "https://github.com/20lives/tailwindcss-rtl",
"scripts": {
"lint": "eslint ./src",
"test": "jest",
"build": "tailwindcss build -c src/tailwind.config.js -o dist/tailwindcss-rtl.css"
},
"keywords": [
Expand All @@ -16,7 +17,12 @@
"author": "20lives",
"license": "MIT",
"private": false,
"peerDependencies": {
"tailwindcss": ">=2.0.0 || >=3.0.0"
},
"devDependencies": {
"tailwindcss": "^1.9.6"
"jest": "^27.2.4",
"postcss": "^8.2.4",
"tailwindcss": "^3.0.0"
}
}
96 changes: 61 additions & 35 deletions src/borderRadiusUtilities.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
const nameClass = require('./util/nameClass');

module.exports = (theme) => {
const generators = [
([modifier, value]) => ({
['[dir="ltr"] ' + nameClass('rounded-e', modifier)]: {
borderTopRightRadius: value,
borderBottomRightRadius: value,
},
['[dir="ltr"] ' + nameClass('rounded-s', modifier)]: {
borderTopLeftRadius: value,
borderBottomLeftRadius: value,
},
['[dir="rtl"] ' + nameClass('rounded-e', modifier)]: {
borderTopLeftRadius: value,
borderBottomLeftRadius: value,
},
['[dir="rtl"] ' + nameClass('rounded-s', modifier)]: {
borderTopRightRadius: value,
borderBottomRightRadius: value,
},
}),
([modifier, value]) => ({
['[dir="ltr"] ' + nameClass('rounded-ts', modifier)]: { borderTopLeftRadius: value },
['[dir="ltr"] ' + nameClass('rounded-te', modifier)]: { borderTopRightRadius: value },
['[dir="ltr"] ' + nameClass('rounded-be', modifier)]: { borderBottomRightRadius: value },
['[dir="ltr"] ' + nameClass('rounded-bs', modifier)]: { borderBottomLeftRadius: value },
['[dir="rtl"] ' + nameClass('rounded-te', modifier)]: { borderTopLeftRadius: value },
['[dir="rtl"] ' + nameClass('rounded-ts', modifier)]: { borderTopRightRadius: value },
['[dir="rtl"] ' + nameClass('rounded-bs', modifier)]: { borderBottomRightRadius: value },
['[dir="rtl"] ' + nameClass('rounded-be', modifier)]: { borderBottomLeftRadius: value },
}),
];

return generators.flatMap(generator => Object.entries(theme('borderRadius')).flatMap(generator));
module.exports = ({ matchUtilities, theme }) => {
matchUtilities(
{
"rounded-e": (value) => ({
'[dir="ltr"] &': {
borderTopRightRadius: value,
borderBottomRightRadius: value,
},
'[dir="rtl"] &': {
borderTopLeftRadius: value,
borderBottomLeftRadius: value,
},
}),
"rounded-s": (value) => ({
'[dir="ltr"] &': {
borderTopLeftRadius: value,
borderBottomLeftRadius: value,
},
'[dir="rtl"] &': {
borderTopRightRadius: value,
borderBottomRightRadius: value,
},
}),
"rounded-te": (value) => ({
'[dir="rtl"] &': {
borderTopLeftRadius: value,
},
'[dir="ltr"] &': {
borderTopRightRadius: value,
},
}),
"rounded-ts": (value) => ({
'[dir="ltr"] &': {
borderTopLeftRadius: value,
},
'[dir="rtl"] &': {
borderTopRightRadius: value,
},
}),
"rounded-be": (value) => ({
'[dir="ltr"] &': {
borderBottomRightRadius: value,
},
'[dir="rtl"] &': {
borderBottomLeftRadius: value,
},
}),
"rounded-bs": (value) => ({
'[dir="rtl"] &': {
borderBottomRightRadius: value,
},
'[dir="ltr"] &': {
borderBottomLeftRadius: value,
},
}),
},
{
supportsNegativeValues: false,
values: theme("inset"),
}
);
};
26 changes: 15 additions & 11 deletions src/borderWidthUtilities.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const nameClass = require('./util/nameClass.js');

module.exports = (theme) => {
const generators = [
([modifier, value]) => ({
[nameClass('border-e', modifier)]: { borderInlineEndWidth: value },
[nameClass('border-s', modifier)]: { borderInlineStartWidth: value },
}),
];

return generators.flatMap(generator => Object.entries(theme('borderWidth')).flatMap(generator));
module.exports = ({ matchUtilities, theme }) => {
matchUtilities(
{
"border-s": (value) => ({
borderInlineStartWidth: value,
}),
"border-e": (value) => ({
borderInlineEndWidth: value,
}),
},
{
supportsNegativeValues: false,
values: theme("borderWidth"),
}
);
};
41 changes: 18 additions & 23 deletions src/divideUtilities.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
const nameClass = require('./util/nameClass');

module.exports = (theme) => {
const generators = [
([modifier, _size]) => {
const size = _size === '0' ? '0px' : _size;
return {
[`${nameClass('divide-s', modifier)} > :not([hidden]) ~ :not([hidden])`]: {
'--tw-divide-s-reverse': '0',
borderInlineEndWidth: `calc(${size} * var(--tw-divide-s-reverse))`,
borderInlineStartWidth: `calc(${size} * calc(1 - var(--tw-divide-s-reverse)))`,
module.exports = ({ matchUtilities, addUtilities, theme }) => {
matchUtilities(
{
"divide-s": (value) => ({
"> :not([hidden]) ~ :not([hidden])": {
"--tw-divide-s-reverse": "0",
borderInlineEndWidth: `calc(${value} * var(--tw-divide-s-reverse))`,
borderInlineStartWidth: `calc(${value} * calc(1 - var(--tw-divide-s-reverse)))`,
},
};
}),
},
];

const divideReverse = {
'.divide-s-reverse > :not([hidden]) ~ :not([hidden])': {
'--tw-divide-s-reverse': '1',
{
supportsNegativeValues: true,
values: theme("divideWidth"),
}
);
addUtilities({
".divide-s-reverse > :not([hidden]) ~ :not([hidden])": {
"--tw-divide-s-reverse": "1",
},
};

return [
...generators.flatMap(generator => Object.entries(theme('divideWidth')).flatMap(generator)),
divideReverse,
] ;
});
};
49 changes: 25 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const plugin = require('tailwindcss/plugin');
const plugin = require("tailwindcss/plugin");

const paddingUtilities = require('./paddingUtilities');
const marginUtilities = require('./marginUtilities');
const floatUtilities = require('./floatUtilities');
const insetUtilities = require('./insetUtilities');
const clearUtilities = require('./clearUtilities');
const borderRadiusUtilities = require('./borderRadiusUtilities');
const borderWidthUtilities = require('./borderWidthUtilities');
const textAlignUtilities = require('./textAlignUtilities');
const transformOriginUtilities = require('./transformOriginUtilities');
const spaceUtilities = require('./spaceUtilities');
const divideUtilities = require('./divideUtilities');
const paddingUtilities = require("./paddingUtilities");
const marginUtilities = require("./marginUtilities");
const floatUtilities = require("./floatUtilities");
const insetUtilities = require("./insetUtilities");
const clearUtilities = require("./clearUtilities");
const borderRadiusUtilities = require("./borderRadiusUtilities");
const borderWidthUtilities = require("./borderWidthUtilities");
const textAlignUtilities = require("./textAlignUtilities");
const transformOriginUtilities = require("./transformOriginUtilities");
const spaceUtilities = require("./spaceUtilities");
const divideUtilities = require("./divideUtilities");

module.exports = plugin(({ addUtilities, theme, variants }) => {
addUtilities(paddingUtilities(theme), variants('padding'));
addUtilities(marginUtilities(theme), variants('margin'));
addUtilities(insetUtilities(theme), variants('inset'));
addUtilities(borderRadiusUtilities(theme), variants('borderRadius'));
addUtilities(clearUtilities(), variants('clear'));
addUtilities(floatUtilities(), variants('float'));
addUtilities(borderWidthUtilities(theme), variants('borderWidth'));
addUtilities(textAlignUtilities(), variants('textAlign'));
addUtilities(transformOriginUtilities(), variants('transformOrigin'));
addUtilities(spaceUtilities(theme), variants('space'));
addUtilities(divideUtilities(theme), variants('divide'));
module.exports = plugin((helpers) => {
const { addUtilities, matchUtilities, theme, variants } = helpers;
paddingUtilities(helpers);
marginUtilities(helpers);
insetUtilities(helpers);
borderRadiusUtilities(helpers);
addUtilities(clearUtilities(helpers), variants("clear"));
addUtilities(floatUtilities(helpers), variants("float"));
borderWidthUtilities(helpers);
addUtilities(textAlignUtilities(helpers), variants("textAlign"));
addUtilities(transformOriginUtilities(), variants("transformOrigin"));
spaceUtilities(helpers);
divideUtilities(helpers);
});
45 changes: 25 additions & 20 deletions src/insetUtilities.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const nameClass = require('./util/nameClass.js');

module.exports = (theme) => {
const generators = [
([modifier, size]) => ({
['[dir="rtl"] ' + nameClass('start', modifier)]: {
right: size,
},
['[dir="rtl"] ' + nameClass('end', modifier)]: {
left: size,
},
['[dir="ltr"] ' + nameClass('end', modifier)]: {
right: size,
},
['[dir="ltr"] ' + nameClass('start', modifier)]: {
left: size,
},
}),
];
return generators.flatMap(generator => Object.entries(theme('inset')).flatMap(generator));
module.exports = ({ matchUtilities, theme }) => {
matchUtilities(
{
start: (value) => ({
'[dir="rtl"] &': {
right: value,
},
'[dir="left"] &': {
left: value,
},
Comment on lines +8 to +10

Choose a reason for hiding this comment

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

Shouldn't this be [dir="ltr"]?

}),
end: (value) => ({
'[dir="rtl"] &': {
left: value,
},
'[dir="left"] &': {
right: value,
},
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

This should be [dir="ltr"] as well.

Choose a reason for hiding this comment

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

Why this suggestion wasn't fixed? It needs to be ltr instead of left.

}),
},
{
supportsNegativeValues: true,
values: theme("inset"),
}
);
};
24 changes: 15 additions & 9 deletions src/marginUtilities.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const nameClass = require('./util/nameClass');

module.exports = (theme) => {
const generators = [([modifier, size]) => ({
[nameClass('ms', modifier)]: { marginInlineStart: size },
[nameClass('me', modifier)]: { marginInlineEnd: size },
})];

return generators.flatMap(generator => Object.entries(theme('margin')).flatMap(generator));
module.exports = ({ matchUtilities, theme }) => {
matchUtilities(
{
ms: (value) => ({
marginInlineStart: value,
}),
me: (value) => ({
marginInlineEnd: value,
}),
},
{
supportsNegativeValues: true,
values: theme("margin"),
}
);
};
26 changes: 15 additions & 11 deletions src/paddingUtilities.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const nameClass = require('./util/nameClass');

module.exports = (theme) => {
const generators = [
([modifier, size]) => ({
[nameClass('ps', modifier)]: { paddingInlineStart: size },
[nameClass('pe', modifier)]: { paddingInlineEnd: size },
}),
];

return generators.flatMap(generator => Object.entries(theme('padding')).flatMap(generator));
module.exports = ({ matchUtilities, theme }) => {
matchUtilities(
{
ps: (value) => ({
paddingInlineStart: value,
}),
pe: (value) => ({
paddingInlineEnd: value,
}),
},
{
supportsNegativeValues: true,
values: theme("padding"),
}
);
};
41 changes: 18 additions & 23 deletions src/spaceUtilities.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
const nameClass = require('./util/nameClass.js');

module.exports = (theme) => {
const generators = [
([modifier, _size]) => {
const size = _size === '0' ? '0px' : _size;
return {
[`${nameClass('space-s', modifier)} > :not([hidden]) ~ :not([hidden])`]: {
'--tw-space-s-reverse': '0',
marginInlineEnd: `calc(${size} * var(--tw-space-s-reverse))`,
marginInlineStart: `calc(${size} * calc(1 - var(--tw-space-s-reverse)))`,
module.exports = ({ matchUtilities, addUtilities, theme }) => {
matchUtilities(
{
"space-s": (value) => ({
"> :not([hidden]) ~ :not([hidden])": {
"--tw-space-s-reverse": "0",
marginInlineEnd: `calc(${value} * var(--tw-space-s-reverse))`,
marginInlineStart: `calc(${value} * calc(1 - var(--tw-space-s-reverse)))`,
},
};
}),
},
];

const spaceReverse = {
'.space-s-reverse > :not([hidden]) ~ :not([hidden])': {
'--tw-space-s-reverse': '1',
{
supportsNegativeValues: true,
values: theme("space"),
}
);
addUtilities({
".space-s-reverse > :not([hidden]) ~ :not([hidden])": {
"--tw-space-s-reverse": "1",
},
};

return [
...generators.flatMap((generator) => Object.entries(theme('space')).flatMap(generator)),
spaceReverse,
];
});
};
Loading