local-iframe 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -0
  3. package/index.js +39 -0
  4. package/package.json +38 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aleksandr Hovhannisyan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # `local-iframe`
2
+
3
+ > A web component that renders templates in a local `<iframe>`.
4
+
5
+ ## Getting Started
6
+
7
+ Install the package:
8
+
9
+ ```sh
10
+ npm install local-iframe
11
+ ```
12
+
13
+ Or include it via CDN:
14
+
15
+ ```html
16
+ <script
17
+ type="module"
18
+ src="https://cdn.jsdelivr.net/npm/local-iframe@version/index.js"
19
+ ></script>
20
+ ```
21
+
22
+ ## Use Cases
23
+
24
+ I wanted to be able to declaratively render fully local, isolated code demos in my tutorials, without embedding third-party Codepens/sandboxes and without creating separate pages for each code demo. This web component uses the [`HTMLIFrameElement.srcdoc`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/srcdoc) attribute to do just that.
25
+
26
+ > [!NOTE]
27
+ > This originally began as an Eleventy plugin: [eleventy-plugin-code-demo](github.com/AleksandrHovhannisyan/eleventy-plugin-code-demo).
28
+
29
+ ## Example Usage
30
+
31
+ There are two ways to define the markup for your iframe:
32
+
33
+ - Render a `<template>` as a child of `<local-iframe>`
34
+ - Define the `<template>` externally and set `template="id-of-your-template"`
35
+
36
+ In both cases, the component will duplicate the content of the template and render it in a local iframe.
37
+
38
+ Any styles and scripts will only affect elements within the iframe itself.
39
+
40
+ ### Child Template
41
+
42
+ The simplest way to use `local-iframe` is to render a `<template>` as a child:
43
+
44
+ ```html
45
+ <local-iframe style="width: 800px; height: 400px;">
46
+ <template>
47
+ <div><h1>Hello, world!</h1></div>
48
+ <p>This is awesome!</p>
49
+ <script>
50
+ console.log("hi");
51
+ </script>
52
+ <script>
53
+ console.log("second console log in same iframe, but a second script tag");
54
+ </script>
55
+ <style>
56
+ body {
57
+ background-color: red;
58
+ font-size: 200%;
59
+ }
60
+ </style>
61
+ </template>
62
+ </local-iframe>
63
+ ```
64
+
65
+ In this example, we render a template with:
66
+
67
+ - Some HTML tags for the iframe's markup
68
+ - Two scripts to execute within the iframe's context
69
+ - Styles to apply within to elements within the iframe
70
+
71
+ > [!TIP]
72
+ > To prevent layout shifts when the page loads, set inline styles on the host element and assign it a width and height. The inner `iframe` will fill that space responsively, shrinking or growing as needed.
73
+
74
+ ### Template Attribute
75
+
76
+ Alternatively, you can define an external `<template>` and reference it via the `template` attribute, like so:
77
+
78
+ ```html
79
+ <template id="template-1">
80
+ <h1>Template 1</h1>
81
+ <style>
82
+ h1 {
83
+ color: red;
84
+ }
85
+ </style>
86
+ </template>
87
+ <local-iframe template="template-1"></local-iframe>
88
+ ```
89
+
90
+ If the `template` attribute changes, the frame will update its markup with the content of the new template.
package/index.js ADDED
@@ -0,0 +1,39 @@
1
+ class LocalIframe extends HTMLElement {
2
+ static observedAttributes = ['template'];
3
+
4
+ #iframe;
5
+
6
+ constructor() {
7
+ super();
8
+ this.#iframe = document.createElement('iframe');
9
+ this.#iframe.style.height = '100%';
10
+ this.#iframe.style.width = '100%';
11
+ this.#iframe.style.maxWidth = '100%';
12
+ this.appendChild(this.#iframe);
13
+ }
14
+
15
+ attributeChangedCallback(name, oldValue, ) {
16
+ if (name === 'template') {
17
+ this.connectedCallback();
18
+ }
19
+ }
20
+
21
+ connectedCallback() {
22
+ const templateId = this.getAttribute('template');
23
+ const template = templateId ? document.getElementById(templateId) : this.querySelector('template');
24
+
25
+ if (!template) {
26
+ throw new Error('No <template> found for local-iframe element.');
27
+ }
28
+
29
+ const html = Array.from(template.content.children).filter(
30
+ (child) => child.tagName.toLowerCase() !== 'style' && child.tagName.toLowerCase() !== 'script'
31
+ ).map((html) => html.outerHTML).join('');
32
+ const css = Array.from(template.content.querySelectorAll('style')).map((css) => css.outerHTML).join('');
33
+ const js = Array.from(template.content.querySelectorAll('script')).map((js) => js.outerHTML).join('');
34
+
35
+ this.#iframe.srcdoc = `<!DOCTYPE html><html><head><meta charset="utf-8">${css}</head><body>${html}${js}</body></html>`;
36
+ }
37
+ }
38
+
39
+ window.customElements.define('local-iframe', LocalIframe);
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "local-iframe",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "exports": {
7
+ ".": "./index.js"
8
+ },
9
+ "description": "A web component that renders templates in a local `<iframe>`",
10
+ "keywords": [
11
+ "iframe",
12
+ "local sandbox",
13
+ "code demo"
14
+ ],
15
+ "files": [
16
+ "./index.js",
17
+ "README.md",
18
+ "package.json",
19
+ "LICENSE"
20
+ ],
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/AleksandrHovhannisyan/local-iframe.git"
24
+ },
25
+ "homepage": "https://github.com/AleksandrHovhannisyan/local-iframe.git",
26
+ "author": {
27
+ "name": "Aleksandr Hovhannisyan",
28
+ "url": "https://www.aleksandrhovhannisyan.com"
29
+ },
30
+ "license": "MIT",
31
+ "scripts": {
32
+ "dev": "vite"
33
+ },
34
+ "devDependencies": {
35
+ "vite": "^6.0.0"
36
+ },
37
+ "packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
38
+ }