Skip to content

Commit

Permalink
feat(rdom): add $wrapEl() DOM element component wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 12, 2023
1 parent 6cb864d commit 298e9a1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/rdom/src/wrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Fn2 } from "@thi.ng/api";
import type { IMountWithState, NumOrElement } from "./api.js";
import { $el, $html, $remove, $text } from "./dom.js";
import type { IComponent, IMountWithState, NumOrElement } from "./api.js";
import { $addChild, $el, $html, $remove, $text } from "./dom.js";
import { SCHEDULER } from "./scheduler.js";

const wrapper =
Expand Down Expand Up @@ -43,3 +43,21 @@ export const $wrapText = wrapper($text);
* @param body - optional initial body
*/
export const $wrapHtml = wrapper($html);

/**
* {@link IComponent} wrapper for an existing DOM element. When mounted, the
* given element will be (re)attached to the parent node provided at that time.
*
* @param el
*/
export const $wrapEl = (el: Element): IComponent => ({
async mount(parent, idx) {
$addChild(parent, el, idx);
return (this.el = el);
},
async unmount() {
$remove(this.el!);
this.el = undefined;
},
update() {},
});

0 comments on commit 298e9a1

Please sign in to comment.