ProgressBar - Documentation | www.HtmlElements.com

Overview

Smart.ProgressBar represents a colored bar that displays the stage of the current process.

There are two types of Smart Progress bar element:

  • Linear - smart-progress-bar
  • Circular - smart-circular-progress-bar

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.progressbar.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.progressbar.js - the JS file containing the definition for the element.

Usage

  • Import a module

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

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

  • 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 #progressbar is a DIV tag.

    import {smartProgressBar} from "../../source/modules/smart.progressbar.js";
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    const progressbar = new smartProgressBar('#progressbar', {
       	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.progressbar.js"></script>
    </head>
    <body>
     <smart-progress-bar></smart-progress-bar>
    </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 progressbar = document.createElement('smart-progress-bar');

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

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

Set a property:
progressbar.propertyName = propertyValue;

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

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

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

progressbar.addEventListener(eventName, eventHandler);

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

Appearance

To set the value of the progress bar the user has to apply a value to the value property as an HTML attribute:

 <smart-progress-bar value="50"></smart-progress-bar>
 <smart-circular-progress-bar value="50"></smart-circular-progress-bar>

Demo

or using javascript:

<!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.progressbar.js"></script>
 <script>
     window.onload = function () {
         document.querySelector('smart-progress-bar').value = 25;
         document.querySelector('smart-circular-progress-bar').value = 25;
     }
 </script>
</head>
<body>
 <smart-progress-bar></smart-progress-bar>
 <smart-circular-progress-bar></smart-circular-progress-bar>
</body>
</html>

Demo

The direction of the progress can be inverted by applying the inverted property:

 <smart-progress-bar value="25" inverted></smart-progress-bar>
 <smart-circular-progress-bar value="25" inverted></smart-circular-progress-bar>

Demo

The linear progress bar has two orientation types:

  • Horizontal - the fill increases from left to right.
  • Vertical - the fill increases from top to bottom.

The orientation property is responsible for the orinetation of the element:

<smart-progress-bar value="25" orientation="vertical"></smart-progress-bar>

Demo

Behavior

By default the progress bar is used to visually display the progress of a process using it's fill. However the element allows numeric indication of the progress as well. This can be enabled by setting the showProgressValue property:

<smart-progress-bar show-progress-value value="54"></smart-progress-bar>

Demo

The progress indicator is placed in the middle of the progress bar and can be customized. The formatFunction property allows the user to control what text is displayed. Here's how to apply a custom formatting function:

<!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.progressbar.js"></script>
 <script>
     window.onload = function () {
         var progressBar = document.querySelector('smart-progress-bar');
         progressBar.formatFunction = function (value) {
            return value + ' ' + 'oz';
         }
     }
 </script>
</head>
<body>
 <smart-progress-bar show-progress-value value="54"></smart-progress-bar>
</body>
</html>

Demo

The start and end value of the element can be reconfigured using the min and max property:

<smart-progress-bar value="54" min="20" max="70"></smart-progress-bar>
<smart-circular-progress-bar value="54" min="20" max="70"></smart-circular-progress-bar>

Demo

Progress bars are often used as loading indicators. For this purpose the user can set the indeterminate property and the progress bar will start an indeterminate animation which can be stopped any time by removing the attribute:

<!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.progressbar.js"></script>
 <script>
     window.onload = function () {
         var progressBar = document.querySelector('smart-progress-bar');
         progressBar.indeterminate = false;
     }
 </script>
</head>
<body>
 <smart-progress-bar indeterminate></smart-progress-bar>
</body>
</html>

Demo

The user can also set the progress bar to indeterminate state by setting the value property to null.

<!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.progressbar.js"></script>
 <script>
     window.onload = function () {
         var progressBar = document.querySelector('smart-circular-progress-bar');
         progressBar.value = null;
     }
 </script>
</head>
<body>
 <smart-circular-progress-bar></smart-circular-progress-bar>
</body>
</html>

Demo

Styling

The progress bar is stylable through classes like the rest of the Smart custom elements. However it does have some specific CSS variables that are used as well:

  • --smart-progress-bar-default-width - used to set the default width of the Linear progress bar. Default is 200px.
  • --smart-progress-bar-default-height - used to set the default height of the Linear progress bar. Default is 30px.
  • --smart-circular-progress-bar-default-size - used to set the default width and height of the Circulcar progress bar. Default is 150px.
  • --smart-circular-progress-bar-fill-size - used to set the width of the fill of the Circular progress bar which is relative to the size of the inner container(the HTML container in the middle of the element). Default value is 20%.