@oxog/react-check 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4552,261 +4552,9 @@ init_browser();
4552
4552
 
4553
4553
  // src/browser/scanner.ts
4554
4554
  init_event_emitter();
4555
- var DEFAULT_CONFIG2 = {
4556
- trackUnnecessary: true,
4557
- fpsThreshold: 30,
4558
- highlightRenders: true,
4559
- animationSpeed: "fast"
4560
- };
4561
- var BrowserScanner = class extends EventEmitter {
4562
- /** Scanner configuration */
4563
- config;
4564
- /** Whether scanner is running */
4565
- running = false;
4566
- /** React version detected */
4567
- reactVersion = "unknown";
4568
- /** Previous fiber states for comparison */
4569
- prevFiberStates = /* @__PURE__ */ new WeakMap();
4570
- /** Component render counts */
4571
- renderCounts = /* @__PURE__ */ new Map();
4572
- /** Original DevTools hook methods */
4573
- originalOnCommitFiberRoot = null;
4574
- /** Window reference */
4575
- win;
4576
- /**
4577
- * Create a new browser scanner
4578
- * @param config - Scanner configuration
4579
- * @param win - Window object (for testing)
4580
- */
4581
- constructor(config = {}, win) {
4582
- super();
4583
- this.config = { ...DEFAULT_CONFIG2, ...config };
4584
- this.win = win ?? (typeof window !== "undefined" ? window : {});
4585
- }
4586
- /**
4587
- * Start the scanner
4588
- */
4589
- start() {
4590
- if (this.running) return;
4591
- try {
4592
- this.hookIntoReact();
4593
- this.running = true;
4594
- } catch (error) {
4595
- this.emit("error", error instanceof Error ? error : new Error(String(error)));
4596
- }
4597
- }
4598
- /**
4599
- * Stop the scanner
4600
- */
4601
- stop() {
4602
- if (!this.running) return;
4603
- this.unhookFromReact();
4604
- this.running = false;
4605
- }
4606
- /**
4607
- * Reset the scanner state
4608
- */
4609
- reset() {
4610
- this.renderCounts.clear();
4611
- this.prevFiberStates = /* @__PURE__ */ new WeakMap();
4612
- }
4613
- /**
4614
- * Hook into React DevTools global hook
4615
- */
4616
- hookIntoReact() {
4617
- const hook = this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__;
4618
- if (!hook) {
4619
- this.installHook();
4620
- return;
4621
- }
4622
- this.originalOnCommitFiberRoot = hook.onCommitFiberRoot.bind(hook);
4623
- hook.onCommitFiberRoot = (rendererID, root, priorityLevel) => {
4624
- if (this.originalOnCommitFiberRoot) {
4625
- this.originalOnCommitFiberRoot(rendererID, root, priorityLevel);
4626
- }
4627
- this.processCommit(root.current);
4628
- };
4629
- this.reactVersion = this.detectReactVersion(hook);
4630
- this.emit("ready", { reactVersion: this.reactVersion });
4631
- }
4632
- /**
4633
- * Install our own hook if DevTools hook is not present
4634
- */
4635
- installHook() {
4636
- const hook = {
4637
- renderers: /* @__PURE__ */ new Map(),
4638
- supportsFiber: true,
4639
- inject: (renderer) => {
4640
- const id = hook.renderers.size + 1;
4641
- hook.renderers.set(id, renderer);
4642
- return id;
4643
- },
4644
- onCommitFiberRoot: (_rendererID, root, _priorityLevel) => {
4645
- this.processCommit(root.current);
4646
- },
4647
- onCommitFiberUnmount: () => {
4648
- }
4649
- };
4650
- this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook;
4651
- this.emit("ready", { reactVersion: "unknown (hook installed)" });
4652
- }
4653
- /**
4654
- * Unhook from React
4655
- */
4656
- unhookFromReact() {
4657
- const hook = this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__;
4658
- if (hook && this.originalOnCommitFiberRoot) {
4659
- hook.onCommitFiberRoot = this.originalOnCommitFiberRoot;
4660
- this.originalOnCommitFiberRoot = null;
4661
- }
4662
- }
4663
- /**
4664
- * Detect React version from hook
4665
- * @param hook - DevTools hook
4666
- * @returns React version string
4667
- */
4668
- detectReactVersion(hook) {
4669
- if (hook.renderers.size === 0) {
4670
- return "unknown";
4671
- }
4672
- const renderer = hook.renderers.values().next().value;
4673
- if (renderer && typeof renderer === "object" && "version" in renderer) {
4674
- return String(renderer.version);
4675
- }
4676
- return "unknown";
4677
- }
4678
- /**
4679
- * Process a fiber commit
4680
- * @param rootFiber - Root fiber of the committed tree
4681
- */
4682
- processCommit(rootFiber) {
4683
- this.walkFiber(rootFiber, null);
4684
- }
4685
- /**
4686
- * Walk the fiber tree
4687
- * @param fiber - Current fiber
4688
- * @param parent - Parent component name
4689
- */
4690
- walkFiber(fiber, parent) {
4691
- if (!fiber) return;
4692
- if (isUserComponent(fiber)) {
4693
- this.processFiber(fiber, parent);
4694
- parent = getComponentName(fiber);
4695
- }
4696
- this.walkFiber(fiber.child, parent);
4697
- this.walkFiber(fiber.sibling, parent);
4698
- }
4699
- /**
4700
- * Process a single fiber
4701
- * @param fiber - Fiber to process
4702
- * @param parent - Parent component name
4703
- */
4704
- processFiber(fiber, _parent) {
4705
- const componentName = getComponentName(fiber);
4706
- if (this.shouldFilterComponent(componentName)) {
4707
- return;
4708
- }
4709
- const prevState = this.prevFiberStates.get(fiber);
4710
- const phase = prevState ? "update" : "mount";
4711
- const renderInfo = createRenderInfo(
4712
- fiber,
4713
- phase,
4714
- prevState?.props,
4715
- prevState?.state
4716
- );
4717
- const currentCount = this.renderCounts.get(componentName) ?? 0;
4718
- const newCount = currentCount + 1;
4719
- this.renderCounts.set(componentName, newCount);
4720
- renderInfo.renderCount = newCount;
4721
- this.prevFiberStates.set(fiber, {
4722
- props: fiber.memoizedProps,
4723
- state: fiber.memoizedState
4724
- });
4725
- this.emit("render", renderInfo);
4726
- }
4727
- /**
4728
- * Check if component should be filtered out
4729
- * @param name - Component name
4730
- * @returns true if should be filtered
4731
- */
4732
- shouldFilterComponent(name) {
4733
- if (name.startsWith("__")) return true;
4734
- if (name === "Fragment") return true;
4735
- if (name === "Suspense") return true;
4736
- if (name === "Profiler") return true;
4737
- const { include, exclude } = this.config;
4738
- if (include && include.length > 0) {
4739
- const included = include.some((pattern) => this.matchPattern(name, pattern));
4740
- if (!included) return true;
4741
- }
4742
- if (exclude && exclude.length > 0) {
4743
- const excluded = exclude.some((pattern) => this.matchPattern(name, pattern));
4744
- if (excluded) return true;
4745
- }
4746
- return false;
4747
- }
4748
- /**
4749
- * Simple glob pattern matching
4750
- * @param name - Component name
4751
- * @param pattern - Glob pattern
4752
- * @returns true if matches
4753
- */
4754
- matchPattern(name, pattern) {
4755
- const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
4756
- try {
4757
- const regex = new RegExp(`^${regexPattern}$`);
4758
- return regex.test(name);
4759
- } catch {
4760
- return false;
4761
- }
4762
- }
4763
- /**
4764
- * Get current configuration
4765
- * @returns Scanner configuration
4766
- */
4767
- getConfig() {
4768
- return { ...this.config };
4769
- }
4770
- /**
4771
- * Update configuration
4772
- * @param config - Partial configuration
4773
- */
4774
- configure(config) {
4775
- this.config = { ...this.config, ...config };
4776
- }
4777
- /**
4778
- * Check if scanner is running
4779
- * @returns true if running
4780
- */
4781
- isRunning() {
4782
- return this.running;
4783
- }
4784
- /**
4785
- * Get detected React version
4786
- * @returns React version string
4787
- */
4788
- getReactVersion() {
4789
- return this.reactVersion;
4790
- }
4791
- /**
4792
- * Get render count for a component
4793
- * @param name - Component name
4794
- * @returns Render count
4795
- */
4796
- getRenderCount(name) {
4797
- return this.renderCounts.get(name) ?? 0;
4798
- }
4799
- /**
4800
- * Get all render counts
4801
- * @returns Map of component names to render counts
4802
- */
4803
- getAllRenderCounts() {
4804
- return new Map(this.renderCounts);
4805
- }
4806
- };
4807
4555
 
