|
3 | 3 | > [!WARNING] |
4 | 4 | > This package is still in the experimental phase. You can expect new functionalities, but with this comes the possibility of more frequent errors |
5 | 5 |
|
6 | | -Directory summary (light theme) | File coverage (dark theme) |
7 | | -:------------------------------:|:----------------------------: |
8 | | - |  |
| 6 | +Home | Stats | Code |
| 7 | +:----:|:---:|:---: |
| 8 | + |  |  |
| 9 | + |  |  |
9 | 10 |
|
10 | 11 | ## Installation |
11 | 12 |
|
@@ -45,17 +46,50 @@ export default defineConfig({ |
45 | 46 | }); |
46 | 47 | ``` |
47 | 48 |
|
48 | | -## Reading coverage report |
| 49 | +## Iframe - theme support |
49 | 50 |
|
50 | | -`coverage-pretty-html-reporter` does not support viewing web-page locally (via file:// protocol) (i.e. double clicking the .html file). You need to publish coverage directory on local network ex.: |
| 51 | +When the reporter UI is embedded in an iframe, the built-in theme toggle button is hidden. In this mode you are expected to control the theme from the parent window. |
| 52 | + |
| 53 | +The iframe listens for postMessage events with the following payload: |
| 54 | + |
| 55 | +```ts |
| 56 | +type CoverageThemeMessage = { |
| 57 | + type: "coverage-theme"; |
| 58 | + theme: "light" | "dark"; |
| 59 | +}; |
| 60 | +``` |
| 61 | + |
| 62 | +Example integration in the parent page: |
| 63 | + |
| 64 | +```html |
| 65 | +<iframe |
| 66 | + id="coverage-report" |
| 67 | + src="/path/to/coverage/index.html" |
| 68 | + style="width: 100%; height: 100%; border: 0;" |
| 69 | +></iframe> |
| 70 | + |
| 71 | +<script> |
| 72 | + const iframe = document.getElementById("coverage-report"); |
| 73 | +
|
| 74 | + function setCoverageTheme(theme) { |
| 75 | + iframe?.contentWindow?.postMessage({ type: "coverage-theme", theme }, "*"); |
| 76 | + } |
| 77 | +
|
| 78 | + // Send initial theme once the iframe is loaded |
| 79 | + iframe.addEventListener("load", () => { |
| 80 | + const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; |
| 81 | + setCoverageTheme(prefersDark ? "dark" : "light"); |
| 82 | + }); |
| 83 | +
|
| 84 | + // Example: hook into your own app theme system |
| 85 | + window.addEventListener("app-theme-changed", (event) => { |
| 86 | + setCoverageTheme(event.detail.theme); // "light" | "dark" |
| 87 | + }); |
| 88 | +<\/script> |
| 89 | +``` |
| 90 | +
|
| 91 | +Inside the iframe, the selected theme is persisted in localStorage under the coverage-theme key and a coverage-theme-changed CustomEvent is dispatched on window so internal components can react to theme changes. |
51 | 92 |
|
52 | | -```sh |
53 | | -# When using npm |
54 | | -npx http-server ./coverage |
55 | | -# When using pnpm |
56 | | -pnpm dlx http-server ./coverage |
57 | | -# When using bun |
58 | | -bunx http-server ./coverage |
59 | 93 | ``` |
60 | 94 |
|
61 | 95 | ## Contributing |
|
0 commit comments