Project

classes

0.0
No commit activity in last 3 years
No release in over 3 years
classes is a helper for building CSS class names in your Rails or Sinatra views. It's great for toggling individual classes or combining class names across your views and partials.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

classes

Tests

classes is a Ruby gem that lets you do:

<div class="<%= classes("notification", "is-danger": alert, "is-info": notice) %>">
  <%= alert || notice %>
</div>

It was inspired by the Classnames npm package.

Install

bundle add classes

Rails

Just start using classes in your views!

Sinatra

First, require classes:

require "classes"

For classic Sinatra apps, you're done.

For modular Sinatra apps, you'll also need to include the helpers:

class App < Sinatra::Base
  helpers Classes::Helpers
end

Now you can start using classes in your views!

Use

The simplest use for classes is to join some strings together:

classes("button", "is-link").to_s # => "button is-link"

A hash makes it easy to conditionally enable classes:

classes("button", "is-link": true, "is-danger": false).to_s # => "button is-link"

classes flattens and de-dupes your classes:

classes(["button", ["button is-link", ["is-link"]]]).to_s # => "button is-link"

You can combine these features as much as you like:

classes(
  :button, # Symbols are converted to strings.
  "is-link is-light",
  { "is-link": true, "is-danger": false },
  [true && "is-link", false && "is-danger"],
  "is-light",
  classes("is-fullwidth": true), # You can nest Classes::ClassList instances.
  " " # Empty or invalid classes are ignored.
).to_s # => "button is-link is-light is-fullwidth"