Skip to main content Sidebar
On this page

Switch

<pc-switch> Since  0.1.0 Experimental

Switches allow the user to toggle an option on or off.

<pc-switch>Switch</pc-switch>
Code Edit

This component works well with standard <form> elements. Please refer to the form controls page to learn more about form submission and client‐side validation.

Demos

Checked

Use the checked attribute to set the switch’s state to enabled.

Edit

Disabled

Use the disabled attribute to disable the switch.

Edit

Sizes

Use the size attribute to change a switch’s size.

Edit

Hint

Add a descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.

Edit

Custom styles

Use the available custom properties to change how the switch is styled.

Edit

Properties

NameDescriptionReflectsDefault
nameThe name of the switch, submitted as a name/value pair with form data.
Type: string
""
valueThe current value of the switch, submitted as a name/value pair with form data.
Type: string | null
sizeThe switch’s size.
Type: "small" | "medium" | "large"
"medium"
disabledDisables the switch.
Type: boolean
false
checkedChecks the switch off.
Type: boolean
false
defaultChecked
checked
The default value of the switch. Primarily used for resetting the switch.
Type: boolean
requiredMakes the switch a required field.
Type: boolean
false
hintThe switch’s hint. if you need to display HTML, use the hint slot instead.
Type: string
""
updateComplete A read‐only promise that resolves when the component has finished updating.

Learn more about customising animations.

Slots

NameDescription
(default)The switch’s label.
hintText that describes how to use the switch. Alternatively, you can use the hint attribute.

Learn more about using slots.

Methods

NameDescriptionArguments
click()Simulates a click on the switch.
focus()Focuses the switch.options: FocusOptions
blur()Unfocuses the switch (i.e., blurs it).

Learn more about methods.

Events

NameDescriptionEvent detail
changeEmitted when the switch’s state changes.Event
inputEmitted when the switch receives input.InputEvent
focusEmitted when the switch gains focus.
blurEmitted when the switch loses focus (i.e., is blurred).
pc-invalidEmitted when the form control has been checked for validity and its constraints aren’t satisfied.

Learn more about events.

Custom properties

NameDescriptionDefault
--widthThe width of the switch.calc(var(--height) * 1.75)
--heightThe height of the switch.var(--pc-form-control-toggle-size)
--thumb-sizeThe size of the thumb.0.75em

Learn more about customising custom properties.

Parts

NameDescription
baseThe component’s base wrapper.
controlThe control that houses the switch’s thumb.
thumbThe switch’s thumb.
labelThe switch’s label.
hintThe hint’s wrapper.

Learn more about customising CSS parts.

Importing

If you’re using the autoloader or the standard loader, you can skip this section. But if you’re cherry picking, you can use any of the following snippets to import this component.

CDN (script tag)CDN (import)npm (import)

To manually import this component from the CDN, copy this code snippet and paste it in your HTML.

<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.4/cdn/components/switch/switch.js"></script>

To manually import this component from the CDN, copy this code snippet and paste it in your JavaScript file.

import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.4/cdn/components/switch/switch.js";

To manually import this component from npm, copy this code snippet and paste it in your JavaScript file.

import "placer-toolkit/dist/components/switch/switch.js";

We’d love to hear from you. Please reach out to us with any questions or enquiries you may have.

You can contact us via e‐mail at placer.coc.reports+contact@gmail.com.

We look forward to hearing from you!

Got it!
Dangerous lands

Whoa! You’ve wandered into the dangerous lands of Placer Toolkit. Version 0 is out of date and doesn’t meet EU privacy standards, including GDPR.

Want the latest powers, security and compliance? Stick with the current version of Placer Toolkit!

Yikes! Power up!

Our site is 100 % cookie‐free! We value your privacy, which is why we don’t store any cookies or personal information related to you.

Your browser history is safe from crumbs, and your data is protected, aligning with modern privacy standards like the GDPR. Enjoy your visit without a single digital cookie in sight! 🍪🚫

View our Privacy Policy for more information.

Got it!