achempion
LiveCapture - zero-boilerplate storybook for LiveView components by Waffle creator
Hey, Elixir Forum. I’m excited to introduce LiveCapture, a storybook-like library for Phoenix LiveView components.
I like having stories for my components, but I’ve always felt some friction when creating and maintaining them. With LiveCapture, I tried to reduce the boilerplate as much as possible, up to the point where you don’t need to write stories at all to get a storybook.
Here’s an example of two simple LiveView components:
defmodule SimpleComponents do
use MyAppWeb, :component
capture_all()
attr :name, required: true, default: "World"
def greeting(assigns), do: ~H"Hello, {@name}!"
attr :title, required: true, examples: ["H1 header"]
def title(assigns), do: ~H"<h1>{@title}</h1>"
end
Just add capture_all() to capture all components in the module. You don’t need to do anything else, LiveCapture can infer component attributes automatically.
I’ve created an example module that shows typical capture patterns. LiveCapture also supports state variants, slots with HEEx templates, dynamic attribute resolution, and more.
Here’s a live example of a hosted storybook for all Phoenix LiveDashboard components.
The main use case for LiveCapture is local development, where you can quickly build and review complex UI states. For example, I was working on a component that renders a live transcription of an ongoing phone call. It helped a lot to be able to render different states without having to actually start a call.
Main features:
- Render HEEx components with predefined state snapshots
- Quickly test visual quality by switching between different width breakpoints
- Explore component documentation with dynamic state inspection
- Nice DSL with a strong focus on ergonomics and simplicity
If you like the project, please give it a star on GitHub.
P.S. I also built the Captures website, which hosts storybooks from other projects.
I’ll appreciate any feedback and your thoughts on the library.
Most Liked
achempion
I’ve created a PR with integration example. Feel free to ping me in the linked PR.
achempion
I’ve released a new version which adds component schema generation with component urls. It’s now become possible to use this schema with Playwright for visual regression testing.
Usage
mix live_capture.gen.schema --module MyAppWeb.LiveCapture --url-prefix /storybook path/to/captures.json
Output Format
captures.json
[
{
"schema": "MyAppWeb.LiveCapture",
"breakpoints": [
{"name": "s", "width": "480px"},
{"name": "m", "width": "768px"}
],
"captures": [
{"module": "MyAppWeb.Example", "function": "simple", "variant": null, "url": "/storybook/raw/components/Example/button"},
{"module": "MyAppWeb.Example", "function": "with_capture_variants", "variant": "main", "url": "/storybook/raw/components/Example/button/main"},
{"module": "MyAppWeb.Example", "function": "with_capture_variants", "variant": "secondary", "url": "/storybook/raw/components/Example/button/secondary"}
]
}
]
achempion
I’ve released a new version that supports binding local variables for slot templates.
It’s now possible to pass :let bindings inside slot templates.
Example
Table component
slot :column do
attr :label, :string, required: true
end
attr :rows, :list, default: []
def table(assigns) do
~H"""
<table>
<tr>
<th :for={col <- @column}>{col.label}</th>
</tr>
<tr :for={row <- @rows}>
<td :for={col <- @column}>{render_slot(col, row)}</td>
</tr>
</table>
"""
end
Capture definition
capture attributes: %{
rows: [1, 2, 3],
column: [
%{inner_block: "row {row_number}", label: "Row number", let: :row_number},
%{inner_block: "column", label: "Column"},
]
}
See how the local variable is referenced with inner_block: "row {row_number}"
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









