Switch
Toggle an immediate binary setting with accessible checked and unavailable states.
Default
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let basicSwitch =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "productUpdates"
_name "productUpdates"
}
fveLabel {
_for "productUpdates"
"Product updates"
}
}This example also uses Field. See their Installation sections for any additional setup.
On this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add switch --config src/Acme.Components/fve.jsonAdd to src/Acme.Web/js/app.js once, alongside your other component imports:
import '../../Acme.Components/Components/Assets/switch.js';The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let basicSwitch =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "productUpdates"
_name "productUpdates"
}
fveLabel {
_for "productUpdates"
"Product updates"
}
}With help text
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let switchWithHelp =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "weeklySummary"
_name "weeklySummary"
_checked true
_ariaDescribedby "weeklySummary-help"
}
fveFieldContent {
fveLabel {
_for "weeklySummary"
"Weekly summary"
}
fveFieldDescription {
_id "weeklySummary-help"
"Receive one email with the week's activity."
}
}
}This example also uses Field. See their Installation sections for any additional setup.
Setting with submission
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let choiceSubmitButton id (label: string) =
fveButton {
_color Color.Primary
_variant ButtonVariant.Solid
_type "submit"
_id id
label
}
let choiceResult id result =
output {
_id id
_role "status"
_class "block min-h-5 text-sm text-[var(--fve-muted-text)]"
defaultArg result "Submit the form to see the result."
}
let switchFormRegion enabled (validation: string option) result =
div {
_id "components-switch-form-region"
_class "grid max-w-sm gap-3"
form {
_dataOn ("submit", "@post('/components/choices/switch', {contentType: 'form'})")
_class "grid gap-3"
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "components-notifications"
_name "notifications"
_checked enabled
_ariaInvalid validation.IsSome
_ariaDescribedby (
String.concat
" "
[ "notifications-help"
if validation.IsSome then
"notifications-error" ]
)
}
fveFieldContent {
fveLabel {
_for "components-notifications"
"Notifications"
}
fveFieldDescription {
_id "notifications-help"
"Receive an update when new items are published."
}
match validation with
| Some message ->
fveFieldError {
_id "notifications-error"
message
}
| None -> ()
}
}
choiceSubmitButton "components-switch-submit" "Save notifications"
}
choiceResult "components-switch-result" result
}
let notificationSetting = switchFormRegion true None NoneThis example also uses Field, Button. See their Installation sections for any additional setup.
Pending
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let pendingNotifications =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "components-pending-notifications"
_name "notifications"
_checked true
_readonly true
_ariaBusy true
}
fveLabel {
_for "components-pending-notifications"
"Saving notifications"
}
}This example also uses Field. See their Installation sections for any additional setup.
Disabled
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let disabledNotifications =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "components-disabled-notifications"
_name "notifications"
_disabled true
}
fveLabel {
_for "components-disabled-notifications"
"Notifications unavailable"
}
}This example also uses Field. See their Installation sections for any additional setup.
Validation error
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let invalidNotifications =
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id "components-invalid-notifications"
_name "notifications"
_ariaInvalid true
_ariaDescribedby "notifications-invalid-error"
}
fveFieldContent {
fveLabel {
_for "components-invalid-notifications"
"Notification service"
}
fveFieldError {
_id "notifications-invalid-error"
"Notification preferences could not be saved."
}
}
}This example also uses Field. See their Installation sections for any additional setup.
Sizes
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let switchSizes =
div {
_class "grid w-full max-w-sm gap-4"
for value, caption, size in
[ "compact", "Compact switch", Size.Small
"standard", "Standard switch", Size.Medium
"prominent", "Prominent switch", Size.Large ] do
fveField {
_layout FieldLayout.Horizontal
fveSwitch {
_id (value + "Alerts")
_name (value + "Alerts")
_size size
_checked true
}
fveLabel {
_for (value + "Alerts")
caption
}
}
}This example also uses Field. See their Installation sections for any additional setup.
Stacked layout
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let stackedSwitch =
fveField {
fveLabel {
_for "automaticExports"
"Automatic exports"
}
fveSwitch {
_id "automaticExports"
_name "automaticExports"
_ariaDescribedby "automaticExports-help"
}
fveFieldDescription {
_id "automaticExports-help"
"Generate an export when new files are published."
}
}This example also uses Field. See their Installation sections for any additional setup.
API reference
module SwitchDsl =
val fveSwitch : ComponentElementBuilder