SwitchButton - Documentation | www.HtmlElements.com

Overview

This element represents a switch button that has three states: null, checked and unchecked. The state can be changed by clicking on the widget, dragging the widgets thumb or by setting the checked property to the desired state.

Getting Started with SwitchButton Web Component

Smart UI for Web Components is distributed as smart-webcomponents NPM package. You can also get the full download from our website with all demos from the Download page.

Setup the SwitchButton

Smart UI for Web Components is distributed as smart-webcomponents NPM package

  1. Download and install the package.

    npm install smart-webcomponents

  2. Once installed, import the SwitchButton module in your application.

    <script type="module" src="node_modules/smart-webcomponents/source/modules/smart.switchbutton.js"></script>

  3. Adding CSS reference

    The smart.default.css CSS file should be referenced using following code.

    <link rel="stylesheet" type="text/css" href="node_modules/smart-webcomponents/source/styles/smart.default.css" />

  4. Add the SwitchButton tag to your Web Page

    <smart-switch-button id="switchbutton"></smart-switch-button>

  5. Create the SwitchButton Component

    	<script type="module">
    		Smart('#switchbutton', class {
    			get properties() {
    				return { checked: true }
    			}
    		});
    	</script>	   
    		

    Another option is to create the SwitchButton is by using the traditional Javascript way:
    	const switchbutton = document.createElement('smart-switch-button');
    
    	switchbutton.disabled = true;
    	document.body.appendChild(switchbutton);
    		

    Smart framework provides a way to dynamically create a web component on demand from a DIV tag which is used as a host. The following imports the web component's module and creates it on demand, when the document is ready. The #switchbutton is the ID of a DIV tag.

    	import "../../source/modules/smart.switchbutton.js";
    
    	document.readyState === 'complete' ? init() : window.onload = init;
    
    	function init() { 
    		const switchbutton = new Smart.SwitchButton('#switchbutton', { checked: true });
    	}
    	

  6. Open the page in your web server.

Appearance

The content of the element in the different check states couls be changed via trueContent and falseContent properties.

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.switchbutton.js"></script>
 <script>
 window.onload = function () {
    let switchButton = document.querySelector('smart-switch-button');
    switchButton.trueContent = 'On';
    switchButton.falseContent = 'Off';
 }
 </script>
</head>
<body>
 <smart-switch-button></smart-switch-button>
</body>
</html>

Demo

smart-switch-button could be horizontal or vertical. The orientation could be changed via "orientation" property.

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.switchbutton.js"></script>
 <script>
 window.onload = function () {
    document.querySelector('smart-switch-button').orientation = 'vertical';
 }
 </script>
</head>
<body>
 <smart-switch-button></smart-switch-button>
</body>
</html>

Demo

Behavior

Using the checked property the user can change the check state of the element dynamically as well. checked property could be set to: true, false or null. By default is set to false.

smart-switch-button also could be inverted. Inverted property changes the position of "true" and "false" states.

The element offers multiple click modes:

  • press
  • release
  • pressAndRelease

clickMode is a property of the button that can be changed either from the HTML tag by setting the attribute click-mode and assigning a new value to it or by following the earlier approach and change it dynamically via javascript during the onload stage of the window object or later

Here's how to set a new clickMode on element initialiation:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.switchbutton.js"></script>
</head>
<body>
 <smart-switch-button switch-mode="click"  click-mode="press"></smart-switch-button>
</body>
</html>

Demo

And here's how to change it via javascript after the element has been initialized:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.switchbutton.js"></script>
 <script>
 window.onload = function () {
        var switchButton = document.getElementsByTagName('smart-switch-button')[0],

        switchButton.switchMode = 'click';
        switchButton.clickMode = 'pressAndRelease';
 }
 </script>
</head>
<body>
 <smart-switch-button></smart-switch-button>
</body>
</html>

Demo

The element offers multiple switch modes:

  • default - the switch button changes it's state on click and drag
  • click - the switch button changes it's state on click only
  • drag - the switch button changes it's state on drag only

When Smart.SwitchButton is readonly it can be used as indicator:

 <smart-switch-button readonly></smart-switch-button>

Demo

Keyboard Support

Smart.SwitchButton check state could be changed via Space. Space should change the state only to Checked or Unchecked. The element is focusable and can be focused using the Tab button.

Create, Append, Remove, Get/Set Property, Invoke Method, Bind to Event


Create a new element:
	const switchbutton = document.createElement('smart-switch-button');
	

Append it to the DOM:
	document.body.appendChild(switchbutton);
	

Remove it from the DOM:
	switchbutton.parentNode.removeChild(switchbutton);
	

Set a property:
	switchbutton.propertyName = propertyValue;
	

