Skip to main content

Toggle Ready

Toggles are graphical interface switches that give user control over a feature or option that can be turned on or off.

Examples #

Open in new window
<duet-toggle label="Checked toggle" value="Some value" checked></duet-toggle>

<script>
// Select the above toggle component
var toggle = document.querySelector("duet-toggle")

// Listen for change events in the toggle
toggle.addEventListener("duetChange", function (e) {
console.log("Change detected in toggle:", e.detail)
})
</script>
Open in new window
<duet-toggle label="Toggle" value="Some value"></duet-toggle>
Open in new window
<duet-toggle label="Label to the left" label-position="left"></duet-toggle>
<duet-toggle label="Label to the right" label-position="right"></duet-toggle>
Open in new window
<duet-paragraph>
When toggle's context is visually clear without label, you can hide the label visually,
so that assistive technologies can access it.
</duet-paragraph>
<duet-divider></duet-divider>
<duet-grid alignment="top">
<duet-grid-item fill>
<duet-heading level="h3">E-invoices</duet-heading>
<duet-paragraph>You can lorem ipsum dolor sit amet, apendisc ellit.</duet-paragraph>
</duet-grid-item>
<duet-toggle label="E-invoices in use" label-hidden></duet-toggle>
</duet-grid>
Open in new window
<duet-grid grid-template="small">
<div>
<duet-toggle label="Loading success (optimistic change)" value="Some value" class="onchange success"></duet-toggle>
<duet-toggle label="Loading failure (optimistic change)" value="Some value" class="onchange failure"></duet-toggle>
<duet-toggle label="Loading success (pending change)" value="Some value" class="onclick success"></duet-toggle>
<duet-toggle label="Loading failure (pending change)" value="Some value" class="onclick failure"></duet-toggle>
<duet-toggle label="Loading failure (pending change)" value="Some value" class="onclick failure" checked></duet-toggle>
</div>
</duet-grid>

<script>
const togglesOnChange = document.querySelectorAll("duet-toggle.onchange")

function clearAlert(el) {
if (el.nextElementSibling?.matches("duet-alert")) {
el.nextElementSibling.remove()
}
}

function addAlertAfter(el) {
const alert = document.createElement("duet-alert")
alert.variation = "warning"
alert.textContent = "Operation failed, try again"
el.after(alert)
}

togglesOnChange.forEach(t => t.addEventListener("duetChange", ev => {

if (t.classList.contains("success")) {
t.loading = true
setTimeout(() => {
t.loading = false
}, 2000)
}

if (t.classList.contains("failure")) {
if (t.checked) {
t.loading = true
clearAlert(t)
setTimeout(() => {
t.loading = false
// revert the state after failure
t.checked = !t.checked
addAlertAfter(t)
}, 2000)
}
}
}))

const togglesOnClick = document.querySelectorAll("duet-toggle.onclick")
togglesOnClick.forEach(t => t.addEventListener("click", ev => {

// prevent checked from changing
ev.preventDefault()

ev.stopImmediatePropagation()
if (t.classList.contains("success")) {
t.loading = true
setTimeout(() => {
t.loading = false
// toggle the state after success
t.checked = !t.checked
}, 2000)
}

if (t.classList.contains("failure")) {
t.loading = true
clearAlert(t)
setTimeout(() => {
t.loading = false
addAlertAfter(t)
}, 2000)
}
}))


</script>

Properties #

Property Attribute Description Type Default
accessibleActiveDescendant accessible-active-descendant Indicates the id of a related component’s visually focused element. string undefined
accessibleControls accessible-controls Use this prop to add an aria-controls attribute. Use the attribute to indicate the id of a component controlled by this component. string undefined
accessibleDescribedBy accessible-described-by Indicates the id of a component that describes the toggle. string undefined
accessibleDescription accessible-description Aria description the button string undefined
accessibleDetails accessible-details Aria Details of the component string undefined
accessibleLabelComplete accessible-label-complete Adds accessible announcement for toggle when "loading" state ends. If not set, default strings are used: fi: "Valmis", sv: "Färdig", en: "Complete" string undefined
accessibleLabelLoading accessible-label-loading Adds accessible announcement for toggle in "loading" state If not set, default strings are used: fi: "Ladataan", sv: "Lastning", en: "Loading" string undefined
accessibleLabelledBy accessible-labelled-by String of id's that indicate alternative labels elements string undefined
accessibleOwns accessible-owns Indicates the id of a component owned by the toggle. string undefined
checked checked Check state of the checkbox. boolean false
disabled disabled Makes the input component disabled. This prevents users from being able to interact with the input, and conveys its inactive state to assistive technologies. boolean false
identifier identifier Adds a unique identifier for the checkbox. string undefined
label label Label for the toggle. string "label"
labelColor label-color Color of the label. string undefined
labelHidden label-hidden Visually hide the label, but still show it to screen readers. boolean false
labelPosition label-position Position of the label "left" | "right" | "top" "top"
labelWeight label-weight Font weight of the label. "normal" | "semi-bold" undefined
loading loading Loading state of the toggle boolean false
margin margin Controls the margin of the component. "auto" | "none" "auto"
name name Name of the checkbox. string undefined
required required Set whether the input is required or not. Please note that this is necessary for accessible inputs when the user is required to fill them. When using this property you need to also set “novalidate” attribute to your form element to prevent browser from displaying its own validation errors. boolean false
role role Defines a specific role attribute for the input. string undefined
theme theme Theme of the card. "" | "default" | "turva" ""
value value The value of the checkbox does not mean if it's checked or not, use the checked property for that. string undefined

Events #

Event Description Type
duetChange Emitted when the checked property has changed. CustomEvent<{ component: "duet-toggle"; checked: boolean; value: string; }>

Methods #

setFocus(options?: FocusOptions) => Promise<void> #

Sets focus on the specified duet-toggle. Use this method instead of the global
input.focus().

Parameters #

Name Type Description
options FocusOptions

Returns #

Type: Promise<void>


Usage #

This section includes guidelines for designers and developers about the usage of this component in different contexts.

When to use #

  • In forms to toggle something on or off.
  • In native iOS or Android app to replace checkbox.

When not to use #

  • For “accepting terms of service” and similar functionality. Use checkbox instead.

Accessibility #

This component has been validated to meet the WCAG 2.1 AA accessibility guidelines. You can find additional information regarding accessibility of this component below.

Additional considerations #


Integration

For integration, event and theming guidelines, please see Using Components. This documentation explains how to implement and use Duet’s components across different technologies like Angular, React or Vanilla JavaScript.

Integration guidelines


Tutorials

Follow these practical tutorials to learn how to build simple page layouts using Duet’s CSS Framework, Web Components and other features:

Tutorials

Building Layouts

Tutorials

Using CLI Tools

Tutorials

Creating Custom Patterns

Tutorials

Server Side Rendering

Tutorials

Sharing Prototypes

Tutorials

Usage With Markdown


Troubleshooting

If you experience any issues while using a component, please head over to the Support page for more guidelines and help.