Ngx UI Switch Component
Description
This is a simple iOS 7 style switch component for Angular.
Live demo
A stackblitz is also available here
Inspired by switchery in Angular.
Installation
npm: npm install ngx-ui-switch --save
yarn: yarn add ngx-ui-switch
Some features are not available in previous versions:
- CSS styling
- Switch content (checkedLabel, uncheckedLabel) #309 #343
- Global options
- beforeChange event #314 #359
Usage
- Import into a module (
AppModule
example below)
// app.module.ts;; @ {}
- Add default css file to appropriate project(s)
angular.json
"styles":
Alternatively, the scss version can be imported into a scss file:
;
Global config
Use when you want to change default values globally.
These settings will override anything passed in via property bindings.
;; @ {}
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
Params
checked
type: boolean
default: false
disabled
type: boolean
default: false
loading
Show a loading state for the toggle button when true. Often utilized with beforeChange.
type: boolean
default: false
change
type: boolean
default: noop
changeEvent
type: MouseEvent
default: noop
valueChange
type: boolean
default: noop
beforeChange
Utilize an observable to check that the toggle event should complete
type: Observable<boolean>
default: noop
OnBeforeChange: Observable<boolean> = new Observable;
size
type: string
default: medium
reverse
type: boolean
default: false
color
type: string
default: rgb(100, 189, 99)
switchColor
type: string
default: #fff
defaultBgColor
Default background color
type: string
default: #fff
defaultBoColor
Default border color
type: string
default: #dfdfdf
checkedLabel
Checked label (on)
type: string
default: null
uncheckedLabel
Unchecked label (off)
type: string
default: null
checkedTextColor
checked text color of the label (on)
type: string
default: black
uncheckedTextColor
Unchecked text color of the label (off)
type: string
default: black
Switch Content
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):
This project follows the all-contributors specification. Contributions of any kind welcome!