Get a property value:
	const propertyValue = switchbutton.propertyName;
	

Invoke a method:
	switchbutton.methodName(argument1, argument2);
	

Add Event Listener:
	const eventHandler = (event) => {
	   // your code here.
	};

	switchbutton.addEventListener(eventName, eventHandler);
	

Remove Event Listener:
	switchbutton.removeEventListener(eventName, eventHandler, true);
	

Using with Typescript

Smart Web Components package includes TypeScript definitions which enables strongly-typed access to the Smart UI Components and their configuration.

Inside the download package, the typescript directory contains .d.ts file for each web component and a smart.elements.d.ts typescript definitions file for all web components. Copy the typescript definitions file to your project and in your TypeScript file add a reference to smart.elements.d.ts

Read more about using Smart UI with Typescript.

Getting Started with Angular SwitchButton Component

Setup Angular Environment

Angular provides the easiest way to set angular CLI projects using Angular CLI tool.

Install the CLI application globally to your machine.

npm install -g @angular/cli

Create a new Application

ng new smart-angular-switchbutton

Navigate to the created project folder

cd smart-angular-switchbutton

Setup the SwitchButton

Smart UI for Angular is distributed as smart-webcomponents-angular NPM package

  1. Download and install the package.
    npm install smart-webcomponents-angular
  2. Once installed, import the SwitchButtonModule in your application root or feature module.

    app.module.ts

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { SwitchButtonModule } from 'smart-webcomponents-angular/switchbutton';
    
    import { AppComponent } from './app.component';
    
    @NgModule({
        declarations: [ AppComponent ],
        imports: [ BrowserModule, SwitchButtonModule ],
        bootstrap: [ AppComponent ],
    	entryComponents: [ AppComponent ]
    })
    
    export class AppModule { }
    	

  3. Adding CSS reference

    The following CSS file is available in ../node_modules/smart-webcomponents-angular/ package folder. This can be referenced in [src/styles.css] using following code.

    	@import 'smart-webcomponents-angular/source/styles/smart.default.css';

    Another way to achieve the same is to edit the angular.json file and in the styles add the style.

    	"styles": [
    		"node_modules/smart-webcomponents-angular/source/styles/smart.default.css"
    	]
    	
  4. Example


    app.component.html

     
    <p>On/off switches toggle the state of a single settings option. The option
        that the switch controls, as well as the state it’s in, should be made
        clear from the corresponding inline label. Switches take on the same visual
        properties of the radio button.</p>
    <br/>
    <br/>
    <smart-switch-button #switchbutton checked></smart-switch-button>
    <br/>
    <br/>
    <smart-switch-button></smart-switch-button>
    		

    app.component.ts

     
    import { Component, ViewChild, OnInit, AfterViewInit } from '@angular/core';
    import { SwitchButtonComponent } from 'smart-webcomponents-angular/switchbutton';
    
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
    	styleUrls: ['./app.component.css']
    })
    
    export class AppComponent implements AfterViewInit, OnInit {	
    	@ViewChild('switchbutton', { read: SwitchButtonComponent, static: false }) switchbutton: SwitchButtonComponent;
    	
     
    	ngOnInit(): void {
    		// onInit code.
    	}
    
    	ngAfterViewInit(): void {
    		// afterViewInit code.
    		this.init();
        }
    		
    	init(): void {
    		// init code.
    	    
        // Your code here
    
    	}	
    }
    		

    app.module.ts

     
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { SwitchButtonModule } from 'smart-webcomponents-angular/switchbutton';
    
    import { AppComponent } from './app.component';
    
    @NgModule({
        declarations: [ AppComponent ],
        imports: [ BrowserModule, SwitchButtonModule ],
        bootstrap: [ AppComponent ],
    	entryComponents: [ AppComponent ]
    })
    
    export class AppModule { }
    		


Running the Angular application

After completing the steps required to render a SwitchButton, run the following command to display the output in your web browser

ng serve
and open localhost:4200 in your favorite web browser.

Read more about using Smart UI for Angular: https://www.htmlelements.com/docs/angular-cli/.

Getting Started with React SwitchButton Component

Setup React Environment

The easiest way to start with React is to use create-react-app. To scaffold your project structure, follow the installation instructions.

	npm install -g create-react-app
	create-react-app my-app
	cd my-app
	npm start
	

Preparation

Open src/App.js andsrc/App.css

  1. Remove everything inside the App div tag in src/App.js:
    <div className="App"> </div>
  2. Remove the logo.svg import
  3. Remove the contents of src/App.css
  4. Remove src/logo.svg

Setup the SwitchButton

