Skip to content

Commit

Permalink
ayeFrame 🏴‍☠️
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Mar 1, 2024
1 parent 73717b4 commit aae5771
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/ayeframe.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {};
export declare function ayeFrame(): void;
36 changes: 34 additions & 2 deletions dist/ayeframe.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/ayeframe.ts
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
import { morph } from "morphlex";

type AyeFrameAction = (targetNode: Element | null, content: ChildNode | null | undefined) => void;

export function ayeFrame(): void {
createFrame("morph", (targetNode, content) => {
if (targetNode && content) morph(targetNode, content);
});

createFrame("replace", (targetNode, content) => {
if (targetNode && content) targetNode.replaceWith(content);
});

createFrame("append", (targetNode, content) => {
if (targetNode && content) targetNode.appendChild(content);
});

createFrame("prepend", (targetNode, content) => {
if (targetNode && content) targetNode.prepend(content);
});

createFrame("remove", (targetNode) => {
if (targetNode) targetNode.remove();
});
}

function createFrame(name: string, action: AyeFrameAction): void {
for (const frame of document.querySelectorAll("iframe")) frame.name === name && frame.remove();

const frame = document.createElement("iframe");

frame.name = name;
frame.hidden = true;
frame.loading = "lazy";
frame.sandbox.add("allow-same-origin");

frame.addEventListener("load", () => {
const id = frame.contentWindow?.location?.hash;
if (id) {
action(document.querySelector(id) as Element | null, frame.contentDocument?.body?.firstChild);
}
});

document.body.appendChild(frame);
}

0 comments on commit aae5771

Please sign in to comment.