Module: Clsx::Helper

Defined in:
lib/clsx/helper.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#clsx(*args) ⇒ String Also known as: cn

The clsx function can take any number of arguments, each of which can be an Hash, Array, Boolean, String, or Symbol.

Important: Any falsy values are discarded! Standalone Boolean values are discarded as well.

Examples:

clsx('foo', 'bar') # => 'foo bar'
clsx(true, { bar: true }) # => 'bar'
clsx('foo', { bar: false }) # => 'foo'
clsx({ bar: true }, 'baz', { bat: false }) # => 'bar baz'

within a view

<div class="<%= clsx('foo', 'bar') %>">
<div class="<%= clsx('foo', active: @is_active, 'another-class' => @condition) %>">
<%= tag.div class: clsx(%w[foo bar], hidden: @condition) do ... end %>

Parameters:

  • args (Mixed)

Returns:

  • (String)

    the joined class names



28
29
30
31
32
# File 'lib/clsx/helper.rb', line 28

def clsx(*args)
  result = clsx_args_processor(*args)
  result.uniq!
  result.join(' ').presence
end