Skip to content

Conversation

@kaeizen
Copy link
Contributor

@kaeizen kaeizen commented Sep 18, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Smoother, more stable preview scaling that reduces flicker during tab changes and content updates.
    • Fewer unnecessary re-renders when a design is selected and the active tab remains the same.
    • More reliable layout behavior by deferring some scale adjustments until content is ready.
  • Documentation
    • Clarified scale reference width in comments (no functional change).

@coderabbitai
Copy link

coderabbitai bot commented Sep 18, 2025

Walkthrough

The hook adds a ref to track the previously selected tab, gates re-renders based on tab changes and selected design state, defers some scale adjustments via setTimeout, and updates the timing of when scale recalculation occurs after content updates. Comments note a 1300px reference width without changing the scale formula.

Changes

Cohort / File(s) Summary of changes
Preview renderer control flow and timing
src/components/design-library-list/use-preview-renderer.js
Added prevSelectedTabRef to track prior tab. Adjusted adjustScale guard to recalc when no design or tab changed. Re-render gating to skip when design selected and tab unchanged. Deferred scale recalculation after content updates using setTimeout(..., 100). Updated comment about 1300px reference width.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User as User/UI
  participant Hook as usePreviewRenderer
  participant Scale as adjustScale
  participant DOM as Layout/DOM

  User->>Hook: Select tab / update content
  Note over Hook: Track previous tab via prevSelectedTabRef

  alt No design selected OR tab changed
    Hook->>Scale: Invoke adjustScale()
    Scale->>DOM: Read sizes, compute scale (ref=1300)
    DOM-->>Hook: Layout metrics
    Hook-->>User: Render scaled preview
  else Design selected AND tab unchanged
    Hook-->>User: Skip re-render
  end

  Note over Hook: On content update
  Hook->>Hook: setTimeout(adjustScale, 100)
  Hook->>Hook: After run, set prevSelectedTabRef = selectedTab
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nudge the tabs, then pause to see,
A hundred ticks for layout’s plea.
With gentle scale, I softly render,
Remembering which tab was sender.
Hop, hop—async through the view,
A rabbit queues the perfect skew. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "fix (design library): preview of selected design when switching tabs" succinctly and accurately describes the primary change: fixing preview behavior when changing tabs in the design library; the diff targets use-preview-renderer.js to adjust re-render gating and scale recalculation around tab/selection changes, which aligns with the title.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/design-library-selected-design-switching-tabs

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

🤖 Pull request artifacts

file commit
pr3594-stackable-3594-merge.zip 75bcfc8

github-actions bot added a commit that referenced this pull request Sep 18, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/components/design-library-list/use-preview-renderer.js (4)

73-76: Tighten guard logic; avoid falsy traps and detached nodes.

Use explicit “not selected” semantics and ensure the node is connected.

-const shouldAdjust = ref.current && shadowRoot &&
-  ( ! selectedNum || // adjust if design is not selected
-    prevSelectedTabRef.current !== selectedTab ) // adjust if selected tab changed even if design is selected
+const shouldAdjust = ref.current?.isConnected && shadowRoot &&
+  ( selectedNum === 0 || // adjust if design is not selected
+    prevSelectedTabRef.current !== selectedTab ) // adjust if tab changed even if a design is selected

85-85: Replace magic number (1300) with a named constant.

Improves readability and single‑source of truth.

-const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 	// Divide by 1300, which is the width of preview in the shadow DOM
+const scaleFactor = cardWidth > 0 ? cardWidth / SHADOW_PREVIEW_WIDTH : 1 // Width of preview in the shadow DOM

Add once near the top (outside this hunk):

const SHADOW_PREVIEW_WIDTH = 1300

275-278: Timeout has no cleanup; prevent setState on unmounted and rapid re-schedules.

Store/clear the timer.

-      setTimeout( adjustScale, 100 )
+      if ( unselectAdjustTimeoutRef.current ) {
+        clearTimeout( unselectAdjustTimeoutRef.current )
+      }
+      unselectAdjustTimeoutRef.current = setTimeout( adjustScale, 100 )

Add outside this hunk:

// at top-level with other refs
const unselectAdjustTimeoutRef = useRef(null)

// one-time cleanup
useEffect(() => () => {
  if (unselectAdjustTimeoutRef.current) clearTimeout(unselectAdjustTimeoutRef.current)
}, [])

297-301: Same: defer with cleanup; consider rAF for layout-timing.

Prevents duplicate queued work in StrictMode and fast tab switches.

-    setTimeout( () => {
-      adjustScale()
-      prevSelectedTabRef.current = selectedTab
-    }, 100 )
+    if ( contentAdjustTimeoutRef.current ) {
+      clearTimeout( contentAdjustTimeoutRef.current )
+    }
+    contentAdjustTimeoutRef.current = setTimeout( () => {
+      adjustScale()
+      prevSelectedTabRef.current = selectedTab
+    }, 100 )

Add outside this hunk:

const contentAdjustTimeoutRef = useRef(null)

useEffect(() => () => {
  if (contentAdjustTimeoutRef.current) clearTimeout(contentAdjustTimeoutRef.current)
}, [])

// Optional: swap setTimeout for rAF for layout coherence
// contentAdjustTimeoutRef.current = requestAnimationFrame(() => { ... })
// and cancel via cancelAnimationFrame(...)

Also consider applying the same cleanup pattern to the setTimeout(() => setCardHeight(...), 500) inside adjustScale.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff366d7 and 75bcfc8.

📒 Files selected for processing (1)
  • src/components/design-library-list/use-preview-renderer.js (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/components/design-library-list/use-preview-renderer.js (2)
src/components/modal-design-library/modal.js (1)
  • selectedTab (62-62)
src/components/design-library-list/index.js (2)
  • previewSize (106-106)
  • cardHeight (105-105)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: PHP 8.2 and WP 6.6.2
  • GitHub Check: PHP 8.2 and WP 6.5.5
  • GitHub Check: PHP 7.3 and WP latest
  • GitHub Check: PHP 8.2 and WP 6.7.2
  • GitHub Check: PHP 8.2 and WP latest
  • GitHub Check: PHP 7.3 and WP 6.5.5
  • GitHub Check: build
🔇 Additional comments (2)
src/components/design-library-list/use-preview-renderer.js (2)

65-65: Good: track previous tab with a ref.

This enables clean gating tied to tab transitions.


262-266: Effect gating could miss tab-only changes; consider adding selectedTab to deps.

If a tab switch doesn’t change template/content, this effect won’t run. The guard already prevents unnecessary work.

-}, [ content, containerScheme, backgroundScheme, enableBackground ] )
+}, [ content, containerScheme, backgroundScheme, enableBackground, selectedTab ] )

Would you confirm whether template always changes on tab switch in all entry points? If not, the above is safer.

@bfintal bfintal merged commit c6aad06 into develop Sep 18, 2025
8 of 9 checks passed
@bfintal bfintal deleted the fix/design-library-selected-design-switching-tabs branch September 18, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants