HTML5 Custom Elements Collection

This page contains a list of (somewhat) useful or interesting Custom Elements.
Include the Javascript of the desired element in your page, and use the HTML and/or JS snippets below each element.

The code was created to conform to the HTML5 Custom Elements API as of May, 2018.

How to use them

All you have to do is import the class definitions in your main script.

Assuming a project structure like this;

root
    > js
        > customelements
            - counter.js 
            - ...
            - ...
        - main.js
    - index.html

All you have to do to use the Counter element is to ...

<!-- set your main script reference to type="module" -->
<script scr="js/main.js" type="module" />
// import the desired element in main.js
import "./customelements/counter.js"
<!-- and use the defined custom element as any other HTML element -->
<num-counter counter-step="1"></num-counter>

num-counter

Attributes
Name Type Default Optional Description
counter-step number "1" optional amount to increment by on click
<num-counter counter-step="1"></num-counter>
<num-counter counter-step="5"></num-counter>
<num-counter counter-step="25"></num-counter>

digital-clock


Attributes
Name Type Default Optional Description
clock-seconds boolean "true" optional show or hide the seconds (hh:mm:ss)
<!-- hh:mmm:ss -->
<digital-clock></digital-clock>
<!-- hh:mm -->
<digital-clock clock-seconds="false"></digital-clock>

current-date



Attributes
Name Type Default Optional Description
date-string boolean "false" optional show the date as a localized date string
(based on browser preferences)
date-timestamp number optional use a UNIX timestamp instead of the current date
<!-- numerical date -->
<current-date></current-date>
<!-- show as a localized string -->
<current-date date-string="true"></current-date>
<!-- use a timestamp -->
<current-date date-string="true" date-timestap="1231231231"></current-date>

image-preview

Attributes
Name Type Default Optional Description
no custom attributes defined yet
<image-preview></image-preview>
// Accessing the input file:
let myImagePreview = document.getElementsByTagName("image-preview")[0];
let inputFile = myImagePreview.file;

placeholder-div

Attributes
Name Type Default Optional Description
data-url string required URL to load the actual data via fetch()
data-delay number "500" optional delay in milliseconds before the data is loaded when the element is in the viewport
data-paragraphs number required number of placeholder paragraphs
data-line-min number required minimum number of lines per paragraph
data-line-max number required maximum number of lines per paragraph
<placeholder-div 
    data-url="https://baconipsum.com/api/?type=all-meat&paras=1&format=html"
    data-delay="500"
    data-paragraphs="3"
    data-line-min="3" 
    data-line-max="6">
</placeholder-div>

placeholder-img

Attributes
Name Type Default Optional Description
data-placeholder string required path to the placeholder image
data-src string required path to the source image
data-delay number "500" optional delay in milliseconds before the image is loaded when the element is in the viewport
data-transition number "500" optional transition between the images in milliseconds
supported in Chrome and Safari as of May 2018
<placeholder-img
    data-placeholder="low-res.jpg"
    data-src="full-size.png"
    data-delay="1000"
    data-transition="1000">
</placeholder-img>
placeholder-img {
    display: block;
    width: 300px;
    height: 300px;
    background-color: rgb(204,204,204); /* just as a fallback */
    border-radius: 50px;
}

tooltip-wrapper

Oh no ...
I'm revealed!


Important  Wow!

This information is soooo important! Glad I didn't miss this ...

Wait! there's more!

... or is there?

Text Relation Number
One = 1
Two < 3
Attributes
Name Type Default Optional Description
original-text string required the hoverable text itself, accepts any HTML markup
<tooltip-wrapper original-text="Please don't hover over me. I'm hiding something...">
    <strong>Oh no ...</strong>
    <br>I'm revealed!
</tooltip-wrapper>

