Skip to content

Latest commit

Β 

History

History
360 lines (237 loc) Β· 9.96 KB

README.md

File metadata and controls

360 lines (237 loc) Β· 9.96 KB

Ngx UI Switch Component

Gitter Build Status npm version npm All Contributors

Description

This is a simple iOS 7 style switch component for Angular.

Live demo

A stackblitz is also available here

alt

Inspired by switchery in Angular.

Installation

Angular v4

Angular v5/v6 uses a different metadata version for decorators, therefore, the Angular v4 compatible versions of this library are 1.4.4 and below. However, versions < 1.6.0 have been deprecated to avoid the confusion brought up in issue #219

The code in the 1.x-stable branch contains the Angular v4 code.

Note: The Angular v4 branch will only receive bug fixes.

yarn add ngx-ui-switch@^1.6.0
# npm install ngx-ui-switch@^1.6.0 --save

Some features are not available in previous versions:

  • CSS styling
  • Switch content (checkedLabel, uncheckedLabel) #309 #343
  • Global options

PRs welcome to add these features for the 1.x line.

Beyond Angular v4

The master branch will contain the latest code for the latest version of Angular. When upgrading this library to a new version of Angular, a new x.y-stable branch will be created to allow bugfixes. Below is how to install the latest version of the library.

yarn add ngx-ui-switch
# npm install ngx-ui-switch --save

Usage

  • Import into a module (AppModule example below)
// app.module.ts
import { UiSwitchModule } from 'ngx-ui-switch';
import { AppComponent } from './app.component';

@NgModule({
  imports: [BrowserModule, UiSwitchModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
  • Add default css file to appropriate project(s) angular.json
"styles": [
  "src/styles.css",
  "./node_modules/ngx-ui-switch/ui-switch.component.css"
]

Alternatively, the scss version can be imported into a scss file:

@import '~ngx-ui-switch/ui-switch.component.scss';

Global config

Use when you want to change default values globally.

These settings will override anything passed in via property bindings.

import { UiSwitchModule } from 'ngx-ui-switch';
import { AppComponent } from './app.component';

@NgModule({
  imports: [
    BrowserModule,
    UiSwitchModule.forRoot({
      size: 'small',
      color: 'rgb(0, 189, 99)',
      switchColor: '#80FFA2',
      defaultBgColor: '#00ACFF',
      defaultBoColor : '#476EFF',
      checkedLabel: 'on',
      uncheckedLabel: 'off'
    })
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
<ui-switch></ui-switch>

Note that if you are using the switch in a child NgModule, such as a lazy loaded module, then you must also import the module in the module itself or within a shared module. Otherwise you will get the error that it is an unknown component as seen in issue #227.

Two way binding

<ui-switch [(ngModel)]="enable"></ui-switch>

Params

checked

type: boolean

default: false

<ui-switch checked></ui-switch>
<ui-switch [checked]="false"></ui-switch>

disabled

type: boolean

default: false

<ui-switch disabled></ui-switch>
<ui-switch checked [disabled]="true"></ui-switch>

loading

Show a loading state for the toggle button when true. Often utilized with beforeChange.

type: boolean

default: false

<ui-switch [loading]="isLoading">
  <i class="fa fa-spinner fa-pulse" *ngIf="isLoading"></i>
</ui-switch>

change

type: boolean

default: noop

<ui-switch (change)="onChange($event)"></ui-switch>

changeEvent

type: MouseEvent

default: noop

<ui-switch (changeEvent)="onChangeEvent($event)"></ui-switch>
<ui-switch (changeEvent)="$event.stopPropagation()"></ui-switch>

valueChange

type: boolean

default: noop

<ui-switch (valueChange)="onValueChange($event)"></ui-switch>

beforeChange

Utilize an observable to check that the toggle event should complete

type: Observable

default: noop

<ui-switch [beforeChange]="OnBeforeChange"></ui-switch>
  OnBeforeChange: Observable<boolean> = Observable.create((observer) => {
    const timeout = setTimeout(() => {
      observer.next(true);
    }, 2000);
    return () => clearTimeout(timeout);
  });

size

type: string

default: medium

<ui-switch size="small"></ui-switch>
<ui-switch size="large"></ui-switch>

reverse

type: boolean

default: false

<ui-switch reverse></ui-switch>

color

type: string

default: rgb(100, 189, 99)

<ui-switch color="red"></ui-switch>

switchColor

type: string

default: #fff

<ui-switch switchColor="#fcfcfc"></ui-switch>

defaultBgColor

Default background color

type: string

default: #fff

<ui-switch defaultBgColor="red"></ui-switch>

defaultBoColor

Default border color

type: string

default: #dfdfdf

<ui-switch defaultBoColor="black"></ui-switch>

checkedLabel

Checked label (on)

type: string

default: null

<ui-switch checkedLabel="on"></ui-switch>

uncheckedLabel

Unchecked label (off)

type: string

default: null

<ui-switch uncheckedLabel="off"></ui-switch>

Switch Content

<ui-switch uncheckedLabel="off">
  <img src=""/>
</ui-switch>

Development

Setup

yarn install

Demo

Edit files in src/app to add to the demo or try changes to the library.

Build library

First, edit version in package.json and src/lib/package.json to publish a new version to npmjs.org

# Build the library into dist/{es5,es2015}
yarn build
# Publish to npm
yarn release

Contributors

Thanks goes to these wonderful people (emoji key):


webcat_black

πŸ’» 🎨 πŸ’‘ πŸ€” πŸ‘€

Chris McKnight

πŸ’¬ πŸ’» πŸ“– πŸ€” πŸš‡ πŸ”Œ πŸ‘€ πŸ”§

Jakub

πŸ’» πŸ“–

Serhii Kovalenko

πŸ’» πŸ’‘ πŸ“¦

Richard McSharry

πŸ“–

bitsprint

πŸš‡ πŸ“¦ πŸ”§

Gianluca Paronitti

πŸ’»

Milos Bejda

πŸ’» πŸ“– πŸ’‘

This project follows the all-contributors specification. Contributions of any kind welcome!

FAQ

License

MIT