|
| 1 | +# React Native View Shot - Web Example |
| 2 | + |
| 3 | +Web example demonstrating `react-native-view-shot` in browsers using `html2canvas`. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install |
| 9 | +npm start |
| 10 | +``` |
| 11 | + |
| 12 | +Opens at `http://localhost:3000` |
| 13 | + |
| 14 | +## What's Included |
| 15 | + |
| 16 | +- **Basic ViewShot**: Simple captures with PNG/JPG formats and Base64/Data URI output |
| 17 | +- **Transparency Test**: PNG transparency support (with html2canvas limitations) |
| 18 | +- **Image Capture**: Views with styled elements and colors |
| 19 | +- **Complex Layouts**: Nested views with flexbox, shadows, borders, and gradients |
| 20 | + |
| 21 | +## Usage Example |
| 22 | + |
| 23 | +```typescript |
| 24 | +import {captureRef} from "react-native-view-shot"; |
| 25 | + |
| 26 | +const viewRef = useRef(null); |
| 27 | + |
| 28 | +const capture = async () => { |
| 29 | + const uri = await captureRef(viewRef.current, { |
| 30 | + format: "png", |
| 31 | + quality: 0.9, |
| 32 | + result: "data-uri", |
| 33 | + }); |
| 34 | + console.log("Captured:", uri); |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +## Tech Stack |
| 39 | + |
| 40 | +- React Native Web for cross-platform components |
| 41 | +- html2canvas for web screenshot capture |
| 42 | +- Webpack for bundling |
| 43 | +- TypeScript for type safety |
| 44 | + |
| 45 | +## Web Limitations |
| 46 | + |
| 47 | +- `result: 'tmpfile'` falls back to 'data-uri' (with warning) |
| 48 | +- `snapshotContentContainer` not implemented |
| 49 | +- Transparency edge cases due to html2canvas |
| 50 | +- CORS restrictions on external images |
| 51 | + |
| 52 | +## Build |
| 53 | + |
| 54 | +```bash |
| 55 | +npm run build # Production build in dist/ |
| 56 | +npm run clean # Remove dist/ |
| 57 | +``` |
| 58 | + |
| 59 | +## Testing |
| 60 | + |
| 61 | +### Automated E2E Tests (Playwright) |
| 62 | + |
| 63 | +The web example has comprehensive Playwright tests that run in CI: |
| 64 | + |
| 65 | +```bash |
| 66 | +npm run test:e2e # Run tests headless |
| 67 | +npm run test:e2e:headed # Run tests with visible browser |
| 68 | +``` |
| 69 | + |
| 70 | +**Tests verify:** |
| 71 | + |
| 72 | +- ✅ App loads and navigation works |
| 73 | +- ✅ PNG capture functionality |
| 74 | +- ✅ JPG capture functionality |
| 75 | +- ✅ Base64 output format |
| 76 | +- ✅ All screens render correctly |
| 77 | +- ✅ Images are actually generated |
| 78 | + |
| 79 | +Tests run automatically in GitHub Actions CI with Chromium. |
| 80 | + |
| 81 | +### Manual Testing |
| 82 | + |
| 83 | +For local development and debugging: |
| 84 | + |
| 85 | +1. **Start dev server:** `npm start` |
| 86 | +2. **Open browser:** `http://localhost:3000` |
| 87 | +3. **Test capture:** |
| 88 | + - Click "Basic ViewShot" |
| 89 | + - Click "📸 PNG (Data URI)" |
| 90 | + - Verify image appears |
| 91 | + - Try download functionality |
| 92 | + |
| 93 | +### CI Pipeline |
| 94 | + |
| 95 | +See `.github/workflows/ci.yml` - the `test-web-example` job runs all Playwright tests in headless Chromium. |
| 96 | + |
| 97 | +**CI Artifacts:** |
| 98 | + |
| 99 | +- `web-snapshots-reference`: Current reference snapshots (download to update local snapshots) |
| 100 | +- `web-snapshots-diff`: Visual diffs when tests fail (actual vs expected) |
| 101 | +- `playwright-report`: Full HTML test report |
| 102 | +- `playwright-test-results`: Detailed test results |
| 103 | + |
| 104 | +**When snapshots differ:** |
| 105 | + |
| 106 | +- The CI will show a clear message in the GitHub Actions summary |
| 107 | +- Download the `web-snapshots-reference` artifact |
| 108 | +- Replace `example-web/e2e/snapshots/reference/` with the downloaded files |
| 109 | +- Commit the updated snapshots |
| 110 | + |
| 111 | +**Note:** Snapshots are platform-specific (Linux vs macOS). The CI runs on Linux, so you may need to update snapshots when running locally on macOS. |
| 112 | + |
| 113 | +## Related |
| 114 | + |
| 115 | +- [Main Library](../) |
| 116 | +- [Native Example](../example/) |
| 117 | +- [html2canvas](https://html2canvas.hertzen.com/) |
0 commit comments