{"id":15431753,"url":"https://github.com/svyatov/clsx-rails","last_synced_at":"2025-07-03T23:34:47.345Z","repository":{"id":225641568,"uuid":"766501177","full_name":"svyatov/clsx-rails","owner":"svyatov","description":"clsx / classnames for Rails views","archived":false,"fork":false,"pushed_at":"2024-12-16T23:48:06.000Z","size":69,"stargazers_count":5,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-24T22:51:22.466Z","etag":null,"topics":["actionview","classnames","clsx","conditional-rendering","rails-helper","ruby-gem","ruby-gems","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/svyatov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-03T12:52:27.000Z","updated_at":"2024-12-05T21:39:31.000Z","dependencies_parsed_at":"2024-03-03T13:03:38.942Z","dependency_job_id":"0ca97f4f-4347-492a-a08d-a629b2233b19","html_url":"https://github.com/svyatov/clsx-rails","commit_stats":null,"previous_names":["svyatov/clsx-rails"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fclsx-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fclsx-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fclsx-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svyatov%2Fclsx-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svyatov","download_url":"https://codeload.github.com/svyatov/clsx-rails/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242358926,"owners_count":20115100,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actionview","classnames","clsx","conditional-rendering","rails-helper","ruby-gem","ruby-gems","ruby-on-rails"],"created_at":"2024-10-01T18:23:58.463Z","updated_at":"2025-03-07T08:30:36.081Z","avatar_url":"https://github.com/svyatov.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# clsx-rails [![Gem Version](https://img.shields.io/gem/v/clsx-rails)](https://rubygems.org/gems/clsx-rails) [![Codecov](https://img.shields.io/codecov/c/github/svyatov/clsx-rails)](https://app.codecov.io/gh/svyatov/clsx-rails) [![CI](https://github.com/svyatov/clsx-rails/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/svyatov/clsx-rails/actions?query=workflow%3ACI) [![GitHub License](https://img.shields.io/github/license/svyatov/clsx-rails)](LICENSE.txt)\n\n\u003e A tiny Rails view helper for constructing CSS class strings conditionally.\n\nThis gem is essentially a clone if the [clsx](https://github.com/lukeed/clsx) npm package.\nIt provides a simple way to conditionally apply classes to HTML elements in Rails views.\nIt is especially useful when you have a lot of conditional classes and you want to keep your views clean and readable.\n\n## Supported Ruby and Rails versions\n\nRuby 3.1+ and Rails 7.0+ are supported.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n```bash\nbundle add clsx-rails\n```\n\nOr add it manually to the Gemfile:\n\n```ruby\ngem 'clsx-rails', '~\u003e 1.0'\n```\n\n## Usage\n\nThe `clsx` helper method can be used in views to conditionally apply classes to HTML elements.\nYou can also use a slightly more conise `cn` alias.\nIt accepts a variety of arguments and returns a string of unique classes.\n\n```ruby\n# Strings (variadic)\nclsx('foo', true \u0026\u0026 'bar', 'baz')\n# =\u003e 'foo bar baz'\n\n# Hashes\ncn({ foo: true, bar: false, baz: a_method_that_returns_true })\n# =\u003e 'foo baz'\n\n# Hashes (variadic)\nclsx({ foo: true }, { bar: false }, nil, { '--foobar': 'hello' })\n# =\u003e 'foo --foobar'\n\n# Arrays\ncn(['foo', nil, false, 'bar'])\n# =\u003e 'foo bar'\n\n# Arrays (variadic)\nclsx(['foo'], ['', nil, false, 'bar'], [['baz', [['hello'], 'there']]])\n# =\u003e 'foo bar baz hello there'\n\n# Kitchen sink (with nesting)\ncn('foo', ['bar', { baz: false, bat: nil }, ['hello', ['world']]], 'cya');\n# =\u003e 'foo bar hello world cya'\n```\n\n```erb\n\u003c%= tag.div class: clsx('foo', 'baz', 'is-active' =\u003e @active) do %\u003e\n  Hello, world!\n\u003c% end %\u003e\n\n\u003cdiv class=\"\u003c%= clsx('foo', 'baz', 'is-active' =\u003e @active) %\u003e\"\u003e\n  Hello, world!\n\u003c/div\u003e\n```\n\n```haml\n%div{class: clsx('foo', 'baz', 'is-active' =\u003e @active)}\n  Hello, world!\n```\n\n```slim\ndiv class=clsx('foo', 'baz', 'is-active' =\u003e @active)\n  | Hello, world!\n```\n\nSo the basic idea is to get rid of constructions like this in your views:\n\n```erb\n\u003c% classes = ['foo', 'baz'] %\u003e\n\u003c% classes \u003c\u003c 'is-active' if @active %\u003e\n\n\u003cdiv class=\"\u003c%= classes.join(' ') %\u003e\"\u003e\n  Hello, world!\n\u003c/div\u003e\n```\n\nor like this:\n\n```erb\n\u003cdiv class=\"\u003c%= ['foo', 'baz', @active ? 'is-active' : nil].compact.join(' ') %\u003e\"\u003e\n  Hello, world!\n\u003c/div\u003e\n```\n\nor like this:\n\n```erb\n\u003cdiv class=\"foo bar \u003c%= 'is-active' if @active %\u003e\"\u003e\n  Hello, world!\n\u003c/div\u003e\n```\n\n## Differences from the original `clsx` package\n\nThis gem reproduces the functionality of the original `clsx` package, but with nuances of Ruby and Rails in mind.\n\nThe main differences are:\n\n1. falsy values in Ruby are only `false` and `nil`, so the `clsx` method will not accept `0`, `''`, `[]`, `{}`, etc. as falsy values.\n   ```ruby\n    clsx('foo' =\u003e 0, bar: []) # =\u003e 'foo bar'\n   ``` \n \n2. `clsx-rails` supports complex hash keys, like `{ %[foo bar] =\u003e true }`, which will be converted to `foo bar` in the resulting string.\n   Meaning, if it's a valid input for the `clsx-rails`, it's a valid hash key.\n   ```ruby\n    clsx([{ foo: true }, 'bar'] =\u003e true) # =\u003e 'foo bar'\n   ``` \n \n3. `clsx-rails` will ignore objects that are `blank?`, boolean (`true` or `false`), or an instance of `Proc` (so, procs and lambdas).\n    ```ruby\n     clsx('', [], {}, proc {}, -\u003e {}, nil, false, true) # =\u003e nil\n    ```\n \n4. `clsx-rails` returns `nil` if there are no classes to apply, instead of an empty string.\n   The reason for that is not to pollute the HTML with empty `class` attributes when using Rails tag helpers: Rails will not render the `class` attribute if it's `nil`.\n   ```ruby\n     clsx(nil, false) # =\u003e nil\n   ```\n   Although, it won't help if you're using it directly in erb:\n   ```erb\n    \u003cdiv class=\"\u003c%= clsx(nil, false) %\u003e\"\u003e\n      Hello, world!\n    \u003c/div\u003e\n   ```\n   This code will render `\u003cdiv class=\"\"\u003eHello, world!\u003c/div\u003e` anyway.\n \n5. `clsx-rails` eliminates duplicate classes:\n   ```ruby\n    clsx('foo', 'foo') # =\u003e 'foo'\n   ```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run\n`rake test` to run the tests. You can also run `bin/console` for an interactive\nprompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To\nrelease a new version, update the version number in `version.rb`, and then run\n`bundle exec rake release`, which will create a git tag for the version, push\ngit commits and the created tag, and push the `.gem` file to\n[rubygems.org](https://rubygems.org).\n\nThere is a simple benchmark script in the `benchmark` directory.\nYou can run it with `bundle exec ruby benchmark/run.rb`.\nI've added it for easier performance testing when making changes to the gem.\n\n## Conventional Commits\n\nThis project uses [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) for commit messages.\n\nTypes of commits are:\n- `feat`: a new feature\n- `fix`: a bug fix\n- `perf`: code that improves performance\n- `chore`: updating build tasks, configs, formatting etc; no code change\n- `docs`: changes to documentation\n- `refactor`: refactoring code\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/svyatov/clsx-rails.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvyatov%2Fclsx-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvyatov%2Fclsx-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvyatov%2Fclsx-rails/lists"}