Skip to content

Commit 1871f90

Browse files
sanscheesethecrypticacereinink
authored
Generate both global styles and classes by default (tailwindlabs#71)
* feat(option): allow both strategies * feat: base and class by default * Refactor * Update changelog * Rework readme Co-authored-by: Jordan Pittman <[email protected]> Co-authored-by: Jonathan Reinink <[email protected]>
1 parent 9a0c0e4 commit 1871f90

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
Nothing yet!
10+
### Changed
11+
12+
- Generate both global styles and classes by default ([#71](https://github.com/tailwindlabs/tailwindcss-forms/pull/71))
1113

1214
## [0.4.1] - 2022-03-02
1315

README.md

+22-15
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,9 @@ Every element has been normalized/reset to a simple visually consistent style th
6767

6868
More customization examples and best practices coming soon.
6969

70-
### Using classes instead of element selectors
70+
### Using classes to style
7171

72-
Although we recommend thinking of this plugin as a "form reset" rather than a collection of form component styles, in some cases our default approach may be too heavy-handed, especially when integrating this plugin into existing projects.
73-
74-
For situations where the default strategy doesn't work well with your project, you can use the `class` strategy to make all form styling _opt-in_ instead of applied globally:
75-
76-
```js
77-
// tailwind.config.js
78-
plugins: [
79-
require("@tailwindcss/forms")({
80-
strategy: 'class',
81-
}),
82-
],
83-
```
84-
85-
When using the `class` strategy, form elements do not receive any reset styles by default, and reset styles are added per element using a new set of `form-*` classes generated by the plugin:
72+
In addition to the global styles, we also generate a set of corresponding classes which can be used to explicitly apply the form styles to an element. This can be useful in situations where you need to make a non-form element, such as a `<div>`, look like a form element.
8673

8774
```html
8875
<input type="email" class="form-input px-4 py-3 rounded-full">
@@ -115,3 +102,23 @@ Here is a complete table of the provided `form-*` classes for reference:
115102
| `select[multiple]` | `form-multiselect` |
116103
| `[type='checkbox']` | `form-checkbox` |
117104
| `[type='radio']` | `form-radio` |
105+
106+
### Using only global styles or only classes
107+
108+
Although we recommend thinking of this plugin as a "form reset" rather than a collection of form component styles, in some cases our default approach may be too heavy-handed, especially when integrating this plugin into existing projects.
109+
110+
If generating both the global (base) styles and classes doesn't work well with your project, you can use the `strategy` option to limit the plugin to just one of these approaches.
111+
112+
```js
113+
// tailwind.config.js
114+
plugins: [
115+
require("@tailwindcss/forms")({
116+
strategy: 'base', // only generate global styles
117+
strategy: 'class', // only generate classes
118+
}),
119+
],
120+
```
121+
122+
When using the `base` strategy, form elements are styled globally, and no `form-{name}` classes are generated.
123+
124+
When using the `class` strategy, form elements are not styled globally, and instead must be styled using the generated `form-{name}` classes.

src/index.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const colors = require('tailwindcss/colors')
55
const [baseFontSize, { lineHeight: baseLineHeight }] = defaultTheme.fontSize.base
66
const { spacing, borderWidth, borderRadius } = defaultTheme
77

8-
const forms = plugin.withOptions(function (options = { strategy: 'base' }) {
8+
const forms = plugin.withOptions(function (options = { strategy: undefined }) {
99
return function ({ addBase, addComponents, theme }) {
10-
const strategy = options.strategy
10+
const strategy = options.strategy === undefined ? ['base', 'class'] : [options.strategy]
1111

1212
const rules = [
1313
{
@@ -283,20 +283,21 @@ const forms = plugin.withOptions(function (options = { strategy: 'base' }) {
283283
},
284284
]
285285

286-
const strategyRules = rules
286+
const getStrategyRules = (strategy) => rules
287287
.map((rule) => {
288-
if (rule[strategy] === null) {
289-
return null
290-
}
288+
if (rule[strategy] === null) return null
291289

292290
return { [rule[strategy]]: rule.styles }
293291
})
294292
.filter(Boolean)
295293

296-
;({
297-
'base': addBase,
298-
'class': addComponents
299-
})[strategy](strategyRules)
294+
if (strategy.includes('base')) {
295+
addBase(getStrategyRules('base'))
296+
}
297+
298+
if (strategy.includes('class')) {
299+
addComponents(getStrategyRules('class'))
300+
}
300301
}
301302
})
302303

0 commit comments

Comments
 (0)