tailwind-merge 0.9.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -21
- package/dist/_virtual/_rollupPluginBabelHelpers.mjs +20 -0
- package/dist/_virtual/_rollupPluginBabelHelpers.mjs.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +5 -2211
- package/dist/index.mjs +9 -0
- package/dist/index.mjs.map +1 -0
- package/dist/{types → lib}/class-utils.d.ts +0 -0
- package/dist/lib/class-utils.mjs +161 -0
- package/dist/lib/class-utils.mjs.map +1 -0
- package/dist/{types → lib}/config-utils.d.ts +0 -0
- package/dist/lib/config-utils.mjs +12 -0
- package/dist/lib/config-utils.mjs.map +1 -0
- package/dist/{types → lib}/create-tailwind-merge.d.ts +0 -0
- package/dist/lib/create-tailwind-merge.mjs +55 -0
- package/dist/lib/create-tailwind-merge.mjs.map +1 -0
- package/dist/{types → lib}/default-config.d.ts +400 -101
- package/dist/{index.module.js → lib/default-config.mjs} +454 -624
- package/dist/lib/default-config.mjs.map +1 -0
- package/dist/{types → lib}/extend-tailwind-merge.d.ts +0 -0
- package/dist/lib/extend-tailwind-merge.mjs +16 -0
- package/dist/lib/extend-tailwind-merge.mjs.map +1 -0
- package/dist/{types → lib}/from-theme.d.ts +0 -0
- package/dist/lib/from-theme.mjs +11 -0
- package/dist/lib/from-theme.mjs.map +1 -0
- package/dist/{types → lib}/lru-cache.d.ts +0 -0
- package/dist/lib/lru-cache.mjs +17 -0
- package/dist/lib/lru-cache.mjs.map +1 -0
- package/dist/{types → lib}/merge-classlist.d.ts +0 -0
- package/dist/lib/merge-classlist.mjs +66 -0
- package/dist/lib/merge-classlist.mjs.map +1 -0
- package/dist/{types → lib}/merge-configs.d.ts +0 -0
- package/dist/lib/merge-configs.mjs +39 -0
- package/dist/lib/merge-configs.mjs.map +1 -0
- package/dist/{types → lib}/tailwind-merge.d.ts +0 -0
- package/dist/lib/tailwind-merge.mjs +7 -0
- package/dist/lib/tailwind-merge.mjs.map +1 -0
- package/dist/{types → lib}/types.d.ts +5 -0
- package/dist/lib/validators.d.ts +11 -0
- package/dist/lib/validators.mjs +79 -0
- package/dist/lib/validators.mjs.map +1 -0
- package/dist/{index.cjs → tailwind-merge.cjs.development.js} +589 -298
- package/dist/tailwind-merge.cjs.development.js.map +1 -0
- package/dist/tailwind-merge.cjs.production.min.js +2 -0
- package/dist/tailwind-merge.cjs.production.min.js.map +1 -0
- package/package.json +29 -22
- package/src/index.ts +8 -8
- package/src/{class-utils.ts → lib/class-utils.ts} +52 -3
- package/src/{config-utils.ts → lib/config-utils.ts} +0 -0
- package/src/{create-tailwind-merge.ts → lib/create-tailwind-merge.ts} +0 -0
- package/src/{default-config.ts → lib/default-config.ts} +415 -271
- package/src/{extend-tailwind-merge.ts → lib/extend-tailwind-merge.ts} +0 -0
- package/src/{from-theme.ts → lib/from-theme.ts} +0 -0
- package/src/{lru-cache.ts → lib/lru-cache.ts} +0 -0
- package/src/{merge-classlist.ts → lib/merge-classlist.ts} +17 -15
- package/src/{merge-configs.ts → lib/merge-configs.ts} +0 -0
- package/src/{tailwind-merge.ts → lib/tailwind-merge.ts} +0 -0
- package/src/{types.ts → lib/types.ts} +5 -0
- package/src/lib/validators.ts +86 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.module.js.map +0 -1
- package/dist/types/index.d.ts +0 -8
- package/dist/types/validators.d.ts +0 -5
- package/src/validators.ts +0 -41
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<br />
|
|
3
3
|
<a href="https://github.com/dcastil/tailwind-merge">
|
|
4
|
-
<!-- AUTOGENERATED START logo-image --><img src="https://github.com/dcastil/tailwind-merge/raw/
|
|
4
|
+
<!-- AUTOGENERATED START logo-image --><img src="https://github.com/dcastil/tailwind-merge/raw/v1.2.0/assets/logo.svg" alt="tailwind-merge" width="221px" /><!-- AUTOGENERATED END -->
|
|
5
5
|
</a>
|
|
6
6
|
</div>
|
|
7
7
|
|
|
@@ -16,16 +16,10 @@ twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]')
|
|
|
16
16
|
// → 'hover:bg-dark-red p-3 bg-[#B91C1C]'
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
- Supports Tailwind
|
|
19
|
+
- Supports Tailwind v3.0 (if you use Tailwind v2, use [tailwind-merge v0.9.0](https://github.com/dcastil/tailwind-merge/tree/v0.9.0))
|
|
20
20
|
- Works in Node >=12 and all modern browsers
|
|
21
21
|
- Fully typed
|
|
22
|
-
- [
|
|
23
|
-
|
|
24
|
-
## Early development
|
|
25
|
-
|
|
26
|
-
This library is in an early pre-v1 development stage and might have some bugs and inconveniences here and there. I use the library in production and intend it to be sufficient for production use, as long as you're fine with some potential breaking changes in minor releases until v1 (lock the version range to patch releases with `~` in your `package.json` to prevent accidental breaking changes).
|
|
27
|
-
|
|
28
|
-
I want to keep the library on v0 until I feel confident enough that there aren't any major bugs or flaws in its API and implementation. If you find a bug or something you don't like, please [submit an issue](https://github.com/dcastil/tailwind-merge/issues/new/choose) or a pull request. I'm happy about any kind of feedback!
|
|
22
|
+
- [Check bundle size on Bundlephobia](https://bundlephobia.com/package/tailwind-merge)
|
|
29
23
|
|
|
30
24
|
## What is it for
|
|
31
25
|
|
|
@@ -61,7 +55,7 @@ function MyGenericInput(props) {
|
|
|
61
55
|
}
|
|
62
56
|
```
|
|
63
57
|
|
|
64
|
-
tailwind-merge
|
|
58
|
+
tailwind-merge overrides conflicting classes and keeps everything else untouched. In the case of the `MySlightlyModifiedInput`, the input now only renders the classes `border rounded p-3`.
|
|
65
59
|
|
|
66
60
|
## Features
|
|
67
61
|
|
|
@@ -92,7 +86,7 @@ twMerge('bottom-auto inset-y-6') // → 'inset-y-6'
|
|
|
92
86
|
twMerge('inline block') // → 'block'
|
|
93
87
|
```
|
|
94
88
|
|
|
95
|
-
### Supports
|
|
89
|
+
### Supports modifiers and stacked modifiers
|
|
96
90
|
|
|
97
91
|
```ts
|
|
98
92
|
twMerge('p-2 hover:p-4') // → 'p-2 hover:p-4'
|
|
@@ -100,13 +94,26 @@ twMerge('hover:p-2 hover:p-4') // → 'hover:p-4'
|
|
|
100
94
|
twMerge('hover:focus:p-2 focus:hover:p-4') // → 'focus:hover:p-4'
|
|
101
95
|
```
|
|
102
96
|
|
|
103
|
-
### Supports
|
|
97
|
+
### Supports arbitrary values
|
|
104
98
|
|
|
105
99
|
```ts
|
|
106
100
|
twMerge('bg-black bg-[color:var(--mystery-var)]') // → 'bg-[color:var(--mystery-var)]'
|
|
107
101
|
twMerge('grid-cols-[1fr,auto] grid-cols-2') // → 'grid-cols-2'
|
|
108
102
|
```
|
|
109
103
|
|
|
104
|
+
### Supports arbitrary properties
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
twMerge('[mask-type:luminance] [mask-type:alpha]') // → '[mask-type:alpha]'
|
|
108
|
+
twMerge('[--scroll-offset:56px] lg:[--scroll-offset:44px]')
|
|
109
|
+
// → '[--scroll-offset:56px] lg:[--scroll-offset:44px]'
|
|
110
|
+
|
|
111
|
+
// Don't do this!
|
|
112
|
+
twMerge('[padding:1rem] p-8') // → '[padding:1rem] p-8'
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Watch out for mixing arbitrary properties which could be expressed as Tailwind classes. tailwind-merge does not resolve conflicts between arbitrary properties and their matching Tailwind classes to keep the bundle size small.
|
|
116
|
+
|
|
110
117
|
### Supports important modifier
|
|
111
118
|
|
|
112
119
|
```ts
|
|
@@ -159,8 +166,11 @@ The tailwind-merge config is an object with a few keys.
|
|
|
159
166
|
const tailwindMergeConfig = {
|
|
160
167
|
// ↓ Set how many values should be stored in cache.
|
|
161
168
|
cacheSize: 500,
|
|
169
|
+
// ↓ Optional prefix from TaiLwind config
|
|
170
|
+
prefix: 'tw-',
|
|
162
171
|
theme: {
|
|
163
172
|
// Theme scales are defined here
|
|
173
|
+
// This is not the theme object from your Tailwind config
|
|
164
174
|
},
|
|
165
175
|
classGroups: {
|
|
166
176
|
// Class groups are defined here
|
|
@@ -193,13 +203,13 @@ E.g. here is the overflow class group which results in the classes `overflow-aut
|
|
|
193
203
|
const overflowClassGroup = [{ overflow: ['auto', 'hidden', 'visible', 'scroll'] }]
|
|
194
204
|
```
|
|
195
205
|
|
|
196
|
-
Sometimes it isn't possible to enumerate all elements in a class group. Think of a Tailwind class which allows
|
|
206
|
+
Sometimes it isn't possible to enumerate all elements in a class group. Think of a Tailwind class which allows arbitrary values. In this scenario you can use a validator function which takes a _class part_ and returns a boolean indicating whether a class is part of a class group.
|
|
197
207
|
|
|
198
208
|
E.g. here is the fill class group.
|
|
199
209
|
|
|
200
210
|
```ts
|
|
201
|
-
const
|
|
202
|
-
const fillClassGroup = [{ fill: ['current',
|
|
211
|
+
const isArbitraryValue = (classPart: string) => /^\[.+\]$/.test(classPart)
|
|
212
|
+
const fillClassGroup = [{ fill: ['current', isArbitraryValue] }]
|
|
203
213
|
```
|
|
204
214
|
|
|
205
215
|
Because the function is under the `fill` key, it will only get called for classes which start with `fill-`. Also, the function only gets passed the part of the class name which comes after `fill-`, this way you can use the same function in multiple class groups. tailwind-merge exports its own [validators](#validators), so you don't need to recreate them.
|
|
@@ -301,6 +311,7 @@ The function takes a callback which returns the config you want to use and retur
|
|
|
301
311
|
import { createTailwindMerge } from 'tailwind-merge'
|
|
302
312
|
|
|
303
313
|
const customTwMerge = createTailwindMerge(() => ({
|
|
314
|
+
cacheSize: 500,
|
|
304
315
|
theme: {},
|
|
305
316
|
classGroups: {
|
|
306
317
|
foo: ['foo', 'foo-2', { 'bar-baz': ['', '1', '2'] }],
|
|
@@ -506,9 +517,15 @@ const customTwMerge = createTailwindMerge(getDefaultConfig, (config) =>
|
|
|
506
517
|
```ts
|
|
507
518
|
interface Validators {
|
|
508
519
|
isLength(classPart: string): boolean
|
|
509
|
-
|
|
520
|
+
isArbitraryLength(classPart: string): boolean
|
|
510
521
|
isInteger(classPart: string): boolean
|
|
511
|
-
|
|
522
|
+
isArbitraryValue(classPart: string): boolean
|
|
523
|
+
isTshirtSize(classPart: string): boolean
|
|
524
|
+
isArbitrarySize(classPart: string): boolean
|
|
525
|
+
isArbitraryPosition(classPart: string): boolean
|
|
526
|
+
isArbitraryUrl(classPart: string): boolean
|
|
527
|
+
isArbitraryWeight(classPart: string): boolean
|
|
528
|
+
isArbitraryShadow(classPart: string): boolean
|
|
512
529
|
isAny(classPart: string): boolean
|
|
513
530
|
}
|
|
514
531
|
```
|
|
@@ -521,10 +538,16 @@ const paddingClassGroup = [{ p: [validators.isLength] }]
|
|
|
521
538
|
|
|
522
539
|
A brief summary for each validator:
|
|
523
540
|
|
|
524
|
-
- `isLength` checks whether a class part is a number (`3`, `1.5`), a fraction (`3/4`), a
|
|
525
|
-
- `
|
|
526
|
-
- `isInteger` checks for integer values (`3`) and
|
|
527
|
-
- `
|
|
541
|
+
- `isLength` checks whether a class part is a number (`3`, `1.5`), a fraction (`3/4`), a arbitrary length (`[3%]`, `[4px]`, `[length:var(--my-var)]`), or one of the strings `px`, `full` or `screen`.
|
|
542
|
+
- `isArbitraryLength` checks for arbitrary length values (`[3%]`, `[4px]`, `[length:var(--my-var)]`).
|
|
543
|
+
- `isInteger` checks for integer values (`3`) and arbitrary integer values (`[3]`).
|
|
544
|
+
- `isArbitraryValue` checks whether the class part is enclosed in brackets (`[something]`)
|
|
545
|
+
- `isTshirtSize`checks whether class part is a T-shirt size (`sm`, `xl`), optionally with a preceding number (`2xl`).
|
|
546
|
+
- `isArbitrarySize` checks whether class part is arbitrary value which starts with with `size:` (`[size:200px_100px]`) which is necessary for background-size classNames.
|
|
547
|
+
- `isArbitraryPosition` checks whether class part is arbitrary value which starts with with `position:` (`[position:200px_100px]`) which is necessary for background-position classNames.
|
|
548
|
+
- `isArbitraryUrl` checks whether class part is arbitrary value which starts with `url:` or `url(` (`[url('/path-to-image.png')]`, `url:var(--maybe-a-url-at-runtime)]`) which is necessary for background-image classNames.
|
|
549
|
+
- `isArbitraryWeight` checks whether class part is arbitrary value whcih starts with `weight:` or is a number (`[weight:var(--value)]`, `[450]`) which is necessary for font-weight classNames.
|
|
550
|
+
- `isArbitraryShadow` checks whether class part is 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.
|
|
528
551
|
- `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 ceertain there are no other class groups in a namespace.
|
|
529
552
|
|
|
530
553
|
### `Config`
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function _extends() {
|
|
2
|
+
_extends = Object.assign || function (target) {
|
|
3
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
4
|
+
var source = arguments[i];
|
|
5
|
+
|
|
6
|
+
for (var key in source) {
|
|
7
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
8
|
+
target[key] = source[key];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return _extends.apply(this, arguments);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { _extends as extends };
|
|
20
|
+
//# sourceMappingURL=_rollupPluginBabelHelpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_rollupPluginBabelHelpers.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { twMerge } from './lib/tailwind-merge';
|
|
2
|
+
export { getDefaultConfig } from './lib/default-config';
|
|
3
|
+
export { extendTailwindMerge } from './lib/extend-tailwind-merge';
|
|
4
|
+
export { createTailwindMerge } from './lib/create-tailwind-merge';
|
|
5
|
+
export type { Config } from './lib/types';
|
|
6
|
+
export * as validators from './lib/validators';
|
|
7
|
+
export { mergeConfigs } from './lib/merge-configs';
|
|
8
|
+
export { fromTheme } from './lib/from-theme';
|