import { Component, render } from 'preact'; import { Icon } from './components/icon'; import { SvgSprite } from './components/svg-sprite'; import { Widget } from './widget'; class ToolbarErrorBoundary extends Component { state: { hasError: boolean; error: Error | null } = { hasError: false, error: null }; static getDerivedStateFromError(error: Error) { return { hasError: true, error }; } handleReset = () => { this.setState({ hasError: false, error: null }); }; render() { if (this.state.hasError) { return (
React Scan ran into a problem
{this.state.error?.message || JSON.stringify(this.state.error)}
); } return this.props.children; } } export const createToolbar = (root: ShadowRoot): HTMLElement => { const container = document.createElement('div'); container.id = 'react-scan-toolbar-root'; window.__REACT_SCAN_TOOLBAR_CONTAINER__ = container; root.appendChild(container); render( <> , container, ); const originalRemove = container.remove.bind(container); container.remove = () => { window.__REACT_SCAN_TOOLBAR_CONTAINER__ = undefined; if (container.hasChildNodes()) { // Double render(null) is needed to fully unmount Preact components. // The first call initiates unmounting, while the second ensures // cleanup of internal VNode references and event listeners. render(null, container); render(null, container); } originalRemove(); }; return container; };