Skip to content

Commit da5f9f1

Browse files
authored
Merge pull request #134 from dcastil/other/123/add-recipes-section-to-docs
Add recipes section to docs
2 parents 52acec1 + f96ecbf commit da5f9f1

File tree

5 files changed

+99
-11
lines changed

5 files changed

+99
-11
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]')
2727

2828
<!-- AUTOGENERATED VERSION START -->
2929

30-
- [What is it for](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/what-is-it-for.md)
31-
- [Features](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/features.md)
32-
- [Configuring tailwind-merge](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/configuring-tailwind-merge.md)
33-
- [API reference](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/api-reference.md)
34-
- [Writing plugins](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/writing-plugins.md)
35-
- [Versioning](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/versioning.md)
36-
- [Contributing](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/.github/CONTRIBUTING.md)
37-
- [Similar packages](https://github.com/dcastil/tailwind-merge/tree/76e06aa8838a6b978e34e5fafa8ad0788d8553af/docs/similar-packages.md)
30+
- [What is it for](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/what-is-it-for.md)
31+
- [Features](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/features.md)
32+
- [Configuring tailwind-merge](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/configuring-tailwind-merge.md)
33+
- [Recipes](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/recipes.md)
34+
- [API reference](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/api-reference.md)
35+
- [Writing plugins](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/writing-plugins.md)
36+
- [Versioning](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/versioning.md)
37+
- [Contributing](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/.github/CONTRIBUTING.md)
38+
- [Similar packages](https://github.com/dcastil/tailwind-merge/tree/32f706cefe892e9c2e95d348723ce95dbe49c5ec/docs/similar-packages.md)
3839

3940
<!-- AUTOGENERATED END -->

docs/api-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,4 @@ TypeScript type for config object. Useful if you want to build a `createConfig`
239239

240240
Next: [Writing plugins](./writing-plugins.md)
241241

242-
Previous: [Configuring tailwind-merge](./configuring-tailwind-merge.md)
242+
Previous: [Recipes](./recipes.md)

docs/configuring-tailwind-merge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,6 @@ const twMerge3 = createTailwindMerge(() => ({ … }), withMagic, withMoreMagic)
208208

209209
---
210210

211-
Next: [API reference](./api-reference.md)
211+
Next: [Recipes](./recipes.md)
212212

213213
Previous: [Features](./features.md)

docs/recipes.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Recipes
2+
3+
How to configure tailwind-merge with some common patterns.
4+
5+
## Adding custom scale from Tailwind config to tailwind-merge config
6+
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+
9+
You'll be able to do this by creating a custom `twMerge` functon with [`extendTailwindMerge`](./api-reference.md#extendtailwindmerge).
10+
11+
First, check whether your particular theme scale is included in tailwind-merge's theme config object [here](./configuring-tailwind-merge.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:
12+
13+
```js
14+
const customTwMerge = extendTailwindMerge({
15+
theme: {
16+
// The `boxShadow` key isn't actually supported
17+
boxShadow: [{ shadow: ['100', '200', '300'] }],
18+
},
19+
})
20+
```
21+
22+
In the case of the `boxShadow` scale, tailwind-merge doesn't include it in the theme object. Instead, we need to check out the [default config of tailwind-merge](../src/lib/default-config.ts) and search for the class group ID of the box shadow scale. After a quick search we find that tailwind-merge is using the key `shadow` for that group. We can add our custom classes to that group like this:
23+
24+
```js
25+
const customTwMerge = extendTailwindMerge({
26+
classGroups: {
27+
shadow: [{ shadow: ['100', '200', '300'] }],
28+
},
29+
})
30+
```
31+
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.
35+
36+
```js
37+
const customTwMerge = createTailwindMerge(() => {
38+
const config = getDefaultConfig()
39+
config.classGroups.shadow = [{ shadow: ['100', '200', '300'] }]
40+
return config
41+
})
42+
```
43+
44+
## Extracting classes with Tailwind's [`@apply`](https://tailwindcss.com/docs/reusing-styles#extracting-classes-with-apply)
45+
46+
> How do I make tailwind-merge resolve conflicts with a custom class created with `@apply`?
47+
>
48+
> ```css
49+
> .btn-primary {
50+
> @apply py-2 px-4 bg-blue-500 text-white rounded-lg hover:bg-blue-700;
51+
> }
52+
> ```
53+
54+
I don't recommend using Tailwind's `@apply` directive for classes that might get processed with tailwind-merge.
55+
56+
tailwind-merge would need to be configured so that it knows about which classes `.btn-primary` is in conflict with. This means: If someone adds another Tailwind class to the `@apply` directive, the tailwind-merge config would need to get modified accordignly, keeping it in sync with the written CSS. This easy-to-miss dependency is brittle and can lead to bugs with incorrect merging behavior.
57+
58+
Instead of creating custom CSS classes, I recommend keeping the collection of Tailwind classes in a string variable in JavaScript and access it whenever you want to apply those styles. This way can reuse the collection of styles but don't need to touch the tailwind-merge config.
59+
60+
```jsx
61+
// React components with JSX syntax used in this example
62+
63+
const BTN_PRIMARY_CLASSNAMES = 'py-2 px-4 bg-blue-500 text-white rounded-lg hover:bg-blue-700'
64+
65+
function ButtonPrimary(props) {
66+
return <button {...props} className={twMerge(BTN_PRIMARY_CLASSNAMES, props.className)} />
67+
}
68+
```
69+
70+
## Modifying inputs and output of `twMege`
71+
72+
> How do I make `twMerge` accept the same argument types as clsx/classnames?
73+
74+
You can wrap `twMerge` in another function which can modify the inputs and/or output.
75+
76+
```js
77+
function customTwMerge(...inputs) {
78+
const modifiedInputs = modifyInputs(inputs)
79+
return twMerge(modifiedInputs)
80+
}
81+
```
82+
83+
---
84+
85+
Next: [API reference](./api-reference.md)
86+
87+
Previous: [Configuring tailwind-merge](./configuring-tailwind-merge.md)

docs/what-is-it-for.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
If you use Tailwind with a component-based UI renderer like [React](https://reactjs.org) or [Vue](https://vuejs.org), you're probably familiar with the situation that you want to change some styles of a component, but only in one place.
44

55
```jsx
6-
import React from 'react'
6+
// React components with JSX syntax used in this example
77

88
function MyGenericInput(props) {
99
const className = `border rounded px-2 py-1 ${props.className || ''}`

0 commit comments

Comments
 (0)