Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/jsx-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { isRef } from "./ref"
import {
forEach,
isArrayLike,
isAttributeHook,
isBoolean,
isChildrenHook,
isComponentClass,
isElement,
isFunction,
Expand Down Expand Up @@ -219,6 +221,8 @@ function appendChild(
const shadowRoot = (node as HTMLElement).attachShadow(child.attr)
appendChild(child.children, shadowRoot)
attachRef(child.ref, shadowRoot)
} else if (isChildrenHook(child)) {
appendChild(child.jsxDomChildrenHook.call(child, node), node)
}
}

Expand Down Expand Up @@ -406,7 +410,11 @@ function attrNS(node: Element, namespace: string, key: string, value: string | n

function attributes(attr: object, node: HTMLElement | SVGElement) {
for (const key of keys(attr)) {
attribute(key, attr[key], node)
let value: any = attr[key]
if (isAttributeHook(value)) {
value = value.jsxDomAttributeHook(node, key)
}
attribute(key, value, node)
}
return node
}
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function isArrayLike(obj: any): obj is ArrayLike<any> {
return isObject(obj) && typeof obj.length === "number" && typeof obj.nodeType !== "number"
}

export function isAttributeHook(val: any): val is { jsxDomAttributeHook: (node: HTMLElement | SVGElement, attr: string) => any } {
return isObject(val) && isFunction(val.jsxDomAttributeHook)
}

export function isChildrenHook(val: any): val is { jsxDomChildrenHook: (parent: HTMLElement | SVGElement) => any } {
return isObject(val) && isFunction(val.jsxDomChildrenHook)
}

export function forEach<V = any>(value: { [key: string]: V }, fn: (value: V, key: string) => void) {
if (!value) return
for (const key of keys(value)) {
Expand Down
50 changes: 49 additions & 1 deletion test/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,52 @@ describe("hooks", () => {
cls.toggle("container", false)
expect(cls.contains("container")).toBe(false)
})
})

it("supports jsxDomAttributeHook", () => {
let callValue: any, callNode: HTMLElement | SVGElement, callTimes: number = 0
function hook(node: HTMLElement | SVGElement, attr: string) {
callNode = node;
callValue = this;
callTimes++
return `hooked:${attr}`
}
const hookedValue = { jsxDomAttributeHook: hook }
const div = <div title={hookedValue} />
expect(callTimes).toBe(1)
expect(callValue).toBe(hookedValue)
expect(callNode).toBe(div)
expect(div.getAttribute("title")).toBe("hooked:title")
})

it("supports jsxDomChildrenHook as sole content", () => {
let callValue: any, callParentNode: HTMLElement | SVGElement, callTimes: number = 0
function hook(parentNode: HTMLElement | SVGElement) {
callParentNode = parentNode
callValue = this
callTimes++
return "test"
}
const hookedValue = { jsxDomChildrenHook: hook }
const div = <div>{hookedValue}</div>
expect(callTimes).toBe(1)
expect(callValue).toBe(hookedValue)
expect(callParentNode).toBe(div)
expect(div.textContent).toBe("test")
})

it("supports jsxDomChildrenHook inside a fragment", () => {
let callValue: any, callParentNode: HTMLElement | SVGElement, callTimes: number = 0
function hook(parentNode: HTMLElement | SVGElement) {
callParentNode = parentNode
callValue = this
callTimes++
return "test"
}
const hookedValue = { jsxDomChildrenHook: hook }
const div = <div><>{hookedValue}</></div>
expect(callTimes).toBe(1)
expect(callValue).toBe(hookedValue)
expect(callParentNode instanceof DocumentFragment).toBe(true)
expect(div.textContent).toBe("test")
})
})
Loading