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.

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.button.js - the JS module which is in the source/modules/ folder and loads all script dependencies.
  • Javascript files
    • smart.element.js - the base class.
    • smart.button.js - the JS file containing the base class about all button elements
    • smart.switchbutton.js - the JS file containing the element definition

Usage

  • Import a module

    With this approach, we import a module and create the web component by using the Smart function. The #button is a smart-switch-button tag.

    import {smartSwitchButton} from "../../../source/modules/smart.button.js";
    
    Smart('#button', class {
    get properties() {
    	return {
    		orientation: 'horizontal'
    	}
    }
    });
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    
    }
    
    Using the Smart function is optional. You can use const button = document.querySelector("#button"); and set the properties like that:
    const button = document.querySelector("#button");
    
    button.orientation = 'horizontal';
    

  • Import a module, Init on Demand

    The following imports the web component's module and creates it on demand, when the document is ready. The #button is a DIV tag.

    import {smartSwitchButton} from "../../../source/modules/smart.button.js";
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    const button = new smartSwitchButton('#button', {
       	orientation: 'horizontal'
    });
    }
    
  • Load scripts

    The following code adds the custom element to the page.

    <!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></smart-switch-button>
    </body>
    </html>

    Note how smart.element.js is declared before everything else. This is mandatory for all custom elements.

Demo

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


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

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

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

Set a property:
button.propertyName = propertyValue;

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

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

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

button.addEventListener(eventName, eventHandler);

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

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.