Select
Select
creates better-styled select boxes that uses a browserβs native <select>
element.
-->
Select
creates better-styled select boxes that uses a browserβs native <select>
element.
You just need a name
, a label
, and a list of items.
If you want to use custom labels, just pass an array of objects into items
.
Add multiple
to get a native multi-select element. These can be styled with CSS to a certain extent. (Just not in Safari).
Just set selected
to the value of the item you want to preselect. This works for both single and multi selects!
Like all Checkboxes
and Radios
, weβve added a readonly
feature to Select
that lets you disable and values to the server without allowing users to change them.
Native <select>
and <option>
elements are quite difficlt to style by default. We made it easier for you with a couple of CSS variables.
That said, styling options are still quite limited since not all browsers respect styles given to <option>
.
This is editable. Give it a try
Select
creates a native select component that lets you select a single item.
Each select item (or option
, as they call it), needs to have a value
.
The label
is optional. If you donβt provide a label
, Select
will create labels for each item by converting value
to Title Case.
For convenience, you can also pass an array of strings to Select
.
You can preselect any item by adding selected
to the respective item.
Another way to preselect an item is to set the itemβs value as selected
.
The Select
component can be disabled by setting the disabled
property. When disabled, Select
will not send data when the form is submitted. Use readonly
if you still want data to be sent.
You can disable each option by setting disabled
to true
.
You can make the Select
component readonly by setting the readonly
property. When readonly, Select
will send data when the form is submitted, but the user will not be able to change the value.
The native <select>
element does not contain a readonly
attribute. We made it possible by adding a hidden input with the same name as the select element, and with the same value as the selected option.
Individual options cannot be set to readonly
.
You can change the select icon by setting icon
. Please see SVG
for more information on how to use icons.
All other properties passed to Select
will be added to the <select>
element. All other properties created for each item will be passed directly to their respective <option>
elements.
Use the native multiple
attribute to create a multi-select component.
Almost every browser shows 4 items in a multi-select by default. You can change this by setting the size
property.
You can preselect items by selected
to true
to the respective item.
Another way to preselect an item is to set the itemβs value as selected
.
Use disabled
to disable the Select
component. When disabled, Select
will not send data when the form is submitted. Use readonly
if you still want data to be sent.
You choose to disable individual options by setting their disabled
property to true
.
Use readonly
to make the Select
component readonly. When readonly, Select
will send data when the form is submitted, but the user will not be able to change the value.
The contents of this section will only work in a .svelte
file and not inside
an .astro
file.
You can bind to the <select>
element with the ref
property.
You can bind
to the selected
property to get the selected value.
If youβre using multiple
, selected
should be an array instead of a string value.
{
selected: null
}
{
selected: [
]
}
Only the change
event is supported on Select
. You can listen to the event like this:
We only support the change
event because Svelte 4 doesnβt have automatic
event-forwarding capability. All events will be forwarded automatically when
we migrate to Svelte 5.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Select
creates a native select component that lets you select a single item.
Each select item (or option
, as they call it), needs to have a value
.
The label
is optional. If you donβt provide a label
, Select
will create labels for each item by converting value
to Title Case.
For convenience, you can also pass an array of strings to Select
.
You can preselect any item by adding selected
to the respective item.
Another way to preselect an item is to set the itemβs value as selected
.
The Select
component can be disabled by setting the disabled
property. When disabled, Select
will not send data when the form is submitted. Use readonly
if you still want data to be sent.
You can disable each option by setting disabled
to true
.
You can make the Select
component readonly by setting the readonly
property. When readonly, Select
will send data when the form is submitted, but the user will not be able to change the value.
The native <select>
element does not contain a readonly
attribute. We made it possible by adding a hidden input with the same name as the select element, and with the same value as the selected option.
Individual options cannot be set to readonly
.
You can change the select icon by setting icon
. Please see SVG
for more information on how to use icons.
All other properties passed to Select
will be added to the <select>
element. All other properties created for each item will be passed directly to their respective <option>
elements.
Use the native multiple
attribute to create a multi-select component.
Almost every browser shows 4 items in a multi-select by default. You can change this by setting the size
property.
You can preselect items by selected
to true
to the respective item.
Another way to preselect an item is to set the itemβs value as selected
.
Use disabled
to disable the Select
component. When disabled, Select
will not send data when the form is submitted. Use readonly
if you still want data to be sent.
You choose to disable individual options by setting their disabled
property to true
.
Use readonly
to make the Select
component readonly. When readonly, Select
will send data when the form is submitted, but the user will not be able to change the value.
The contents of this section will only work in a .svelte
file and not inside
an .astro
file.
You can bind to the <select>
element with the ref
property.
You can bind
to the selected
property to get the selected value.
If youβre using multiple
, selected
should be an array instead of a string value.
{
selected: null
}
{
selected: [
]
}
Only the change
event is supported on Select
. You can listen to the event like this:
We only support the change
event because Svelte 4 doesnβt have automatic
event-forwarding capability. All events will be forwarded automatically when
we migrate to Svelte 5.
Select
uses styles from both Splendid Styles and Splendid Layouts. The easiest way to include all basic styles is to import all styles from both libraries:
If you wish to include only the necessary styles in your project, you can import the following files:
Select
produces the following HTML:
TextField
and InputGroup
are used by most text-related components to mantain a consistent style.Stack
and Pos-right
(from Splendid Layouts
) are used to place the SVG on the right of the component.When multiple
is used, Select
produces a similar HTML, but without Stack
and Pos-right
.
If readonly
is present, Select
produces a hidden input for the selected item. If multiple
produces a hidden input for each selected item.
You cannot style the <option>
when <select>
is used in single-mode. You can only style <option>
when multiple
is used. Even then, only Chrome and Firefox respects styles given to <option>
, not Safari.
Weβve added the Fill
system to help you style both select and its options.
The following classes are used by Select
:
Class | Description |
---|---|
Select | Styles component container |
Label | Styles the label |
The following properties can be used to add classes to Select
.
Property | Description |
---|---|
class | Add classes to Select |
labelClass | Add classes to the label |
Select
uses the following CSS variables to define its appearance
Variable | Default | Description |
---|---|---|
--border-width | 1px | Border width |
--border-style | solid | Border style |
--border-color | black | Border color |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Select
uses styles from both Splendid Styles and Splendid Layouts. The easiest way to include all basic styles is to import all styles from both libraries:
If you wish to include only the necessary styles in your project, you can import the following files:
Select
produces the following HTML:
TextField
and InputGroup
are used by most text-related components to mantain a consistent style.Stack
and Pos-right
(from Splendid Layouts
) are used to place the SVG on the right of the component.When multiple
is used, Select
produces a similar HTML, but without Stack
and Pos-right
.
If readonly
is present, Select
produces a hidden input for the selected item. If multiple
produces a hidden input for each selected item.
You cannot style the <option>
when <select>
is used in single-mode. You can only style <option>
when multiple
is used. Even then, only Chrome and Firefox respects styles given to <option>
, not Safari.
Weβve added the Fill
system to help you style both select and its options.
The following classes are used by Select
:
Class | Description |
---|---|
Select | Styles component container |
Label | Styles the label |
The following properties can be used to add classes to Select
.
Property | Description |
---|---|
class | Add classes to Select |
labelClass | Add classes to the label |
Select
uses the following CSS variables to define its appearance
Variable | Default | Description |
---|---|---|
--border-width | 1px | Border width |
--border-style | solid | Border style |
--border-color | black | Border color |
You can use these properties to customize Select
.
Property | Type | Description |
---|---|---|
label | string | Label for the select element |
id | string | id of the select element. A random id will be generated if this is omitted. |
name | string | Name of the select. (Required). |
items | array | Select items. See items . (Required). |
disabled | boolean | Adds the disabled state |
readonly | boolean | Adds the readonly state |
ref | object | Reference for the select element |
List of options for Select
. We named it items
instead of options
because the options
term can have double-meaning when creating components.
string[] | object[]
If a list of strings are passed into items
, each string will be converted to the value of the option.
Properties that can be passed into each option are as follows:
Property | Type | Description |
---|---|---|
value | string | Value for the option. (Required) |
label | string | Label for the option |
disabled | boolean | Adds the disabled state |
readonly | boolean | Adds the readonly state |
selected | boolean | Selects the option |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
You can use these properties to customize Select
.
Property | Type | Description |
---|---|---|
label | string | Label for the select element |
id | string | id of the select element. A random id will be generated if this is omitted. |
name | string | Name of the select. (Required). |
items | array | Select items. See items . (Required). |
disabled | boolean | Adds the disabled state |
readonly | boolean | Adds the readonly state |
ref | object | Reference for the select element |
List of options for Select
. We named it items
instead of options
because the options
term can have double-meaning when creating components.
string[] | object[]
If a list of strings are passed into items
, each string will be converted to the value of the option.
Properties that can be passed into each option are as follows:
Property | Type | Description |
---|---|---|
value | string | Value for the option. (Required) |
label | string | Label for the option |
disabled | boolean | Adds the disabled state |
readonly | boolean | Adds the readonly state |
selected | boolean | Selects the option |