Modal - Web Component
How do I make an modal - web component?
A modal I made using web component technologies.. What is a modal - web component? How do you make a modal - web component? This script and codes were developed by Cliff Pyles on 15 January 2023, Sunday.
Modal - Web Component - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Modal - Web Component</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <x-modal open> <p>This is a sample modal. It is made using web component technologies. Once you click on the black background, the modal will close.</p> <p>The valid attributes are "open" and "disabled".</p>
</x-modal>
<script>
class XModal extends HTMLElement { constructor() { super(); this.el = this.attachShadow({mode: 'closed'}); if (this.hasAttribute('open')) { this._renderContainer(); } } static get observedAttributes() { return ['disabled', 'open']; } get disabled() { return this.hasAttribute('disabled'); } set disabled(val) { if (val) { this.setAttribute('disabled', ''); } else { this.removeAttribute('disabled'); } } get open() { return this.hasAttribute('open'); } set open(val) { if (val) { this.setAttribute('open', ''); } else { this.removeAttribute('open'); } } connectedCallback() { const overlay = this.el.querySelector('#overlay'); const main = this.el.querySelector('#main'); this._boundOnOverlayClick = this._onOverlayClick.bind(this); overlay.addEventListener('click', this._boundOnOverlayClick); } attributeChangedCallback(name, oldValue, newValue) { if (this.open) { this._renderContainer(); } else { this.el.innerHTML = ''; } } _onOverlayClick() { if (this.disabled) { return; } this.setAttribute('open', !this.getAttribute('open')); this.el.innerHTML = ''; } _renderContainer() { let container = ` <style> * { box-sizing: border-box; } :host, .overlay { position: fixed; display: flex; align-items: center; justify-content: center; top: 0px; left: 0px; width: 100vw; height: 100vh; padding: 1rem; contain: content; box-sizing: border-box; background-color: rgba(0, 0, 0, 0.85); } .main { z-index: 1; background: #fff; width: 450px; height: 350px; max-width: 100%; max-height: 100%; padding: 1rem; } </style> <div id="overlay" class="overlay"></div> <div id="main" class="main"> <slot></slot> </div> `; this.el.innerHTML = container; }
}
if (window.customElements) { customElements.define('x-modal', XModal);
} else { document.querySelector('x-modal').innerHTML = `<p class="not-supported">Web components use experimental technologies. The browser you are using is not currently supported.</p>`;
}
</script>
</body>
</html>
Modal - Web Component - Script Codes CSS Codes
p { margin: 1rem 0px; margin-top: 0px;
}
p + p { margin-top: 1rem;
}
.not-supported { padding: 1rem;
}
Developer | Cliff Pyles |
Username | cliffpyles |
Uploaded | January 15, 2023 |
Rating | 3 |
Size | 2,586 Kb |
Views | 6,072 |
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!
Name | Size |
Animated Lamp Highlighting a Header | 3,529 Kb |
Ember Component Starter | 2,702 Kb |
Hexadecimal Color Previewer | 3,898 Kb |
Three Column Flexbox Layout | 2,200 Kb |
CSS Grid Overlay | 3,090 Kb |
Circular Progress Bar | 3,939 Kb |
Fading Slideshow | 4,122 Kb |
Content Preview | 2,857 Kb |
Terminal Interface | 4,222 Kb |
SVG Starter | 2,685 Kb |
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!
Name | Username | Size |
Simple search using AngularJS | Haykou | 1,802 Kb |
Countdown with Rings | Ewganoel | 2,490 Kb |
Ionic Infinite outside ion-content with ion-pane | Felquis | 3,117 Kb |
Email Marketing Mock | Kristenzirkler | 8,224 Kb |
SVG Animation | Pollardld | 3,133 Kb |
Pictos font library from CodePen | Jstam | 3,790 Kb |
Popup Modal | Aldlevine | 3,696 Kb |
Magnus 3 | ARocketfish | 7,944 Kb |
Fixed Scrolling Nav Bar | Philsinatra | 0 Kb |
A Pen by Moeid Saleem | Moeidsaleem | 1,862 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!