# ViewComponent in the Wild III: TailwindCSS classes & HTML attributes

> Learn how to manage TailwindCSS classes and HTML attributes in view components via the APIs provided by the view_component-contrib gem.

- Date: 2024-01-23T00:00:00.000Z
- Authors: Vladimir Dementyev, Travis Turner
- Categories: Rails
- URL: https://evilmartians.com/chronicles/viewcomponent-in-the-wild-embracing-tailwindcss-classes-and-html-attributes

---

Ruby on Rails full-stack development is back on track; HTML-first is the way! To move forward, we need better tools to manage the view layer. GitHub’s ViewComponent library is still the top choice among Rails developers for bringing sanity to HTML. We’ve been using this library at Evil Martians for a while, and we’re ready to present new tips and tricks accumulated since this series started. In this post, we’ll discuss integrating TailwindCSS styles and the propagation of HTML attributes in view components. Buckle up!

We started adopting [ViewComponent][] a few years ago to help us organize HTML templates and partials. At first, we mostly used it as a software design pattern to help keep code under control. Yet, as the ecosystem and our experience evolved, ViewComponent became a keystone element for creating application _design systems_.

A design system is a collection of reusable elements and guidelines for crafting a UI. In code, a design system is expressed via the _UI kit_, a part of the view layer responsible for implementing the design system's UI elements. "Reusable elements" naturally lead to _components_. But maintaining a UI kit is impossible without having a _storybook_ for it. ViewComponent, together with [Lookbook][] (which we talked about in the [last post][part-2]), gives you everything you need to get started with design-system-driven UI development.

Rails focuses on productivity, and the most productive way to write code is to not write code at all. Ready-made UI kits for Ruby and Rails applications are still rare beasts, and most of them are still in the _alpha_ stage. So, chances are that you'll have to craft a custom UI library for your application. But no worries! We've been through this a few times and are ready to share valuable tricks to simplify UI kit development with ViewComponent. Here's what is on today's menu:

