UNPKG

@stencil/react-output-target

Version:

React output target for @stencil/core components.

116 lines (114 loc) • 5.54 kB
import { EventName } from '@lit/react'; import { StencilReactComponent } from './create-component.js'; type EventNames = Record<string, EventName | string>; export type { ReactWebComponent, WebComponentProps } from '@lit/react'; export type SerializeShadowRootOptions = 'declarative-shadow-dom' | 'scoped' | { 'declarative-shadow-dom'?: string[]; scoped?: string[]; default: 'declarative-shadow-dom' | 'scoped'; } | boolean; /** * these types are defined by a Stencil hydrate app so we have to copy the minimal types here */ export interface RenderToStringOptions { fullDocument?: boolean; prettyHtml?: boolean; /** * Configure how Stencil serializes the components shadow root. * - If set to `declarative-shadow-dom` the component will be rendered within a Declarative Shadow DOM. * - If set to `scoped` Stencil will render the contents of the shadow root as a `scoped: true` component * and the shadow DOM will be created during client-side hydration. * - Alternatively you can mix and match the two by providing an object with `declarative-shadow-dom` and `scoped` keys, * the value arrays containing the tag names of the components that should be rendered in that mode. * * Examples: * - `{ 'declarative-shadow-dom': ['my-component-1', 'another-component'], default: 'scoped' }` * Render all components as `scoped` apart from `my-component-1` and `another-component` * - `{ 'scoped': ['an-option-component'], default: 'declarative-shadow-dom' }` * Render all components within `declarative-shadow-dom` apart from `an-option-component` * - `'scoped'` Render all components as `scoped` * - `false` disables shadow root serialization * * *NOTE* `true` has been deprecated in favor of `declarative-shadow-dom` and `scoped` * @default 'declarative-shadow-dom' */ serializeShadowRoot?: SerializeShadowRootOptions; /** * Render into one long-lived window instead of creating one for every call, so the SSR runtime * and components are set up once. Stencil v5+ only (see {@link HydrateModule.resetReusableWindows}). */ reuseWindow?: boolean; /** @deprecated use `beforeSsr` — kept for v4/v5 compatibility */ beforeHydrate?: (document: Document) => void | Promise<void>; } export interface HydrateStyleElement { id?: string; href?: string | null; content?: string; } type RenderToString = (html: string, options: RenderToStringOptions) => Promise<{ html: string | null; styles: HydrateStyleElement[]; }>; export type HydrateModule = { renderToString: RenderToString; transformTag: (tagName: string) => string; setTagTransformer: (transformer: (tagName: string) => string) => void; /** * Exported by SSR modules that support the `reuseWindow` render option (Stencil v5+). Its * presence is how we detect that support: older modules don't have it. */ resetReusableWindows?: () => void; }; interface CreateComponentForServerSideRenderingOptions { tagName: string; properties: Record<string, string>; renderToString: RenderToString; serializeShadowRoot?: SerializeShadowRootOptions; transformTag?: (tagName: string) => string; /** * Pass `reuseWindow: true` to `renderToString`. Every Stencil component renders with its own * `renderToString` call, so setting the SSR runtime up once instead of per call is a large share * of each render. */ reuseWindow?: boolean; } type ParseHTML = typeof import('html-react-parser').htmlToDOM; /** * Split a component rendered with `renderToString({ fullDocument: false })` into its parts. The * fragment is parsed (so quotes, comments and `<` in attribute values are handled) and the parts * are sliced from the original string, so they keep Stencil's markup exactly. * * Stencil v5 starts a fragment with the `<style>`s its scoped components need (e.g. a scoped * child in the light DOM); those are skipped, as they're also in the results' `styles` and each * nested Stencil component renders its own. * * @param rendered - the fragment * @param tagNames - the tag names the component may have been rendered with * @param parseHTML - `htmlToDOM` from `html-react-parser`, which is only imported on the server * @returns the component's own HTML, its light DOM (when it has no shadow root), its * declarative shadow root (if any) */ export declare const splitRenderedComponent: (rendered: string, tagNames: string[], parseHTML: ParseHTML) => { html: string; lightDom: string; template: undefined; } | { html: string; lightDom: undefined; template: { delegatesFocus: boolean; content: string; }; }; type CreateComponentForSSROptions<I extends HTMLElement, E extends EventNames = {}, C = Omit<I, keyof HTMLElement>, R extends keyof C = never> = Omit<CreateComponentForServerSideRenderingOptions, 'renderToString' | 'transformTag'> & { hydrateModule: Promise<HydrateModule> | undefined; transformTag?: (tag: string) => string; getTagTransformer?: () => ((tag: string) => string) | undefined; clientModule?: StencilReactComponent<I, E, C, R>; }; /** * Defines a custom element and creates a React component for server side rendering. * @public */ export declare const createComponent: <I extends HTMLElement, E extends EventNames = {}, C = Omit<I, keyof HTMLElement>, R extends keyof C = never>(options: CreateComponentForSSROptions<I, E, C, R>) => StencilReactComponent<I, E, C, R>;