PhoenixKitWeb.Components.MediaBrowser.Embed (phoenix_kit v1.7.125)

Copy Markdown View Source

One-line embedder for PhoenixKitWeb.Components.MediaBrowser.

LiveView uploads must live on the parent socket (not the LiveComponent), so embedding the MediaBrowser requires three small pieces of plumbing on the parent: an allow_upload, a "validate" event stub for the upload channel, and a handle_info delegator for component→parent messages.

This module bundles all three into a single use call.

Usage

defmodule MyAppWeb.MediaPage do
  use MyAppWeb, :live_view
  use PhoenixKitWeb.Components.MediaBrowser.Embed

  def mount(_params, _session, socket) do
    {:ok, socket}
  end
end

Then in the template:

<.live_component
  module={PhoenixKitWeb.Components.MediaBrowser}
  id="media-browser"
  parent_uploads={@uploads}
/>

What gets injected

  • on_mount — calls MediaBrowser.setup_uploads/1 so @uploads.media_files is available on every mount of this LiveView.
  • Fallback handle_event("validate", _, socket) — absorbs the upload channel's phx-change events. User-defined clauses with other event names still win because they are defined first.
  • Fallback handle_info({MediaBrowser, _, _}, socket) — forwards to MediaBrowser.handle_parent_info/2 for component registration and upload piping.
  • Fallback handle_info({:leaf_changed, _}, socket) — routes Leaf editor content updates from the sidebar comments (when PhoenixKitComments is installed) to PhoenixKitComments.Web.CommentsComponent.forward_leaf_event/2. Without this, comment Leaf editors render but the typed content never reaches the server. The clause only injects when PhoenixKitComments.Web.CommentsComponent is loaded — otherwise it's compiled away. User-defined :leaf_changed clauses (e.g. for a post-content editor on the same page) still win because they are defined first.

All fallbacks are injected via @before_compile, so user clauses declared earlier in the module match before them.

Summary

Functions

on_mount(atom, params, session, socket)