Lesson 8 — Lookbook previews for interactive components
Lookbook renders previews inside an iframe that loads your full
application layout — including JavaScript. With javascript_importmap_tags
in the preview layout, Stimulus controllers connect automatically when
the preview loads. Every interactive component built in this module
works in Lookbook exactly as it does in the app.
Check your preview layout
Open app/views/layouts/lookbook/preview.html.erb and confirm it
includes javascript_importmap_tags:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>
<body class="p-8 bg-surface text-text">
<%= yield %>
</body>
</html>If that tag is missing, add it now. Without it the Stimulus controllers won’t connect and interactive components will render but not respond.
Alert previews
The dismissible scenario tests the full dismiss flow — click the button and the alert fades out and is removed from the DOM.
|
|
Modal previews
The modal renders open by default in Lookbook — passing open: true
means you see the modal immediately without needing a trigger button.
Test backdrop click, the × button, and the Escape key.
|
|
Note that inside Lookbook preview classes you must use
render Components::Button.new(...) rather than the Kit shorthand
Button(...) — preview classes inherit from Lookbook::Preview, not
Components::Base, so Kit methods are not available.
Dropdown previews
|
|
Test click-outside close by clicking anywhere in the preview pane outside the menu. Test arrow key navigation by opening the menu and pressing ↑ and ↓. Test Escape to close.
Toast previews
duration: 0 disables auto-dismiss so the toast stays visible in
Lookbook. Without it the toast disappears after four seconds — usually
before you have finished inspecting it.
|
|
The auto_dismiss scenario is the one exception — it uses the real
duration so you can observe the fade-out animation. Reload the preview
to see it again.
Accordion previews
|
|
The single_expand scenario verifies that opening one panel closes the
others. The multiple_expand scenario verifies that multiple panels can
be open simultaneously.
Tabs previews
|
|
Module 8 summary
- Stimulus enhances server-rendered HTML — controllers attach via
data-controller, read configuration fromdata-*attributes, and disconnect automatically when elements are removed from the DOM - In Phlex,
data-*attributes are written explicitly in the component — clear, readable, and co-located with the HTML they describe.eagerLoadControllersFromregisters controllers automatically — no manual imports needed Alert— properly wired dismiss with CSS transitionModal— focus trap, keyboard escape, backdrop click to close, ARIAdialogroleDropdown— click-outside close, arrow key navigation, full keyboard accessibilityToast— queued ephemeral notifications, auto-dismiss, accessible live region, Turbo Stream integration, replaces flash messagesAccordion— single and multi-expand, CSS transitions, keyboard accessibleTabs— server-rendered tab navigation, full ARIA tab pattern- Cross-controller communication — three patterns: outlets for tight coupling, custom events for loose coupling, common ancestor for coordinating multiple children
Components built this module
Components::ModalComponents::DropdownComponents::ToastContainerComponents::ToastComponents::AccordionComponents::Tabs
Stimulus controllers written this module
alert_controller.jsmodal_controller.jsdropdown_controller.jstoast_controller.jsaccordion_controller.jstabs_controller.js