Skip to content
Open
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
36 changes: 17 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ function addSelectors(container, modifierFunction) {
return rules;
}

function generator({ addVariant, e }) {
addVariant('direction', ({ container, separator }) => {
const result = container.clone({ nodes: [] });
module.exports = function({ sameLevel = true } = {}) {
return function({ addVariant, e }) {
addVariant('direction', ({ container, separator }) => {
const result = container.clone({ nodes: [] });

['ltr', 'rtl'].forEach(dir => {
result.nodes = result.nodes.concat(
addSelectors(container, className => {
return [
`[dir='${dir}'] .${dir}${e(separator)}${className}`,
`[dir='${dir}'].${dir}${e(separator)}${className}`,
];
})
);
});
['ltr', 'rtl'].forEach(dir => {
result.nodes = result.nodes.concat(
addSelectors(container, className => {
return [
`[dir='${dir}'] .${dir}${e(separator)}${className}`,
... sameLevel ? [`[dir='${dir}'].${dir}${e(separator)}${className}`] : [],
];
})
);
});

return result;
});
}

module.exports = function() {
return generator;
return result;
});
}
};
7 changes: 0 additions & 7 deletions tests/config.js

This file was deleted.

53 changes: 51 additions & 2 deletions tests/variantGenerator.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const postcss = require('postcss');
const tailwindcss = require('tailwindcss');
const config = require('./config');
const defaultConfig = require('tailwindcss/defaultConfig');
const plugin = require('../src/index');

test('it generates direction variants', () => {
const input = `
Expand Down Expand Up @@ -40,10 +41,58 @@ test('it generates direction variants', () => {
}
`;

return postcss([tailwindcss(config)])
return postcss([tailwindcss({ ...defaultConfig, plugins: [plugin()] })])
.process(input, { from: undefined })
.then(result => {
expect(result.css).toMatchCss(output);
expect(result.warnings().length).toBe(0);
});
});

test('it generates direction variants without same level', () => {
const input = `
@variants direction {
.banana { color: yellow; }
.tomato { color: red; }
}
`;

const output = `
.banana {
color: yellow;
}

.tomato {
color: red;
}

[dir='ltr'] .ltr\\:banana {
color: yellow;
}

[dir='ltr'] .ltr\\:tomato {
color: red;
}

[dir='rtl'] .rtl\\:banana {
color: yellow;
}

[dir='rtl'] .rtl\\:tomato {
color: red;
}
`;

return postcss([tailwindcss({
...defaultConfig,
plugins: [
plugin({
sameLevel: false
})
]
})]).process(input, { from: undefined })
.then(result => {
expect(result.css).toMatchCss(output);
expect(result.warnings().length).toBe(0);
});
});