Skip to content

Commit fbdc1ec

Browse files
authored
Update for Tailwind v4 (#247)
1 parent 0ae2adf commit fbdc1ec

File tree

11 files changed

+1478
-1750
lines changed

11 files changed

+1478
-1750
lines changed

.changeset/chatty-days-sip.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"tailwindcss-capsize": major
3+
---
4+
5+
Update plugin for Tailwind v4 support
6+
7+
v4 was a major change to how projects are configured as well as what plugins are allowed to modify. Previously, this plugin disabled `corePlugins` like `fontSize` in order to include custom CSS properties within the same utilities. This is no longer possible in v4, so while the *usage* hasn't changed, the CSS being output now includes duplicate declarations — one from the plugin and one from Tailwind itself.
8+
9+
v4 also allows configuration within CSS itself. However, this plugin relies on object values which aren't supported in CSS, so a JavaScript config file is still required. You can either use the JS file for all your settings, or just the settings for this plugin and configure the rest of your project in CSS.
10+
11+
The `mode` option has also been removed. This also removes the dependency on `@capsizecss/core`.

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ jobs:
1010
- name: Checkout repo
1111
uses: actions/checkout@v3
1212

13-
- name: Setup Node.js 18.x
13+
- name: Setup Node.js 20.x
1414
uses: actions/setup-node@v3.1.1
1515
with:
16-
node-version: 18.x
16+
node-version: 20.x
1717

1818
- name: Use cached node_modules
19-
uses: actions/cache@v3.0.2
19+
uses: actions/cache@v4
2020
with:
2121
path: node_modules
2222
key: nodeModules-${{ hashFiles('**/package-lock.json') }}

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
with:
1717
fetch-depth: 0
1818

19-
- name: Setup Node.js 18.x
19+
- name: Setup Node.js 20.x
2020
uses: actions/setup-node@v3.1.1
2121
with:
22-
node-version: 18.x
22+
node-version: 20.x
2323
cache: npm
2424

2525
- name: Install dependencies

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.DS_Store
33
node_modules
44
dist
5+
__tests__/tailwind.config.js

README.md

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
[![npm version][npm-img]][npm-url]
44

5-
> A TailwindCSS plugin that generates leading-trim utility classes using [Capsize](https://github.com/seek-oss/capsize).
5+
> A TailwindCSS plugin that generates text-box-trim utility classes using [Capsize].
66
77
```bash
88
npm install --save-dev tailwindcss-capsize
99
```
1010

11-
## leading-trim?
11+
## text-box-trim?
1212

13-
A [proposed CSS property](https://www.w3.org/TR/css-inline-3/#leading-trim) to remove the extra space from text bounding boxes, which affects optical alignment. This [article from Microsoft Design][] outlines the problem and how the proposed solution works.
13+
Formerly known as `leading-trim`, a [proposed CSS property] to remove the extra space from text bounding boxes, which affects optical alignment. This [article from Microsoft Design] outlines the problem and how the proposed solution works.
1414

1515
## Configuration
1616

17-
This plugin requires a `fontMetrics` key added to your Tailwind theme. It should be an object with keys matching those in your `fontFamily` definitions, and each key should have an object of the following shape:
17+
A JavaScript config file is required due to the values needed by the plugin.
18+
19+
```css
20+
@config "../tailwind.config.js";
21+
```
22+
23+
Other Tailwind settings can be configured through CSS directives or included in the config file.
24+
25+
This plugin requires a `fontMetrics` key added to your Tailwind theme. It should
26+
be an object with keys matching those in your `fontFamily` definitions, and each key should have an object of the following shape:
1827

1928
```ts
2029
{
@@ -26,18 +35,20 @@ This plugin requires a `fontMetrics` key added to your Tailwind theme. It should
2635
}
2736
```
2837

29-
These values can be determined by using the [Capsize website](https://seek-oss.github.io/capsize/), [fontkit](https://github.com/foliojs/fontkit), [FontDrop!](https://fontdrop.info), or some other means.
38+
These values can be determined by using the [Capsize website], [fontkit], [FontDrop!], or some other means.
3039

3140
### A full example
3241

3342
```js
3443
// tailwind.config.js
35-
module.exports = {
44+
import pluginCapsize from 'tailwindcss-capsize'
45+
export default {
3646
theme: {
3747
fontFamily: {
3848
sans: ['Inter', 'sans-serif'],
3949
},
4050
fontMetrics: {
51+
// Keys here must match those in fontFamily.
4152
sans: {
4253
capHeight: 2048,
4354
ascent: 2728,
@@ -46,11 +57,9 @@ module.exports = {
4657
unitsPerEm: 2816,
4758
},
4859
},
49-
fontSize: { ... },
50-
lineHeight: { ... },
51-
...
60+
// ...
5261
},
53-
plugins: [require('tailwindcss-capsize')],
62+
plugins: [pluginCapsize],
5463
}
5564
```
5665

@@ -73,7 +82,7 @@ The new `.capsize` utility class should be applied to the _direct parent_ elemen
7382
The plugin assumes a root font-size of `16px` when converting from rem values. To use a different value, pass it in (without units) to the plugin options.
7483

7584
```js
76-
require('tailwindcss-capsize')({ rootSize: 12 })
85+
pluginCapsize({ rootSize: 12 })
7786
```
7887

7988
### className
@@ -83,30 +92,7 @@ require('tailwindcss-capsize')({ rootSize: 12 })
8392
The default `.capsize` utility class can be replaced with a custom class name if preferred.
8493

8594
```js
86-
require('tailwindcss-capsize')({ className: 'leading-trim' })
87-
```
88-
89-
### mode
90-
91-
#### type: `'modern' | 'classic'` (optional, default: `'modern'`)
92-
93-
By default the plugin replaces the `fontFamily`, `fontSize`, and `lineHeight` core plugins, adding custom properties to the output of each which are used in the `calc()` expressions in the utility class.
94-
95-
```diff
96-
.font-sans {
97-
+ --ascent-scale: 0.9688;
98-
+ --descent-scale: 0.2415;
99-
+ --cap-height-scale: 0.7273;
100-
+ --line-gap-scale: 0;
101-
+ --line-height-scale: 1.2102;
102-
font-family: Inter, sans-serif;
103-
}
104-
```
105-
106-
If you need to support browsers that don’t support custom properties, setting `mode` to `'classic'` will handle all the calculation at build-time before the CSS is output. This will require that the markup matches a specific selector format.
107-
108-
```js
109-
require('tailwindcss-capsize')({ mode: 'classic' })
95+
pluginCapsize({ className: 'leading-trim' })
11096
```
11197

11298
## Tips and tricks
@@ -135,8 +121,14 @@ To solve this, a child element to the element with the `.capsize` class should w
135121

136122
## Related
137123

138-
[🔡 tailwindcss-opentype](https://github.com/stormwarning/tailwindcss-opentype) — Utility classes for advanced typographic features.
124+
[🔡 tailwindcss-opentype] — Utility classes for advanced typographic features.
139125

140126
[npm-url]: https://www.npmjs.com/package/tailwindcss-capsize
141127
[npm-img]: https://img.shields.io/npm/v/tailwindcss-capsize.svg?style=flat-square
128+
[Capsize]: https://github.com/seek-oss/capsize
129+
[proposed CSS property]: https://www.w3.org/TR/css-inline-3/#leading-trim
142130
[article from microsoft design]: https://medium.com/microsoft-design/leading-trim-the-future-of-digital-typesetting-d082d84b202
131+
[Capsize website]: https://seek-oss.github.io/capsize/
132+
[fontkit]: https://github.com/foliojs/fontkit
133+
[FontDrop!]: https://fontdrop.info
134+
[🔡 tailwindcss-opentype]: https://github.com/stormwarning/tailwindcss-opentype

0 commit comments

Comments
 (0)