Lesson 1 — Lookbook: a component browser for Phlex
What is Lookbook?
Lookbook is a UI development environment for Rails. It gives you a
browser-based component preview system where you can view every
Phlex::UI component in isolation, with different props and states, in a
resizable viewport. Think of it as a living style guide that lives inside
your app and updates in real time as you work.
In Module 3 we used demo.rb — a standalone Ruby script that rendered
all components to an HTML file. Lookbook replaces that with something far
more powerful: a proper component browser integrated into the Rails app,
accessible at /lookbook during development.
Installation
Add Lookbook to the development group in your Gemfile:
|
|
For live UI updates when you change component or preview files, also add:
|
|
listen and actioncable are optional — without them Lookbook still
works, you just need to manually refresh the browser to see changes.
Many Rails apps already include these gems. Run bundle install.
Mount the engine
|
|
Mounting inside Rails.env.development? ensures Lookbook is never
accidentally exposed in production.
Tell Lookbook where your components live
By default Lookbook looks for Phlex views in app/views. Our components
live in app/components — add that path to the Lookbook config:
|
|
Preview files
Previews live in test/components/previews/. Each preview class
inherits from Lookbook::Preview and defines one method per scenario:
|
|
Start the server and visit http://localhost:3000/lookbook. You should
see Button in the sidebar with all eight scenarios listed. Click each
one to preview it in the viewport.
Preview layout
By default Lookbook renders previews without any surrounding HTML — just the component in isolation. This is fine for most components, but for components that need a page context (correct font, background colour etc.) you can specify a layout:
|
|
Create the layout at app/views/layouts/lookbook/preview.html.erb:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
</head>
<body class="p-8 bg-white">
<%= yield %>
</body>
</html>This gives every preview the Tailwind stylesheet and comfortable padding. Set it as the default in the Lookbook config so you don’t have to specify it on every preview class:
|
|
Previews for all existing components
Create a preview file for each component in Phlex::UI. These follow
the same pattern as ButtonPreview — one method per meaningful scenario:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Visit http://localhost:3000/lookbook — you should see all Phlex::UI
components in the sidebar, each with multiple preview scenarios.
Exercise
Add a # @param annotation to ButtonPreview so the variant can be
changed interactively in the Lookbook UI:
|
|
This adds an interactive controls panel to the Lookbook UI where you can change the variant, label, and disabled state without editing code. Note that we can’t allow an empty label so we provide a default as a fallback.