Skip to content
On this page

Custom Renderer API

createRenderer()

Creates a custom renderer. By providing platform-specific node creation and manipulation APIs, you can leverage Kdu's core runtime to target non-DOM environments.

  • Type

    function createRenderer<HostNode, HostElement>(
      options: RendererOptions<HostNode, HostElement>
    ): Renderer<HostElement>
    
    interface Renderer<HostElement> {
      render: RootRenderFunction<HostElement>
      createApp: CreateAppFunction<HostElement>
    }
    
    interface RendererOptions<HostNode, HostElement> {
      patchProp(
        el: HostElement,
        key: string,
        prevValue: any,
        nextValue: any,
        // the rest is unused for most custom renderers
        isSVG?: boolean,
        prevChildren?: KNode<HostNode, HostElement>[],
        parentComponent?: ComponentInternalInstance | null,
        parentSuspense?: SuspenseBoundary | null,
        unmountChildren?: UnmountChildrenFn
      ): void
      insert(
        el: HostNode,
        parent: HostElement,
        anchor?: HostNode | null
      ): void
      remove(el: HostNode): void
      createElement(
        type: string,
        isSVG?: boolean,
        isCustomizedBuiltIn?: string,
        knodeProps?: (KNodeProps & { [key: string]: any }) | null
      ): HostElement
      createText(text: string): HostNode
      createComment(text: string): HostNode
      setText(node: HostNode, text: string): void
      setElementText(node: HostElement, text: string): void
      parentNode(node: HostNode): HostElement | null
      nextSibling(node: HostNode): HostNode | null
    
      // optional, DOM-specific
      querySelector?(selector: string): HostElement | null
      setScopeId?(el: HostElement, id: string): void
      cloneNode?(node: HostNode): HostNode
      insertStaticContent?(
        content: string,
        parent: HostElement,
        anchor: HostNode | null,
        isSVG: boolean
      ): [HostNode, HostNode]
    }
    
  • Example

    import { createRenderer } from '@kdujs/runtime-core'
    
    const { render, createApp } = createRenderer({
      patchProp,
      insert,
      remove,
      createElement
      // ...
    })
    
    // `render` is the low-level API
    // `createApp` returns an app instance
    export { render, createApp }
    
    // re-export Kdu core APIs
    export * from '@kdujs/runtime-core'
    

    Kdu's own @kdujs/runtime-dom is implemented using the same API.

Custom Renderer API has loaded