<!-- tooltip with a bit more HTML -->
<em>Important</em> 
<tooltip-wrapper original-text="<img src='/assets/info.png' style='width:20px;'>">
    <strong>Wow!</strong>
    <p>This information is <em>soooo</em> important! Glad I didn't miss this ...</p>
    <strong>Wait!</strong> there's more!
    <p>... or is there?</p>
    <table style="text-align: center">
        <!-- bla bla table html -->
    </table>
</tooltip-wrapper>

NOTE: this element is a bit messy and will be refactored later.

textarea-autosize

Express your thoughts ...

Todo: auto downsize

Attributes
Name Type Default Optional Description
autosize-width number "300" optional min-width of the textarea
autosize-height number "20" optional min-height of the textarea
autosize-max-height number "100" optional max-height of the textarea
<textarea-autosize 
    autosize-width="100%"
    autosize-height="30px"
    autosize-max-height="300px">
    Express your thoughts ...
</textarea-autosize>

input-history

Todo: history length attribute (-1 = infinite)

FIXME: add docs

Attributes
Name Type Default Optional Description
no custom attributes defined yet
<input type="text" is="history-input">

input-switch

  • active / true
  • active / false
  • disabled / true
  • disabled / false

Use any color:

Attributes
Name Type Default Optional Description
no custom attributes defined yet
<!-- NOTE: this element is NOT self-closing! -->
<input-switch></input-switch>

<!-- use the familiar checked and disabled attributes -->
<input-switch checked></input-switch>
<input-switch disabled></input-switch>
<input-switch disabled checked></input-switch>
/* set the color attribute
to change the active color */
input-switch { 
    color: rgb(78, 218, 78); 
}
let mySwitch = document.getElementByTagName("input-switch")[0];

// Use getters and setters
mySwitch.disabled = true;                   // -> true
console.log(mySwitch.checked);              // -> true
mySwitch.checked = false;                   // -> false

// Or use getAttribute(), setAttribute() and removeAttribute()
mySwitch.setAttribute("checked", "true");   // -> "true"
mySwitch.getAttribute("disabled");          // -> "true"
mySwitch.removeAttribute("disabled");

bar-chart

[20, 15, 10, 5, 8, 20, 12, 15, 40, 22, 15]
[
    { "label": "Row #1", "value": 33, "color": "#123" }, 
    { "label": "My awesome row", "value": 12, "color": "rgba(125, 125, 0, 0.6)" },
    { "label": "Custom label", "value": 20, "color": "limegreen" },
    { "label": "Default color", "value": 14}
]
["a", 20, 12] <- this is invalid

FIXME: add docs

Attributes
Name Type Default Optional Description
no custom attributes defined yet

line-chart

[0, 3, 70, 9, 10, 4, 0, 15, 40, 22, 15]
[
    { "label": "Row #1", "value": 33, "color": "#c33" },
    { "label": "My awesome row", "value": 12, "color": "rgba(125, 125, 0, 0.5)" },
    { "label": "Custom label", "value": 20, "color": "limegreen" },
    { "label": "Default color", "value": 14}
]
["a", 20, 12] <- this is invalid
Attributes
Name Type Default Optional Description
no custom attributes defined yet

modal-window

Attributes
Name Type Default Optional Description
modal-accept string "Accept" required text to use for the accept button
modal-cancel string "Cancel" optional text to use for the cancel button
If emitted, the modal will not have a cancel button
modal-background boolean "false" optional allow the background to fade
<modal-window browser-modal-window="Sure!" modal-cancel="Naaah" modal-background="true">
                <strong>Hi! This is a modal.</strong>
                <p>Here, you can write anything you want 
                your users to know before proceeding forward.</p>
                <em>Maybe answer some questions as well?</em>
            </modal-window>
let myModal = document.getElementsByTagName("modal-window")[0];

// Add callback functions to the buttons (accept, cancel)
myModal.defineCallbacks(
    function () {
        console.log("modal-window: 'OK'");
    },
    function () {
        console.log("modal-window: 'cancel'");
    }
);

// Test button
document.getElementById("open-modal-window")
    .addEventListener("click", function () {
        myModal.showModal();
    });