Rails UI components
Table of Contents
Classical approach #
Classical Rails view (V from MVC) approach is: ERB plus helpers. Which conceptually it the same as PHP4. And it’s understandable - when Rails were invented it was the only way.
There are alternatives to ERB, like: HAML, Slim, liquid. But this is essentially the same approach
New approach #
There are different opinions about React, somebody loves it, somebody hates it. But no one can’t argue that it changed frontend development practices. And the main changes are JSX and components. React components broke old stigma that separation of concerns should be horizontal: HTML, CSS, JS. Instead React component can encapsulate all that it needs. One can pick up component of the shelf (npm) and use it directly (almost) without configuration.
And this idea slowly cripples in Rails world. There are several libraries which adopt components approach:
And benchmark if you’re interested.
ViewComponents #
ViewComponents created by Github. To my taste it requires a lot of boilerplate code. Second concern is that Github took course to migrate to React, so ViewComponents may be abandoned eventually.
Component libraries:
Form builders:
Storybook-like:
Phlex #
Ligtweight syntax compared to ViewComponents.
Component libraries:
Form builders:
Storybook-like:
Ideas #
HTML-over-the-Wire comparison #
Alpine.js UI libraries #
Alpine.js is a good JS option for “backend” components. I imagine it would be easy to port components:
Tailwind UI libraries #
Tailwind is a good CSS options for “backend” components. I imagine it would be easy to port components:
Other #
It is worth to mention RailsUI, shadcn.rails-components.com and hotwire.io.
And it would be nice to have component for icons from iconify.
Read more: Server-side UI Components, Ruby tooling