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
function extendTailwindMerge(...createConfig:Array<(config:Config) =>Config>):TailwindMerge
81
+
function extendTailwindMerge(...createConfig:((config:Config) =>Config)[]):TailwindMerge
80
82
```
81
83
82
-
Function to create merge function with custom config which extends the default config. Use this if you use the default Tailwind config and just extend it in some places.
84
+
Function to create merge function with custom config which extends the default config. Use this if you use the default Tailwind config and just modified it in some places.
83
85
84
86
> **Note**
85
87
> The function `extendTailwindMerge` computes a large data structure based on the config passed to it. I recommend to call it only once and store the result in a top-level variable instead of calling it inline within another repeatedly called function.
@@ -88,40 +90,82 @@ You provide it a `configExtension` object which gets [merged](#mergeconfigs) wit
88
90
89
91
```ts
90
92
const customTwMerge = extendTailwindMerge({
91
-
cacheSize: 0, // ← Disabling cache
93
+
// ↓ Optional cache size
94
+
// Here we're disabling the cache
95
+
cacheSize: 0,
92
96
// ↓ Optional prefix from TaiLwind config
93
97
prefix: 'tw-',
94
98
// ↓ Optional separator from TaiLwind config
95
99
separator: '_',
96
-
// ↓ Add values to existing theme scale or create a new one
97
-
// Not all theme keys form the Tailwind config are supported by default.
98
-
theme: {
99
-
spacing: ['sm', 'md', 'lg'],
100
-
},
101
-
// ↓ Here you define class groups
102
-
classGroups: {
103
-
// ↓ The `foo` key here is the class group ID
104
-
// ↓ Creates group of classes which have conflicting styles
// ↓ Conflicts across different groups to extend or create
157
+
conflictingClassGroups: {
158
+
// ↓ ID of class group which creates a conflict with…
159
+
// ↓ …classes from groups with these IDs
160
+
// In this case `twMerge('aspect-w-5 aspect-none') → 'aspect-none'`
161
+
'aspect-reset': ['aspect-w', 'aspect-h'],
162
+
},
163
+
// ↓ Conflicts between the postfix modifier of a group and a different class group to
164
+
// extend or create
165
+
conflictingClassGroupModifiers: {
166
+
// You probably won't need this, but it follows the same shape as
167
+
// `conflictingClassGroups`.
168
+
},
125
169
},
126
170
})
127
171
```
@@ -200,16 +244,22 @@ But don't merge configs like that. Use [`mergeConfigs`](#mergeconfigs) instead.
200
244
function mergeConfigs(baseConfig:Config, configExtension:Partial<Config>):Config
201
245
```
202
246
203
-
Helper function to merge multiple config objects. Objects are merged, arrays are concatenated, scalar values are overridden and `undefined` does nothing. The function assumes that both parameters are tailwind-merge config objects and shouldn't be used as a generic merge function.
247
+
Helper function to merge multiple tailwind-merge configs. Properties with the value `undefined` are skipped.
Copy file name to clipboardExpand all lines: docs/configuration.md
+24-18Lines changed: 24 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,29 +160,35 @@ If you modified one of these theme scales in your Tailwind config, you can add a
160
160
161
161
### Extending the tailwind-merge config
162
162
163
-
If you only need to extend 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.
163
+
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.
Copy file name to clipboardExpand all lines: docs/recipes.md
+14-18Lines changed: 14 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,19 @@ How to configure tailwind-merge with some common patterns.
6
6
7
7
> I have a custom shadow scale with the keys 100, 200 and 300 configured in Tailwind. How do I make tailwind-merge resolve conflicts among those?
8
8
9
-
You'll be able to do this by creating a custom `twMerge` functon with [`extendTailwindMerge`](./api-reference.md#extendtailwindmerge).
9
+
We'll be able to do this by creating a custom `twMerge` functon with [`extendTailwindMerge`](./api-reference.md#extendtailwindmerge).
10
10
11
-
First, check whether your particular theme scale is included in tailwind-merge's theme config object [here](./configuration.md#theme). In the hypothetical case that tailwind-merge supported Tailwind's `boxShadow` theme scale, you could add it to the tailwind-merge config like this:
11
+
First, we need to know whether we want to override or extend the default scale. Let's say we extended the default config by putting the scale into the `extend` key in the Tailwind config.
12
+
13
+
Then we check whether our particular theme scale is included in tailwind-merge's theme config object [here](./configuration.md#theme). In the hypothetical case that tailwind-merge supported Tailwind's `boxShadow` theme scale, we could add it to the tailwind-merge config like this:
12
14
13
15
```js
14
16
constcustomTwMerge=extendTailwindMerge({
15
-
theme: {
16
-
// The `boxShadow` key isn't actually supported
17
-
boxShadow: [{ shadow: ['100', '200', '300'] }],
17
+
extend: {
18
+
theme: {
19
+
// The `boxShadow` key isn't actually supported
20
+
boxShadow: [{ shadow: ['100', '200', '300'] }],
21
+
},
18
22
},
19
23
})
20
24
```
@@ -23,23 +27,15 @@ In the case of the `boxShadow` scale, tailwind-merge doesn't include it in the t
23
27
24
28
```js
25
29
constcustomTwMerge=extendTailwindMerge({
26
-
classGroups: {
27
-
shadow: [{ shadow: ['100', '200', '300'] }],
30
+
extend: {
31
+
classGroups: {
32
+
shadow: [{ shadow: ['100', '200', '300'] }],
33
+
},
28
34
},
29
35
})
30
36
```
31
37
32
-
Note that by using `extendTailwindMerge` we're only adding our custom classes to the existing ones in the config, so `twMerge('shadow-200 shadow-lg')` will return the string `shadow-lg`. In most cases that's fine because you won't use that class in your project.
33
-
34
-
If you expect classes like `shadow-lg` to be input in `twMerge` and don't want the class to cause incorrect merges, you can explicitly override the class group with [`createTailwindMerge`](./api-reference.md#createtailwindmerge), removing the default classes.
Note that by using the `extend` object we're only adding our custom classes to the existing ones in the config, so `twMerge('shadow-200 shadow-lg')` will return the string `shadow-lg`. If we want to override the class instead, we need to use the `override` object instead.
43
39
44
40
## Extracting classes with Tailwind's [`@apply`](https://tailwindcss.com/docs/reusing-styles#extracting-classes-with-apply)
0 commit comments