Developer builds clsx gem after discovering Rails has a built-in class name helper
A developer published clsx-ruby, a framework-agnostic CSS class builder gem, after realising two years earlier that Rails already ships a built-in helper called class_names for the same purpose. The built-in class_names helper, available since Rails 6.1, handles strings, arrays, and hashes to conditionally build HTML class strings, but it is tied to the ActionView module. This dependency makes it impractical to use in non-Rails contexts such as Sinatra apps or plain Ruby service objects without loading the entire view framework. The developer split the logic into two packages: clsx-ruby, a zero-dependency core library, and clsx-rails, a thin Rails adapter on top of it. A key behavioural difference is that clsx-ruby returns nil when no classes apply, causing Rails tag helpers to omit the attribute entirely, whereas class_names returns an empty string.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in