slot-variants 1.2.0 → 1.3.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/AGENTS.md +102 -10
- package/README.md +184 -14
- package/dist/eslint-plugin.cjs +1067 -0
- package/dist/eslint-plugin.d.ts +26 -0
- package/dist/eslint-plugin.js +1042 -0
- package/dist/index.cjs +354 -218
- package/dist/index.d.ts +162 -52
- package/dist/index.js +354 -218
- package/dist/src/cn.d.ts +14 -0
- package/dist/src/eslint-plugin.d.ts +24 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/sv.d.ts +187 -0
- package/dist/test/cn.test.d.ts +1 -0
- package/dist/test/eslint-plugin.test.d.ts +1 -0
- package/dist/test/sv.test.d.ts +1 -0
- package/package.json +35 -16
package/AGENTS.md
CHANGED
|
@@ -211,12 +211,10 @@ const button = sv('btn', {
|
|
|
211
211
|
},
|
|
212
212
|
cacheSize: 512 // increase cache for complex components
|
|
213
213
|
});
|
|
214
|
-
|
|
215
|
-
// Access cache methods
|
|
216
|
-
button.getCacheSize();
|
|
217
|
-
button.clearCache();
|
|
218
214
|
```
|
|
219
215
|
|
|
216
|
+
Cache inspection methods (`getCacheSize`, `clearCache`) are only exposed when `introspection: true` is set — see rule 11.
|
|
217
|
+
|
|
220
218
|
Note: Using `class` or `className` prop bypasses caching.
|
|
221
219
|
|
|
222
220
|
### 10. Use Presets for Reusable Variant Combinations
|
|
@@ -241,7 +239,7 @@ button({ preset: 'cta' });
|
|
|
241
239
|
|
|
242
240
|
### 11. Use Introspection for Single Source of Truth
|
|
243
241
|
|
|
244
|
-
|
|
242
|
+
Set `introspection: true` in the config to expose configuration properties and cache methods on the returned function. Introspection is **off by default** — opt in only when you need runtime access to variant/slot definitions or cache controls:
|
|
245
243
|
|
|
246
244
|
```typescript
|
|
247
245
|
const button = sv('btn', {
|
|
@@ -264,7 +262,8 @@ const button = sv('btn', {
|
|
|
264
262
|
requiredVariants: ['intent'],
|
|
265
263
|
presets: {
|
|
266
264
|
cta: { size: 'lg', intent: 'primary' }
|
|
267
|
-
}
|
|
265
|
+
},
|
|
266
|
+
introspection: true
|
|
268
267
|
});
|
|
269
268
|
|
|
270
269
|
button.variantKeys; // ['size', 'intent']
|
|
@@ -277,8 +276,12 @@ button.presetKeys; // ['cta']
|
|
|
277
276
|
button.presets; // { cta: { size: 'lg', intent: 'primary' } }
|
|
278
277
|
button.getVariantValues('size'); // ['sm', 'lg']
|
|
279
278
|
button.getVariantValues('intent'); // ['primary', 'danger']
|
|
279
|
+
button.getCacheSize(); // current cache size
|
|
280
|
+
button.clearCache(); // clear the cache
|
|
280
281
|
```
|
|
281
282
|
|
|
283
|
+
Without `introspection: true`, only the variant function itself is returned — accessing these properties is a type error.
|
|
284
|
+
|
|
282
285
|
Use introspection to share variant/slot definitions with other parts of your codebase:
|
|
283
286
|
|
|
284
287
|
```typescript
|
|
@@ -299,7 +302,8 @@ export const button = sv('btn font-medium rounded-lg', {
|
|
|
299
302
|
defaultVariants: {
|
|
300
303
|
size: 'md',
|
|
301
304
|
intent: 'primary'
|
|
302
|
-
}
|
|
305
|
+
},
|
|
306
|
+
introspection: true
|
|
303
307
|
});
|
|
304
308
|
|
|
305
309
|
// Reuse variant keys for form validation
|
|
@@ -313,7 +317,8 @@ const card = sv('card', {
|
|
|
313
317
|
slots: {
|
|
314
318
|
header: 'font-bold',
|
|
315
319
|
body: 'py-4'
|
|
316
|
-
}
|
|
320
|
+
},
|
|
321
|
+
introspection: true
|
|
317
322
|
});
|
|
318
323
|
|
|
319
324
|
// Dynamically render all slots
|
|
@@ -400,11 +405,13 @@ export const Card = (props: CardProps) => {
|
|
|
400
405
|
|
|
401
406
|
## Configuration Reference
|
|
402
407
|
|
|
408
|
+
Class values inside the config (`base`, `variants` values, `slots` values, and `compound*` `class`/`className`) accept only `string`, `string[]`, or `undefined`. Dynamic class values (objects, booleans, nested arrays) belong on the `class`/`className` runtime prop, not in the config.
|
|
409
|
+
|
|
403
410
|
| Option | Type | Description |
|
|
404
411
|
| ------------------ | -------------------------------- | --------------------------------- |
|
|
405
|
-
| `base` | `
|
|
412
|
+
| `base` | `string \| string[]` | Additional base classes |
|
|
406
413
|
| `variants` | `Record<string, VariantConfig>` | Variant definitions |
|
|
407
|
-
| `slots` | `Record<string,
|
|
414
|
+
| `slots` | `Record<string, string \| string[]>` | Named slot definitions |
|
|
408
415
|
| `compoundVariants` | `CompoundVariant[]` | Conditional class combinations |
|
|
409
416
|
| `compoundSlots` | `CompoundSlot[]` | Multi-slot conditional classes |
|
|
410
417
|
| `defaultVariants` | `Record<string, Value>` | Static or function-based defaults |
|
|
@@ -412,6 +419,7 @@ export const Card = (props: CardProps) => {
|
|
|
412
419
|
| `presets` | `Record<string, Partial<Props>>` | Named preset combinations |
|
|
413
420
|
| `postProcess` | `(className: string) => string` | Class transformation |
|
|
414
421
|
| `cacheSize` | `number` | Cache size (default: 256) |
|
|
422
|
+
| `introspection` | `boolean` | Expose introspection and cache methods (default: false) |
|
|
415
423
|
|
|
416
424
|
## Exported Types
|
|
417
425
|
|
|
@@ -430,6 +438,90 @@ import { sv, cn } from 'slot-variants';
|
|
|
430
438
|
import type { VariantProps, VariantValue, SlotClassProps, ClassValue } from 'slot-variants';
|
|
431
439
|
```
|
|
432
440
|
|
|
441
|
+
## ESLint / oxlint Plugin
|
|
442
|
+
|
|
443
|
+
Subpath export `slot-variants/eslint-plugin` ships five rules that statically analyze `sv()` and `cn()` calls. Works under ESLint v9+ (flat config) and under oxlint via its `jsPlugins` config. The plugin is a separate entry point — it adds no runtime code to the library bundle.
|
|
444
|
+
|
|
445
|
+
```js
|
|
446
|
+
// eslint.config.js
|
|
447
|
+
import svPlugin from 'slot-variants/eslint-plugin';
|
|
448
|
+
|
|
449
|
+
export default [{
|
|
450
|
+
plugins: { 'slot-variants': svPlugin },
|
|
451
|
+
rules: {
|
|
452
|
+
'slot-variants/no-duplicate-classes': 'error',
|
|
453
|
+
'slot-variants/no-dynamic-classes': 'error',
|
|
454
|
+
'slot-variants/no-empty-classes': 'error',
|
|
455
|
+
'slot-variants/no-redundant-spaces': 'error',
|
|
456
|
+
'slot-variants/no-shared-tokens': 'error'
|
|
457
|
+
}
|
|
458
|
+
}];
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
```json
|
|
462
|
+
// .oxlintrc.json
|
|
463
|
+
{
|
|
464
|
+
"jsPlugins": ["slot-variants/eslint-plugin"],
|
|
465
|
+
"rules": {
|
|
466
|
+
"slot-variants/no-duplicate-classes": "error",
|
|
467
|
+
"slot-variants/no-dynamic-classes": "error",
|
|
468
|
+
"slot-variants/no-empty-classes": "error",
|
|
469
|
+
"slot-variants/no-redundant-spaces": "error",
|
|
470
|
+
"slot-variants/no-shared-tokens": "error"
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
All rules only analyze calls where `sv` or `cn` is a **named import** from `'slot-variants'`. Default, namespace, and aliased-to-other-identifier imports are ignored.
|
|
476
|
+
|
|
477
|
+
### `slot-variants/no-duplicate-classes`
|
|
478
|
+
|
|
479
|
+
Reports class tokens that will appear more than once in the output of an `sv()` or `cn()` call.
|
|
480
|
+
|
|
481
|
+
- For `sv()` with a config, flags a token as duplicated when two of its sources can both be active at runtime: base + any variant, base + compound, two variants with different keys, two compound entries, or the same literal token repeated inside a single source.
|
|
482
|
+
- Does **not** flag the same token appearing in different values of the **same** variant key (those are mutually exclusive).
|
|
483
|
+
- For `cn()` (and `sv()` called without a config — the cn-style calling convention), flags any token that appears in more than one always-present source: across args, inside arrays, template literals without expressions, or within a single literal.
|
|
484
|
+
- Bails out silently on dynamic inputs (identifiers, spreads, computed keys, template literals with expressions, cn-style `{ cls: condition }` records) — no false positives for code it can't statically resolve.
|
|
485
|
+
|
|
486
|
+
### `slot-variants/no-dynamic-classes`
|
|
487
|
+
|
|
488
|
+
Reports class-bearing positions in `sv()` and `cn()` calls that aren't statically inferrable. Pair this rule with `no-duplicate-classes` to guarantee every call is fully analyzable.
|
|
489
|
+
|
|
490
|
+
- Accepts only string literals, template literals without expressions, and arrays of those as class values. Identifiers, member access, calls, spreads, non-string literals, templates with expressions, and object records are reported.
|
|
491
|
+
- For `sv()` config, validates `base`, `slots` values, `variants` values (both record form and boolean shorthand), and the `class` / `className` of `compoundVariants` / `compoundSlots` entries. The `slots` array of `compoundSlots` must be a static array of string literals.
|
|
492
|
+
- Top-level config keys must be statically known — spreads and computed keys cause the call to fall through to the cn-style path, which then reports the entire object as dynamic.
|
|
493
|
+
- Non-class-bearing config keys (`defaultVariants`, `presets`, `requiredVariants`, `cacheSize`, `postProcess`, `introspection`) are not validated. Runtime variant matchers inside compound entries are also left alone — only the class value (and `compoundSlots`' `slots` array) is checked.
|
|
494
|
+
- Move dynamic class strings to the runtime `class` / `className` prop on the function returned by `sv()` — that call site is intentionally outside the analyzer's scope.
|
|
495
|
+
|
|
496
|
+
### `slot-variants/no-empty-classes`
|
|
497
|
+
|
|
498
|
+
Reports empty class values — empty strings, empty arrays, and empty objects — at any class-bearing position reachable from an `sv()` or `cn()` call, plus zero-argument `sv()` / `cn()` calls (which always produce an empty class string).
|
|
499
|
+
|
|
500
|
+
- Reports empty literals as positional arguments to `cn()` and `sv()` (cn-style or as base args alongside a config), as well as empty literals nested inside class arrays.
|
|
501
|
+
- Inside an `sv()` config, reports empty values at `base`, in `variants` value records and boolean-shorthand values, and in the `class`/`className` of `compoundVariants` / `compoundSlots` entries. Also reports empty top-level `slots`, `variants`, `compoundVariants`, and `compoundSlots` containers.
|
|
502
|
+
- A direct empty string at `slots[key]` is allowed — declaring a slot with no default classes is a real use case (`sv({ slots: { extra: '' } })`). Empty strings inside slot-value arrays are still reported.
|
|
503
|
+
- Reports `sv()` / `cn()` invocations with zero arguments — they always return an empty string and have no effect.
|
|
504
|
+
- Recurses into arrays but not into objects: values inside cn-style `{ cls: condition }` records are conditions, not class values, so they are left alone.
|
|
505
|
+
|
|
506
|
+
### `slot-variants/no-redundant-spaces`
|
|
507
|
+
|
|
508
|
+
Reports class strings whose whitespace isn't canonical — that is, whose value differs from `value.split(/\s+/).filter(Boolean).join(' ')`.
|
|
509
|
+
|
|
510
|
+
- Flags leading or trailing whitespace, repeated spaces, and non-space whitespace (tabs, newlines, etc.) inside any string literal or expressionless template literal reachable from the call's arguments.
|
|
511
|
+
- Walks recursively into arrays and objects, so values nested inside `slots`, `variants` records, `compoundVariants`, `compoundSlots`, `defaultVariants`, `presets`, etc. are all inspected.
|
|
512
|
+
- Bails silently on dynamic expressions and non-string literals — false positives are impossible by construction.
|
|
513
|
+
- Reports once per offending literal at the whole-node location. Fix by trimming and collapsing the string, or by splitting it into array entries.
|
|
514
|
+
|
|
515
|
+
### `slot-variants/no-shared-tokens`
|
|
516
|
+
|
|
517
|
+
Reports class tokens that appear in every value of an exhaustively-covered variant — the token is constant in the rendered output and belongs in `base` or the corresponding `slots[slot]` entry rather than being repeated across every variant value.
|
|
518
|
+
|
|
519
|
+
- Only analyzes `sv()` calls with a config; `cn()` calls and cn-style `sv()` calls are ignored.
|
|
520
|
+
- Treats a variant as exhaustive when it has a `defaultVariants` entry or is listed in `requiredVariants`.
|
|
521
|
+
- Compares tokens per slot, so slot-based variants are checked against the specific slot they affect rather than only against `base`.
|
|
522
|
+
- Skips non-exhaustive variants, single-value variants, boolean shorthand, slot-keyed boolean shorthand, and dynamic or partially-uninspectable variant value records.
|
|
523
|
+
- Reports every repeated occurrence that should be lifted out of the variant so each value only contains classes that actually vary.
|
|
524
|
+
|
|
433
525
|
## Performance Notes
|
|
434
526
|
|
|
435
527
|
1. **Caching is automatic** - Results are cached by default
|
package/README.md
CHANGED
|
@@ -172,15 +172,14 @@ badge({ color: 'green', size: 'sm' });
|
|
|
172
172
|
// 'badge bg-green-100 text-green-800 text-xs px-2 py-0.5'
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
-
Variant values
|
|
175
|
+
Variant values accept a string or an array of strings:
|
|
176
176
|
|
|
177
177
|
```typescript
|
|
178
178
|
const button = sv('btn', {
|
|
179
179
|
variants: {
|
|
180
180
|
size: {
|
|
181
|
-
sm: ['px-2', 'py-1', 'text-sm'],
|
|
182
|
-
lg: 'px-6 py-3 text-lg'
|
|
183
|
-
xl: { 'px-8': true, 'py-4': true, 'text-xl': true } // object
|
|
181
|
+
sm: ['px-2', 'py-1', 'text-sm'], // array of strings
|
|
182
|
+
lg: 'px-6 py-3 text-lg' // string
|
|
184
183
|
}
|
|
185
184
|
}
|
|
186
185
|
});
|
|
@@ -679,16 +678,15 @@ const button = sv('btn', {
|
|
|
679
678
|
lg: 'text-lg'
|
|
680
679
|
}
|
|
681
680
|
},
|
|
682
|
-
cacheSize: 512
|
|
681
|
+
cacheSize: 512 // customize the cache size
|
|
683
682
|
});
|
|
684
|
-
|
|
685
|
-
button.getCacheSize(); // current number of cached entries
|
|
686
|
-
button.clearCache(); // clear all cached entries
|
|
687
683
|
```
|
|
688
684
|
|
|
685
|
+
Cache inspection and control methods (`getCacheSize`, `clearCache`) are exposed on the returned function only when `introspection: true` is set — see [Introspection](#introspection).
|
|
686
|
+
|
|
689
687
|
### Introspection
|
|
690
688
|
|
|
691
|
-
|
|
689
|
+
Set `introspection: true` in the config to expose configuration properties and cache controls on the returned function for runtime inspection. Introspection is **disabled by default** to keep the returned function lean; opt in only when you need it:
|
|
692
690
|
|
|
693
691
|
```typescript
|
|
694
692
|
const button = sv('btn', {
|
|
@@ -711,7 +709,8 @@ const button = sv('btn', {
|
|
|
711
709
|
requiredVariants: ['intent'],
|
|
712
710
|
presets: {
|
|
713
711
|
cta: { size: 'lg', intent: 'primary' }
|
|
714
|
-
}
|
|
712
|
+
},
|
|
713
|
+
introspection: true
|
|
715
714
|
});
|
|
716
715
|
|
|
717
716
|
button.variantKeys; // ['size', 'intent']
|
|
@@ -724,8 +723,12 @@ button.presetKeys; // ['cta']
|
|
|
724
723
|
button.presets; // { cta: { size: 'lg', intent: 'primary' } }
|
|
725
724
|
button.getVariantValues('size'); // ['sm', 'lg']
|
|
726
725
|
button.getVariantValues('intent'); // ['primary', 'danger']
|
|
726
|
+
button.getCacheSize(); // current number of cached entries
|
|
727
|
+
button.clearCache(); // clear all cached entries
|
|
727
728
|
```
|
|
728
729
|
|
|
730
|
+
Without `introspection: true`, only the variant function itself is returned — accessing introspection or cache properties is a type error.
|
|
731
|
+
|
|
729
732
|
## TypeScript
|
|
730
733
|
|
|
731
734
|
`slot-variants` is fully typed. Variant props are inferred from your config:
|
|
@@ -871,11 +874,13 @@ When used on an `sv()` definition without slots, `SlotClassProps` resolves to `{
|
|
|
871
874
|
|
|
872
875
|
## Config Reference
|
|
873
876
|
|
|
877
|
+
Class values inside the config (`base`, `variants`, `slots`, and `compound*` `class`/`className`) accept `string`, `string[]`, or `undefined`. Dynamic class values (objects, booleans, nested arrays) are only accepted at call time via the `class`/`className` prop.
|
|
878
|
+
|
|
874
879
|
| Option | Type | Description |
|
|
875
880
|
| --- | --- | --- |
|
|
876
|
-
| `base` | `
|
|
877
|
-
| `variants` | `
|
|
878
|
-
| `slots` | `
|
|
881
|
+
| `base` | `string \| string[]` | Additional base classes merged with the base argument and `slots.base` |
|
|
882
|
+
| `variants` | `Record<string, Record<string \| number, string \| string[]>>` | Variant definitions mapping variant names to their possible values |
|
|
883
|
+
| `slots` | `Record<string, string \| string[]>` | Named slot definitions for multi-element components |
|
|
879
884
|
| `compoundVariants` | `Array` | Additional classes applied when multiple variant conditions match |
|
|
880
885
|
| `compoundSlots` | `Array` | Classes applied to multiple slots based on variant conditions |
|
|
881
886
|
| `defaultVariants` | `Object` | Default values for variants (static values or functions) |
|
|
@@ -883,6 +888,171 @@ When used on an `sv()` definition without slots, `SlotClassProps` resolves to `{
|
|
|
883
888
|
| `presets` | `Record<string, Partial<VariantProps>>` | Named combinations of variant values selectable via `preset` prop |
|
|
884
889
|
| `postProcess` | `(className: string) => string` | Custom transformation applied to final class strings |
|
|
885
890
|
| `cacheSize` | `number` | Maximum number of cached results (default: `256`) |
|
|
891
|
+
| `introspection` | `boolean` | When `true`, exposes variant/slot/preset introspection and cache methods on the returned function (default: `false`) |
|
|
892
|
+
|
|
893
|
+
## ESLint / oxlint Plugin
|
|
894
|
+
|
|
895
|
+
`slot-variants` ships an ESLint-compatible plugin at the `slot-variants/eslint-plugin` subpath. It runs under ESLint v9+ (flat config) and under [oxlint](https://oxc.rs/docs/guide/usage/linter/js-plugins) via its `jsPlugins` API. The plugin is a separate entry point with no runtime imports — consuming it doesn't pull any library code into your bundle.
|
|
896
|
+
|
|
897
|
+
### Rules
|
|
898
|
+
|
|
899
|
+
- **`slot-variants/no-duplicate-classes`** — flags class name tokens that will appear more than once in the output of an `sv()` or `cn()` call. For `sv()`, detects duplicates within `base`, across different variant keys, inside compound variants and compound slots, between `base` and a variant value, and within a single literal. For `cn()` (and the cn-style calling convention of `sv()` without a config), flags any token that appears in more than one always-present source — across args, inside arrays, template literals, or within a single literal.
|
|
900
|
+
|
|
901
|
+
- **`slot-variants/no-dynamic-classes`** — flags class-bearing positions in `sv()` and `cn()` calls that aren't statically inferrable. Only string literals, template literals without expressions, flat arrays of those in config, and explicit `undefined` config class values are accepted, and config objects must use static keys (no spreads, no computed keys). Identifiers, member access, calls, spreads, non-string literals, templates with expressions, nested config arrays, and runtime conditional records are reported. Non-class-bearing config keys (`defaultVariants`, `presets`, `requiredVariants`, `cacheSize`, `postProcess`, `introspection`) are not validated, and runtime variant matchers inside compound entries are left alone — only the `class`/`className` value (and the `slots` array of `compoundSlots`) is checked.
|
|
902
|
+
|
|
903
|
+
- **`slot-variants/no-empty-classes`** — flags empty class values — empty strings, empty arrays, and empty objects — at any class-bearing position reachable from an `sv()` or `cn()` call, plus zero-argument `sv()` / `cn()` calls themselves (which always produce an empty class string). Reports empties in positional arguments (and inside arrays nested in those), in `base`, in variants including slot-keyed variant branches, in the `class`/`className` of `compoundVariants` and `compoundSlots` entries, and in the top-level `slots`, `variants`, `compoundVariants`, and `compoundSlots` containers themselves. Inside an `sv()` config, an empty string is still allowed as a direct `slots[key]` value — declaring a slot with no default classes is a valid use case.
|
|
904
|
+
|
|
905
|
+
- **`slot-variants/no-redundant-spaces`** — flags class strings whose whitespace isn't canonical. Inside a class string, whitespace is canonical only as a single ASCII space between two non-whitespace tokens, so leading or trailing whitespace, repeated spaces, and non-space whitespace (tabs, newlines, etc.) are reported. The rule walks every string and expressionless template literal reachable from a call's arguments — including values nested inside arrays and objects — and bails silently on dynamic expressions.
|
|
906
|
+
|
|
907
|
+
- **`slot-variants/no-shared-tokens`** — flags class tokens that appear in every value of an exhaustively-covered variant, where “exhaustive” means the variant has a statically defined default value or is listed in `requiredVariants`. Those tokens are constant in the rendered output, so they belong in `base` or the corresponding `slots[slot]` entry rather than being repeated in every variant value. The rule only analyzes `sv()` calls with a config, compares tokens per-slot, skips non-exhaustive variants, single-value variants, boolean shorthand, undefined or dynamic defaults, and dynamic or partially-analyzable variant value records, and reports every repeated occurrence that should be lifted out.
|
|
908
|
+
|
|
909
|
+
Only calls where `sv` or `cn` is a named import from `'slot-variants'` are analyzed. `no-duplicate-classes` skips dynamic inputs silently to avoid false positives; `no-dynamic-classes` is the opposite — it flags exactly those positions so the static analyzer can fully reason about every call. `no-shared-tokens` sits between them: it needs a fully statically analyzable, exhaustive variant before it can prove a token is constant across every value. `no-empty-classes` and `no-redundant-spaces` are independent and complement the structural rules: they cover empty and badly-shaped literals reachable from a call's arguments, regardless of whether the surrounding call is fully static.
|
|
910
|
+
|
|
911
|
+
### ESLint (flat config)
|
|
912
|
+
|
|
913
|
+
```js
|
|
914
|
+
import svPlugin from 'slot-variants/eslint-plugin';
|
|
915
|
+
|
|
916
|
+
export default [
|
|
917
|
+
{
|
|
918
|
+
plugins: { 'slot-variants': svPlugin },
|
|
919
|
+
rules: {
|
|
920
|
+
'slot-variants/no-duplicate-classes': 'error',
|
|
921
|
+
'slot-variants/no-dynamic-classes': 'error',
|
|
922
|
+
'slot-variants/no-empty-classes': 'error',
|
|
923
|
+
'slot-variants/no-redundant-spaces': 'error',
|
|
924
|
+
'slot-variants/no-shared-tokens': 'error'
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
];
|
|
928
|
+
```
|
|
929
|
+
|
|
930
|
+
### oxlint
|
|
931
|
+
|
|
932
|
+
```json
|
|
933
|
+
{
|
|
934
|
+
"jsPlugins": ["slot-variants/eslint-plugin"],
|
|
935
|
+
"rules": {
|
|
936
|
+
"slot-variants/no-duplicate-classes": "error",
|
|
937
|
+
"slot-variants/no-dynamic-classes": "error",
|
|
938
|
+
"slot-variants/no-empty-classes": "error",
|
|
939
|
+
"slot-variants/no-redundant-spaces": "error",
|
|
940
|
+
"slot-variants/no-shared-tokens": "error"
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
### Example
|
|
946
|
+
|
|
947
|
+
```typescript
|
|
948
|
+
import { sv, cn } from 'slot-variants';
|
|
949
|
+
|
|
950
|
+
const button = sv({
|
|
951
|
+
base: 'flex items-center',
|
|
952
|
+
variants: {
|
|
953
|
+
orientation: {
|
|
954
|
+
row: ['flex', 'flex-row'], // 'flex' duplicates base
|
|
955
|
+
col: ['flex', 'flex-col'] // 'flex' duplicates base
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
});
|
|
959
|
+
|
|
960
|
+
cn('flex items-center', 'flex'); // 'flex' duplicated across args
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
`no-duplicate-classes` reports `flex` on the `base` literal and on both variant values; for the `cn()` call, both occurrences of `'flex'` are flagged. Move the shared class into `base` — or use compound variants — so each class has a single source.
|
|
964
|
+
|
|
965
|
+
```typescript
|
|
966
|
+
import { sv, cn } from 'slot-variants';
|
|
967
|
+
|
|
968
|
+
const extra = getDynamicClass();
|
|
969
|
+
|
|
970
|
+
sv({ base: extra }); // dynamic base
|
|
971
|
+
sv({ base: `text-sm ${size}` }); // template with expression
|
|
972
|
+
sv({ ...rest, variants: {} }); // spread inside config
|
|
973
|
+
sv({ variants: { [key]: 'x' } }); // computed variant key
|
|
974
|
+
sv({ slots: { body: ['p-4', ...rest] } }); // spread inside slot array
|
|
975
|
+
cn(extra, 'flex'); // identifier argument
|
|
976
|
+
```
|
|
977
|
+
|
|
978
|
+
`no-dynamic-classes` reports each of the dynamic positions above. Replace dynamic class strings with static ones (or move them to the runtime `class` / `className` prop on the returned function, which is intentionally outside the analyzer's scope) so every call can be statically verified.
|
|
979
|
+
|
|
980
|
+
```typescript
|
|
981
|
+
import { sv, cn } from 'slot-variants';
|
|
982
|
+
|
|
983
|
+
sv({ base: '' }); // empty base
|
|
984
|
+
sv({ base: [] }); // empty array
|
|
985
|
+
sv({ variants: { size: { sm: '' } } }); // empty variant value
|
|
986
|
+
sv({ compoundVariants: [{ size: 'lg', class: '' }] }); // empty compound class
|
|
987
|
+
cn('flex', ''); // empty cn arg
|
|
988
|
+
sv(); // zero-arg call
|
|
989
|
+
cn(); // zero-arg call
|
|
990
|
+
```
|
|
991
|
+
|
|
992
|
+
`no-empty-classes` reports each empty class value — strings, arrays, or objects — plus zero-argument `sv()` / `cn()` calls (they always produce an empty string). The one exception is a direct empty string at `slots[key]`, which is allowed because declaring a slot with no default classes is a real use case (`sv({ slots: { extra: '' } })`). Either remove the empty value or replace it with a meaningful class string.
|
|
993
|
+
|
|
994
|
+
```typescript
|
|
995
|
+
import { sv, cn } from 'slot-variants';
|
|
996
|
+
|
|
997
|
+
sv({ base: ' flex items-center' }); // leading space
|
|
998
|
+
sv({ base: 'flex items-center' }); // double space
|
|
999
|
+
sv({ slots: { body: 'p-4 ' } }); // trailing space
|
|
1000
|
+
cn(`flex\titems-center`); // tab between tokens
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
`no-redundant-spaces` reports each literal whose whitespace deviates from the canonical "tokens separated by exactly one space" form. Trim and collapse the strings — or split them into array entries — so the stored class output is byte-stable and easy to scan in diffs.
|
|
1004
|
+
|
|
1005
|
+
```typescript
|
|
1006
|
+
import { sv } from 'slot-variants';
|
|
1007
|
+
|
|
1008
|
+
const button = sv({
|
|
1009
|
+
variants: {
|
|
1010
|
+
size: {
|
|
1011
|
+
sm: 'rounded text-sm',
|
|
1012
|
+
lg: 'rounded text-lg'
|
|
1013
|
+
}
|
|
1014
|
+
},
|
|
1015
|
+
defaultVariants: { size: 'sm' }
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
const card = sv({
|
|
1019
|
+
slots: { root: 'flex', body: 'p-4' },
|
|
1020
|
+
variants: {
|
|
1021
|
+
size: {
|
|
1022
|
+
sm: { root: 'rounded text-sm', body: 'p-1' },
|
|
1023
|
+
lg: { root: 'rounded text-lg', body: 'p-2' }
|
|
1024
|
+
}
|
|
1025
|
+
},
|
|
1026
|
+
requiredVariants: ['size']
|
|
1027
|
+
});
|
|
1028
|
+
```
|
|
1029
|
+
|
|
1030
|
+
`no-shared-tokens` reports `rounded` in both `button` variant values and in both `card` `root` slot values, because the token is present in every value of an exhaustive variant. Lift that class into `base` — or into `slots.root` for slot-based variants — so each variant value contains only the classes that actually vary.
|
|
1031
|
+
|
|
1032
|
+
## IntelliSense Setup (Optional)
|
|
1033
|
+
|
|
1034
|
+
If you're using Tailwind CSS, you can opt into class autocompletion and automatic class sorting inside `sv()` and `cn()` calls.
|
|
1035
|
+
|
|
1036
|
+
### VSCode
|
|
1037
|
+
|
|
1038
|
+
The [Tailwind CSS IntelliSense](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss) extension recognizes calls listed in `tailwindCSS.classFunctions`. Add `cn` and `sv` to your workspace or user settings:
|
|
1039
|
+
|
|
1040
|
+
```json
|
|
1041
|
+
{
|
|
1042
|
+
"tailwindCSS.classFunctions": ["cn", "sv"]
|
|
1043
|
+
}
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
### Prettier
|
|
1047
|
+
|
|
1048
|
+
The [`prettier-plugin-tailwindcss`](https://github.com/tailwindlabs/prettier-plugin-tailwindcss) plugin sorts Tailwind classes inside the functions listed in `tailwindFunctions`:
|
|
1049
|
+
|
|
1050
|
+
```js
|
|
1051
|
+
module.exports = {
|
|
1052
|
+
plugins: [require('prettier-plugin-tailwindcss')],
|
|
1053
|
+
tailwindFunctions: ['cn', 'sv']
|
|
1054
|
+
};
|
|
1055
|
+
```
|
|
886
1056
|
|
|
887
1057
|
## Migrating from CVA / tailwind-variants
|
|
888
1058
|
|
|
@@ -958,4 +1128,4 @@ const button = sv({
|
|
|
958
1128
|
variants: { size: { sm: 'px-2 py-1' } },
|
|
959
1129
|
postProcess: twMerge
|
|
960
1130
|
});
|
|
961
|
-
```
|
|
1131
|
+
```
|