Current section
Files
Jump to
Current section
Files
lib/mead/component.ex
defmodule Mead.Component do
@moduledoc """
The `~PDF` authoring surface.
defmodule MyDoc do
use Mead.Component
attr :title, :string, required: true
slot :inner_block
def panel(assigns) do
~PDF\"\"\"
<view padding="10" background="#EEF1FA" gap="6">
<text font_size="14" color="#283C86">{@title}</text>
{render_slot(@inner_block)}
</view>
\"\"\"
end
end
Mead.render(MyDoc.panel(%{title: "Hello", inner_block: []}))
`use Mead.Component` pulls in `Phoenix.Component` (for `attr`/`slot`
declarations and their compile-time validation) and the `~PDF` sigil.
Attributes are string-typed and CSS-like; see `Mead.Markup` for the
accepted values. Templates compile through `Phoenix.LiveView.TagEngine`
with `Mead.TagHandler`, so unknown tags and malformed component calls
are compile-time errors.
"""
defmacro __using__(_opts) do
quote do
use Phoenix.Component
import Mead.Component, only: [sigil_PDF: 2]
end
end
@doc """
Compiles a Mead template. Requires an `assigns` variable in scope.
"""
defmacro sigil_PDF({:<<>>, meta, [expr]}, []) do
if !Macro.Env.has_var?(__CALLER__, {:assigns, nil}) do
raise ~s(~PDF requires a variable named "assigns" to exist and be set to a map)
end
Phoenix.LiveView.TagEngine.compile(expr,
file: __CALLER__.file,
line: __CALLER__.line + 1,
caller: __CALLER__,
indentation: meta[:indentation] || 0,
tag_handler: Mead.TagHandler
)
end
end