Smart UI for React is distributed as smart-webcomponents-react NPM package

  1. Download and install the package.
    npm install smart-webcomponents-react
  2. Once installed, import the React SwitchButton Component and CSS files in your application and render it app.js

    import 'smart-webcomponents-react/source/styles/smart.default.css';
    import React from "react";
    import ReactDOM from "react-dom";
    import { SwitchButton } from 'smart-webcomponents-react/switchbutton';
    
    class App extends React.Component {
    
    	init() {
    	
    	    // Your code here
    	}
    
    
    	componentDidMount() {
    
    	}
    
    	render() {
    		return (
    			<div>
    			    <p>On/off switches toggle the state of a single settings option. The option
    			        that the switch controls, as well as the state it’s in, should be made
    			        clear from the corresponding inline label. Switches take on the same visual
    			        properties of the radio button.</p>
    			    <br/>
    			    <br/>
    			    <SwitchButton  checked></SwitchButton>
    			    <br/>
    			    <br/>
    			    <SwitchButton ></SwitchButton>
    			</div>
    		);
    	}
    }
    
    ReactDOM.render(<App />, document.querySelector("#root"));
    
    export default App;
    	

Running the React application

Start the app with
npm start
and open localhost:3000 in your favorite web browser to see the output.

Read more about using Smart UI for React: https://www.htmlelements.com/docs/react/.

Getting Started with Vue SwitchButton Component


Setup Vue Environment

We will use vue-cli to get started. Let's install vue-cli

npm install -g @vue/cli

Then we can start creating our Vue.js projects with:

vue create my-project

Setup the SwitchButton

Open the "my-project" folder and run:

npm install smart-webcomponents

Setup with Vue 3.x

  • Make Vue ignore custom elements defined outside of Vue (e.g., using the Web Components APIs). Otherwise, it will throw a warning about an Unknown custom element, assuming that you forgot to register a global component or misspelled a component name.

    Open src/main.js in your favorite text editor and change its contents to the following:

    main.js

    import { createApp } from 'vue'
    import App from './App.vue'
    
    const app = createApp(App)
    
    app.config.isCustomElement = tag => tag.startsWith('smart-');
    app.mount('#app')
    		
  • Open src/App.vue in your favorite text editor and change its contents to the following:

    App.vue

    <template>
      <div class="vue-root">
        <p>
          On/off switches toggle the state of a single settings option. The option
          that the switch controls, as well as the state it’s in, should be made
          clear from the corresponding inline label. Switches take on the same visual
          properties of the radio button.
        </p>
        <br />
        <br />
        <smart-switch-button checked></smart-switch-button>
        <br />
        <br />
        <smart-switch-button></smart-switch-button>
      </div>
    </template>
    
    <script>
    import { onMounted } from "vue";
    import "smart-webcomponents/source/styles/smart.default.css";
    import "smart-webcomponents/source/modules/smart.switchbutton.js";
    
    export default {
      name: "app",
      setup() {
        onMounted(() => {
          // Your code here
        });
      }
    };
    </script>
    
    <style>
    smart-switch-button {
      margin-left: 50px;
    }
    </style>
    		
    We can now use the smart-switch-button with Vue 3. Data binding and event handlers will just work right out of the box.

Setup with Vue 2.x

  • Make Vue ignore custom elements defined outside of Vue (e.g., using the Web Components APIs). Otherwise, it will throw a warning about an Unknown custom element, assuming that you forgot to register a global component or misspelled a component name.

    Open src/main.js in your favorite text editor and change its contents to the following:

    main.js

    import Vue from 'vue'
    import App from './App.vue'
    
    Vue.config.productionTip = false
    Vue.config.ignoredElements = [
    'smart-switch-button'
    ]
    
    new Vue({
      render: h => h(App),
    }).$mount('#app')
  • Open src/App.vue in your favorite text editor and change its contents to the following:

    App.vue

    <template>
      <div>
        <smart-switch-button checked></smart-switch-button>
        <br/><br/>
    	  <smart-switch-button></smart-switch-button>
      </div>
    </template> 
    
    <script>
    import "smart-webcomponents/source/modules/smart.button.js";
    import "smart-webcomponents/source/styles/smart.default.css";
    
    export default {
      name: "app"
    };
    </script>
    
    <style>
      smart-switch-button {
        margin-left: 50px;
      }
    </style>
    		
    We can now use the smart-switch-button with Vue. Data binding and event handlers will just work right out of the box.
    We have bound the properties of the smart-switch-button to values in our Vue component.

Running the Vue application

Start the app with
npm run serve
and open localhost:8080 in your favorite web browser to see the output below:

Read more about using Smart UI for Vue: https://www.htmlelements.com/docs/vue/.