@oxog/react-check 1.1.0 → 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,10 +4976,31 @@ 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);
5165
4980
  }
5166
4981
  this.updateStats();
5167
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
+ }
5168
5004
  /**
5169
5005
  * Update or create a badge for a component
5170
5006
  * @param render - Render information
@@ -5182,6 +5018,7 @@ var Overlay = class {
5182
5018
  this.shadowRoot.appendChild(badge);
5183
5019
  this.badges.set(render.componentName, badge);
5184
5020
  }
5021
+ const isNewBadge = !this.badges.has(render.componentName);
5185
5022
  badge.textContent = `${render.componentName}: ${count}`;
5186
5023
  badge.style.cssText = `
5187
5024
  position: fixed;
@@ -5197,109 +5034,419 @@ var Overlay = class {
5197
5034
  pointer-events: none;
5198
5035
  z-index: 2;
5199
5036
  white-space: nowrap;
5037
+ ${isNewBadge ? "animation: badge-pop 0.3s ease-out;" : ""}
5200
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
+ }
5201
5268
  }
5202
5269
  /**
5203
- * Get severity level for a render
5204
- * @param render - Render information
5205
- * @returns Severity level
5270
+ * Stop the scanner
5206
5271
  */
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";
5272
+ stop() {
5273
+ if (!this.running) return;
5274
+ this.unhookFromReact();
5275
+ this.disableOverlay();
5276
+ this.running = false;
5212
5277
  }
5213
5278
  /**
5214
- * Record a render event (updates stats even without DOM element)
5215
- * @param render - Render information
5279
+ * Reset the scanner state
5216
5280
  */
5217
- recordRender(render) {
5218
- const count = (this.renderCounts.get(render.componentName) ?? 0) + 1;
5219
- this.renderCounts.set(render.componentName, count);
5220
- this.totalRenders++;
5221
- this.updateStats();
5281
+ reset() {
5282
+ this.renderCounts.clear();
5283
+ this.prevFiberStates = /* @__PURE__ */ new WeakMap();
5222
5284
  }
5223
- /** Total render count */
5224
- totalRenders = 0;
5225
5285
  /**
5226
- * Update toolbar stats
5286
+ * Hook into React DevTools global hook
5227
5287
  */
5228
- updateStats() {
5229
- if (!this.shadowRoot) return;
5230
- let critical = 0;
5231
- let warning = 0;
5232
- let healthy = 0;
5233
- for (const [, count] of this.renderCounts) {
5234
- if (count > 50) critical++;
5235
- else if (count > 20) warning++;
5236
- else healthy++;
5288
+ hookIntoReact() {
5289
+ const hook = this.win.__REACT_DEVTOOLS_GLOBAL_HOOK__;
5290
+ if (!hook) {
5291
+ this.installHook();
5292
+ return;
5237
5293
  }
5238
- const criticalEl = this.shadowRoot.getElementById("rc-critical");
5239
- const warningEl = this.shadowRoot.getElementById("rc-warning");
5240
- const healthyEl = this.shadowRoot.getElementById("rc-healthy");
5241
- const rendersEl = this.shadowRoot.getElementById("rc-renders");
5242
- if (criticalEl) criticalEl.textContent = String(critical);
5243
- if (warningEl) warningEl.textContent = String(warning);
5244
- if (healthyEl) healthyEl.textContent = String(healthy);
5245
- if (rendersEl) rendersEl.textContent = String(this.totalRenders);
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 });
5246
5303
  }
5247
5304
  /**
5248
- * Clear all highlights
5305
+ * Install our own hook if DevTools hook is not present
5249
5306
  */
5250
- clearHighlights() {
5251
- for (const highlight of this.highlights.values()) {
5252
- highlight.remove();
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)" });
5324
+ }
5325
+ /**
5326
+ * Unhook from React
5327
+ */
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;
5253
5333
  }
5254
- this.highlights.clear();
5255
5334
  }
