Build your web apps using Smart UI
Smart.PowerButton - configuration and usage
Overview
This element is a clickable toggle button with two states: checked and unchecked. The state can be changed by clicking on the button or by setting the checked property.
Getting Started with PowerButton 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 PowerButton
Smart UI for Web Components is distributed as smart-webcomponents NPM package
- Download and install the package.
npm install smart-webcomponents
- Once installed, import the PowerButton module in your application.
<script type="module" src="node_modules/smart-webcomponents/source/modules/smart.powerbutton.js"></script>
-
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" />
- Add the PowerButton tag to your Web Page
<smart-power-button id="powerbutton"></smart-power-button>
- Create the PowerButton Component
<script type="module"> Smart('#powerbutton', class { get properties() { return {disabled: true} } }); </script>
Another option is to create the PowerButton is by using the traditional Javascript way:
const powerbutton = document.createElement('smart-power-button'); powerbutton.disabled = true; document.body.appendChild(powerbutton);
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 #powerbutton is the ID of a DIV tag.
import "../../source/modules/smart.powerbutton.js"; document.readyState === 'complete' ? init() : window.onload = init; function init() { const powerbutton = new Smart.PowerButton('#powerbutton', {disabled: true}); }
- Open the page in your web server.
Appearance
If the user wants to change the check state of the power button, this can be accomplished by setting the checked property of the element, like so:
<!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.powerbutton.js"></script> <script> window.onload = function () { document.querySelector('smart-power-button').checked = true; } </script> </head> <body> <smart-power-button></smart-power-button> </body> </html>
Demo
Behavior
Using the checked property the user can change the check state of the element dynamically as well.
By default the checked of the check box is set to false. It could be set to true or false
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.powerbutton.js"></script> </head> <body> <smart-power-button click-mode="press"></smart-power-button> </body> </html>
Keyboard Support
Smart.PowerButton 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 powerbutton = document.createElement('smart-power-button');
Append it to the DOM:
document.body.appendChild(powerbutton);
Remove it from the DOM:
powerbutton.parentNode.removeChild(powerbutton);
Set a property:
powerbutton.propertyName = propertyValue;
Get a property value:
const propertyValue = powerbutton.propertyName;
Invoke a method:
powerbutton.methodName(argument1, argument2);
Add Event Listener:
const eventHandler = (event) => { // your code here. }; powerbutton.addEventListener(eventName, eventHandler);
Remove Event Listener:
powerbutton.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 PowerButton 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-powerbutton
Navigate to the created project folder
cd smart-angular-powerbutton
Setup the PowerButton
Smart UI for Angular is distributed as smart-webcomponents-angular NPM package
- Download and install the package.
npm install smart-webcomponents-angular
- Once installed, import the PowerButtonModule in your application root or feature module.
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { PowerButtonModule } from 'smart-webcomponents-angular/powerbutton'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, PowerButtonModule ], bootstrap: [ AppComponent ] }) export class AppModule { }
- 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" ]
-
Example
app.component.html
<div class="smart-demo-container"> <section id="power-button"> <div class="module"> <p>Power button is a two-state type of toggle button with an indicator.</p> <p>Used as indicator.</p> </div> <div class="module power-button-flat-light"> <div> <smart-power-button></smart-power-button> <br /> <smart-power-button #powerbutton hover></smart-power-button> <br /> <smart-power-button #powerbutton2 checked></smart-power-button> <br /> <smart-power-button #powerbutton3 disabled></smart-power-button> </div> </div> <div class="module"></div> <div class="module power-button-light"> <div> <smart-power-button #powerbutton4 class="raised"></smart-power-button> <br /> <smart-power-button #powerbutton5 class="raised"></smart-power-button> <br /> <smart-power-button #powerbutton6 class="checked raised" checked></smart-power-button> <br /> <smart-power-button #powerbutton7 class="raised" disabled></smart-power-button> </div> <p>Raised Power button</p> </div> <div class="module power-button-dark"> <h2>Demo usage</h2> </div> <div class="module power-button"> <div> <smart-circular-progress-bar id="progressBarAnimated" indeterminate value="100"> <smart-power-button #powerbutton8 style="border:none;" id="powerButtonAnimation"></smart-power-button> </smart-circular-progress-bar> </div> <p>A power button can control the loading process.</p> </div> </section> </div>
app.component.ts
import { Component, ViewChild, OnInit, AfterViewInit } from '@angular/core'; import { PowerButtonComponent } from 'smart-webcomponents-angular/powerbutton'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements AfterViewInit, OnInit { @ViewChild('powerbutton', { read: PowerButtonComponent, static: false }) powerbutton!: PowerButtonComponent; @ViewChild('powerbutton2', { read: PowerButtonComponent, static: false }) powerbutton2!: PowerButtonComponent; @ViewChild('powerbutton3', { read: PowerButtonComponent, static: false }) powerbutton3!: PowerButtonComponent; @ViewChild('powerbutton4', { read: PowerButtonComponent, static: false }) powerbutton4!: PowerButtonComponent; @ViewChild('powerbutton5', { read: PowerButtonComponent, static: false }) powerbutton5!: PowerButtonComponent; @ViewChild('powerbutton6', { read: PowerButtonComponent, static: false }) powerbutton6!: PowerButtonComponent; @ViewChild('powerbutton7', { read: PowerButtonComponent, static: false }) powerbutton7!: PowerButtonComponent; @ViewChild('powerbutton8', { read: PowerButtonComponent, static: false }) powerbutton8!: PowerButtonComponent; ngOnInit(): void { // onInit code. } ngAfterViewInit(): void { // afterViewInit code. this.init(); } init(): void { // init code. document.getElementById('powerButtonAnimation').addEventListener('change', function (event) { document.getElementById('progressBarAnimated').indeterminate = event.detail.value ? false : true; }); } }
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { PowerButtonModule } from 'smart-webcomponents-angular/powerbutton'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, PowerButtonModule ], bootstrap: [ AppComponent ] }) export class AppModule { }
Running the Angular application
After completing the steps required to render a PowerButton, run the following command to display the output in your web browser
ng serveand 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 PowerButton 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
- Remove everything inside the App div tag in src/App.js:
<div className="App"> </div>
- Remove the logo.svg import
- Remove the contents of src/App.css
- Remove src/logo.svg
Setup the PowerButton
Smart UI for React is distributed as smart-webcomponents-react NPM package
- Download and install the package.
npm install smart-webcomponents-react
Once installed, import the React PowerButton 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 { PowerButton } from "smart-webcomponents-react/button"; import { CircularProgressBar } from "smart-webcomponents-react/progressbar"; class App extends React.Component { componentDidMount() { } render() { return ( <div> <div class="smart-demo-container"> <section id="power-button"> <div class="module"> <p>Power button is a two-state type of toggle button with an indicator.</p> <p>Used as indicator.</p> </div> <div class="module power-button-flat-light"> <div> <PowerButton></PowerButton> <br /> <PowerButton hover></PowerButton> <br /> <PowerButton checked></PowerButton> <br /> <PowerButton disabled></PowerButton> </div> </div> <div class="module"></div> <div class="module power-button-light"> <div> <PowerButton class="raised"></PowerButton> <br /> <PowerButton class="raised"></PowerButton> <br /> <PowerButton class="checked raised" checked></PowerButton> <br /> <PowerButton class="raised" disabled></PowerButton> </div> <p>Raised Power button</p> </div> <div class="module power-button-dark"> <h2>Demo usage</h2> </div> <div class="module power-button"> <div> <CircularProgressBar id="progressBarAnimated" indeterminate value={100}> <PowerButton style={{ border: "none" }} id="powerButtonAnimation"></PowerButton> </CircularProgressBar> </div> <p>A power button can control the loading process.</p> </div> </section> </div> </div> ); } } ReactDOM.render(<App />, document.querySelector("#root")); export default App;
Running the React application
Start the app withnpm startand 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 PowerButton 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 PowerButton
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 vite.config.js in your favorite text editor and change its contents to the following:
vite.config.js
import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue({ template: { compilerOptions: { isCustomElement: tag => tag.startsWith('smart-') } } }) ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } } })
-
Open src/App.vue in your favorite text editor and change its contents to the following:
App.vue
<template> <div class="vue-root"> <smart-power-button style="border:none;" id="powerButtonAnimation"></smart-power-button> </div> </template> <script> import { onMounted } from "vue"; import "smart-webcomponents/source/styles/smart.default.css"; import "smart-webcomponents/source/modules/smart.button.js"; export default { name: "app", setup() { onMounted(() => { }); } }; </script> <style> </style>
We can now use the smart-power-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-power-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> <smart-power-button id="powerbutton"></smart-power-button> </template> <script> import "smart-webcomponents/source/styles/smart.default.css"; import 'smart-webcomponents/source/modules/smart.powerbutton.js'; export default { name: "app", mounted: function() { window.Smart( "#powerbutton", class { get properties() { return {disabled: true} } } ); } }; </script> <style> body { min-height: 700px; } </style>
We can now use the smart-power-button with Vue. Data binding and event handlers will just work right out of the box.
We have bound the properties of the smart-power-button to values in our Vue component.
Running the Vue application
Start the app withnpm run devand 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/.