I’m focused on developing a Tailwind CSS plugin that generates custom typography scales. The idea is to provide code, an explanation, and configuration examples, but keep everything concise yet usable. I want the plugin to create utility classes like text-scale-0, text-scale-1, etc., based on a base font size and different ratios, or even map to existing header classes. I'll ensure it integrates with theme.typographyScale or theme.fontSize and supports responsive options as well.Refining the Tailwind plugin process
I'm working on a simplification of the Tailwind CSS plugin configuration. The basic setup involves defining plugin options in the tailwind.config.cjs file, but I realize that plugin options need to be passed correctly when requiring the plugin. I should create an instance with the recommended pattern, ensuring it generates utility classes like .type-xs to .type-lg based on the scale definitions from theme.typographyScale. Each class will have font-size, line-height, and possibly letter-spacing, while also allowing for customization.Recalling Tailwind plugin API
I'm reviewing the Tailwind CSS plugin API, focusing on the function format, which includes addUtilities, addComponents, addVariant, e, and theme. To enable customization, I can utilize plugin.withOptions. My main goals are to provide the plugin code in typography-scale.js, demonstrate how to configure it in tailwind.config.js, and show resulting utility classes. While the plugin will generate custom typography scales, it might be beneficial to integrate with the fontSize theme to enhance usability. I want to keep it simple while considering optional fluid typography support as well.Implementing typography scale plugin
I'm focused on implementing a typography scale plugin based on a defined theme. This includes settings for base size, ratio, and line height, along with specific steps for typography scaling. The plugin will need to compute fontSize entries that go into the theme extension. While I can use withOptions to return an extended theme, I want to be careful not to override existing font sizes; instead, I’ll aim to extend them safely. Additionally, I can create custom utility classes like .fs-xs.Finalizing typography scale plugin features
I'm considering how to implement a Tailwind CSS plugin for generating typography scales. I want to ensure it allows customization while also supporting default settings. The plugin will compute a fontSize scale based on user-defined configurations. I'll provide clear code and a structured example in the tailwind.config.js file. The focus will be on extending the fontSize theme and creating utility classes. I'll weigh the benefits of either extending fontSize or adding new custom utilities, leaning towards a more standalone approach that emphasizes simplicity and usability for the user.Structuring the typography scale plugin
I think the user wants a straightforward way to customize the typography scale without conflicting with built-in classes. The plugin should generate a theme.fontSize scale based on high-level parameters like base size and ratio to allow customization across the project. I'll set up the plugin to extend this scale, which makes sense for generating utility classes. The options will shape its functionality, allowing for a flexible mapping of steps while maintaining simplicity and intuitiveness. Finally, I'll provide examples of use in the tailwind.config.js file for clarity.Creating a typography scale plugin
I see the user is interested in a plugin that allows customizing typography, especially for an editorial site. They want a flexible way to define font sizes using options like base size and ratio. I'll implement steps that allow varied line heights and possibly letter spacing support while keeping the plugin simple. It’s important to define the structure clearly, including object entries for steps. I'll also include comprehensive examples in the config file, highlighting generated classes and their sizes for practical understanding. Lastly, I’ll clarify assumptions about using rem units for consistency. Here’s a complete, configurable Tailwind CSS plugin that generates a typography scale (and wires it into Tailwind’s fontSize so you can use text-* classes as usual).
1. Plugin file: plugins/typography-scale.js
js
// plugins/typography-scale.js
const plugin = require('tailwindcss/plugin');
/**
* Tailwind plugin to generate a modular typography scale.
*
* API:
* plugins: [
* typographyScale({
* baseSize: 1, // base font-size in rem (1rem = 16px)
* ratio: 1.25, // modular scale ratio
* lineHeight: 1.5, // default line-height for all sizes
* decimalPlaces: 4, // precision for rem values
* steps: {
* // name: stepIndex | [stepIndex, lineHeightForThisStep]
* xs: -2,
* sm: -1,
* base: 0,
* lg: 1,
* xl: 2,
* '2xl': 3,
* '3xl': 4,
* },
* }),
* ]
*
* Result:
* - Generates theme.fontSize entries so you can use:
* text-xs, text-sm, text-base, text-lg, text-xl, text-2xl, text-3xl
*/
module.exports = plugin.withOptions(
// Runtime plugin function (we only modify theme config, so this is a no-op)
function (_options = {}) {
return function () {
// Intentionally empty
};
},
// Config generator (runs at build-time to extend theme)
function (options = {}) {
const {
baseSize = 1, // in rem
ratio = 1.25, // modular scale ratio
lineHeight = 1.5, // default line-height
decimalPlaces = 4,
steps = {
xs: -2,
sm: -1,
base: 0,
lg: 1,
xl: 2,
'2xl': 3,
'3xl': 4,
'4xl': 5,
},
} = options;
/**
* steps can be:
* - number: step index
* - [number, lh]: [step index, line-height for that size]
*/
const fontSizeScale = Object.entries(steps).reduce(
(acc, [name, conf]) => {
let stepIndex;
let stepLineHeight = lineHeight;
if (Array.isArray(conf)) {
[stepIndex, stepLineHeight = lineHeight] = conf;
} else if (typeof conf === 'number') {
stepIndex = conf;
} else {
throw new Error(
`Invalid "steps" value for "${name}". ` +
`Use a number or [number, lineHeight].`
);
}
const sizeRem = (baseSize * Math.pow(ratio, stepIndex)).toFixed(
decimalPlaces
);
acc[name] = [`${sizeRem}rem`, { lineHeight: stepLineHeight }];
return acc;
},
{}
);
return {
theme: {
extend: {
fontSize: fontSizeScale,
},
},
};
}
);
Each with a font-size following the ratio and line-height you specified.
Example usage:
html
<p class="text-body">
Regular body text
</p>
<p class="text-body-sm text-gray-600">
Small body text
</p>
<h1 class="text-heading-xl font-semibold">
Page title
</h1>
<h2 class="text-heading-lg font-semibold">
Section title
</h2>
3. Example of a “Tailwind-like” scale override
If you simply want to redefine the standard text-xs, text-sm, text-base, etc. using a modular scale:
use your custom modular scale rather than Tailwind’s default numbers.
You can extend this pattern further (e.g., generate letter-spacing or fluid clamp() values per step), but this gives you a solid, production-ready plugin for generating custom typography scales.