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