@umbraco-ui/uui-toggle
Version:
Umbraco style toggle component
207 lines (185 loc) • 6.1 kB
JavaScript
import { UUIHorizontalShakeKeyframes, UUIHorizontalShakeAnimationValue } from '@umbraco-ui/uui-base/lib/animations';
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { UUIBooleanInputElement } from '@umbraco-ui/uui-boolean-input/lib';
import { iconCheck, iconRemove } from '@umbraco-ui/uui-icon-registry-essential/lib/svgs';
import { css, html } from 'lit';
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (decorator(result)) || result;
return result;
};
let UUIToggleElement = class extends UUIBooleanInputElement {
constructor() {
super("switch");
}
renderCheckbox() {
return html`
<div id="toggle">
<div id="icon-checked">${iconCheck}</div>
<div id="icon-unchecked">${iconRemove}</div>
</div>
`;
}
};
/**
* This is a static class field indicating that the element is can be used inside a native form and participate in its events. It may require a polyfill, check support here https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attachInternals. Read more about form controls here https://web.dev/more-capable-form-controls/
* @type {boolean}
*/
UUIToggleElement.formAssociated = true;
UUIToggleElement.styles = [
...UUIBooleanInputElement.styles,
UUIHorizontalShakeKeyframes,
css`
:host {
--uui-toggle-size: 18px;
--uui-toggle-switch-width: calc(2 * var(--uui-toggle-size));
}
#toggle {
position: relative;
grid-area: 'input';
display: flex;
align-items: center;
flex-shrink: 0;
width: var(--uui-toggle-switch-width);
height: var(--uui-toggle-size);
border-radius: 100px;
background-color: var(
--uui-toggle-background-color,
var(--uui-color-border,#d8d7d9)
);
border: 1px solid
var(--uui-toggle-border-color, var(--uui-color-border-standalone,#c2c2c2));
font-size: calc(var(--uui-toggle-size) * 0.6);
}
label:hover input:not([disabled]) ~ #toggle {
border-color: var(
--uui-toggle-border-color-hover,
var(--uui-color-border-emphasis,#a1a1a1)
);
background-color: var(
--uui-toggle-background-color-hover,
var(--uui-color-border,#d8d7d9)
);
}
label:focus #toggle {
border-color: var(
--uui-toggle-border-color-focus,
var(--uui-color-focus,#3879ff)
);
background-color: var(
--uui-toggle-background-color-focus,
var(--uui-color-surface-emphasis,rgb(
250,
250,
250
))
);
}
input:checked ~ #toggle {
background-color: var(--uui-color-selected,#3544b1);
}
label:hover input:checked:not([disabled]) ~ #toggle {
background-color: var(--uui-color-selected-emphasis,rgb(
70,
86,
200
));
}
label:focus input:checked ~ #toggle {
background-color: var(--uui-color-selected-emphasis,rgb(
70,
86,
200
));
}
#icon-checked,
#icon-unchecked {
position: absolute;
vertical-align: middle;
width: 1em;
height: 1em;
line-height: 0;
transition: color 120ms;
}
#icon-checked {
margin-left: -0.5em;
left: calc((var(--uui-toggle-size) + 1px) * 0.5);
color: var(--uui-color-interactive,#1b264f);
}
#icon-unchecked {
margin-right: -0.5em;
right: calc((var(--uui-toggle-size) + 1px) * 0.5);
color: var(--uui-color-interactive,#1b264f);
}
input:checked ~ #toggle #icon-checked {
color: var(--uui-color-selected-contrast,#fff);
}
#toggle::after {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: calc(var(--uui-toggle-size) - 4px);
height: calc(var(--uui-toggle-size) - 4px);
border-radius: 100px;
background-color: var(--uui-color-selected-contrast,#fff);
transition:
width 120ms ease,
left 120ms ease,
transform 120ms ease,
background-color 120ms;
}
input:checked ~ #toggle::after {
left: calc(100% - 2px);
transform: translateX(-100%);
}
input:focus ~ #toggle {
outline: calc(2px * var(--uui-show-focus-outline, 1)) solid
var(--uui-color-focus,#3879ff);
}
:host(:not([disabled], [readonly])) label:active #toggle::after {
/** Stretch when mouse down */
width: calc(1.06 * var(--uui-toggle-size));
}
:host([disabled]) #toggle {
background-color: var(--uui-color-disabled-standalone,rgb(
226,
226,
226
));
}
:host([disabled]) input:checked ~ #toggle {
background-color: var(--uui-color-disabled-contrast,#c4c4c4);
}
:host([disabled]) #toggle::after {
background-color: var(--uui-color-disabled,#f3f3f5);
}
:host([disabled]) #toggle #icon-unchecked {
color: var(--uui-color-disabled-contrast,#c4c4c4);
}
:host([disabled]) label:active #toggle {
animation: ${UUIHorizontalShakeAnimationValue};
}
:host([disabled]) input:checked #toggle #icon-checked {
color: var(--uui-color-disabled-contrast,#c4c4c4);
}
:host(:not([pristine]):invalid) #toggle,
:host(:not([pristine]):invalid) label:hover #toggle,
/* polyfill support */
:host(:not([pristine])[internals-invalid]) #toggle,
:host(:not([pristine])[internals-invalid]) label:hover #toggle {
border: 1px solid var(--uui-color-invalid-standalone,rgb(
191,
33,
78
));
}
`
];
UUIToggleElement = __decorateClass([
defineElement("uui-toggle")
], UUIToggleElement);
export { UUIToggleElement };