Skip to content

Class merging

Components that accept a class prop need to merge it with their own classes so the caller wins: p-4 passed in should replace the component’s p-2, not sit next to it. tailwind-merge does that for Tailwind’s built-in utilities. @nyxwire/tailwind/merge extends it with the Nyxwire utilities.

import { cn } from '@nyxwire/tailwind/merge'
cn('rounded-md bg-panel p-2', props.class)
cn('glow-md glow-flux', isAlarm && 'glow-danger')
cn('bg-canvas', { 'bg-panel': raised }, [false, 'p-4'])

cn(...inputs) accepts strings, numbers, bigints, null, undefined, booleans, nested readonly arrays and objects whose truthy keys are added. Falsy values are dropped, and it needs no clsx. When two classes set the same thing, the later one wins. Every result below was computed by the shipped cn when this page was built:

cn() calls and their merged class lists
CallResult
cn('glow-sm glow-flux', 'glow-lg')'glow-flux glow-lg'
cn('shadow-md', 'glow-sm')'glow-sm'
cn('text-glow text-glow-nw-indigo', 'text-glow-matrix')'text-glow text-glow-matrix'
cn('text-sm text-fg', 'text-fg-muted')'text-sm text-fg-muted'
cn('bg-canvas px-4', 'bg-panel')'px-4 bg-panel'
cn('bg-panel backdrop-blur-sm', 'glass')'glass'
cn('glass', 'bg-panel')'glass bg-panel'
cn('grid-bg', 'bg-wire')'bg-wire'
cn('rounded-lg border bg-panel', 'cut')'cut'
cn('border-line', 'wire')'wire'
cn('text-fg bg-clip-padding', 'text-wire')'text-wire'
cn('rounded-md shadow-sm', 'rounded-xl shadow-lg')'rounded-xl shadow-lg'
cn('font-sans', 'font-display')'font-display'
cn('animate-fade-in', 'animate-glow-pulse')'animate-glow-pulse'
cn('ease-standard', 'ease-back')'ease-back'

What the configuration teaches tailwind-merge:

  • Glow sizes (glow-sm, glow-md, glow-lg, wire-glow) share the box-shadow group, so they replace each other and any shadow-*.
  • Glow colors have a group of their own, shared by glow-* and text-glow-*: the last color wins, and a size never removes a color.
  • text-glow belongs to the text-shadow group.
  • bg-wire belongs to the background-image group, so it replaces another gradient and can sit next to a background color.
  • grid-bg paints its own background image and size, so it conflicts with bg-wire, cut, wire and text-wire.
  • cut replaces an earlier radius, border width or background color: its shape and fill come from --nw-cut and --nw-fill.
  • wire replaces an earlier border color.
  • glass replaces an earlier bg-* color or backdrop-blur-*. A color written after it still applies.
  • text-wire replaces an earlier text color, background image or background clip.
  • scanlines and glitch have groups of their own and never remove anything.
  • The type scale, radius, easings and animations extend Tailwind’s scales: text-2xs, rounded-xs, ease-back and animate-glow-pulse merge like their built-in neighbors.

A later override of one part of a composite, such as bg-panel after glass, keeps the other parts intact.

To use the configuration with your own tailwind-merge instance, pass it to extendTailwindMerge:

import { extendTailwindMerge } from 'tailwind-merge'
import { twMergeConfig } from '@nyxwire/tailwind/merge'
export const merge = extendTailwindMerge(twMergeConfig)

twMergeConfig is typed as TwMergeConfig. The module also exports the ClassValue, ClassDictionary and NyxwireClassGroupIds types.

tailwind-variants builds class lists from variants. Create its tv with createTV from the tailwind-variants/config entry point, so every recipe merges with the Nyxwire configuration:

src/lib/tv.ts
import { createTV } from 'tailwind-variants/config'
import { twMergeConfig } from '@nyxwire/tailwind/merge'
export const tv = createTV({ twMergeConfig })
src/lib/signal.ts
import { tv } from './tv'
export const signal = tv({
base: 'inline-flex items-center gap-1 rounded-md px-2 py-0.5 font-mono text-xs',
variants: {
intent: {
primary: 'bg-primary-soft text-primary',
danger: 'bg-danger-soft text-danger',
},
glowing: { true: 'glow-sm' },
},
compoundVariants: [{ intent: 'danger', glowing: true, class: 'glow-danger' }],
defaultVariants: { intent: 'primary' },
})

The same recipe, rendered when this page was built. In the last row the caller’s rounded-full replaces the base rounded-md:

Calls of the signal recipe, the rendered chip and the resulting class list
CallRenderedClasses
signal()uplinkinline-flex items-center gap-1 rounded-md px-2 py-0.5 font-mono text-xs bg-primary-soft text-primary
signal({ glowing: true })uplinkinline-flex items-center gap-1 rounded-md px-2 py-0.5 font-mono text-xs bg-primary-soft text-primary glow-sm
signal({ intent: 'danger', glowing: true })uplinkinline-flex items-center gap-1 rounded-md px-2 py-0.5 font-mono text-xs bg-danger-soft text-danger glow-sm glow-danger
signal({ intent: 'danger', class: 'rounded-full' })uplinkinline-flex items-center gap-1 px-2 py-0.5 font-mono text-xs bg-danger-soft text-danger rounded-full

@nyxwire/vue exports the same two helpers, already configured: cn is the function above and tv is created with createTV({ twMergeConfig }). Its components merge their class prop with them, so overrides behave the same everywhere.

import { cn, tv } from '@nyxwire/vue'