Module: Clsx

Extended by:
Helper
Defined in:
lib/clsx.rb,
lib/clsx/helper.rb,
lib/clsx/version.rb,
lib/clsx/tailwind_merge.rb

Overview

Opt-in Tailwind merging. Requiring this file (once, at boot) pulls in the tailwind_merge gem and adds a merged variant of clsxHelper#twm, the Helper#twm mixin method, and the Twm[] shortcut — that resolves conflicting Tailwind utilities (e.g. "px-2 px-4" becomes "px-4").

+clsx+/+cn+ are left untouched and stay pure; only +twm+/+Twm+ merge. Without requiring this file the core gem carries no dependency.

Examples:

require 'clsx/tailwind_merge'
Twm['px-2 px-4']  # => "px-4"

Custom merger (configure once at boot)

Clsx.merger = TailwindMerge::Merger.new(config: { prefix: 'tw' })

Defined Under Namespace

Modules: Helper, Twm

Constant Summary collapse

VERSION =
'1.2.0'

Constants included from Helper

Helper::TAB_OR_NEWLINE

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helper

clsx, twm

Class Attribute Details

.merger#merge

Process-wide merger, built once. Building a TailwindMerge::Merger is expensive, so double-checked locking constructs it exactly once even under concurrent first use; after that the lock is never taken again.

Returns:

  • (#merge)


34
35
36
# File 'lib/clsx/tailwind_merge.rb', line 34

def merger
  @merger || @merger_mutex.synchronize { @merger ||= TailwindMerge::Merger.new }
end

Class Method Details

.[]String?

Build a CSS class string from an arbitrary mix of arguments.

Falsy values (+nil+, false) and standalone true are discarded. Duplicates are eliminated. Returns nil (not "") when no classes apply.

Examples:

Strings and hashes

clsx('foo', 'bar')                      # => "foo bar"
clsx(foo: true, bar: false, baz: true)  # => "foo baz"

Nested arrays

clsx('a', ['b', nil, ['c']])            # => "a b c"
clsx(%w[foo bar], hidden: true)         # => "foo bar hidden"

Parameters:

  • args (String, Symbol, Hash, Array, Numeric, nil, false)

    class descriptors to merge into a single space-separated string

Returns:

  • (String)

    space-joined class string

  • (nil)

    when no classes apply



17
18
19
# File 'lib/clsx.rb', line 17

def self.[](*)
  clsx(*)
end