Repository navigation
Merge classes like tailwind-merge and add a cva-style variants helper - #2505
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2434
What
Class merging and variants, so a shadcn example translates into Razor with the same class names and behaves the same when a caller passes its own classes.
TailwindMerge: a C# port of tailwind-merge (MIT) for the Tailwind v4 utilities. When two classes set the same CSS property under the same variants, the later one wins and the earlier one is dropped. It keeps tailwind-merge's algorithm:!p-4andp-4!) and arbitrary properties ([mask-type:luminance]) have their own scope./50is read as an opacity, andtext-lg/7also replacesleading-*.preplacespxandpe,inset-xreplacesstartandend,roundedreplacesrounded-ss,sizereplaceswandh, and so on.text-sm,text-startandtext-muted-foreground;border-2,border-dashedandborder-input;ring-[3px]andring-ring/50;shadow-xsandshadow-black/5;bg-cover,bg-noneandbg-red-500.my-cardis never read as a margin.ClassNames.Cnnow merges as well as joins, as shadcn'scn()does. Every component already puts itsClassparameter last, so a caller's class overrides the component's own:<Input Class="h-8" />dropsh-9.Variants: shadcn'scva(), with the base classes, the classes for each value of each variant, and the defaults.Classes(chosen, className)merges them with the caller's classes last. An unknown value adds nothing and a missing one takes the default, as in cva.ButtonVariantsandBadgenow use it, so the size of a button replaces its basegap-2, as it does in shadcn.ButtonTests: Button had no bUnit tests. Each component inComponents/Uinow has its own.CONTRIBUTING.md: a paragraph onCnandVariants.Boxes on #2434
To tick when this merges:
tests/FluentCMS.Tests": with ButtonTests, every component in the set has its own test class.Why
Componentsfound nothing that the merge drops.pxandinset-xarepadding-inlineandinset-inlinein v4, so they also replaceps,pe,startandend. tailwind-merge still pairs them with the physical sides only.No new dependency.
Tests
TailwindMergeTests: cases from tailwind-merge's documentation and test suite, rewritten for v4. They cover layout and spacing shorthands, logical utilities, the text, background, border, ring, outline and shadow families, variants and their order, important, arbitrary values, variables, negative values, postfixes, unknown classes and whitespace. One case is a shadcn button with aClassoverride.VariantsTests: defaults, a chosen value replacing the base classes it conflicts with, null and unknown values, and the class parameter winning.ButtonTests: native button, each variant and size, aClassoverride, a click and the disabled state, and form attributes passed through.dotnet build -warnaserror, the full suite and markdownlint pass locally.Checklist
v2