Skip to content

Commit

Permalink
refactor(jsx/dom): declare types explicitly.
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed May 26, 2024
1 parent 21b9596 commit 2e0dbca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/jsx/dom/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
* This module provides APIs for `hono/jsx/dom/client`, which is compatible with `react-dom/client`.
*/

import type { Child } from '../base'
import { useState } from '../hooks'
import { buildNode, renderNode } from './render'
import type { NodeObject } from './render'

type CreateRootOptions = Record<string, unknown>
export interface Root {
render(children: Child): void
unmount(): void
}
export type RootOptions = Record<string, unknown>

/**
* Create a root object for rendering
* @param element Render target
Expand All @@ -16,8 +22,8 @@ type CreateRootOptions = Record<string, unknown>
*/
export const createRoot = (
element: HTMLElement | DocumentFragment,
options: CreateRootOptions = {}
) => {
options: RootOptions = {}
): Root => {
let setJsxNode:
| undefined // initial state
| ((jsxNode: unknown) => void) // rendered
Expand Down Expand Up @@ -69,9 +75,9 @@ export const createRoot = (
*/
export const hydrateRoot = (
element: HTMLElement | DocumentFragment,
reactNode: unknown,
options: CreateRootOptions = {}
) => {
reactNode: Child,
options: RootOptions = {}
): Root => {
const root = createRoot(element, options)
root.render(reactNode)
return root
Expand Down
2 changes: 1 addition & 1 deletion src/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ export const renderNode = (node: NodeObject, container: Container) => {
container.replaceChildren(fragment)
}

export const render = (jsxNode: unknown, container: Container) => {
export const render = (jsxNode: Child, container: Container) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderNode(buildNode({ tag: '', props: { children: jsxNode } } as any) as NodeObject, container)
}
Expand Down

0 comments on commit 2e0dbca

Please sign in to comment.