@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
225 lines • 6.75 kB
CSS
/*
* Switch component
*
*/
/*
* Utilities
*/
.dnb-switch {
--switch-width--medium: 2.5rem;
--switch-height--medium: 1.5rem;
--switch-width--large: 3.25rem;
--switch-height--large: 2rem;
--switch-border-width--medium: 0.0625rem;
--switch-border-width--large: 0.125rem;
display: inline-flex;
flex-direction: column;
font-size: var(--font-size-small);
line-height: var(--line-height-basis);
}
.dnb-switch__inner {
display: inline-flex;
flex-direction: column;
justify-content: center;
font-size: var(--font-size-basis);
}
.dnb-switch__shell {
position: relative;
display: flex;
align-items: center;
-webkit-user-select: none;
user-select: none;
}
.dnb-switch--large {
line-height: var(--switch-height--large);
}
.dnb-switch--large .dnb-switch__shell {
height: var(--switch-height--large);
}
.dnb-switch__row {
display: inline-flex;
align-items: center;
height: var(--switch-height--medium);
}
.dnb-switch__focus, .dnb-switch__button {
position: absolute;
z-index: 4;
border-radius: 50%;
}
.dnb-switch__button {
left: 0;
width: var(--switch-height--medium);
height: var(--switch-height--medium);
background-color: lightgrey;
border: var(--switch-border-width--medium) solid transparent;
transition: transform 160ms ease-out 125ms;
}
.dnb-switch--large .dnb-switch__button {
width: var(--switch-height--large);
height: var(--switch-height--large);
border-width: var(--switch-border-width--large);
}
.dnb-switch__focus {
display: none;
position: absolute;
z-index: 2;
width: 100%;
height: 100%;
outline: none;
}
html[data-whatinput=keyboard] .dnb-switch__focus {
--border-color: var(--focus-ring-color);
--border-width: var(--focus-ring-width);
box-shadow: 0 0 0 var(--border-width) var(--border-color);
border-color: transparent;
}
.dnb-switch {
/*
* When switched OFF
* aka when the switch is not :checked
*/
}
.dnb-switch__input:not(:checked) ~ .dnb-switch__button {
transform: translateX(0);
transform: translateX(calc(0px - var(--switch-border-width--medium)));
}
.dnb-switch {
/*
* When switched ON
* aka when the switch is :checked
*/
}
.dnb-switch__input:checked ~ .dnb-switch__button {
transform: translateX(1rem);
transform: translateX(calc(var(--switch-width--medium) - 1.5rem + var(--switch-border-width--medium)));
}
.dnb-switch--large .dnb-switch__input:not(:checked) ~ .dnb-switch__button {
transform: translateX(calc(0px - var(--switch-border-width--large)));
}
.dnb-switch--large .dnb-switch__input:checked ~ .dnb-switch__button {
transform: translateX(calc(var(--switch-width--large) - 2rem + var(--switch-border-width--large)));
}
.dnb-switch__background {
position: relative;
display: flex;
flex-direction: row;
align-items: center;
overflow: hidden;
width: var(--switch-width--medium);
height: calc(var(--switch-height--medium) - 0.5rem);
background-color: grey;
border-radius: calc(var(--switch-height--medium) - 0.25rem);
}
.dnb-switch__background::after {
content: "";
position: absolute;
transition: transform 0.3s ease-out, position 0.3s ease-out;
}
.dnb-switch--large .dnb-switch__background {
width: var(--switch-width--large);
height: calc(var(--switch-height--large) - 0.75rem);
border-radius: calc(var(--switch-height--large) - 0.375rem);
}
.dnb-switch__input {
opacity: 0;
position: absolute;
left: 0;
right: 0;
z-index: 5;
display: block;
width: var(--switch-width--medium);
height: var(--switch-height--medium);
}
.dnb-switch--large .dnb-switch__input {
width: var(--switch-width--large);
height: var(--switch-height--large);
}
.dnb-switch__input {
margin: 0;
padding: 0;
border: 0;
}
.dnb-switch__input:not([disabled]) {
cursor: pointer;
}
.dnb-switch__input:not([disabled]):focus ~ .dnb-switch__background, .dnb-switch__input:not([disabled]):active ~ .dnb-switch__background {
outline: none;
}
html[data-whatinput=keyboard] .dnb-switch__input:not([disabled]):focus ~ .dnb-switch__background, html[data-whatinput=keyboard] .dnb-switch__input:not([disabled]):active ~ .dnb-switch__background {
--border-color: var(--focus-ring-color);
--border-width: var(--focus-ring-width);
box-shadow: 0 0 0 var(--border-width) var(--border-color);
border-color: transparent;
}
.dnb-switch__input:not([disabled]):focus ~ .dnb-switch__button .dnb-switch__focus, .dnb-switch__input:not([disabled]):active ~ .dnb-switch__button .dnb-switch__focus {
display: block;
}
.dnb-switch .dnb-form-label {
padding-right: 0.5rem;
margin-right: 0;
margin-left: 0;
margin-bottom: 0;
}
.dnb-switch__order {
display: inline-flex;
}
.dnb-switch__suffix {
order: 4;
}
.dnb-switch--label-position-left .dnb-switch__suffix {
padding-left: 1rem;
}
.dnb-switch--label-position-left .dnb-switch__order .dnb-switch__inner {
order: 2;
}
.dnb-switch--label-position-left .dnb-switch__order .dnb-form-label {
order: 1;
align-self: center;
}
.dnb-switch--label-position-left .dnb-switch__order .dnb-form-status {
order: 3;
margin-top: 0.5rem;
}
.dnb-switch--label-position-right .dnb-switch__order .dnb-switch__inner {
order: 1;
}
.dnb-switch--label-position-right .dnb-switch__order .dnb-form-label {
order: 2;
padding-left: 0.5rem;
}
.dnb-switch--label-position-right .dnb-switch__order .dnb-form-status {
order: 3;
margin-top: 0.5rem;
}
.dnb-switch.dnb-skeleton .dnb-switch__input[disabled] ~ .dnb-switch__button, .dnb-switch.dnb-skeleton .dnb-switch__input[disabled] ~ .dnb-switch__background::after {
border-color: var(--skeleton-color);
}
.dnb-switch.dnb-skeleton .dnb-switch__input[disabled] ~ .dnb-switch__background {
background-color: var(--skeleton-color);
}
.dnb-switch.dnb-skeleton .dnb-switch__input[disabled]:not(:checked) ~ .dnb-switch__background::after {
content: "";
border-color: var(--skeleton-color--contrast);
}
.dnb-switch__status--error .dnb-switch__input:not([disabled]):not(:focus):not(:active) ~ .dnb-switch__button .dnb-switch__focus {
display: block;
--border-color: red;
--border-width: var(--focus-ring-width, 0.125rem);
box-shadow: 0 0 0 var(--border-width) var(--border-color);
border-color: transparent;
}
.dnb-switch__status--error .dnb-switch__input:not([disabled]):not(:focus):not(:active) ~ .dnb-switch__background {
background-color: tomato;
--border-color: red;
--border-width: var(--focus-ring-width, 0.125rem);
box-shadow: 0 0 0 var(--border-width) var(--border-color);
border-color: transparent;
}
@supports (-ms-ime-align: auto) {
.dnb-switch__input:not(:checked) ~ .dnb-switch__button {
transform: translateX(0);
}
.dnb-switch__input:checked ~ .dnb-switch__button {
transform: translateX(calc(var(--switch-width--medium) - 1.5rem));
}
}