Skip to content

Commit 5e8e5b5

Browse files
committed
Added callback function prop to facilitate logging
Added timout prop to allow user to set the renderscan highlight duration Added documentation for callback and duration options 1.1.0 ReadMe update
1 parent 2643907 commit 5e8e5b5

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ The overlay button appears in the bottom right corner and is enabled by default.
2929
- `initialEnabled` (default: `true`) - Start with render scanning enabled
3030
- `offsetLeft` (default: `0`) - Offset the button left from its default position
3131
- `hideIcon` (default: `false`) - Hide the render scan button while keeping functionality active
32+
- `callback` (default: `undefined`) - Optional user defined function that gets called once per valid mutation. Signature is `(mutation:MutationRecord)=>void;`
33+
- `duration` (defult: `1000`) - Adjust how long the render scan highlights remain on screen in ms
3234

3335
## Development
3436

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-render-scan",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"license": "MIT",
55
"homepage": "https://khromov.github.io/svelte-render-scan/",
66
"repository": {

src/lib/components/RenderScan.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
let {
88
initialEnabled = true,
99
offsetLeft = 0,
10-
hideIcon = false
10+
hideIcon = false,
11+
callback = undefined,
12+
duration = 1000
1113
} = $props<{
1214
initialEnabled?: boolean;
1315
offsetLeft?: number;
1416
hideIcon?: boolean;
17+
callback?: (mutationRecord: MutationRecord) => void;
18+
duration?: number;
1519
}>();
1620
1721
// State management
@@ -37,7 +41,7 @@
3741
</script>
3842

3943
{#if enabled}
40-
<RenderScanObserver />
44+
<RenderScanObserver {callback} {duration} />
4145
{/if}
4246

4347
<!-- Floating button -->

src/lib/components/RenderScanObserver.svelte

+11-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929
get title() {
3030
// Get entries once and use them for both the reasons string and total
3131
const entries = [...this.#reasons.entries()];
32-
const reasons = entries
33-
.map(([key, count]) => `${key} (${count})`)
34-
.join(' , ');
35-
32+
const reasons = entries.map(([key, count]) => `${key} (${count})`).join(' , ');
33+
3634
// Calculate total using the same entries array
3735
const total = entries.reduce((sum, [_, count]) => sum + count, 0);
3836
@@ -94,7 +92,7 @@
9492
this.#element.remove();
9593
activeHighlights.delete(this);
9694
}, 250); // Match the CSS transition duration
97-
}, 1000);
95+
}, duration);
9896
});
9997
}
10098
@@ -129,6 +127,12 @@
129127
}
130128
}
131129
130+
// Props with defaults
131+
let { duration = 1000, callback = undefined } = $props<{
132+
duration: number;
133+
callback: (mutation: MutationRecord) => void;
134+
}>();
135+
132136
let overlayEl: HTMLDivElement | null = null;
133137
let mutationObserver: MutationObserver | null = null;
134138
let cache = new WeakMap<Node, Highlight>();
@@ -185,6 +189,7 @@
185189
default:
186190
console.log(`Unhandled mutation type: ${mutation.type}`);
187191
}
192+
if (callback) callback(mutation);
188193
}
189194
});
190195
@@ -213,4 +218,4 @@
213218
activeHighlights.clear();
214219
};
215220
});
216-
</script>
221+
</script>

src/routes/+page.svelte

+22
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
offsetLeft={60}
8484
/>`
8585
};
86+
// Callback usage code snippet
87+
const callbackCode = `function customCallback(mutation: MutationRecord) {
88+
// Custom code...
89+
}
90+
91+
<RenderScan callback={customCallback} />`;
8692
</script>
8793

8894
<div class="border-b-4 bg-[#faf6f4] py-12">
@@ -269,6 +275,22 @@
269275
<div class="code-block">{`<RenderScan hideIcon={true} />`}</div>
270276
</div>
271277

278+
<div>
279+
<h3 class="mb-2 font-bold">Highlight Duration</h3>
280+
<p class="mb-3 text-gray-600">
281+
Adjust how long the render scan highlights remain on screen (default=1000):
282+
</p>
283+
<div class="code-block">{`<RenderScan duration={2000} />`}</div>
284+
</div>
285+
286+
<div>
287+
<h3 class="mb-2 font-bold">Callback Function</h3>
288+
<p class="mb-3 text-gray-600">
289+
Optional user defined function that gets called once per valid mutation:
290+
</p>
291+
<div class="code-block">{callbackCode}</div>
292+
</div>
293+
272294
<div>
273295
<h3 class="mb-2 font-bold">Combined Props</h3>
274296
<p class="mb-3 text-gray-600">Use multiple props together:</p>

0 commit comments

Comments
 (0)