4808
4556
  // src/browser/overlay.ts
4809
- var DEFAULT_CONFIG3 = {
4557
+ var DEFAULT_CONFIG2 = {
4810
4558
  enabled: true,
4811
4559
  highlightRenders: true,
4812
4560
  animationSpeed: "fast",
@@ -4825,6 +4573,13 @@ var ANIMATION_DURATIONS = {
4825
4573
  fast: 300,
4826
4574
  off: 0
4827
4575
  };
4576
+ var FLASH_COLORS = {
4577
+ critical: "rgba(239, 68, 68, 0.35)",
4578
+ warning: "rgba(234, 179, 8, 0.3)",
4579
+ info: "rgba(59, 130, 246, 0.25)",
4580
+ healthy: "rgba(34, 197, 94, 0.2)",
4581
+ unnecessary: "rgba(107, 114, 128, 0.15)"
4582
+ };
4828
4583
  var Overlay = class {
4829
4584
  /** Shadow root for isolation */
4830
4585
  shadowRoot = null;
@@ -4847,7 +4602,7 @@ var Overlay = class {
4847
4602
  * @param config - Overlay configuration
4848
4603
  */
4849
4604
  constructor(config = {}) {
4850
- this.config = { ...DEFAULT_CONFIG3, ...config };
4605
+ this.config = { ...DEFAULT_CONFIG2, ...config };
4851
4606
  }
4852
4607
  /**
4853
4608
  * Initialize the overlay
@@ -4901,12 +4656,65 @@ var Overlay = class {
4901
4656
  pointer-events: none;
4902
4657
  border-width: 2px;
4903
4658
  border-style: solid;
4904
- transition: opacity 0.3s ease-out;
4905
4659
  z-index: 1;
4660
+ animation: highlight-pulse 0.5s ease-out;
4906
4661
  }
4907
4662
 
4908
4663
  .highlight.fade {
4909
4664
  opacity: 0;
4665
+ transition: opacity 0.3s ease-out;
4666
+ }
4667
+
4668
+ .flash-overlay {
4669
+ position: fixed;
4670
+ pointer-events: none;
4671
+ z-index: 0;
4672
+ animation: flash-pulse 0.4s ease-out forwards;
4673
+ border-radius: 4px;
4674
+ }
4675
+
4676
+ @keyframes flash-pulse {
4677
+ 0% {
4678
+ opacity: 1;
4679
+ transform: scale(1);
4680
+ }
4681
+ 50% {
4682
+ opacity: 0.8;
4683
+ transform: scale(1.02);
4684
+ }
4685
+ 100% {
4686
+ opacity: 0;
4687
+ transform: scale(1);
4688
+ }
4689
+ }
4690
+
4691
+ @keyframes highlight-pulse {
4692
+ 0% {
4693
+ opacity: 0;
4694
+ box-shadow: 0 0 0 0 currentColor;
4695
+ }
4696
+ 30% {
4697
+ opacity: 1;
4698
+ box-shadow: 0 0 8px 2px currentColor;
4699
+ }
4700
+ 100% {
4701
+ opacity: 1;
4702
+ box-shadow: 0 0 0 0 transparent;
4703
+ }
4704
+ }
4705
+
4706
+ @keyframes badge-pop {
4707
+ 0% {
4708
+ transform: scale(0.8);
4709
+ opacity: 0;
4710
+ }
4711
+ 50% {
4712
+ transform: scale(1.1);
4713
+ }
4714
+ 100% {
4715
+ transform: scale(1);
4716
+ opacity: 1;
4717
+ }
4910
4718
  }
4911
4719
 
4912
4720
  .badge {
@@ -5124,7 +4932,7 @@ var Overlay = class {
5124
4932
  });
5125
4933
  }
5126
4934
  /**
5127
- * Highlight a component render
4935
+ * Highlight a component render with flash effect
5128
4936
  * @param render - Render information
5129
4937
  * @param element - DOM element to highlight
5130
4938
  */
@@ -5133,7 +4941,9 @@ var Overlay = class {
5133
4941
  const rect = element.getBoundingClientRect();
5134
4942
  if (rect.width === 0 || rect.height === 0) return;
5135
4943
  const severity = this.getSeverity(render);
5136
- const color = SEVERITY_COLORS[severity];
4944
+ const borderColor = SEVERITY_COLORS[severity];
4945
+ const flashColor = FLASH_COLORS[severity];
4946
+ this.createFlashOverlay(rect, flashColor);
5137
4947
  let highlight = this.highlights.get(render.componentName);
5138
4948
  if (!highlight) {
5139
4949
  highlight = document.createElement("div");
@@ -5141,15 +4951,20 @@ var Overlay = class {
5141
4951
  this.shadowRoot.appendChild(highlight);
5142
4952
  this.highlights.set(render.componentName, highlight);
5143
4953
  }
4954
+ highlight.style.animation = "none";
4955
+ highlight.offsetHeight;
4956
+ highlight.style.animation = "";
5144
4957
  highlight.style.cssText = `
5145
4958
  position: fixed;
5146
4959
  left: ${rect.left}px;
5147
4960
  top: ${rect.top}px;
5148
4961
  width: ${rect.width}px;
5149
4962
  height: ${rect.height}px;
5150
- border: 2px solid ${color};
4963
+ border: 2px solid ${borderColor};
4964
+ color: ${borderColor};
5151
4965
  pointer-events: none;
5152
4966
  z-index: 1;
4967
+ animation: highlight-pulse 0.5s ease-out;
5153
4968
  `;
5154
4969
  highlight.classList.remove("fade");
5155
4970
  const duration = ANIMATION_DURATIONS[this.config.animationSpeed];
@@ -5159,133 +4974,476 @@ var Overlay = class {
5159
4974
  }, duration);
5160
4975
  }
5161
4976
  if (this.config.showBadges) {
5162
- this.updateBadge(render, rect, color);
4977
+ this.updateBadge(render, rect, borderColor);
4978
+ }
4979
+ this.updateStats();
4980
+ }
4981
+ /**
4982
+ * Create a flash overlay effect on the element
4983
+ * @param rect - Element bounding rectangle
4984
+ * @param color - Flash color
4985
+ */
4986
+ createFlashOverlay(rect, color) {
4987
+ if (!this.shadowRoot) return;
4988
+ const flash = document.createElement("div");
4989
+ flash.className = "flash-overlay";
4990
+ flash.style.cssText = `
4991
+ left: ${rect.left}px;
4992
+ top: ${rect.top}px;
4993
+ width: ${rect.width}px;
4994
+ height: ${rect.height}px;
4995
+ background: ${color};
4996
+ `;
4997
+ this.shadowRoot.appendChild(flash);
4998
+ setTimeout(() => {
4999
+ flash.remove();
5000
+ }, 500);
5001
+ }
5002
+ /**
5003
+ * Update or create a badge for a component
5004
+ * @param render - Render information
5005
+ * @param rect - Bounding rectangle
5006
+ * @param color - Badge color
5007
+ */
5008
+ updateBadge(render, rect, color) {
5009
+ if (!this.shadowRoot) return;
5010
+ const count = (this.renderCounts.get(render.componentName) ?? 0) + 1;
5011
+ this.renderCounts.set(render.componentName, count);
5012
+ let badge = this.badges.get(render.componentName);
5013
+ if (!badge) {
5014
+ badge = document.createElement("div");
5015
+ badge.className = "badge";
5016
+ this.shadowRoot.appendChild(badge);
5017
+ this.badges.set(render.componentName, badge);
5018
+ }
5019
+ const isNewBadge = !this.badges.has(render.componentName);
5020
+ badge.textContent = `${render.componentName}: ${count}`;
5021
+ badge.style.cssText = `
5022
+ position: fixed;
5023
+ left: ${rect.left}px;
5024
+ top: ${Math.max(0, rect.top - 20)}px;
5025
+ background: ${color};
5026
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
5027
+ font-size: 10px;
5028
+ font-weight: bold;
5029
+ padding: 2px 6px;
5030
+ border-radius: 4px;
5031
+ color: white;
5032
+ pointer-events: none;
5033
+ z-index: 2;
5034
+ white-space: nowrap;
5035
+ ${isNewBadge ? "animation: badge-pop 0.3s ease-out;" : ""}
5036
+ `;
5037
+ if (!isNewBadge) {
5038
+ badge.style.transform = "scale(1.15)";
5039
+ setTimeout(() => {
5040
+ if (badge) badge.style.transform = "scale(1)";
5041
+ }, 150);
5042
+ }
5043
+ }
5044
+ /**
5045
+ * Get severity level for a render
5046
+ * @param render - Render information
5047
+ * @returns Severity level
5048
+ */
5049
+ getSeverity(render) {
5050
+ if (!render.necessary) return "unnecessary";
5051
+ if (render.renderCount > 50) return "critical";
5052
+ if (render.renderCount > 20) return "warning";
5053
+ return "healthy";
5054
+ }
5055
+ /**
5056
+ * Record a render event (updates stats even without DOM element)
5057
+ * @param render - Render information
5058
+ */
5059
+ recordRender(render) {
5060
+ const count = (this.renderCounts.get(render.componentName) ?? 0) + 1;
5061
+ this.renderCounts.set(render.componentName, count);
5062
+ this.totalRenders++;
5063
+ this.updateStats();
5064
+ }
5065
+ /** Total render count */
5066
+ totalRenders = 0;
5067
+ /**
5068
+ * Update toolbar stats
5069
+ */
5070
+ updateStats() {
5071
+ if (!this.shadowRoot) return;
5072
+ let critical = 0;
5073
+ let warning = 0;
5074
+ let healthy = 0;
5075
+ for (const [, count] of this.renderCounts) {
5076
+ if (count > 50) critical++;
5077
+ else if (count > 20) warning++;
5078
+ else healthy++;
5079
+ }
5080
+ const criticalEl = this.shadowRoot.getElementById("rc-critical");
5081
+ const warningEl = this.shadowRoot.getElementById("rc-warning");
5082
+ const healthyEl = this.shadowRoot.getElementById("rc-healthy");
5083
+ const rendersEl = this.shadowRoot.getElementById("rc-renders");
5084
+ if (criticalEl) criticalEl.textContent = String(critical);
5085
+ if (warningEl) warningEl.textContent = String(warning);
5086
+ if (healthyEl) healthyEl.textContent = String(healthy);
5087
+ if (rendersEl) rendersEl.textContent = String(this.totalRenders);
5088
+ }
5089
+ /**
5090
+ * Clear all highlights
5091
+ */
5092
+ clearHighlights() {
5093
+ for (const highlight of this.highlights.values()) {
5094
+ highlight.remove();
5095
+ }
5096
+ this.highlights.clear();
5097
+ }
5098
+ /**
5099
+ * Clear all badges
5100
+ */
5101
+ clearBadges() {
5102
+ for (const badge of this.badges.values()) {
5103
+ badge.remove();
5104
+ }
5105
+ this.badges.clear();
5106
+ }
5107
+ /**
5108
+ * Clear all visualizations
5109
+ */
5110
+ clear() {
5111
+ this.clearHighlights();
5112
+ this.clearBadges();
5113
+ this.renderCounts.clear();
5114
+ this.totalRenders = 0;
5115
+ this.updateStats();
5116
+ }
5117
+ /**
5118
+ * Show the overlay
5119
+ */
5120
+ show() {
5121
+ if (this.host) {
5122
+ this.host.style.display = "block";
5123
+ this.visible = true;
5124
+ }
5125
+ }
5126
+ /**
5127
+ * Hide the overlay
5128
+ */
5129
+ hide() {
5130
+ if (this.host) {
5131
+ this.host.style.display = "none";
5132
+ this.visible = false;
5133
+ }
5134
+ }
5135
+ /**
5136
+ * Toggle overlay visibility
5137
+ */
5138
+ toggle() {
5139
+ if (this.visible) {
5140
+ this.hide();
5141
+ } else {
5142
+ this.show();
5143
+ }
5144
+ }
5145
+ /**
5146
+ * Update configuration
5147
+ * @param config - Partial configuration
5148
+ */
5149
+ configure(config) {
5150
+ this.config = { ...this.config, ...config };
5151
+ }
5152
+ /**
5153
+ * Check if overlay is visible
5154
+ * @returns true if visible
5155
+ */
5156
+ isVisible() {
5157
+ return this.visible;
5158
+ }
5159
+ };
5160
+
5161
+ // src/browser/scanner.ts
5162
+ var DEFAULT_CONFIG3 = {
5163
+ trackUnnecessary: true,
5164
+ fpsThreshold: 30,
5165
+ highlightRenders: true,
5166
+ animationSpeed: "fast"
5167
+ };
5168
+ var BrowserScanner = class extends EventEmitter {
5169
+ /** Scanner configuration */
5170
+ config;
5171
+ /** Whether scanner is running */
5172
+ running = false;
5173
+ /** React version detected */
5174
+ reactVersion = "unknown";
5175
+ /** Previous fiber states for comparison */
5176
+ prevFiberStates = /* @__PURE__ */ new WeakMap();
5177
+ /** Component render counts */
5178
+ renderCounts = /* @__PURE__ */ new Map();
5179
+ /** Original DevTools hook methods */
5180
+ originalOnCommitFiberRoot = null;
5181
+ /** Window reference */
5182
+ win;
5183
+ /** Overlay for visual feedback */
5184
+ overlay = null;
5185
+ /**
5186
+ * Create a new browser scanner
5187
+ * @param config - Scanner configuration
5188
+ * @param win - Window object (for testing)
5189
+ */
5190
+ constructor(config = {}, win) {
5191
+ super();
5192
+ this.config = { ...DEFAULT_CONFIG3, ...config };
5193
+ this.win = win ?? (typeof window !== "undefined" ? window : {});
5194
+ }
5195
+ /**
5196
+ * Get DOM element from fiber node
5197
+ * React stores the DOM node in fiber.stateNode for host components
5198
+ * or we need to walk down to find the first DOM node
5199
+ * @param fiber - Fiber node
5200
+ * @returns DOM Element or null
5201
+ */
5202
+ getDOMElement(fiber) {
5203
+ if (fiber.stateNode instanceof Element) {
5204
+ return fiber.stateNode;
5205
+ }
5206
+ let current = fiber.child;
5207
+ while (current) {
5208
+ if (current.stateNode instanceof Element) {
5209
+ return current.stateNode;
5210
+ }
5211
+ if (current.child) {
5212
+ current = current.child;
5213
+ } else if (current.sibling) {
5214
+ current = current.sibling;
5215
+ } else {
5216
+ let parent = current.return;
5217
+ while (parent && parent !== fiber && !parent.sibling) {
5218
+ parent = parent.return;
5219
+ }
5220
+ current = parent && parent !== fiber ? parent.sibling : null;
5221
+ }
5222
+ }
5223
+ return null;
5224
+ }
5225
+ /**
5226
+ * Enable visual overlay for render highlighting
5227
+ */
5228
+ enableOverlay() {
5229
+ if (this.overlay) return;
5230
+ this.overlay = new Overlay({
5231
+ enabled: true,
5232
+ highlightRenders: this.config.highlightRenders ?? true,
5233
+ animationSpeed: this.config.animationSpeed ?? "fast",
5234
+ showBadges: true,
5235
+ showToolbar: true
5236
+ });
5237
+ this.overlay.init();
5238
+ }
5239
+ /**
5240
+ * Disable visual overlay
5241
+ */
5242
+ disableOverlay() {
5243
+ if (this.overlay) {
5244
+ this.overlay.destroy();
5245
+ this.overlay = null;
5246
+ }
5247
+ }
5248
+ /**
5249
+ * Get overlay instance
5250
+ * @returns Overlay instance or null
5251
+ */
5252
+ getOverlay() {
5253
+ return this.overlay;
5254
+ }
5255
+ /**
5256
+ * Start the scanner
5257
+ */
5258
+ start() {
5259
+ if (this.running) return;
5260
+ try {
5261
+ this.hookIntoReact();
5262
+ this.running = true;
5263
+ } catch (error) {
5264
+ this.emit("error", error instanceof Error ? error : new Error(String(error)));
5265
+ }
5266
+ }
5267
+ /**
5268
+ * Stop the scanner
5269
+ */
5270
+ stop() {
5271
+ if (!this.running) return;
5272
+ this.unhookFromReact();
5273
+ this.disableOverlay();
5274
+ this.running = false;
5275
+ }
5276
+ /**
5277
+ * Reset the scanner state
5278
+ */
5279
+ reset() {
5280
+ this.renderCounts.clear();
5281
+ this.prevFiberStates = /* @__PURE__ */ new WeakMap();
5282
+ }
5283
+ /**
5284
+ * Hook into React DevTools global hook
5285
+ */
5286
+ hookIntoReact() {
5287
+ const hook = this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__;
5288
+ if (!hook) {
5289
+ this.installHook();
5290
+ return;
5163
5291
  }
5164
- this.updateStats();
5292
+ this.originalOnCommitFiberRoot = hook.onCommitFiberRoot.bind(hook);
5293
+ hook.onCommitFiberRoot = (rendererID, root, priorityLevel) => {
5294
+ if (this.originalOnCommitFiberRoot) {
5295
+ this.originalOnCommitFiberRoot(rendererID, root, priorityLevel);
5296
+ }
5297
+ this.processCommit(root.current);
5298
+ };
5299
+ this.reactVersion = this.detectReactVersion(hook);
5300
+ this.emit("ready", { reactVersion: this.reactVersion });
5165
5301
  }
5166
5302
  /**
5167
- * Update or create a badge for a component
5168
- * @param render - Render information
5169
- * @param rect - Bounding rectangle
5170
- * @param color - Badge color
5303
+ * Install our own hook if DevTools hook is not present
5171
5304
  */
5172
- updateBadge(render, rect, color) {
5173
- if (!this.shadowRoot) return;
5174
- const count = (this.renderCounts.get(render.componentName) ?? 0) + 1;
5175
- this.renderCounts.set(render.componentName, count);
5176
- let badge = this.badges.get(render.componentName);
5177
- if (!badge) {
5178
- badge = document.createElement("div");
5179
- badge.className = "badge";
5180
- this.shadowRoot.appendChild(badge);
5181
- this.badges.set(render.componentName, badge);
5182
- }
5183
- badge.textContent = `${render.componentName}: ${count}`;
5184
- badge.style.cssText = `
5185
- position: fixed;
5186
- left: ${rect.left}px;
5187
- top: ${Math.max(0, rect.top - 20)}px;
5188
- background: ${color};
5189
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
5190
- font-size: 10px;
5191
- font-weight: bold;
5192
- padding: 2px 6px;
5193
- border-radius: 4px;
5194
- color: white;
5195
- pointer-events: none;
5196
- z-index: 2;
5197
- white-space: nowrap;
5198
- `;
5305
+ installHook() {
5306
+ const hook = {
5307
+ renderers: /* @__PURE__ */ new Map(),
5308
+ supportsFiber: true,
5309
+ inject: (renderer) => {
5310
+ const id = hook.renderers.size + 1;
5311
+ hook.renderers.set(id, renderer);
5312
+ return id;
5313
+ },
5314
+ onCommitFiberRoot: (_rendererID, root, _priorityLevel) => {
5315
+ this.processCommit(root.current);
5316
+ },
5317
+ onCommitFiberUnmount: () => {
5318
+ }
5319
+ };
5320
+ this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook;
5321
+ this.emit("ready", { reactVersion: "unknown (hook installed)" });
5199
5322
  }
5200
5323
  /**
5201
- * Get severity level for a render
5202
- * @param render - Render information
5203
- * @returns Severity level
5324
+ * Unhook from React
5204
5325
  */
5205
- getSeverity(render) {
5206
- if (!render.necessary) return "unnecessary";
5207
- if (render.renderCount > 50) return "critical";
5208
- if (render.renderCount > 20) return "warning";
5209
- return "healthy";
5326
+ unhookFromReact() {
5327
+ const hook = this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__;
5328
+ if (hook && this.originalOnCommitFiberRoot) {
5329
+ hook.onCommitFiberRoot = this.originalOnCommitFiberRoot;
5330
+ this.originalOnCommitFiberRoot = null;
5331
+ }
5210
5332
  }
5211
5333
  /**
5212
- * Update toolbar stats
5334
+ * Detect React version from hook
5335
+ * @param hook - DevTools hook
5336
+ * @returns React version string
5213
5337
  */
5214
- updateStats() {
5215
- if (!this.shadowRoot) return;
5216
- let critical = 0;
5217
- let warning = 0;
5218
- let healthy = 0;
5219
- let total = 0;
5220
- for (const [, count] of this.renderCounts) {
5221
- total += count;
5222
- if (count > 50) critical++;
5223
- else if (count > 20) warning++;
5224
- else healthy++;
5338
+ detectReactVersion(hook) {
5339
+ if (hook.renderers.size === 0) {
5340
+ return "unknown";
5225
5341
  }
5226
- const criticalEl = this.shadowRoot.getElementById("rc-critical");
5227
- const warningEl = this.shadowRoot.getElementById("rc-warning");
5228
- const healthyEl = this.shadowRoot.getElementById("rc-healthy");
5229
- const rendersEl = this.shadowRoot.getElementById("rc-renders");
5230
- if (criticalEl) criticalEl.textContent = String(critical);
5231
- if (warningEl) warningEl.textContent = String(warning);
5232
- if (healthyEl) healthyEl.textContent = String(healthy);
5233
- if (rendersEl) rendersEl.textContent = String(total);
5342
+ const renderer = hook.renderers.values().next().value;
5343
+ if (renderer && typeof renderer === "object" && "version" in renderer) {
5344
+ return String(renderer.version);
5345
+ }
5346
+ return "unknown";
5234
5347
  }
5235
5348
  /**
5236
- * Clear all highlights
5349
+ * Process a fiber commit
5350
+ * @param rootFiber - Root fiber of the committed tree
5237
5351
  */
5238
- clearHighlights() {
5239
- for (const highlight of this.highlights.values()) {
5240
- highlight.remove();
5241
- }
5242
- this.highlights.clear();
5352
+ processCommit(rootFiber) {
5353
+ this.walkFiber(rootFiber, null);
5243
5354
  }
5244
5355
  /**
5245
- * Clear all badges
5356
+ * Walk the fiber tree
5357
+ * @param fiber - Current fiber
5358
+ * @param parent - Parent component name
5246
5359
  */
5247
- clearBadges() {
5248
- for (const badge of this.badges.values()) {
5249
- badge.remove();
5360
+ walkFiber(fiber, parent) {
5361
+ if (!fiber) return;
5362
+ if (isUserComponent(fiber)) {
5363
+ this.processFiber(fiber, parent);
5364
+ parent = getComponentName(fiber);
5250
5365
  }
5251
- this.badges.clear();
5366
+ this.walkFiber(fiber.child, parent);
5367
+ this.walkFiber(fiber.sibling, parent);
5252
5368
  }
5253
5369
  /**
5254
- * Clear all visualizations
5370
+ * Process a single fiber
5371
+ * @param fiber - Fiber to process
5372
+ * @param parent - Parent component name
5255
5373
  */
5256
- clear() {
5257
- this.clearHighlights();
5258
- this.clearBadges();
5259
- this.renderCounts.clear();
5260
- this.updateStats();
5374
+ processFiber(fiber, _parent) {
5375
+ const componentName = getComponentName(fiber);
5376
+ if (this.shouldFilterComponent(componentName)) {
5377
+ return;
5378
+ }
5379
+ const prevState = this.prevFiberStates.get(fiber);
5380
+ const phase = prevState ? "update" : "mount";
5381
+ const renderInfo = createRenderInfo(
5382
+ fiber,
5383
+ phase,
5384
+ prevState?.props,
5385
+ prevState?.state
5386
+ );
5387
+ const currentCount = this.renderCounts.get(componentName) ?? 0;
5388
+ const newCount = currentCount + 1;
5389
+ this.renderCounts.set(componentName, newCount);
5390
+ renderInfo.renderCount = newCount;
5391
+ this.prevFiberStates.set(fiber, {
5392
+ props: fiber.memoizedProps,
5393
+ state: fiber.memoizedState
5394
+ });
5395
+ if (this.overlay && this.config.highlightRenders) {
5396
+ const domElement = this.getDOMElement(fiber);
5397
+ if (domElement) {
5398
+ this.overlay.highlight(renderInfo, domElement);
5399
+ } else {
5400
+ this.overlay.recordRender(renderInfo);
5401
+ }
5402
+ }
5403
+ this.emit("render", renderInfo);
5261
5404
  }
5262
5405
  /**
5263
- * Show the overlay
5406
+ * Check if component should be filtered out
5407
+ * @param name - Component name
5408
+ * @returns true if should be filtered
5264
5409
  */
5265
- show() {
5266
- if (this.host) {
5267
- this.host.style.display = "block";
5268
- this.visible = true;
5410
+ shouldFilterComponent(name) {
5411
+ if (name.startsWith("__")) return true;
5412
+ if (name === "Fragment") return true;
5413
+ if (name === "Suspense") return true;
5414
+ if (name === "Profiler") return true;
5415
+ const { include, exclude } = this.config;
5416
+ if (include && include.length > 0) {
5417
+ const included = include.some((pattern) => this.matchPattern(name, pattern));
5418
+ if (!included) return true;
5419
+ }
5420
+ if (exclude && exclude.length > 0) {
5421
+ const excluded = exclude.some((pattern) => this.matchPattern(name, pattern));
5422
+ if (excluded) return true;
5269
5423
  }
5424
+ return false;
5270
5425
  }
5271
5426
  /**
5272
- * Hide the overlay
5427
+ * Simple glob pattern matching
5428
+ * @param name - Component name
5429
+ * @param pattern - Glob pattern
5430
+ * @returns true if matches
5273
5431
  */
5274
- hide() {
5275
- if (this.host) {
5276
- this.host.style.display = "none";
5277
- this.visible = false;
5432
+ matchPattern(name, pattern) {
5433
+ const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
5434
+ try {
5435
+ const regex = new RegExp(`^${regexPattern}$`);
5436
+ return regex.test(name);
5437
+ } catch {
5438
+ return false;
5278
5439
  }
5279
5440
  }
5280
5441
  /**
5281
- * Toggle overlay visibility
5442
+ * Get current configuration
5443
+ * @returns Scanner configuration
5282
5444
  */
5283
- toggle() {
5284
- if (this.visible) {
5285
- this.hide();
5286
- } else {
5287
- this.show();
5288
- }
5445
+ getConfig() {
5446
+ return { ...this.config };
5289
5447
  }
5290
5448
  /**
5291
5449
  * Update configuration
@@ -5295,11 +5453,33 @@ var Overlay = class {
5295
5453
  this.config = { ...this.config, ...config };
5296
5454
  }
5297
5455
  /**
5298
- * Check if overlay is visible
5299
- * @returns true if visible
5456
+ * Check if scanner is running
5457
+ * @returns true if running
5300
5458
  */
5301
- isVisible() {
5302
- return this.visible;
5459
+ isRunning() {
5460
+ return this.running;
5461
+ }
5462
+ /**
5463
+ * Get detected React version
5464
+ * @returns React version string
5465
+ */
5466
+ getReactVersion() {
5467
+ return this.reactVersion;
5468
+ }
5469
+ /**
5470
+ * Get render count for a component
5471
+ * @param name - Component name
5472
+ * @returns Render count
5473
+ */
5474
+ getRenderCount(name) {
5475
+ return this.renderCounts.get(name) ?? 0;
5476
+ }
5477
+ /**
5478
+ * Get all render counts
5479
+ * @returns Map of component names to render counts
5480
+ */
5481
+ getAllRenderCounts() {
5482
+ return new Map(this.renderCounts);
5303
5483
  }
5304
5484
  };
5305
5485
 
@@ -5357,6 +5537,7 @@ var ReactCheckInjector = class {
5357
5537
  setupScannerEvents() {
5358
5538
  this.scanner.on("render", (render) => {
5359
5539
  this.send({ type: "render", payload: render });
5540
+ this.overlay.recordRender(render);
5360
5541
  const element = this.findDOMElement(render.componentName);
5361
5542
  if (element) {
5362
5543
  this.overlay.highlight(render, element);
@@ -5445,8 +5626,42 @@ var ReactCheckInjector = class {
5445
5626
  case "config":
5446
5627
  this.handleConfig(message.payload);
5447
5628
  break;
5629
+ case "highlight":
5630
+ break;
5631
+ case "toggle-overlay":
5632
+ this.handleToggleOverlay(message.payload.enabled);
5633
+ break;
5634
+ }
5635
+ }
5636
+ /**
5637
+ * Toggle overlay visibility
5638
+ * @param enabled - Whether overlay should be visible
5639
+ */
5640
+ handleToggleOverlay(enabled) {
5641
+ if (enabled) {
5642
+ this.overlay.show();
5643
+ this.overlay.configure({ highlightRenders: true });
5644
+ console.log("[ReactCheck] Overlay enabled");
5645
+ } else {
5646
+ this.overlay.hide();
5647
+ this.overlay.configure({ highlightRenders: false });
5648
+ console.log("[ReactCheck] Overlay disabled");
5448
5649
  }
5449
5650
  }
5651
+ /**
5652
+ * Get overlay visibility status
5653
+ * @returns Whether overlay is visible
5654
+ */
5655
+ isOverlayEnabled() {
5656
+ return this.overlay.isVisible();
5657
+ }
5658
+ /**
5659
+ * Toggle overlay (public method for console access)
5660
+ */
5661
+ toggleOverlay() {
5662
+ const enabled = !this.overlay.isVisible();
5663
+ this.handleToggleOverlay(enabled);
5664
+ }
5450
5665
  /**
5451
5666
  * Handle config update
5452
5667
  * @param config - New configuration