// tailwind-typography-scale.js
// A Tailwind CSS plugin for generating custom typography scale utilities.
//
// ## Installation
// 1. Save this as `tailwind-typography-scale.js` in your project.
// 2. Add it to your `tailwind.config.js`:
// ```js
// module.exports = {
// theme: {
// extend: {
// typography: {
// scale: [
// // h1: Display
// { fontSize: '3.75rem', lineHeight: 1, letterSpacing: '-0.05em', fontWeight: '700' },
// // h2: Hero / Display
// { fontSize: '3rem', lineHeight: 1, letterSpacing: '-0.04em', fontWeight: '700' },
// // h3: Hero / Display
// { fontSize: '2.25rem', lineHeight: 1.1, letterSpacing: '-0.03em', fontWeight: '700' },
// // h4: Headline
// { fontSize: '1.875rem', lineHeight: 1.2, letterSpacing: '-0.02em', fontWeight: '700' },
// // h5: Title
// { fontSize: '1.5rem', lineHeight: 1.3, letterSpacing: '-0.01em', fontWeight: '600' },
// // h6: Title
// { fontSize: '1.25rem', lineHeight: 1.4, fontWeight: '600' },
// // Body / Lead
// { fontSize: '1.125rem', lineHeight: 1.6, fontWeight: '400' },
// // Body
// { fontSize: '1rem', lineHeight: 1.6, fontWeight: '400' },
// // Caption / Small
// { fontSize: '0.875rem', lineHeight: 1.6, fontWeight: '400' },
// // Fine / Micro
// { fontSize: '0.75rem', lineHeight: 1.5, fontWeight: '400' },
// ],
// },
// },
// },
// plugins: [
// require('./tailwind-typography-scale'),
// ],
// }
// ```
// 3. Run your Tailwind build process. Now use classes like `text-scale-1`, `text-scale-2`, etc.
//
// ## Features
// - Generates `text-scale-1`, `text-scale-2`, ..., `text-scale-N` utilities.
// - Supports any CSS typography properties (fontSize, lineHeight, letterSpacing, fontWeight, etc.).
// - Fluid typography: Use `clamp(2rem, 5vw, 4rem)` in your config values.
// - Responsive: Works with prefixes like `md:text-scale-3 lg:text-scale-1`.
// - Variants: Hover, focus, dark mode, etc., work automatically.
// - Zero-config example scale provided above (Modular Scale inspired).
//
// ## Customization
// - Override `theme.typography.scale` in your config.
// - Scale is an array of objects; order matters (index + 1 = class name).
// - Add custom properties (e.g., `{ fontSize: '...', fontFamily: 'serif' }`).
const camelToKebab = (str) => str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
function typographyScalePlugin({ addUtilities, matchUtilities, theme, e }) {
const scale = theme('typography.scale', []);
// Static utilities (e.g., text-scale-1)
const utilities = {};
scale.forEach((step, index) => {
const className = e(`text-scale-${index + 1}`);
utilities[className] = {};
Object.entries(step).forEach(([key, value]) => {
const cssProp = camelToKebab(key);
utilities[className][cssProp] = value;
});
});
addUtilities(utilities);
// Arbitrary value support (e.g., text-scale-[2.5])
matchUtilities(
{
'text-scale': (value) => {
const step = scale[parseFloat(value) - 1];
if (!step) return null;
const styles = {};
Object.entries(step).forEach(([key, stepValue]) => {
const cssProp = camelToKebab(key);
styles[cssProp] = stepValue;
});
return styles;
},
},
{ values: scale.map((_, i) => (i + 1).toString()) }
);
}
module.exports = typographyScalePlugin;
Usage Examples
<!-- Basic -->
<h1 class="text-scale-1 font-bold">Heading 1</h1>
<p class="text-scale-8">Body text</p>
<small class="text-scale-10">Fine text</small>
<!-- Responsive -->
<h1 class="text-scale-3 md:text-scale-1 lg:text-scale-2">Responsive Heading</h1>
<!-- Fluid (define in config) -->
<!-- Config: { fontSize: 'clamp(2rem, 5vw, 5rem)' } -->
<h1 class="text-scale-1">Fluid Heading</h1>
<!-- Arbitrary (Tailwind v3.1+) -->
<p class="text-scale-[6.5]">Interpolated scale (between 6 & 7)</p>
<!-- With variants -->
<h1 class="dark:text-scale-2 hover:text-scale-1 transition-all">Dark mode + hover</h1>
This plugin is production-ready, lightweight (~1KB minified), and fully extensible. It uses Tailwind's matchUtilities for arbitrary values (Tailwind v3.1+). Test it in your project and tweak the default scale as needed!