You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-reference.md
+69-24Lines changed: 69 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -325,35 +325,80 @@ interface Validators {
325
325
}
326
326
```
327
327
328
-
An object containing all the validators used in tailwind-merge. They are useful if you want to use a custom config with [`extendTailwindMerge`](#extendtailwindmerge) or [`createTailwindMerge`](#createtailwindmerge). E.g. the `classGroup` for padding is defined as
328
+
An object containing all the validators used in tailwind-merge. They are useful if you want to use a custom config with [`extendTailwindMerge`](#extendtailwindmerge) or [`createTailwindMerge`](#createtailwindmerge).
- `isAny` always returns true. Be careful with this validator as it might match unwanted classes. I use it primarily to match colors or when I'm certain there are no other class groups in a namespace.
337
-
- `isAnyNonArbitrary` checks if the class part is not an arbitrary value or arbitrary variable.
338
-
- `isArbitraryImage` checks whether class part is an arbitrary value which is an image, e.g. by starting with `image:`, `url:`, `linear-gradient(` or `url(` (`[url('/path-to-image.png')]`, `image:var(--maybe-an-image-at-runtime)]`) which is necessary for background-image classNames.
339
-
- `isArbitraryLength` checks for arbitrary length values (`[3%]`, `[4px]`, `[length:var(--my-var)]`).
340
-
- `isArbitraryNumber` checks whether class part is an arbitrary value which starts with `number:` or is a number (`[number:var(--value)]`, `[450]`) which is necessary for font-weight and stroke-width classNames.
341
-
- `isArbitraryPosition` checks whether class part is an arbitrary value which starts with `position:` (`[position:200px_100px]`) which is necessary for background-position classNames.
342
-
- `isArbitraryShadow` checks whether class part is an arbitrary value which starts with the same pattern as a shadow value (`[0_35px_60px_-15px_rgba(0,0,0,0.3)]`), namely with two lengths separated by a underscore, optionally prepended by `inset`.
343
-
- `isArbitrarySize` checks whether class part is an arbitrary value which starts with `size:` (`[size:200px_100px]`) which is necessary for background-size classNames.
344
-
- `isArbitraryValue` checks whether the class part is enclosed in brackets (`[something]`)
345
-
- `isArbitraryVariable` checks whether the class part is an arbitrary variable (`(--my-var)`)
346
-
- `isArbitraryVariableFamilyName` checks whether class part is an arbitrary variable with the `family-name` label (`(family-name:--my-font)`)
347
-
- `isArbitraryVariableImage` checks whether class part is an arbitrary variable with the `image` or `url` label (`(image:--my-image)`)
348
-
- `isArbitraryVariableLength` checks whether class part is an arbitrary variable with the `length` label (`(length:--my-length)`)
349
-
- `isArbitraryVariablePosition` checks whether class part is an arbitrary variable with the `position` label (`(position:--my-position)`)
350
-
- `isArbitraryVariableShadow` checks whether class part is an arbitrary variable with the `shadow` label or not label at all (`(shadow:--my-shadow)`, `(--my-shadow)`)
351
-
- `isArbitraryVariableSize` checks whether class part is an arbitrary variable with the `size`, `length` or `percentage` label (`(size:--my-size)`)
352
-
- `isFraction` checks whether class part is a fraction of two numbers (`1/2`, `127/256`)
353
-
- `isInteger` checks for integer values (`3`).
354
-
- `isNumber` checks for numbers (`3`, `1.5`)
355
-
- `isPercent` checks for percent values (`12.5%`) which is used for color stop positions.
356
-
- `isTshirtSize` checks whether class part is a T-shirt size (`sm`, `xl`), optionally with a preceding number (`2xl`).
337
+
### Simple Type Validators
338
+
339
+
These validators check for basic patterns and types:
| `isAny` | Always returns `true`. Use carefully - matches everything. Best when certain no other class groups exist in a namespace. | Matches any value |
344
+
| `isAnyNonArbitrary` | Checks if class part is NOT an arbitrary value or variable | `red`, `lg`, `4` |
Copy file name to clipboardExpand all lines: docs/configuration.md
+100-8Lines changed: 100 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -107,16 +107,28 @@ Sometimes there are conflicts across Tailwind classes which are more complex tha
107
107
108
108
One example is the combination of the classes `px-3` (setting `padding-left` and `padding-right`) and `pr-4` (setting `padding-right`).
109
109
110
-
If they are passed to `twMerge` as `pr-4 px-3`, you most likely intend to apply `padding-left` and `padding-right` from the `px-3` class and want `pr-4` to be removed, indicating that both these classes should belong to a single class group.
110
+
**Scenario 1**: When classes are ordered as `pr-4 px-3`:
111
111
112
-
But if they are passed to `twMerge` as `px-3 pr-4`, you want to set the `padding-right` from `pr-4` but still want to apply the `padding-left` from `px-3`, so `px-3` shouldn't be removed when inserting the classes in this order, indicating they shouldn't be in the same class group.
112
+
- You want `px-3` to apply both `padding-left` and `padding-right`
113
+
- The earlier `pr-4` should be removed since `px-3` also sets `padding-right`
114
+
- Result: `twMerge('pr-4 px-3') // → 'px-3'`
113
115
114
-
To summarize, `px-3` should stand in conflict with `pr-4`, but `pr-4` should not stand in conflict with `px-3`. To achieve this, we need to define asymmetric conflicts across class groups.
116
+
**Scenario 2**: When classes are ordered as `px-3 pr-4`:
115
117
116
-
This is what the `conflictingClassGroups` object in the tailwind-merge config is for. You define a key in it which is the ID of a class group which _creates_ a conflict and the value is an array of IDs of class group which _receive_ a conflict.
118
+
- You want `px-3` to set `padding-left`
119
+
- You want `pr-4` to override just the `padding-right` from `px-3`
120
+
- The `px-3` class should NOT be removed
121
+
- Result: `twMerge('px-3 pr-4') // → 'px-3 pr-4'`
122
+
123
+
To summarize, `px-3` should stand in conflict with `pr-4`, but `pr-4` should not stand in conflict with `px-3`. To achieve this, we need to define **asymmetric conflicts** across class groups.
124
+
125
+
#### Defining asymmetric conflicts
126
+
127
+
This is what the `conflictingClassGroups` object in the tailwind-merge config is for. You define a key in it which is the ID of a class group which _creates_ a conflict and the value is an array of IDs of class groups which _receive_ a conflict.
117
128
118
129
```ts
119
130
const conflictingClassGroups = {
131
+
// ↓ px creates a conflict with pr and pl
120
132
px: ['pr', 'pl'],
121
133
}
122
134
```
@@ -125,6 +137,17 @@ If a class group _creates_ a conflict, it means that if it appears in a class li
125
137
126
138
When we think of our example, the `px` class group creates a conflict which is received by the class groups `pr` and `pl`. This way `px-3` removes a preceding `pr-4`, but not the other way around.
### Postfix modifiers conflicting with class groups
129
152
130
153
Tailwind CSS allows postfix modifiers for some classes. E.g. you can set font-size and line-height together with `text-lg/7` with `/7` being the postfix modifier. This means that any line-height classes preceding a font-size class with a modifier should be removed.
@@ -174,22 +197,91 @@ In the Tailwind config you can modify your theme variable namespace to add class
174
197
175
198
If you modified one of the theme namespaces in your Tailwind config, you need to add the variable names to the `theme` object in tailwind-merge as well so that tailwind-merge knows about them.
176
199
177
-
E.g. let's say you added the variable `--text-huge-af: 100px` to your Tailwind config which enables classes like `text-huge-af`. To make sure that tailwind-merge merges these classes correctly, you need to configure tailwind-merge like this:
200
+
#### Example: Adding custom font sizes
201
+
202
+
Let's say you added a custom font size variable `--text-huge: 100px` to your Tailwind config:
203
+
204
+
```css
205
+
/* In your CSS or Tailwind config */
206
+
@theme {
207
+
--text-huge: 100px;
208
+
}
209
+
```
210
+
211
+
This enables the class `text-huge` in your HTML. To make sure tailwind-merge merges these classes correctly, you need to configure tailwind-merge like this:
178
212
179
213
```ts
180
214
import { extendTailwindMerge } from 'tailwind-merge'
181
215
182
-
consttwMerge=extendTailwindMerge({
216
+
const customTwMerge = extendTailwindMerge({
183
217
extend: {
184
218
theme: {
185
219
// ↓ `text` is the key of the namespace `--text-*`
186
-
// ↓ `huge-af` is the variable name in the namespace
187
-
text: ['huge-af'],
220
+
// ↓ `huge` is the variable name without the namespace prefix
221
+
text: ['huge'],
188
222
},
189
223
},
190
224
})
225
+
226
+
// Now tailwind-merge correctly handles your custom font size
**Note**: The `spacing` theme scale is used by many utilities including padding (`p-*`), margin (`m-*`), gap (`gap-*`), top/right/bottom/left positioning, and more. Adding a value to the `spacing` theme makes it available across all these utilities.
261
+
262
+
#### Note about custom colors
263
+
264
+
Custom colors in the `--color-*` namespace **do not need to be configured** in tailwind-merge. The library uses a permissive validator that accepts any color name, so custom colors work out of the box:
This applies to all color utilities: `bg-*`, `text-*`, `border-*`, `ring-*`, etc.
284
+
193
285
### Extending the tailwind-merge config
194
286
195
287
If you only need to slightly modify the default tailwind-merge config, [`extendTailwindMerge`](./api-reference.md#extendtailwindmerge) is the easiest way to extend the config. You provide it a `configExtension` object which gets [merged](./api-reference.md#mergeconfigs) with the default config. Therefore, all keys here are optional.
0 commit comments