Replicate core components in earlier version of Phoenix Elixir but do not have attr

Hello everyone,

I am trying to come up with a core component file in earlier version of Phoenix Elixir.

E.g. in the new version, we have something like

@doc """
  Renders a label.
  """
  attr(:for, :string, default: nil)
  slot(:inner_block, required: true)

  def label(assigns) do
    ~H"""
    <label for={@for} class="block text-sm font-semibold leading-6 text-zinc-800">
      <%= render_slot(@inner_block) %>
    </label>
    """
  end

The attr(:for, :string, default: nil) handle the case for when @for is not found. However, there is no attr in earlier version. How can i go about doing it?

Thanks!

def label(assigns) do
  assigns = assign_new(assigns, :for, fn -> nil end)

  ~H"""
  ...
  """
end
1 Like