Skip to content

Commit

Permalink
feat(toggle): add toggle component (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
zankevich authored and yggg committed Aug 9, 2019
1 parent e6c4966 commit d86edce
Show file tree
Hide file tree
Showing 23 changed files with 1,070 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DEV_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ _your-component.component.theme.scss (optional, styles that depends on theme var
- register your component in framework
````
src/framework/theme/index.ts (add exports of your component and module)
src/framework/theme/public_api.ts (add exports of your component and module)
src/framework/theme/styles/global/_components.scss (if you create _your-component.component.theme.scss you have to register mixin)

````
Expand Down
1 change: 1 addition & 0 deletions docs/assets/images/components/toggle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ export const structure = [
'NbCheckboxComponent',
],
},
{
type: 'tabs',
name: 'Toggle',
icon: 'toggle.svg',
source: [
'NbToggleComponent',
],
},
{
type: 'tabs',
name: 'Radio',
Expand Down
24 changes: 24 additions & 0 deletions e2e/toggle.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { browser, by, element } from 'protractor';

describe('nb-toggle', () => {
beforeEach((done) => {
browser.get('#/toggle/toggle-test.component').then(() => done());
});

it('should turn on on click', () => {
const input = element(by.css('#first input'));
const indicator = element(by.css('#first .toggle'));

expect(input.getAttribute('checked')).toBeFalsy();
indicator.click();
expect(input.getAttribute('checked')).toBeTruthy();
indicator.click();
expect(input.getAttribute('checked')).toBeFalsy();
});
});
41 changes: 41 additions & 0 deletions src/app/playground-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,47 @@ export const PLAYGROUND_COMPONENTS: ComponentLink[] = [
},
],
},
{
path: 'toggle',
children: [
{
path: 'toggle-disabled.component',
link: '/toggle/toggle-disabled.component',
component: 'ToggleDisabledComponent',
name: 'Toggle Disabled',
},
{
path: 'toggle-showcase.component',
link: '/toggle/toggle-showcase.component',
component: 'ToggleShowcaseComponent',
name: 'Toggle Showcase',
},
{
path: 'toggle-status.component',
link: '/toggle/toggle-status.component',
component: 'ToggleStatusComponent',
name: 'Toggle Status',
},
{
path: 'toggle-test.component',
link: '/toggle/toggle-test.component',
component: 'ToggleTestComponent',
name: 'Toggle Test',
},
{
path: 'toggle-label-position.component',
link: '/toggle/toggle-label-position.component',
component: 'ToggleLabelPositionComponent',
name: 'Toggle Label Position',
},
{
path: 'toggle-form.component',
link: '/toggle/toggle-form.component',
component: 'ToggleFormComponent',
name: 'Toggle Form',
},
],
},
{
path: 'context-menu',
children: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class NbCheckboxComponent implements ControlValueAccessor {

/**
* Checkbox status.
* Possible values are: `primary` (default), `success`, `warning`, `danger`, `info`
* Possible values are: `primary`, `success`, `warning`, `danger`, `info`
*/
@Input()
status: '' | NbComponentStatus = '';
Expand Down
130 changes: 130 additions & 0 deletions src/framework/theme/components/toggle/_toggle.component.theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

@mixin nb-toggle-theme() {
nb-toggle {
.toggle {
height: nb-theme(toggle-height);
width: nb-theme(toggle-width);
background-color: nb-theme(toggle-background-color);
border: nb-theme(toggle-border-width) solid nb-theme(toggle-border-color);
border-radius: nb-theme(toggle-border-radius);
cursor: nb-theme(toggle-cursor);
}

.toggle.checked {
background-color: nb-theme(toggle-primary-checked-background-color);
border-color: nb-theme(toggle-primary-checked-border-color);
}

.native-input {
&:focus + .toggle {
border-color: nb-theme(toggle-primary-focus-border-color);
box-shadow: 0 0 0 nb-theme(toggle-outline-width) nb-theme(toggle-outline-color);
}

&:active + .toggle {
background-color: nb-theme(toggle-primary-active-background-color);
border-color: nb-theme(toggle-primary-active-border-color);
}
}

.toggle:hover {
background-color: nb-theme(toggle-primary-hover-background-color);
border-color: nb-theme(toggle-primary-hover-border-color);
}

.native-input:disabled {
& + .toggle {
background-color: nb-theme(toggle-disabled-background-color);
border-color: nb-theme(toggle-disabled-border-color);
cursor: nb-theme(toggle-disabled-cursor);

nb-icon {
color: nb-theme(toggle-disabled-switcher-checkmark-color);
}
}

& ~ .text {
color: nb-theme(toggle-disabled-text-color);
}
}

.toggle-switcher {
width: nb-theme(toggle-switcher-size);
height: nb-theme(toggle-switcher-size);
background-color: nb-theme(toggle-switcher-background-color);

nb-icon {
color: nb-theme(toggle-switcher-checkmark-color);
}
}

.text {
color: nb-theme(toggle-text-color);
font-family: nb-theme(toggle-text-font-family);
font-size: nb-theme(toggle-text-font-size);
font-weight: nb-theme(toggle-text-font-weight);
line-height: nb-theme(toggle-text-line-height);
}
}

@each $status in nb-get-statuses() {
@include nb-toggle-status($status);
}
}

@mixin nb-toggle-status($status: '') {
nb-toggle.status-#{$status} {

.toggle {
background-color: nb-theme(toggle-#{$status}-background-color);
border-color: nb-theme(toggle-#{$status}-border-color);
}

.toggle.checked {
background-color: nb-theme(toggle-#{$status}-checked-background-color);
border-color: nb-theme(toggle-#{$status}-checked-border-color);
}

.native-input {
&:focus + .toggle {
border-color: nb-theme(toggle-#{$status}-focus-border-color);
}

&:active + .toggle {
background-color: nb-theme(toggle-#{$status}-active-background-color);
border-color: nb-theme(toggle-#{$status}-active-border-color);
}
}

.toggle:hover {
background-color: nb-theme(toggle-#{$status}-hover-background-color);
border-color: nb-theme(toggle-#{$status}-hover-border-color);
}

.toggle-switcher {
nb-icon {
color: nb-theme(toggle-#{$status}-checked-switcher-checkmark-color);
}
}

.native-input:disabled {
& + .toggle {
background-color: nb-theme(toggle-disabled-background-color);
border-color: nb-theme(toggle-disabled-border-color);

nb-icon {
color: nb-theme(toggle-disabled-switcher-checkmark-color);
}
}

& ~ .text {
color: nb-theme(toggle-disabled-text-color);
}
}
}
}
59 changes: 59 additions & 0 deletions src/framework/theme/components/toggle/toggle.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@import '../../styles/core/mixins';

:host {
display: inline-flex;
outline: none;
}

:host(.toggle-label-left) .text {
padding-right: 0.6875rem;
@include nb-ltr(order, -1);
@include nb-rtl(order, 1);
}

:host(.toggle-label-right) .text {
padding-left: 0.6875rem;
@include nb-ltr(order, 1);
@include nb-rtl(order, -1);
}

:host(.toggle-label-start) .toggle-label {
flex-direction: row-reverse;

.text {
@include nb-ltr(padding-right, 0.6875rem);
@include nb-rtl(padding-left, 0.6875rem);
}
}

:host(.toggle-label-end) .text {
@include nb-ltr(padding-left, 0.6875rem);
@include nb-rtl(padding-right, 0.6875rem);
}

.toggle-label {
position: relative;
display: inline-flex;
align-items: center;
}

.toggle {
position: relative;
display: inline-flex;
box-sizing: content-box;
@include nb-component-animation(background-color, border, box-shadow);
}

.toggle-switcher {
position: absolute;
border-radius: 50%;
margin: 1px;

nb-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40%;
}
}
Loading

0 comments on commit d86edce

Please sign in to comment.