Pax.Plugin behaviour (Pax v0.0.1-dev)

View Source

Pax.Plugin is a behaviour for defining plugins that can be used with Pax.Interface or Pax.Admin. This is the base behavior that all Plugins must implement. However, most plugins will use Pax.Interface.Plugin or use Pax.Admin.Plugin instead of implementing this behaviour directly.

Summary

Types

The plugin specification, with or without init options

A Phoenix.LiveView socket

t()

The plugin struct

Callbacks

A function that returns the key for the plugin's configuration. Must be unique.

A function that returns a valid Pax.Config spec for configuration keys and types accepted by the plugin.

The plugin initialization function, must return a map of initialized plugin state, which is passed to all other callback functions in the plugin.

A function that merges any additional configuration options into the plugin's opts.

Render a plugin component.

The type of plugin

Functions

Initialize the given plugin with the provided callback module and options.

Render plugins.

Types

pluginspec()

@type pluginspec() :: module() | {module(), opts :: keyword()}

The plugin specification, with or without init options

socket()

@type socket() :: Phoenix.LiveView.Socket.t()

A Phoenix.LiveView socket

t()

@type t() :: %Pax.Plugin{
  config_key: term(),
  module: module(),
  opts: map(),
  type: atom()
}

The plugin struct

Callbacks

config_key()

@callback config_key() :: atom()

A function that returns the key for the plugin's configuration. Must be unique.

config_spec()

@callback config_spec() :: map()

A function that returns a valid Pax.Config spec for configuration keys and types accepted by the plugin.

init(callback_module, opts)

@callback init(callback_module :: module(), opts :: keyword()) :: map()

The plugin initialization function, must return a map of initialized plugin state, which is passed to all other callback functions in the plugin.

merge_config(opts, config, socket)

@callback merge_config(opts :: map(), config :: map(), socket()) :: map()

A function that merges any additional configuration options into the plugin's opts.

render(opts, section, assigns)

@callback render(opts :: map(), section :: atom(), assigns :: map()) ::
  Phoenix.LiveView.Rendered.t() | nil

Render a plugin component.

type()

@callback type() :: :interface | :admin

The type of plugin

Functions

config_key(plugin)

config_spec(plugin)

init(callback_module, plugin_module)

@spec init(callback_module :: module(), pluginspec()) :: t()

Initialize the given plugin with the provided callback module and options.

merge_config(plugin, config, socket)

render(section, assigns)

Render plugins.

You can define your own plugin sections in your interface, then create plugins that implement those areas.

<div>
  {Pax.Plugin.render(:my_plugin_section, assigns)}
</div>

Then in your plugin, you can define a render function for that section.

defmodule MyPaxInterfacePlugin do
  use Pax.Interface.Plugin
  use Phoenix.Component

  def render(_opts, :my_plugin_section, assigns) do
    ~H"""
    <div>{@pax.plural_name}</div>
    """
  end
end