Web Component Switches

Developer
Size
4,335 Kb
Views
14,168

How do I make an web component switches?

Encapsulated switches built with web components and shadow DOM. But I'm not so happy with the description at the buttons. Would like to wrap the text into , but it doesn't et displayed…. What is a web component switches? How do you make a web component switches? This script and codes were developed by Daniel Gooß on 10 November 2022, Thursday.

Web Component Switches Previews

Web Component Switches - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Web Component Switches</title> <meta name="viewport" content="width=device-width; initial-scale=1.0; user-scalable=no"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Montserrat'> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ body { font-family: 'Montserrat', sans-serif;
}
.content { padding: 0 20px;
}
.list { padding: 0; list-style: none;
}
.list > li { padding: 10px 0; border-width: 0 0 1px; border-style: solid; border-color: #ccc;
}
.list > li:first-child { border-top-width: 1px;
}
.list > li switch-button { margin: 0 20px 0 0;
}
switch-button { display: inline-block; vertical-align: middle;
}
switch-button::shadow .switch { font-size: 0; line-height: 0; display: block; overflow: hidden; width: 70px; height: 30px; margin: -5px 0 0; padding: 5px 0 1px; cursor: pointer; user-select: none; perspective: 2000px; perspective-origin: 50% 50%; transform-style: preserve-3d;
}
switch-button::shadow .switch:before,
switch-button::shadow .switch:after { content: 'O'; font-size: 1rem; line-height: 1; display: inline-flex; width: 50%; height: 100%; transition: all .05s linear; transform: rotateX(20deg) rotateY(0deg); transform-origin: 100% 50%; color: #fff; background-color: #eee; box-shadow: 0 10px 0 #bbbbbb; justify-content: center; align-items: center;
}
switch-button::shadow .switch:after { content: 'I'; background-color: #e1e1e1; transform: rotateX(20deg) rotateY(-20deg); transform-origin: 0% 50%;
}
switch-button::shadow .state:checked ~ .switch::before { background-color: #b1d540; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(20deg);
}
switch-button::shadow .state:checked ~ .switch::after { background-color: #bada55; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(0deg);
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <main class="content"> <h1>Devices</h1> <ul class="list"> <li> <switch-button data-name="on"></switch-button> TV </li> <li> <switch-button data-name="on"></switch-button> Radio </li> <li> <switch-button data-name="on"></switch-button> Coffee Machine </li> </ul>
</main>
<template class="js_switch-button-tpl"> <input type="checkbox" hidden class="state" value="" /> <label class="switch js_switch"> </label>
</template> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Web Component Switches - Script Codes CSS Codes

body { font-family: 'Montserrat', sans-serif;
}
.content { padding: 0 20px;
}
.list { padding: 0; list-style: none;
}
.list > li { padding: 10px 0; border-width: 0 0 1px; border-style: solid; border-color: #ccc;
}
.list > li:first-child { border-top-width: 1px;
}
.list > li switch-button { margin: 0 20px 0 0;
}
switch-button { display: inline-block; vertical-align: middle;
}
switch-button::shadow .switch { font-size: 0; line-height: 0; display: block; overflow: hidden; width: 70px; height: 30px; margin: -5px 0 0; padding: 5px 0 1px; cursor: pointer; user-select: none; perspective: 2000px; perspective-origin: 50% 50%; transform-style: preserve-3d;
}
switch-button::shadow .switch:before,
switch-button::shadow .switch:after { content: 'O'; font-size: 1rem; line-height: 1; display: inline-flex; width: 50%; height: 100%; transition: all .05s linear; transform: rotateX(20deg) rotateY(0deg); transform-origin: 100% 50%; color: #fff; background-color: #eee; box-shadow: 0 10px 0 #bbbbbb; justify-content: center; align-items: center;
}
switch-button::shadow .switch:after { content: 'I'; background-color: #e1e1e1; transform: rotateX(20deg) rotateY(-20deg); transform-origin: 0% 50%;
}
switch-button::shadow .state:checked ~ .switch::before { background-color: #b1d540; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(20deg);
}
switch-button::shadow .state:checked ~ .switch::after { background-color: #bada55; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(0deg);
}

Web Component Switches - Script Codes JS Codes

// Create new prototype element
var SwitchButtonProto = Object.create(HTMLElement.prototype);
// Callback function when prototype element was created
SwitchButtonProto.createdCallback = function(){ // Create new shadow DOM element var shadow = this.createShadowRoot(); // Create DOM nodes for markup in shadow DOM var input = document.createElement('input'); input.type = 'checkbox'; input.hidden = 'true'; input.className = 'state'; input.name = this.dataset.name; input.id = 'switch' var label = document.createElement('label'); label.setAttribute('for', 'switch'); label.classList.add('switch','js_switch'); // Append nodes to wrapping element shadow.appendChild(input); // Append collected nodes to shadow element shadow.appendChild(label);
}
// register custom element for usage in DOM
var SwitchButton = document.registerElement('switch-button', { prototype: SwitchButtonProto
});
Web Component Switches - Script Codes
Web Component Switches - Script Codes
Home Page Home
Developer Daniel Gooß
Username daniel_gooss
Uploaded November 10, 2022
Rating 3
Size 4,335 Kb
Views 14,168
Do you need developer help for Web Component Switches?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Daniel Gooß (daniel_gooss) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!