5256
5335
  /**
5257
- * Clear all badges
5336
+ * Detect React version from hook
5337
+ * @param hook - DevTools hook
5338
+ * @returns React version string
5258
5339
  */
5259
- clearBadges() {
5260
- for (const badge of this.badges.values()) {
5261
- badge.remove();
5340
+ detectReactVersion(hook) {
5341
+ if (hook.renderers.size === 0) {
5342
+ return "unknown";
5262
5343
  }
5263
- this.badges.clear();
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";
5264
5349
  }
5265
5350
  /**
5266
- * Clear all visualizations
5351
+ * Process a fiber commit
5352
+ * @param rootFiber - Root fiber of the committed tree
5267
5353
  */
5268
- clear() {
5269
- this.clearHighlights();
5270
- this.clearBadges();
5271
- this.renderCounts.clear();
5272
- this.totalRenders = 0;
5273
- this.updateStats();
5354
+ processCommit(rootFiber) {
5355
+ this.walkFiber(rootFiber, null);
5274
5356
  }
5275
5357
  /**
5276
- * Show the overlay
5358
+ * Walk the fiber tree
5359
+ * @param fiber - Current fiber
5360
+ * @param parent - Parent component name
5277
5361
  */
5278
- show() {
5279
- if (this.host) {
5280
- this.host.style.display = "block";
5281
- this.visible = true;
5362
+ walkFiber(fiber, parent) {
5363
+ if (!fiber) return;
5364
+ if (isUserComponent(fiber)) {
5365
+ this.processFiber(fiber, parent);
5366
+ parent = getComponentName(fiber);
5282
5367
  }
5368
+ this.walkFiber(fiber.child, parent);
5369
+ this.walkFiber(fiber.sibling, parent);
5283
5370
  }
5284
5371
  /**
5285
- * Hide the overlay
5372
+ * Process a single fiber
5373
+ * @param fiber - Fiber to process
5374
+ * @param parent - Parent component name
5286
5375
  */
5287
- hide() {
5288
- if (this.host) {
5289
- this.host.style.display = "none";
5290
- this.visible = false;
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
+ }
5291
5404
  }
5405
+ this.emit("render", renderInfo);
5292
5406
  }
5293
5407
  /**
5294
- * Toggle overlay visibility
5408
+ * Check if component should be filtered out
5409
+ * @param name - Component name
5410
+ * @returns true if should be filtered
5295
5411
  */
5296
- toggle() {
5297
- if (this.visible) {
5298
- this.hide();
5299
- } else {
5300
- this.show();
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;
5425
+ }
5426
+ return false;
5427
+ }
5428
+ /**
5429
+ * Simple glob pattern matching
5430
+ * @param name - Component name
5431
+ * @param pattern - Glob pattern
5432
+ * @returns true if matches
5433
+ */
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;
5301
5441
  }
5302
5442
  }
5443
+ /**
5444
+ * Get current configuration
5445
+ * @returns Scanner configuration
5446
+ */
5447
+ getConfig() {
5448
+ return { ...this.config };
5449
+ }
5303
5450
  /**
5304
5451
  * Update configuration
5305
5452
  * @param config - Partial configuration
@@ -5308,11 +5455,33 @@ var Overlay = class {
5308
5455
  this.config = { ...this.config, ...config };
5309
5456
  }
5310
5457
  /**
5311
- * Check if overlay is visible
5312
- * @returns true if visible
5458
+ * Check if scanner is running
5459
+ * @returns true if running
5313
5460
  */
5314
- isVisible() {
5315
- 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);
5316
5485
  }
5317
5486
  };
5318
5487
 
@@ -5459,8 +5628,42 @@ var ReactCheckInjector = class {
5459
5628
  case "config":
5460
5629
  this.handleConfig(message.payload);
5461
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");
5462
5651
  }
5463
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
+ }
5464
5667
  /**
5465
5668
  * Handle config update
5466
5669
  * @param config - New configuration