Created
July 15, 2022 09:55
-
-
Save yuheiy/a84ca3dd74d36ccad7eba89ab515aa36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const plugin = require("tailwindcss/plugin"); | |
const rem = (px) => `${px / 16}rem`; | |
// https://www.smashingmagazine.com/2022/01/modern-fluid-typography-css-clamp/ | |
const getFluidSize = (minSize, maxSize, minWidth = 640, maxWidth = 1280) => { | |
const v = (100 * (maxSize - minSize)) / (maxWidth - minWidth); | |
const r = (minWidth * maxSize - maxWidth * minSize) / (minWidth - maxWidth); | |
return `clamp(${rem(minSize)}, ${v.toPrecision(3)}vw + ${rem(r.toPrecision(3))}, ${rem( | |
maxSize | |
)})`; | |
}; | |
const fluidText = plugin(function ({ matchUtilities }) { | |
matchUtilities({ | |
"fluid-text": (value) => { | |
const [minSize, maxSize] = value.split(",").map((v) => { | |
const matched = /^(\d+)px$/.exec(v); | |
if (!matched) { | |
throw new Error(`"${v}" is not a valid value`); | |
} | |
return Number(matched[1]); | |
}); | |
return { | |
"font-size": getFluidSize(minSize, maxSize), | |
}; | |
}, | |
}); | |
}); | |
module.exports = { | |
fluidText, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment