carbon-components-svelte/src/Toggle/Toggle.svelte
Eric Liu 8ddf2def8b
Align v10.36 (#696)
* chore(deps-dev): upgrade carbon-components to v10.36.0

* feat(structured-list): add condensed, flush props

* fix(structured-list): deprecate the border prop

* fix(code-snippet): set min/max height for multi-line code snippet #656

Fixes #656

* fix(image-loader): make SSR compatible using a window type check guard

* docs(tag): add separate disabled example for filterable/interactive tags

* docs: update number of supported chart types

* feat(side-nav): support rail variant

* feat(ui-shell): add isSelected prop to HeaderNavItem

* fix(ui-shell): default isSelected to false in SideNavMenuItem

* fix(text-area): forward missing keydown event #665

Fixes #665

* feat: forward keyup event to components with inputs

* feat(checkbox): make labelText slottable #563

Closes #563

* feat: make labelText slottable

Related #563

* docs(component-api): account for undefined type

* docs(ui-shell): link to correct rail source

* fix(ui-shell): default isSelected in HeaderNavItem to false
2021-06-26 14:39:49 -07:00

90 lines
1.9 KiB
Svelte

<script>
/**
* @event {{ toggled: boolean; }} toggle
*/
/**
* Specify the toggle size
* @type {"default" | "sm"}
*/
export let size = "default";
/** Set to `true` to toggle the checkbox input */
export let toggled = false;
/** Set to `true` to disable checkbox input */
export let disabled = false;
/** Specify the label for the untoggled state */
export let labelA = "Off";
/** Specify the label for the toggled state */
export let labelB = "On";
/** Specify the label text */
export let labelText = "";
/** Set an id for the input element */
export let id = "ccs-" + Math.random().toString(36);
/**
* Specify a name attribute for the checkbox input
* @type {string}
*/
export let name = undefined;
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
$: dispatch("toggle", { toggled });
</script>
<div
class:bx--form-item="{true}"
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
>
<input
type="checkbox"
class:bx--toggle-input="{true}"
class:bx--toggle-input--small="{size === 'sm'}"
checked="{toggled}"
on:change
on:change="{() => {
toggled = !toggled;
}}"
on:keyup
on:keyup="{(e) => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
toggled = !toggled;
}
}}"
on:focus
on:blur
disabled="{disabled}"
id="{id}"
name="{name}"
/>
<label
aria-label="{labelText ? undefined : $$props['aria-label'] || 'Toggle'}"
for="{id}"
class:bx--toggle-input__label="{true}"
>
<slot name="labelText">
{labelText}
</slot>
<span class:bx--toggle__switch="{true}">
<span aria-hidden="true" class:bx--toggle__text--off="{true}">
{labelA}
</span>
<span aria-hidden="true" class:bx--toggle__text--on="{true}">
{labelB}
</span>
</span>
</label>
</div>