UNPKG

react-code-canvas

Version:

A browser-based canvas for rendering React components at runtime

37 lines (36 loc) 2.08 kB
import React, { ComponentPropsWithoutRef } from 'react'; export type LiveErrorProps = ComponentPropsWithoutRef<'pre'> & { /** style overrides for the toast container (not the message element) */ containerStyle?: React.CSSProperties; /** replaces the default toast markup entirely. Receives the error message and * a `dismiss` callback wired to the same per-message dismissal the built-in * toast uses. When set, `id` and the styles below are not applied — the caller * owns the markup, including keeping `#react-code-error` if they depend on it. */ render?: (error: string, dismiss: () => void) => React.ReactNode; /** allow dismissal. `false` hides the built-in close button and makes the * `dismiss` callback a no-op, so the toast clears only when code compiles. */ dismissible?: boolean; }; /** * Error toast, pinned to the top-right of the viewport. Renders nothing when * there is no error, so it clears itself once the code compiles again. * * Dismissal is tracked by message, not by a boolean: a *different* error after * a dismiss shows the toast again, while re-renders of the same error stay * hidden. No effect needed to reset it. * * Two layout constraints worth preserving if you restyle this: * * 1. `#react-code-error` must never itself be `position: fixed`. Downstream * consumers poll it with * err.offsetParent !== null && err.textContent.trim().length > 0 * to detect "this code cannot render" without waiting out a full timeout, * and `offsetParent` is null for fixed-position elements. The container may * be fixed — offsetParent then resolves to it — but the id must stay on a * statically positioned descendant. * * 2. Incoming props (including `id`) are spread onto that inner `<pre>`, whose * textContent is only the message. The warning glyph and dismiss button sit * outside it so scraped error text stays clean. */ export declare const LiveError: ({ style, containerStyle, render, dismissible, ...rest }: LiveErrorProps) => React.JSX.Element | null;