|
| 1 | +import type { ComponentType, JSX, PropsWithChildren } from "react"; |
| 2 | + |
| 3 | +export interface ServerSideRenderProps { |
| 4 | + /** The identifier of the block to be serverside rendered. */ |
| 5 | + block: string; |
| 6 | + /** The block attributes to be sent to the server for rendering. */ |
| 7 | + attributes?: Record<string, unknown> | null; |
| 8 | + /** Additional classes to apply to the wrapper element. */ |
| 9 | + className?: string; |
| 10 | + /** The HTTP method to use (‘GET’ or ‘POST’). Default is ‘GET’ */ |
| 11 | + httpMethod?: "GET" | "POST"; |
| 12 | + /** Additional query arguments to append to the request URL. */ |
| 13 | + urlQueryArgs?: Record<string, string | number | boolean | undefined>; |
| 14 | + /** Whether to remove block support attributes before sending. */ |
| 15 | + skipBlockSupportAttributes?: boolean; |
| 16 | + /** Component rendered when the API response is empty. */ |
| 17 | + EmptyResponsePlaceholder?: ComponentType<ServerSideRenderProps>; |
| 18 | + /** Component rendered when the API response is an error. */ |
| 19 | + ErrorResponsePlaceholder?: ComponentType<ServerSideRenderProps & { message: string }>; |
| 20 | + /** Component rendered while the API request is loading. */ |
| 21 | + LoadingResponsePlaceholder?: ComponentType<PropsWithChildren<ServerSideRenderProps>>; |
| 22 | +} |
| 23 | + |
| 24 | +/** A component that renders server-side content for blocks. */ |
| 25 | +export function ServerSideRender(props: ServerSideRenderProps): JSX.Element; |
| 26 | + |
| 27 | +/** |
| 28 | + * @deprecated Use `ServerSideRender` non-default export instead: |
| 29 | + * ```js |
| 30 | + * import { ServerSideRender } from '@wordpress/server-side-render'; |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +declare const ServerSideRenderDefault: typeof ServerSideRender; |
| 34 | +export default ServerSideRenderDefault; |
| 35 | + |
| 36 | +export type UseServerSideRenderArgs = Pick< |
| 37 | + ServerSideRenderProps, |
| 38 | + "block" | "attributes" | "httpMethod" | "urlQueryArgs" | "skipBlockSupportAttributes" |
| 39 | +>; |
| 40 | + |
| 41 | +export type ServerSideRenderStatus = "idle" | "loading" | "success" | "error"; |
| 42 | + |
| 43 | +export interface ServerSideRenderResponse { |
| 44 | + /** The current request status: 'idle', 'loading', 'success', or 'error'. */ |
| 45 | + status: ServerSideRenderStatus; |
| 46 | + /** The rendered block content (available when status is 'success'). */ |
| 47 | + content?: string; |
| 48 | + /** The error message (available when status is 'error'). */ |
| 49 | + error?: string; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * A hook for server-side rendering a preview of dynamic blocks to display in the editor. |
| 54 | + * |
| 55 | + * Handles fetching server-rendered previews for blocks, managing loading states, |
| 56 | + * and automatically debouncing requests to prevent excessive API calls. It supports both |
| 57 | + * GET and POST requests, with POST requests used for larger attribute payloads. |
| 58 | + * |
| 59 | + * @example |
| 60 | + * Basic usage: |
| 61 | + * |
| 62 | + * ```jsx |
| 63 | + * import { RawHTML } from '@wordpress/element'; |
| 64 | + * import { useServerSideRender } from '@wordpress/server-side-render'; |
| 65 | + * |
| 66 | + * function MyServerSideRender( { attributes, block } ) { |
| 67 | + * const { content, status, error } = useServerSideRender( { |
| 68 | + * attributes, |
| 69 | + * block, |
| 70 | + * } ); |
| 71 | + * |
| 72 | + * if ( status === 'loading' ) { |
| 73 | + * return <div>Loading...</div>; |
| 74 | + * } |
| 75 | + * |
| 76 | + * if ( status === 'error' ) { |
| 77 | + * return <div>Error: { error }</div>; |
| 78 | + * } |
| 79 | + * |
| 80 | + * return <RawHTML>{ content }</RawHTML>; |
| 81 | + * } |
| 82 | + * ``` |
| 83 | + */ |
| 84 | +export function useServerSideRender(args: UseServerSideRenderArgs): ServerSideRenderResponse; |
0 commit comments