Skip to content

Commit

Permalink
feat(switch): new design (#1184)
Browse files Browse the repository at this point in the history
* feat(switch): new design

* css and test tidy

* update test screenshot

* hover states

* remove success connotation

* deprecated message about connotation-success

* deprecated message about connotation-success in scss

* Update components/switch/src/vwc-switch.scss

* fix test & css-variable names

* update switch inside data grid ui-test

* switch variables

* switch variables

* switch variables

* switch variables

* ui test

Co-authored-by: yinon <yinon@hotmail.com>
  • Loading branch information
rachelbt and yinonov committed Jan 9, 2022
1 parent 57e40b3 commit b23d988
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 83 deletions.
7 changes: 7 additions & 0 deletions components/switch/src/partials/_vwc-switch-variables.scss
@@ -0,0 +1,7 @@
//switch colors
$switch-default-color: #757575;
$switch-surface: #f2f2f2;

//switch sizes
$knob-translate-x: --knob-translate-x;
$knob-size: --knob-size;
180 changes: 102 additions & 78 deletions components/switch/src/vwc-switch.scss
@@ -1,138 +1,162 @@
@use 'sass:selector';
@use '@vonage/vvd-design-tokens/build/scss/semantic-variables/scheme-variables';
@use '@vonage/vvd-foundation/scss/mixins/connotation/config' with (
//'Connotation.Success' is *deprecated* and will be removed in vivid-3. Use any of the other connotation predefined values:
$connotations-set: primary cta success alert,
$default: primary,
);
@use '@vonage/vvd-foundation/scss/mixins/connotation' as connotation;
@use 'partials/vwc-switch-variables' as switch-variables;

:host {
// starting variables
#{switch-variables.$knob-size}: 12px;
#{switch-variables.$knob-translate-x}: 16px;

contain: content;
}

.mdc-switch {
--mdc-theme-secondary: var(#{connotation.$vvd-color-connotation});

--vvd-switch-unchecked-background: var(#{scheme-variables.$vvd-color-neutral});
--vvd-switch-unchecked-background: trasparent;
--vvd-switch-unchecked-background-hover: var(#{scheme-variables.$vvd-color-neutral});
--vvd-switch-unchecked-background-focus: var(#{scheme-variables.$vvd-color-neutral});

--vvd-switch-disabled-background: var(#{scheme-variables.$vvd-color-neutral-30});
--vvd-switch-shadow-focus: 0, 0, 0, 0.5;

--mdc-theme-surface: var(#{scheme-variables.$vvd-color-neutral-10}); // unchecked thumb
--mdc-theme-on-surface: var(--vvd-switch-unchecked-background); // unchecked track

@include connotation.connotation;
}

:host {
border-radius: 16px;

.mdc-switch,
.mdc-switch__native-control {
width: 30px;
height: 20px;
}

.mdc-switch__track {
width: 100%;
height: 100%;
border-width: 0;
border-radius: 16px;
}

.mdc-switch__thumb-underlay {
top: 0;
left: 0;
width: 20px;
height: 100%;

&.mdc-switch--checked {
.mdc-switch__thumb {
width: 16px;
height: 16px;
border: 0 none;
#{switch-variables.$knob-size}: 14px;
background-color: #{switch-variables.$switch-surface};
}
}

.mdc-switch--checked {
.mdc-switch__thumb-underlay {
transform: translateX(10px);
transform: translateX(var(#{switch-variables.$knob-translate-x}));
}

.mdc-switch__native-control {
transform: translateX(-10px);
transform: translateX(calc(var(#{switch-variables.$knob-translate-x}) * -1));
}

&:hover {
.mdc-switch__thumb,
.mdc-switch__track {
filter: brightness(90%);
}
}

}
//:(not) is needed to overide mdc style
&:not(.mdc-switch--checked) {
.mdc-switch__track {
border: 1px solid #{switch-variables.$switch-default-color};
}

.mdc-switch {
.mdc-switch__thumb {
box-shadow: none;
background-color: #{switch-variables.$switch-default-color};
}

&.mdc-switch--checked .mdc-switch__thumb {
background-color: var(#{scheme-variables.$vvd-color-neutral-10});
&:hover {
.mdc-switch__thumb {
background-color: var(#{scheme-variables.$vvd-color-neutral-70});
}
.mdc-switch__track {
border-color: var(#{scheme-variables.$vvd-color-neutral-70});
}
}
}

&:hover .mdc-switch__thumb {
opacity: 0.8;
&:not(.connotation-cta, .connotation-success, .connotation-alert, .mdc-switch--disabled) {
&.mdc-switch--checked {
.mdc-switch__thumb {
background-color: var(#{scheme-variables.$vvd-color-neutral-20});
}
}
}


.mdc-switch__track,
&.mdc-switch--checked .mdc-switch__track {
.mdc-switch__track,
&.mdc-switch--checked .mdc-switch__track {
opacity: initial;
}

&.mdc-switch--disabled {
opacity: initial;
.mdc-switch__track {
opacity: initial;
}

&:not(.mdc-switch--checked):hover .mdc-switch__track {
background-color: var(--vvd-switch-unchecked-background-hover);
&.mdc-switch--checked {
.mdc-switch__track {
background-color: var(--vvd-switch-disabled-background);
}
.mdc-switch__thumb {
background-color: var(#{scheme-variables.$vvd-color-neutral-20});
}
}

&.mdc-switch--disabled {
opacity: initial;

&:not(.mdc-switch--checked) {
.mdc-switch__track {
border-color: var(--vvd-switch-disabled-background);
}
.mdc-switch__thumb {
background-color: var(--vvd-switch-disabled-background);
opacity: initial;
}
}
}
}

:host([enlarged]) {
.mdc-switch,
.mdc-switch__native-control {
width: 50px;
height: 32px;
}

.mdc-switch__thumb-underlay {
width: 32px;

.mdc-switch__thumb {
width: 24px;
height: 24px;
}
}
.mdc-switch,
.mdc-switch__native-control {
width: 36px;
height: 20px;
}

.mdc-switch--checked {
.mdc-switch__thumb-underlay {
transform: translateX(18px);
}
.mdc-switch__track {
width: 100%;
height: 100%;
border-width: 0;
border-radius: 16px;
}

.mdc-switch__native-control {
transform: translateX(-18px);
}
.mdc-switch__thumb-underlay {
top: 0;
left: 0;
width: 20px;
height: 100%;

.mdc-switch__thumb {
width: var(#{switch-variables.$knob-size});
height: var(#{switch-variables.$knob-size});
border: 0;
box-shadow: none;
}
}

:host(:not([disabled]):focus-within) {
.mdc-switch__track {
box-shadow: 0 0 2px 1px rgba(var(--vvd-switch-shadow-focus));
:host([enlarged]) {
#{switch-variables.$knob-size}: 24px;
#{switch-variables.$knob-translate-x}: 28px;
.mdc-switch,
.mdc-switch__native-control {
width: 60px;
height: 32px;
}

.mdc-switch:not(.mdc-switch--checked) {
.mdc-switch__track {
background-color: var(--vvd-switch-unchecked-background-focus);
.mdc-switch {
&__thumb-underlay {
width: 32px;
}

&:hover .mdc-switch__track {
background-color: var(--vvd-switch-unchecked-background-hover);
&.mdc-switch--checked {
.mdc-switch__thumb {
#{switch-variables.$knob-size}: 26px;
}
}
}
}


3 changes: 3 additions & 0 deletions components/switch/src/vwc-switch.ts
Expand Up @@ -22,6 +22,9 @@ declare global {
// @ts-ignore
MWCSwitch.styles = [styleCoupling, mwcSwitchStyles, vwcSwitchStyle];

/**
* 'Connotation.Success' is *deprecated* and will be removed in vivid-3. Use any of the other connotation
*/
type SwitchConnotation = Extract<
Connotation,
Connotation.Primary | Connotation.CTA | Connotation.Success | Connotation.Alert
Expand Down
2 changes: 1 addition & 1 deletion components/switch/stories/arg-types.js
Expand Up @@ -5,7 +5,7 @@ export const argTypes = {
control: {
type: 'select',
options: Object.values(Connotation).filter(c => [
Connotation.Primary, Connotation.CTA, Connotation.Success, Connotation.Alert
Connotation.Primary, Connotation.CTA, Connotation.Alert
].includes(c)),
}
},
Expand Down
4 changes: 2 additions & 2 deletions components/switch/test/switch.test.js
Expand Up @@ -35,7 +35,7 @@ describe('switch', () => {
const actualElement = addedElements[0];
await waitNextTask();
const expectedStyles = {
width: '30px',
width: '36px',
height: '20px',
};
assertComputedStyle(actualElement, expectedStyles);
Expand All @@ -48,7 +48,7 @@ describe('switch', () => {
const actualElement = addedElements[0];
await waitNextTask();
const expectedStyles = {
width: '50px',
width: '60px',
height: '32px',
};
assertComputedStyle(actualElement, expectedStyles);
Expand Down
Binary file modified ui-tests/snapshots/vwc-data-grid.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ui-tests/snapshots/vwc-switch.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 68 additions & 2 deletions ui-tests/tests/vwc-switch/index.js
Expand Up @@ -4,10 +4,76 @@ import '@vonage/vwc-switch';
export async function createElementVariations(wrapper) {
const elementWrapper = document.createElement('div');
elementWrapper.innerHTML = `
<style>
.grid {
display: grid;
grid-template-columns: repeat(6, 60px);
gap: 1rem;
background-color: var(--vvd-color-canvas);
padding: 1rem;
margin-bottom: 2rem;
}
</style>
<div class="grid ">
<vwc-switch></vwc-switch>
<vwc-switch checked></vwc-switch>
<vwc-switch checked connotation="cta" enlarged></vwc-switch>
<vwc-switch checked connotation="success"></vwc-switch>
<vwc-switch disabled></vwc-switch>
<vwc-switch checked disabled></vwc-switch>
<vwc-switch enlarged></vwc-switch>
<vwc-switch checked enlarged></vwc-switch>
<vwc-switch connotation="cta"></vwc-switch>
<vwc-switch connotation="cta" checked></vwc-switch>
<vwc-switch connotation="cta" disabled></vwc-switch>
<vwc-switch connotation="cta" checked disabled></vwc-switch>
<vwc-switch connotation="cta" enlarged></vwc-switch>
<vwc-switch connotation="cta" checked enlarged></vwc-switch>
<vwc-switch connotation="success"></vwc-switch>
<vwc-switch connotation="success" checked></vwc-switch>
<vwc-switch connotation="success" disabled></vwc-switch>
<vwc-switch connotation="success" checked disabled></vwc-switch>
<vwc-switch connotation="success" enlarged></vwc-switch>
<vwc-switch connotation="success" checked enlarged></vwc-switch>
<vwc-switch connotation="alert"></vwc-switch>
<vwc-switch connotation="alert" checked></vwc-switch>
<vwc-switch connotation="alert" disabled></vwc-switch>
<vwc-switch connotation="alert" checked disabled></vwc-switch>
<vwc-switch connotation="alert" enlarged></vwc-switch>
<vwc-switch connotation="alert" checked enlarged></vwc-switch>
</div>
<div class="grid vvd-scheme-alternate">
<vwc-switch></vwc-switch>
<vwc-switch checked></vwc-switch>
<vwc-switch disabled></vwc-switch>
<vwc-switch checked disabled></vwc-switch>
<vwc-switch enlarged></vwc-switch>
<vwc-switch checked enlarged></vwc-switch>
<vwc-switch connotation="cta"></vwc-switch>
<vwc-switch connotation="cta" checked></vwc-switch>
<vwc-switch connotation="cta" disabled></vwc-switch>
<vwc-switch connotation="cta" checked disabled></vwc-switch>
<vwc-switch connotation="cta" enlarged></vwc-switch>
<vwc-switch connotation="cta" checked enlarged></vwc-switch>
<vwc-switch connotation="success"></vwc-switch>
<vwc-switch connotation="success" checked></vwc-switch>
<vwc-switch connotation="success" disabled></vwc-switch>
<vwc-switch connotation="success" checked disabled></vwc-switch>
<vwc-switch connotation="success" enlarged></vwc-switch>
<vwc-switch connotation="success" checked enlarged></vwc-switch>
<vwc-switch connotation="alert"></vwc-switch>
<vwc-switch connotation="alert" checked></vwc-switch>
<vwc-switch connotation="alert" disabled></vwc-switch>
<vwc-switch connotation="alert" checked disabled></vwc-switch>
<vwc-switch connotation="alert" enlarged></vwc-switch>
<vwc-switch connotation="alert" checked enlarged></vwc-switch>
</div>
`;
wrapper.appendChild(elementWrapper);
}
Expand Down

0 comments on commit b23d988

Please sign in to comment.