Examples of UI component libraries for Ruby and Rails include [PhlexUI](https://phlexui.com) (this one uses [Phlex][], an alternative view component library for Ruby), [RailsUI](https://railsui.com), [ZestUI](https://zestui.com).

1. [Style variants](#style-variants)
2. [HTML attributes propagation](#html-attribute-propagation)
3. [Browser tests for components](#browser-tests-for-components)

## Style variants

[TailwindCSS][] has conquered the world of UI development. Why bother with CSS rules, nesting, and naming (BEM, SMACSS, and so on) when you can define all your styling with HTML classes? Making HTML a single source of truth plays especially nicely with the ["No Build" ideology][no-build] of modern full-stack Rails.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/5-best-practices-for-preventing-chaos-in-tailwind-css

With TailwindCSS, you can develop a consistent UI without ever touching CSS files. You can define your design system's fundamentals (typography, grids, and so forth) and _design tokens_ in the `tailwind.config.js` file and enjoy the magic of atomic (and dynamic) CSS classes. However, this comes with a price: dozens of classes in your HTML. Let's consider an example Button component:

```rb
class UIKit::Button::Component < ApplicationComponent
  option :type, default: proc { "button" }

  erb_template <<~ERB
    <button type="<%= type %>"
      class="items-center justify-center rounded-md border border-slate-300
      bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-sm ring-blue-700
      hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2
      focus:ring-offset-blue-50 dark:border-slate-950 dark:bg-blue-700
      dark:text-blue-50 dark:ring-blue-950 dark:hover:bg-blue-800 dark:focus:ring-offset-blue-700">
      <%= content %>
    </button>
  ERB
end
```

In the example above, we use ViewComponent's [inline template feature](https://viewcomponent.org/guide/templates.html#inline), added in v3, and the `ApplicationComponent` class generated via the [view_component-contrib][] library (learn more in [the second part][part-2] of the series). We can render it as follows:

```erb
<%= render UIKit::Button::Component.new do %>
  Click Me
<% end %>
```

This results in the following UI:

*Image: Button component*

But that's just the beginning because button components never come in just one single format; every UI kit contains multiple button variants. Let's consider adding an outline version of the button. This would require including some classes conditionally, depending on the variant selected:

```ruby
class UIKit::Button::Component < ApplicationComponent
  option :type, default: proc { "button" }
  option :variant, default: proc { :default }

  STYLES = {
    default: "text-white bg-blue-600 ring-blue-700 \
      hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 \
      focus:ring-offset-blue-50 dark:border-slate-950 dark:bg-blue-700 \
      dark:text-blue-50 dark:ring-blue-950 dark:hover:bg-blue-800 dark:focus:ring-offset-blue-700",
    outline: "bg-slate-50 hover:bg-slate-100 focus:outline-none \
      focus:ring-2 focus:ring-slate-600 focus:ring-offset-2 \
      focus:ring-offset-blue-50 dark:border-slate-950 dark:bg-slate-700 \
      dark:ring-slate-950 dark:hover:bg-slate-800 dark:focus:ring-offset-slate-700"
  }.freeze

  erb_template <<~ERB
    <button type="<%= type %>"
      class="items-center justify-center rounded-md border border-slate-300
      px-4 py-2 text-sm font-medium shadow-sm <%= STYLES.fetch(variant) %>">
      <%= content %>
    </button>
  ERB
end
```

Now, we can specify an optional variant when rendering a button:

```erb

  <%= render UIKit::Button::Component.new do %>
    Click Me
  <% end %>
  <%= render UIKit::Button::Component.new(variant: :outline) do %>
    Click Me
  <% end %>

```

*Image: Button variants*

Extracting dynamic parts of the class list into a constant did the trick. But that's just for a single variation dimension and a couple of variants. In practice, there are usually a greater number of variants and variations.

For example, we may have different sizing variants (small, full, and so on). And sometimes, these variations are not independent and require adding additional classes for some combinations. To illustrate, dealing with the `disabled` state for our button component might require us to write the following code:

```ruby
class UIKit::Button::Component < ApplicationComponent
  option :type, default: proc { "button" }
  option :variant, default: proc { :default }
  option :disabled, default: proc { false }

  STYLES = {
    # ...
  }.freeze

  erb_template <<~ERB
    <button type="<%= type %>"
      class="items-center justify-center rounded-md border border-slate-300
      px-4 py-2 text-sm font-medium shadow-sm <%= STYLES.fetch(variant) %>
      <%= disabled_classes if disabled %>"
      <%= "disabled" if disabled %>>
      <%= content %>
    </button>
  ERB

  def disabled_classes
    if variant == :outline
      "opacity-75 bg-slate-300 pointer-events-none"
    else
      "opacity-50 pointer-events-none"
    end
  end
end
```

Not only has the component's code become increasingly entangled, but it doesn't work the way we'd expect. The problem is that for the outline version of the button in the disabled state, we now have two conflicting classes, `bg-slate-50` (from `STYLES[:outline]`) and `bg-slate-300` (from `#disabled_classes`). (And the former wins.)

How do we bring back clarity, maintainability, and correctness to our component styles? Allow me to introduce _Style Variants_.

Style Variants is a plugin included in the [view_component-contrib](https://github.com/palkan/view_component-contrib#style-variants) package which was inspired by the [Tailwind Variants][] and [CVA](https://cva.style/docs/getting-started/variants) projects. It allows you to define your styling rules in a declarative way, as follows:

```ruby
class UIKit::Button::Component < ApplicationComponent
  option :type, default: proc { "button" }
  option :variant, default: proc { :default }
  option :disabled, default: proc { false }

  style do
    base {
      %w[
        items-center justify-center px-4 py-2
        text-sm font-medium
        border border-slate-300 shadow-sm rounded-md
        focus:outline-none focus:ring-offset-2
      ]
    }
    variants {
      variant {
        primary {
          %w[
            text-white bg-blue-600 ring-blue-700
            hover:bg-blue-700
            focus:ring-offset-blue-50
            dark:border-slate-950 dark:bg-blue-700 dark:text-blue-50 dark:ring-blue-950
            dark:hover:bg-blue-800
            dark:focus:ring-offset-blue-700
          ]
        }
        outline {
          %w[
            bg-slate-50
            hover:bg-slate-100
            focus:ring-slate-600 focus:ring-offset-blue-50
            dark:border-slate-950 dark:bg-slate-700 dark:ring-slate-950
            dark:hover:bg-slate-800
            dark:focus:ring-offset-slate-700
          ]
        }
      }
      disabled {
        yes { %w[opacity-50 pointer-events-none] }
      }
    }
    defaults { {variant: :primary, disabled: false} }
    # The "compound" directive allows us to declare additional classes to add
    # when the provided combination is used
    compound(variant: :outline, disabled: true) { %w[opacity-75 bg-slate-300] }
  end

  erb_template <<~ERB
    <button type="<%= type %>" class="<%= style(variant:, disabled:) %>"<%= " disabled" if disabled %>>
      <%= content %>
    </button>
  ERB
end
```

All the styling logic is described within the `style do ... end` block. In the HTML template, we only use the `#style(**)` helper and pass variant values to it. Thus, we no longer have classes scattered all over the component code.

We may enforce custom conventions to be used in style variant definitions. For example, you may notice that we've grouped classes by their modifiers (e.g., the `focus:*` and `dark:*` classes are placed on separate lines). It's possible to write a custom RuboCop cop to enforce this convention (and even auto-arrange classes). So, switching from text fragments to Ruby code also opens up new DX possibilities.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/custom-cops-for-rubocop-an-emergency-service-for-your-codebase

The `compound` directive replaces our previous `#disabled_classes` method. How does it solve the class conflict issue? By default, the Style Variants plugin has no assumptions regarding the nature of your classes (i.e., it's not TailwindCSS-specific). To make it a bit smarter and _teach_ it how to resolve CSS conflicts, we can integrate it with the [tailwind_merge][] gem:

```ruby
class ApplicationComponent < ViewComponentContrib::Base
  include ViewComponentContrib::StyleVariants

  style_config.postprocess_with do |classes|
    TailwindMerge::Merger.new.merge(classes.join(" "))
  end
end
```

Style Variants significantly improves the DX of working with TailwindCSS-backed UI components. HTML is no longer _polluted_ with verbose style definitions; CSS classes are well-organized and statically analyzable. With this approach, we've managed to simplify defining `class` HTML attributes. What about all the others? Well, let's talk about HTML attribute propagation.

## HTML attribute propagation

For basic (_atomic_) UI elements, like button and form inputs, it's necessary to support all possible _functional_ HTML attributes (e.g., `required`, `disabled`, `autocomplete`, and so on). Imagine a generic input component defined as follows:

```ruby
class UIKit::Input::Component < ApplicationComponent
  option :name

  option :id, default: proc { nil }
  option :type, default: proc { "text" }
  option :value, default: proc { nil }
  option :autocomplete, default: proc { "off" }
  option :placeholder, default: proc { nil }
  option :required, default: proc { false }
  option :disabled, default: proc { false }

  erb_template <<~ERB

      <input type="<%= type %>"
        <% if id %> id="<%= id %>"<% end %>
        <% if value %> value="<%= value %>"<% end %>
        <% if name %> name="<%= name %>"<% end %>
        autocomplete="<%= autocomplete %>"
        <% if placeholder %> placeholder="<%= placeholder %>"<% end %>
        <% if required %> required<% end %>
        <% if disabled %> disabled<% end %>
      >

  ERB
end
```

Here we have a monstrous template for injecting these attributes (only if they're defined). Why deal with raw strings when we have `content_tag` (or even `text_field_tag`)? The reasons vary (from "avoiding mixing helpers with pure HTML" to "for performance sake"). In any case, the important thing here is that such code can be found in the wild; I haven't just pulled it out of thin air.

We can also declare all of the HTML attributes that we want to expose (via the `.option` DSL from [dry-initializer][]). But this turned out to be far from optimal. First of all, there could be many of them, and most must be injected into HTML as is. Second, there are often attrubutes coming from _the outside_, and we don't want our _isolated_ components be responsible for them (e.g., Stimulus data attributes, or `test_id` for [browser testing](https://evilmartians.com/chronicles/system-of-a-test-2-robust-rails-browser-testing-with-siteprism)).

As the first step towards clarity, we came up with the idea of the _bag of attributes_:

```ruby
class UIKit::Input::Component < ApplicationComponent
  option :name

  option :html_attrs, default: proc { {} }
  option :input_attrs, default: proc { {} }, type: -> { {autocomplete: "off", required: false}.merge(_1) }

  erb_template <<~ERB
    >
      <input <%= tag.attributes(**input_attrs) %>>

  ERB

  def before_render
    input_attrs.merge({name:})
  end
end
```

Instead of enumerating all possible options, we've added just two: `html_attrs` (for the container element) and `input_attrs`. To convert a hash into an HTML attributes string, we use Rails' built-in `tag.attributes` helper (available since Rails 7). Here's the new interface for using a component:

```erb
<%= render UIKit::Input::Component.new(
  name: "name",
  input_attrs: {placeholder: "Enter your name", autocomplete: "on", autofocus: true}) %>
```

Note that we still accept the name of the input field as a separate option to underline its essential role (and that fact that it's required).

The declaration DSL still doesn't look ideal (especially the `type: ...` part). Therefore, we can go even further and _sugarize_ the API like this:

```ruby
class UIKit::Input::Component < ApplicationComponent
  option :name

  html_option :html_attrs
  html_option :input_attrs, default: {autocomplete: "off", required: false}

  erb_template <<~ERB
    >
      <input <%= dots(input_attrs) %>>

  ERB
end
```

Note that the `#dots` alias is a reference to the JS object spread operator (`...`).

## Browser tests for components

To finish off our yearly "ViewComponent on Mars" report, I'd like to share one more tiny extension we've recently released aimed at improving the developer experience when using view components.

In [the first part][part-1] of this series, we mentioned _testability_ as one of the key benefits of switching to components. But, in addition to unit tests, you can also use Rails system tests to test interactive components:

```ruby
# spec/system/components/my_component_spec.rb

it "does some dynamic stuff" do
  visit("/rails/view_components/my_component/default")
  click_on("JavaScript-infused button")

  expect(page).to have_content("dynamic stuff")
end
```

Tests like these rely on the preview functionality, which messes with your storybook and introduces coupling between test and development environments. To deal with this, we built _inline templating_ for tests:

```ruby
it "does some dynamic stuff" do
  visit_template <<~ERB
    <form id="myForm" onsubmit="event.preventDefault(); this.innerHTML = '';">
      <h2>Self-destructing form</h2>
      <%= render Button::Component.new(type: :submit, kind: :info) do %>
        Destroy me!
      <% end %>
    </form>
  ERB

  expect(page).to have_text "Self-destructing form"

  click_on("Destroy me!")

  expect(page).to have_no_text "Self-destructing form"
end
```

Now, the HTML being tested can be defined right inside the test itself, and this functionality is available via the [rails-intest-views][] gem. Give it a try!

## A last note

Since the first post from this series was published about a year and a half ago at the time of writing, the way we build Rails full-stack applications has changed. TailwindCSS has won the battle for CSS (we used to experiment with PostCSS Modules), Webpacker retired, and UI component libraries finally came to the Rails world.

Nevertheless, ViewComponent remains our core UI technology and easily allows us to keep up with ever-changing software development trends. And that's precisely why we continue investing in it!

---

At Evil Martians, we transform growth-stage startups into unicorns, build developer tools, and create open source products. If you’re ready to engage warp drive, [give us a shout](https://evilmartians.com/contact-us)!

[ViewComponent]: https://viewcomponent.org
[Lookbook]: https://lookbook.build
[part-2]: https://evilmartians.com/chronicles/viewcomponent-in-the-wild-supercharging-your-components#setting-up-a-storybook-bonus
[TailwindCSS]: https://tailwindcss.com
[no-build]: https://world.hey.com/dhh/you-can-t-get-faster-than-no-build-7a44131c
[view_component-contrib]: https://github.com/palkan/view_component-contrib
[Tailwind Variants]: https://www.tailwind-variants.org
[tailwind_merge]: https://github.com/gjtorikian/tailwind_merge
[Phlex]: https://www.phlex.fun
[part-1]: https://evilmartians.com/chronicles/viewcomponent-in-the-wild-building-modern-rails-frontends
[rails-intest-views]: https://github.com/palkan/rails-intest-views
[dry-initializer]: https://dry-rb.org/gems/dry-initializer

---

This article was written by [Evil Martians](https://evilmartians.com), a product development consultancy trusted by teams from early-stage startups to public companies. If you're working through a hard problem, [we can help](https://evilmartians.com/contact-us)
