Welcome to the React Iframe monorepo! This project is a comprehensive toolkit for building advanced iframe-based applications in React, featuring powerful DOM manipulation, device emulation, and design mode capabilities.
Created & Maintained by:
- Sean Filimon
- Etc.
This monorepo is managed with Turbo and organized into applications and packages:
apps/demo: A generic Vite + React application demonstrating how to consume the@react-iframeecosystem. It showcases element selection, style injection, and device simulation.
-
packages/react-iframe(@react-iframe/core):- The core component library.
- Provides the
<AdvancedIframe />component. - Handles basic iframe rendering, ref forwarding, and event bridging.
-
packages/dom(@react-iframe/dom):- Utilities for advanced DOM interaction.
- State management for "Design Mode" features (selection, hovering, editing).
- Tools for injecting styles and scripts dynamically.
-
packages/devices(@react-iframe/devices):- Device definitions (iPhone, Pixel, Desktop, etc.).
- Utilities for responsive frame sizing and scaling logic.
-
packages/dom-parsers(@react-iframe/dom-parsers):- Parsers for analyzing and extracting structure from various framework projects.
- Helps bridge the gap between source code and the rendered iframe DOM.
- Declarative Iframe Rendering: Render iframes with React-friendly APIs.
- Deep DOM Selection: Select, highlight, and manipulate elements inside the iframe using standard CSS selectors.
- Design Mode: Built-in logic for selecting elements, handling hover states, and editing content visually.
- Device Emulation: Preview content across different device sizes and viewports.
- Style & Script Injection: Seamlessly inject custom CSS or JS into the iframe context.
- TypeScript Support: Fully typed for a robust development experience.
- Node.js (v16+)
- pnpm (recommended) or npm
Install dependencies for the entire workspace:
pnpm install
# or
npm installStart the development environment:
pnpm dev
# or
npm run devThis command uses Turbo to start the build/watch processes for dependent packages and launches the demo app (typically at http://localhost:5173).
To build all packages and apps for production:
pnpm buildHere is a simple example using the core package:
import React, { useRef } from 'react';
import { AdvancedIframe, AdvancedIframeHandle } from '@react-iframe/core';
const MyComponent = () => {
const iframeRef = useRef<AdvancedIframeHandle>(null);
const handleReady = (handle: AdvancedIframeHandle) => {
// Select an element and change its color
const title = handle.select<HTMLElement>('h1');
if (title) {
title.style.color = 'blue';
}
// Inject custom CSS
handle.injectCSS('body { background-color: #f0f0f0; }');
};
return (
<AdvancedIframe
ref={iframeRef}
initialContent="<h1>Hello World</h1>"
onReady={handleReady}
/>
);
};