achempion

achempion

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.

https://github.com/achempion/live_capture

Showing Posts 1 to 10

netoum

netoum

Very interesting and an amazing alternative.
Is it possible to capture components from a dependency ?
Or the components must live in the same app where live capture is running
I am working a UI library and I would like to keep a separation between the main library and testing/playground?

achempion

achempion OP

Thank you! I built LiveCapture with the UI-library use case in mind.

You can mount the Storybook from any project that uses LiveCapture inside a playground app. For example, here’s how I mounted the Phoenix LiveDashboard Storybook inside the Captures app, which hosts storybooks from other open-source projects and libraries.

If you’d like, you can add your library there as well, and it’ll be available on the Captures website.

P.S. There’s a common pattern libraries use to set up a development playground for local testing. It’s usually a bare-bones Phoenix app defined in dev.exs (note the .exs extension). See LiveCapture’s dev.exs and Phoenix LiveDashboard’s dev.exs for reference.

Let me know if you have any questions or run into any rough edges. I’m happy to fix them.

achempion

achempion OP

Presented LiveCapture at Copenhagen Elixir meetup

netoum

netoum

Thanks for the detailed reply.
So I tried couple of way, but I am bit lost :sweat_smile:
My goal is to keep the component files as it is without modifying them
Is it possible to capture the components, without adding capture_all() inside the component file.
Here is the library if it helps, I would like to add live capture as a dependency but only in e2e app without modifying the original components

https://github.com/corex-ui/corex

achempion

achempion OP

I’ve created a PR with integration example. Feel free to ping me in the linked PR.

netoum

netoum

Thanks a lot, you are a saver. It works like a charm.
I will keep you updated with some stories

achempion

achempion OP

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"}
    ]
  }
]
netoum

netoum

@achempion I hope you are doing great

I have added some Captures on the Live Demo.
I could edit the root layout and it works good.
I have 2 questions you can maybe help with

1.Can we catch the Live View handle events to display them ?

2.Slots with :let
Currently when using slot with :let , I use an internal function but ideally it would render the way the user would write the slot

Instead of this:

trigger: [%{inner_block: &__MODULE__.custom_trigger/2}],

we would do:

trigger: [%{
inner_block: """<.icon name={item.data.meta.icon} />{item.data.trigger}""",
let: item
}],

on the component thant dont require :let it works good and render the full item as the user would create it

<Listbox.listbox
class="listbox"
collection={[...]}>
<:label>
Choose a country
</:label>
<:item_indicator>
<.icon name="hero-check" />
</:item_indicator>
</Listbox.listbox>
achempion

achempion OP

Thank you for the feedback, I’ll get back to you in a couple of days.

achempion

achempion OP

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}"

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews