Skip to content

Commit 46053d4

Browse files
authored
New: Add custom className option (#103)
Fixes #28
1 parent ae2102f commit 46053d4

File tree

6 files changed

+85
-12
lines changed

6 files changed

+85
-12
lines changed

.changeset/empty-flies-exist.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'tailwindcss-capsize': minor
3+
---
4+
5+
Allow custom activation class via `className` option
6+
7+
```js
8+
require('tailwindcss-capsize')({ className: 'leading-trim' })
9+
```

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,27 @@ module.exports = {
5050
}
5151
```
5252

53+
## Usage
54+
55+
The new `.capsize` utility class should be applied to the _direct parent_ element surrounding a text node. This class only provides the neccessary styles for trimming whitespace, utility classes for setting `font-family`, `font-size`, and `line-height` will need to be applied as well.
56+
57+
## Options
58+
59+
### rootSize
60+
5361
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.
5462

5563
```js
5664
require('tailwindcss-capsize')({ rootSize: 12 })
5765
```
5866

59-
## Usage
67+
### className
6068

61-
The new `.capsize` utility class should be applied to the _direct parent_ element surrounding a text node. This class only provides the neccessary styles for trimming whitespace, utility classes for setting `font-family`, `font-size`, and `line-height` will need to be applied as well.
69+
The default `.capsize` utility class can be replaced with a custom class name if preferred.
70+
71+
```js
72+
require('tailwindcss-capsize')({ className: 'leading-trim' })
73+
```
6274

6375
[npm-url]: https://www.npmjs.com/package/tailwindcss-capsize
6476
[npm-img]: https://img.shields.io/npm/v/tailwindcss-capsize.svg?style=flat-square

__tests__/plugin.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,48 @@ describe('Plugin', () => {
325325
`)
326326
})
327327
})
328+
329+
it('generates utility classes with a custom activation class', async () => {
330+
return await postcss(
331+
tailwindcss({
332+
theme: {
333+
...THEME_CONFIG,
334+
fontSize: {
335+
sm: '1rem',
336+
},
337+
lineHeight: {
338+
md: '1.5',
339+
},
340+
},
341+
corePlugins: false,
342+
plugins: [capsizePlugin({ className: 'leading-trim' })],
343+
}),
344+
)
345+
.process('@tailwind components; @tailwind utilities', {
346+
from: undefined,
347+
})
348+
.then((result) => {
349+
expect(result.css).toMatchCss(`
350+
.font-sans.text-sm.leading-md.leading-trim::before,
351+
.font-sans .text-sm.leading-md.leading-trim::before,
352+
.font-sans .text-sm .leading-md.leading-trim::before,
353+
.text-sm .font-sans.leading-md.leading-trim::before,
354+
.text-sm .font-sans .leading-md.leading-trim::before {
355+
content: '';
356+
margin-bottom: -0.3864em;
357+
display: table;
358+
}
359+
360+
.font-sans.text-sm.leading-md.leading-trim::after,
361+
.font-sans .text-sm.leading-md.leading-trim::after,
362+
.font-sans .text-sm .leading-md.leading-trim::after,
363+
.text-sm .font-sans.leading-md.leading-trim::after,
364+
.text-sm .font-sans .leading-md.leading-trim::after {
365+
content: '';
366+
margin-top: -0.3864em;
367+
display: table;
368+
}
369+
`)
370+
})
371+
})
328372
})

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugin.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { makeCssSelectors, normalizeValue } from './utils'
77

88
export interface PluginOptions {
99
/** The root font-size, in pixels */
10-
rootSize: number
10+
rootSize?: number
11+
/** Custom utility classname */
12+
className?: string
1113
}
1214

1315
export default plugin.withOptions<Partial<PluginOptions>>(
14-
({ rootSize = 16 } = {}) => {
16+
({ rootSize = 16, className = 'capsize' } = {}) => {
1517
return function ({ addUtilities, theme }) {
1618
let fontMetrics = theme('fontMetrics', {}) as Record<
1719
string,
@@ -43,7 +45,12 @@ export default plugin.withOptions<Partial<PluginOptions>>(
4345
})
4446

4547
utilities[
46-
makeCssSelectors(fontFamily, sizeName, leading)
48+
makeCssSelectors(
49+
fontFamily,
50+
sizeName,
51+
leading,
52+
className,
53+
)
4754
] = {
4855
'&::before': before,
4956
'&::after': after,

src/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ export function makeCssSelectors(
3737
fontFamily: string,
3838
sizeName: string,
3939
leading: string,
40+
className: string,
4041
): string {
4142
return (
42-
`.font-${fontFamily}.text-${sizeName}.leading-${leading}.capsize,` +
43-
`.font-${fontFamily} .text-${sizeName}.leading-${leading}.capsize,` +
44-
`.font-${fontFamily} .text-${sizeName} .leading-${leading}.capsize,` +
45-
`.text-${sizeName} .font-${fontFamily}.leading-${leading}.capsize,` +
46-
`.text-${sizeName} .font-${fontFamily} .leading-${leading}.capsize`
43+
`.font-${fontFamily}.text-${sizeName}.leading-${leading}.${className},` +
44+
`.font-${fontFamily} .text-${sizeName}.leading-${leading}.${className},` +
45+
`.font-${fontFamily} .text-${sizeName} .leading-${leading}.${className},` +
46+
`.text-${sizeName} .font-${fontFamily}.leading-${leading}.${className},` +
47+
`.text-${sizeName} .font-${fontFamily} .leading-${leading}.${className}`
4748
)
4849
}

0 commit comments

Comments
 (0)