Packages

Expiphany is an Elixir/Phoenix wrapper around [Epiphany](https://github.com/epiphany) components to make working with Epiphany in Phoenix seamless.

Current section

Files

Jump to
expiphany lib expiphany button.ex
Raw

lib/expiphany/button.ex

defmodule Expiphany.Button do
import Phoenix.HTML.Tag, only: [content_tag: 3]
alias Expiphany.Tagname
@submit_button_attributes [
:disabled,
:full,
:name,
:size,
:value,
:variant
]
@moduledoc """
`Expiphany.Button` module is responsible for helper functions for rendering Epiphany buttons.
"""
@doc """
sumbit renders a submit button with the same API as the original phoenix helper. It allows you to set all the relevant fields for ep-button that are necessary for a sumbmit button. The submit function can not render link-style buttons.
## Examples
iex> import Phoenix.HTML, only: [safe_to_string: 1]
iex> safe_to_string(Expiphany.Button.submit("Test"))
"<ep-button type=\\"submit\\">Test</ep-button>"
iex> safe_to_string(Expiphany.Button.submit("Test", size: "small", full: true, disabled: true))
"<ep-button disabled full size=\\"small\\" type=\\"submit\\">Test</ep-button>"
iex> safe_to_string(Expiphany.Button.submit("Test", href: "http://example.com", type: "button", target: "foo"))
"<ep-button type=\\"submit\\">Test</ep-button>"
iex> safe_to_string(Expiphany.Button.submit("Test", prefix_icon: "search"))
"<ep-button type=\\"submit\\"><ep-svg name=\\"search\\" slot=\\"prefix\\"></ep-svg>Test</ep-button>"
iex> safe_to_string(Expiphany.Button.submit("Test", suffix_icon: "search"))
"<ep-button type=\\"submit\\">Test<ep-svg name=\\"search\\" slot=\\"suffix\\"></ep-svg></ep-button>"
"""
def submit(content, opts \\ [])
def submit(opts, [do: _] = block_option) do
"ep-button"
|> Tagname.cast()
|> content_tag(build_opts(opts), block_option)
end
def submit(content, opts) do
"ep-button"
|> Tagname.cast()
|> content_tag(build_button_content(content, opts), build_opts(opts))
end
defp build_button_content(content, prefix_icon: icon) do
[
build_icon(icon, "prefix"),
content
]
end
defp build_button_content(content, suffix_icon: icon) do
[
content,
build_icon(icon, "suffix")
]
end
defp build_button_content(content, _opts) do
content
end
defp build_icon(icon, slot) do
content_tag(Tagname.cast("ep-svg"), "", name: icon, slot: slot)
end
defp build_opts(opts) do
opts
|> Keyword.take(@submit_button_attributes)
|> Keyword.put_new(:type, "submit")
end
end