Skip to content

Commit 1d70c04

Browse files
docs(rdom): update $compile, $wrapEl, $wrapHtml docs (#456)
1 parent 6994a63 commit 1d70c04

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/rdom/src/compile.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ import { $wrapEl, $wrapText } from "./wrap.js";
2929
* target attribute. If used as element body, the reactive value will be wrapped
3030
* using a {@link $sub} `<span>` with the value as its reactive body.
3131
*
32+
* **Important:** Use {@link $replace}, {@link $refresh} or {@link $switch} to
33+
* wrap any reactive values/subscriptions which produce actual HTML
34+
* elements/components/subtrees (in hiccup format). See docs for these functions
35+
* for details & examples. Not using any of these wrappers will result in
36+
* unexpected outcomes.
37+
*
38+
* Also see {@link $wrapText}, {@link $wrapHtml} or {@link $wrapEl} for DOM
39+
* element related component wrappers.
40+
*
3241
* @param tree -
3342
*/
3443
export const $compile = (tree: any): IComponent =>

packages/rdom/src/wrap.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ export const $wrapText = wrapper($text);
3737
* Returns a component wrapper for a single DOM element whose HTML body can be
3838
* later updated/replaced via `.update()`, similarly to setting `.innerHTML`.
3939
*
40+
* @remarks
41+
* Setting `.innerHtml` considered dangerous — please use with caution or use
42+
* {@link $wrapText} if the source of the HTML body given to `.update()` cannot
43+
* be trusted!
44+
*
45+
* @example
46+
* ```ts
47+
* import { $compile, $wrapHtml } from "@thi.ng/rdom";
48+
*
49+
* // create pre-configured updatable element
50+
* const title = $wrapHtml("h1", { style: { color: "red" } });
51+
*
52+
* // embed inside rdom tree
53+
* $compile(["div", {}, title, "world..."]).mount(document.body);
54+
*
55+
* // update element body (only after element has been mounted!)
56+
* title.update("<em>hello</em>");
57+
* ```
58+
*
4059
* @param tag - element name
4160
* @param attribs - element attribs
4261
* @param body - optional initial body
@@ -47,6 +66,17 @@ export const $wrapHtml = wrapper($html);
4766
* {@link IComponent} wrapper for an existing DOM element. When mounted, the
4867
* given element will be (re)attached to the parent node provided at that time.
4968
*
69+
* @example
70+
* ```ts
71+
* import { $compile, $wrapEl } from "@thi.ng/rdom";
72+
*
73+
* const title = document.createElement("h1");
74+
* title.innerText = "hello";
75+
*
76+
* // embed existing DOM element inside an rdom tree
77+
* $compile(["div", {}, $wrapEl(title), "world..."]).mount(document.body);
78+
* ```
79+
*
5080
* @param el
5181
*/
5282
export const $wrapEl = (el: Element): IComponent => ({

0 commit comments

Comments
 (0)