Toggle field
v0.0.10
mix mishka.ui.gen.component toggle_field
Toggle component
The toggle_field component is a switch-like input used to enable or disable a setting or feature. It provides a clear and interactive way for users to select between two states, such as "on" and "off" or "enabled" and "disabled." With its simple, intuitive design, the toggle_field is often used for settings, preferences, or activating specific features in applications. It enhances the user experience by offering a quick and clear method of making binary choices.
Toggle component
<.toggle_field color="base" />
<.toggle_field color="danger" />
<.toggle_field color="dark" />
<.toggle_field color="warning" />
<.toggle_field color="natural" />
<.toggle_field color="white" />
<.toggle_field color="success" />
<.toggle_field color="silver" />
<.toggle_field color="misc" />
<.toggle_field color="dawn" />
<.toggle_field color="info" />
<.toggle_field color="primary" />
<.toggle_field color="secondary" />Toggle rounded prop
The rounded prop is a versatile feature that controls the corner radius of toggle elements, allowing you to adjust how rounded or circular the toggle appears. It offers predefined sizes such as extra_small, small, medium, large, extra_large, and full, each applying different levels of rounding to both the toggle base and circle. For instance, extra_small gives subtle rounded corners, while full creates a completely circular toggle. By default, the prop is set to full, ensuring a polished and modern look. Additionally, you can specify a custom value to achieve unique designs if needed, making it a flexible option for styling toggles to fit various design contexts.
Toggle rounded prop
<!--Default is full-->
<.toggle_field rounded="extra_small" />
<.toggle_field rounded="small" />
<.toggle_field rounded="medium" />
<.toggle_field rounded="large" />
<.toggle_field rounded="extra_large" />
<.toggle_field rounded="full" />Toggle checked prop
The checked prop in the toggle_field component determines whether the toggle is initially set to "on" or "off". When the checked prop is set to true, the toggle will be in the "on" position by default. This prop allows you to control the initial state of the toggle, making it useful for settings or features that should be enabled when the form or page is first loaded.
Toggle checked prop
<.toggle_field color="primary" />
<.toggle_field color="secondary" checked={true} />toggle_check helper
Here is a real-world example provided for you.
toggle_check helper
<.form_wrapper
for={@form}
id="user-form"
phx-change="validate"
phx-submit="save"
class="space-y-5"
>
<.input label="Full name" field={@form[:fullname]} />
<.input
field={@form[:job]}
type="select"
label="Choose your Job"
options={Ecto.Enum.values(User, :job)}
/>
<.h3>What are your hobbies?</.h3>
<.group_checkbox variation="horizontal" field={@form[:hobbies]} color="danger">
<:checkbox
value="reading"
checked={checkbox_check(:list, {:hobbies, "reading"}, @form)}
>
Reading
</:checkbox>
<:checkbox
value="programming"
checked={checkbox_check(:list, {:hobbies, "programming"}, @form)}
>
Programming
</:checkbox>
<:checkbox
value="photography"
checked={checkbox_check(:list, {:hobbies, "photography"}, @form)}
>
Photography
</:checkbox>
</.group_checkbox>
<.toggle_field
field={@form[:accepted_terms]}
label="Terms"
checked={toggle_check(:accepted_terms, @form)}
color="primary"
phx-debounce="300"
/>
<:actions>
<.button phx-disable-with="Saving...">Save User</.button>
</:actions>
</.form_wrapper>Attributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | No | A unique identifier is used to manage state and interaction |
class
|
any
| — | — | No | Custom CSS class for additional styling |
color
|
string
|
primary
|
basewhitenaturalprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
| No | Determines color theme |
rounded
|
string
|
full
| — | No | Determines border radius |
description
|
string
| — | — | No | Determines a short description |
border
|
string
|
extra_small
| — | No | Determines border style |
label_class
|
string
| — | — | No | Determines the label class |
label_wrapper_class
|
string
| — | — | No | Determines the label wrapper |
description_class
|
string
| — | — | No | Determines the label description |
toggle_wrapper_class
|
string
| — | — | No | Determines the toggle wrapper |
toggle_field_wrapper_class
|
string
| — | — | No | Determines the toggle field wrapper |
toggle_base_class
|
string
| — | — | No | Determines the toggle base |
toggle_circle_class
|
string
| — | — | No | Determines the toggle circle |
size
|
string
|
medium
|
extra_smallsmallmediumlargeextra_large
| No | Determines the overall size of the elements, including padding, font size, and other items |
checked
|
boolean
| — | — | No | Determines if the toggle is initially checked |
error_icon
|
string
| — | — | No | Icon to be displayed alongside error messages |
label
|
string
| — | — | No | Specifies text for the label |
errors
|
list
|
[]
| — | No | List of error messages to be displayed |
name
|
any
| — | — | No | Name of input |
value
|
any
| — | — | No | — |
field
|
Phoenix.HTML.FormField
| — | — | No | a form field struct retrieved from the form |
rest
|
global
| — | — | No | Global attributes can define defaults which are merged with attributes provided by the caller |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--color
|
basewhitenaturalprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
|
--size
|
extra_smallsmallmediumlargeextra_large
|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.component toggle_field