Skip to content

Commit

Permalink
tailwind 2 compatibility,
Browse files Browse the repository at this point in the history
added default values
  • Loading branch information
MathGeniusJodie committed Nov 23, 2020
1 parent ae47ada commit 8949184
Show file tree
Hide file tree
Showing 3 changed files with 625 additions and 18 deletions.
84 changes: 72 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
const plugin = require("tailwindcss/plugin");
const { map, fromPairs, find } = require("lodash");
const { map, fromPairs, find, isPlainObject, get } = require("lodash");

const parser = require("postcss-selector-parser");

const escapeClassName = (className) => {
const node = parser.className();
node.value = className;
return get(node, "raws.value", node.value);
};

const stripUnit = (string) => string.match(/[\d.]+/)[0];

const asClass = (name) => `.${escapeClassName(name)}`;

const nameClass = (classPrefix, key) => {
if (key === "DEFAULT") {
return asClass(classPrefix);
}

if (key === "-") {
return asClass(`-${classPrefix}`);
}

if (key.startsWith("-")) {
return asClass(`-${classPrefix}${key}`);
}

return asClass(`${classPrefix}-${key}`);
};

const offsetFromLineHeight = (value) => {
const valueStripped = stripUnit(value);

return valueStripped === value
? (Number(value) - 1) / -2 + "em"
: `calc( (${value} - 1em) / -2)`;
};

const fontMetricsList = require("./metrics.json");

module.exports = plugin(
({ addUtilities, e, theme, variants }) => {
addUtilities({
".trim-start": {
"margin-top":
"calc( var(--leading-offset) + var(--font-offset-start,0) )",
"calc( var(--leading-offset,-.25em) + var(--font-offset-start,0em) )",
},
".trim-end": {
"margin-bottom":
"calc( var(--leading-offset) + var(--font-offset-end,0) )",
"calc( var(--leading-offset,-.25em) + var(--font-offset-end,0em) )",
},
".trim-both": {
"margin-top":
"calc( var(--leading-offset) + var(--font-offset-start,0) )",
"calc( var(--leading-offset,-.25em) + var(--font-offset-start,0em) )",
"margin-bottom":
"calc( var(--leading-offset) + var(--font-offset-end,0) )",
"calc( var(--leading-offset,-.25em) + var(--font-offset-end,0em) )",
},
});

Expand Down Expand Up @@ -65,19 +99,44 @@ module.exports = plugin(

addUtilities(fontFamilyUtilities, variants("fontFamily"));

const fontSizeUtilities = fromPairs(
map(theme("fontSize"), (value, modifier) => {
const [fontSize, options] = Array.isArray(value) ? value : [value];
const { lineHeight, letterSpacing } = isPlainObject(options)
? options
: {
lineHeight: options,
};

return [
nameClass("text", modifier),
{
"font-size": fontSize,
...(lineHeight === undefined
? {}
: {
"line-height": lineHeight,
"--leading-offset": offsetFromLineHeight(lineHeight),
}),
...(letterSpacing === undefined
? {}
: {
"letter-spacing": letterSpacing,
}),
},
];
})
);

addUtilities(fontSizeUtilities, variants("fontSize"));

const lineHeightUtilities = fromPairs(
map(theme("lineHeight"), (value, modifier) => {
const valueStripped = stripUnit(value);

const offset =
valueStripped === value
? (Number(value) - 1) / -2 + "em"
: `calc( (${value} - 1em) / -2)`;
return [
`.${e(`leading-${modifier}`)}`,
{
"line-height": value,
"--leading-offset": offset,
"--leading-offset": offsetFromLineHeight(value),
},
];
})
Expand All @@ -88,6 +147,7 @@ module.exports = plugin(
{
corePlugins: {
fontFamily: false,
fontSize: false,
lineHeight: false,
},
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
],
"license": "WTFPL",
"devDependencies": {
"tailwindcss": "^1.7.6",
"tailwindcss-rtl": "^0.4.0"
"tailwindcss": "^2.0.1",
"tailwindcss-rtl": "^0.6.0",
"postcss": "^8.1.9"
},
"dependencies": {
"capsize": "^1.1.0",
Expand Down
Loading

0 comments on commit 8949184

Please sign in to comment.