Packages
corex
0.1.0-alpha.28
0.1.2
0.1.1
0.1.0
0.1.0-rc.1
0.1.0-rc.0
0.1.0-beta.5
0.1.0-beta.4
0.1.0-beta.3
0.1.0-beta.2
0.1.0-beta.1
0.1.0-alpha.33
0.1.0-alpha.32
0.1.0-alpha.31
0.1.0-alpha.30
0.1.0-alpha.29
0.1.0-alpha.28
0.1.0-alpha.27
0.1.0-alpha.26
0.1.0-alpha.25
0.1.0-alpha.24
0.1.0-alpha.23
0.1.0-alpha.22
0.1.0-alpha.21
0.1.0-alpha.20
0.1.0-alpha.19
0.1.0-alpha.18
0.1.0-alpha.17
0.1.0-alpha.16
0.1.0-alpha.15
0.1.0-alpha.14
0.1.0-alpha.13
0.1.0-alpha.12
0.1.0-alpha.11
0.1.0-alpha.10
0.1.0-alpha.9
0.1.0-alpha.8
0.1.0-alpha.7
0.1.0-alpha.6
0.1.0-alpha.5
0.1.0-alpha.4
0.1.0-alpha.3
0.1.0-alpha.2
0.1.0-alpha.1
Accessible and unstyled UI components library written in Elixir and TypeScript that integrates Zag.js state machines into the Phoenix Framework.
Current section
Files
Jump to
Current section
Files
lib/components/url_input.ex
defmodule Corex.UrlInput do
@moduledoc ~S'''
URL input component with Corex design and form field support.
## Examples
### Basic
```heex
<.url_input id="website" name="user[website]" class="url-input">
<:label>Website</:label>
</.url_input>
```
### With icon
```heex
<.url_input id="website" name="user[website]" class="url-input">
<:label>Website</:label>
<:icon><.icon name="hero-link" class="icon" /></:icon>
</.url_input>
```
### With form field
```heex
<.url_input field={@form[:website]} class="url-input">
<:label>Website</:label>
<:error :let={msg}>
<.icon name="hero-exclamation-circle" class="icon" />
{msg}
</:error>
</.url_input>
```
## Styling
Use data attributes to target elements:
```css
[data-scope="url-input"][data-part="root"] {}
[data-scope="url-input"][data-part="label"] {}
[data-scope="url-input"][data-part="control"] {}
[data-scope="url-input"][data-part="icon"] {}
[data-scope="url-input"][data-part="input"] {}
[data-scope="url-input"][data-part="error"] {}
```
Use `data-no-icon` on the root when the icon slot is not provided for proper border radius styling.
Use the class `url-input` on the component for default Corex styling.
'''
@doc type: :component
use Phoenix.Component
attr(:id, :string, required: false)
attr(:name, :string, required: false)
attr(:value, :any, default: nil)
attr(:form, :string, required: false)
attr(:errors, :list, default: [], doc: "List of error messages to display")
attr(:class, :any, default: nil)
attr(:field, Phoenix.HTML.FormField,
doc: "A form field struct from the form, e.g. @form[:website]"
)
attr(:rest, :global,
include: ~w(autocomplete disabled maxlength minlength pattern placeholder readonly required
cols list form)
)
slot(:label, required: false)
slot(:icon, required: false)
slot(:error, required: false) do
attr(:class, :string, required: false)
end
def url_input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []
assigns =
assigns
|> assign(field: nil)
|> assign(:errors, Enum.map(errors, &Corex.Gettext.translate_error(&1)))
|> assign_new(:id, fn -> field.id end)
|> assign_new(:name, fn -> field.name end)
|> assign_new(:value, fn -> field.value end)
|> assign_new(:form, fn -> field.form.id end)
url_input(assigns)
end
def url_input(assigns) do
assigns =
assigns
|> assign_new(:id, fn -> "url-input-#{System.unique_integer([:positive])}" end)
~H"""
<div id={@id} class={@class} data-no-icon={if @icon == [], do: "", else: nil}>
<div data-scope="url-input" data-part="root">
<label :if={@label != []} data-scope="url-input" data-part="label" for={"#{@id}-input"}>
{render_slot(@label)}
</label>
<div data-scope="url-input" data-part="control">
<span :if={@icon != []} data-scope="url-input" data-part="icon" aria-hidden="true">
{render_slot(@icon)}
</span>
<input
type="url"
id={"#{@id}-input"}
name={@name}
value={Phoenix.HTML.Form.normalize_value("url", @value)}
data-scope="url-input"
data-part="input"
{@rest}
/>
</div>
</div>
<div :if={@error} :for={msg <- @errors} data-scope="url-input" data-part="error">
{render_slot(@error, msg)}
</div>
</div>
"""
end
end