Lavash.Component.Helpers (Lavash v0.3.0-rc.2)

Copy Markdown View Source

Helper functions for using Lavash components in LiveViews.

Summary

Functions

Gets the state for a specific component by ID.

Extracts component states from connect_params and stores them in socket assigns.

Builds the optimistic state map for a component.

Functions

get_component_state(component_states, component_id)

Gets the state for a specific component by ID.

init_component_states(socket)

Extracts component states from connect_params and stores them in socket assigns.

Call this in your LiveView's mount/3:

def mount(_params, _session, socket) do
  socket = Lavash.Component.Helpers.init_component_states(socket)
  {:ok, socket}
end

Then pass the state to components:

<.live_component
  module={MyComponent}
  id="my-component"
  __lavash_initial_state__={Lavash.Component.Helpers.get_component_state(@__lavash_component_states__, "my-component")}
/>

optimistic_state(module, assigns)

Builds the optimistic state map for a component.

This is used to expose component state via data attributes for potential client-side access (without a JavaScript hook).