Skip to content

fix(BTabs): correct id/index on ssr #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

xvaara
Copy link
Contributor

@xvaara xvaara commented Jun 3, 2025

closes #2723

Describe the PR

A clear and concise description of what the pull request does.

Small replication

A small replication or video walkthrough can help demonstrate the changes made. This is optional, but can help observe the intended changes. A mentioned issue that contains a replication also works.

PR checklist

What kind of change does this PR introduce? (check at least one)

  • Bugfix 🐛 - fix(...)
  • Feature - feat(...)
  • ARIA accessibility - fix(...)
  • Documentation update - docs(...)
  • Other (please describe)

The PR fulfills these requirements:

  • Pull request title and all commits follow the Conventional Commits convention or has an override in this pull request body This is very important, as the CHANGELOG is generated from these messages, and determines the next version type. Pull requests that do not follow conventional commits or do not have an override will be denied

Summary by CodeRabbit

  • New Features

    • Improved handling of disabled tabs, ensuring they are correctly reflected in the tab navigation.
    • Enhanced tab ID management for more consistent and stable tab identification, even before registration.
    • Refined logic for selecting the initial active tab, prioritizing non-disabled and explicitly active tabs.
  • Bug Fixes

    • Tabs now maintain correct order and registration, improving reliability when tabs are dynamically updated.

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

coderabbitai bot commented Jun 3, 2025

Walkthrough

This change refactors tab ID generation and synchronization in the BTabs and BTab components. It updates the registerTab function to return tab IDs, improves initial tab activation logic to avoid delays, and adds better handling for disabled tabs. The changes also ensure tab IDs remain consistent and synchronized during registration.

Changes

Files/Groups Change Summary
packages/bootstrap-vue-next/src/components/BTabs/BTab.vue Refactored tab pane ID logic to use a computed property prioritizing props.id, localId, or fallback. Introduced localId ref and updated registration to synchronize with parent ID.
packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue Enhanced disabled tab handling, improved tab ID management before registration, refined active tab selection, updated registerTab to return tab ID, and changed lifecycle hook to onMounted.
packages/bootstrap-vue-next/src/utils/keys.ts Updated registerTab type signature in tabsInjectionKey to return a string (tab ID) instead of void.

Sequence Diagram(s)

sequenceDiagram
    participant Parent as BTabs
    participant Child as BTab

    Child->>Parent: registerTab(tabRef)
    Parent->>Child: return tabId
    Child->>Child: Update localId if tabId differs
    Parent->>Parent: Sort tabs if initialized
    Parent->>Parent: Determine initial active tab (skip disabled, respect explicit active)
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure the first tab is open by default if no id/index is defined, avoiding nextTick delay (#2723)
Improve SSR output and prevent fade transition on first load due to missing tab content (#2723)

Possibly related PRs

  • #2720: Also modifies registerTab and tab registration logic, focusing on initialization and sorting.
  • #2716: Changes tab ID usage for modelValue and v-model:index, affecting tab registration and active tab tracking.
  • #2664: Alters tab registration timing and sorting, related to tab registration processes.

Suggested reviewers

  • VividLemon

Poem

A tabby tale of IDs anew,
Where tabs now know just what to do.
No more delays, the first tab’s here,
With smoother fades and logic clear.
🐇✨
The tabs all cheer, “Our IDs align!”
Thanks to this code, they work just fine.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 41dddcd and 22c6139.

📒 Files selected for processing (3)
  • packages/bootstrap-vue-next/src/components/BTabs/BTab.vue (3 hunks)
  • packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue (6 hunks)
  • packages/bootstrap-vue-next/src/utils/keys.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
packages/bootstrap-vue-next/src/components/BTabs/BTab.vue (2)
Learnt from: xvaara
PR: bootstrap-vue-next/bootstrap-vue-next#2716
File: packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:384-404
Timestamp: 2025-05-28T07:57:19.915Z
Learning: In BTabs component (packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue), the activeIndex and activeId watchers are intentionally designed with separation of concerns: activeIndex watcher handles activate-tab event emission and complex validation logic, while activeId watcher is kept simple for synchronization between activeId and activeIndex values only.
Learnt from: xvaara
PR: bootstrap-vue-next/bootstrap-vue-next#2716
File: packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:240-261
Timestamp: 2025-05-28T07:01:55.095Z
Learning: In BTabs component (packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue), the complex initialization logic with updateInitialActiveIndex and updateInitialActiveId flags is necessary for SSR compatibility. Tab initialization cannot be deferred to mounted lifecycle because tabs must be available for server-side rendering of the initial HTML state.
packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue (3)
Learnt from: xvaara
PR: bootstrap-vue-next/bootstrap-vue-next#2716
File: packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:240-261
Timestamp: 2025-05-28T07:01:55.095Z
Learning: In BTabs component (packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue), the complex initialization logic with updateInitialActiveIndex and updateInitialActiveId flags is necessary for SSR compatibility. Tab initialization cannot be deferred to mounted lifecycle because tabs must be available for server-side rendering of the initial HTML state.
Learnt from: xvaara
PR: bootstrap-vue-next/bootstrap-vue-next#2716
File: packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:425-430
Timestamp: 2025-05-28T07:32:45.658Z
Learning: In BTabs component (packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue), the activeIndex synchronization within sortTabs() is necessary and correct. When sorting tabs by DOM position, indices change, so activeIndex must be updated immediately to maintain consistency with activeId. This is not an unwanted side effect but a required consequence of the sorting operation.
Learnt from: xvaara
PR: bootstrap-vue-next/bootstrap-vue-next#2716
File: packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:384-404
Timestamp: 2025-05-28T07:57:19.915Z
Learning: In BTabs component (packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue), the activeIndex and activeId watchers are intentionally designed with separation of concerns: activeIndex watcher handles activate-tab event emission and complex validation logic, while activeId watcher is kept simple for synchronization between activeId and activeIndex values only.
🧬 Code Graph Analysis (1)
packages/bootstrap-vue-next/src/utils/keys.ts (1)
packages/bootstrap-vue-next/src/types/Tab.ts (1)
  • TabType (4-18)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (8)
packages/bootstrap-vue-next/src/utils/keys.ts (1)

58-58: LGTM! Type signature update aligns with implementation.

The change from void to string return type correctly reflects that registerTab now returns the tab's ID, which is necessary for the SSR id/index synchronization fix.

packages/bootstrap-vue-next/src/components/BTabs/BTab.vue (2)

56-58: Good improvement to tab ID management for SSR consistency.

The new ID priority chain (props.idlocalIdinternalId) ensures that tabs maintain consistent IDs across SSR and client hydration. The separation of internalId generation is cleaner.


73-94: Excellent synchronization of tab IDs with parent component.

Capturing the returned ID from registerTab and updating localId ensures that the tab's ID stays synchronized with the parent's registration. This is crucial for fixing the SSR id/index mismatch issue.

packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue (5)

45-45: Good accessibility improvement.

Adding the disabled attribute to the button element ensures proper HTML semantics and improves accessibility for screen readers.


162-179: Excellent solution for SSR tab ID consistency.

Pre-generating tab IDs using useId before registration ensures that tabs have stable IDs during SSR. The isChildActive tracking helps determine the correct initial active tab based on child props.


266-276: Improved initial active tab selection logic.

The logic now correctly:

  1. Respects child components with explicit active props
  2. Skips disabled tabs when selecting the initial active tab
  3. Handles both cases where child active state is defined vs. undefined

This prevents disabled tabs from being initially active.


469-474: Smart optimization for tab synchronization.

Returning the tab ID from registerTab and triggering immediate sorting when initialized improves responsiveness. The fallback to initialIds ensures consistent IDs during the initialization phase.


477-477: Better lifecycle hook choice for initialization.

Using onMounted instead of nextTick provides more predictable timing for the initialization sequence, ensuring DOM elements are ready before sorting tabs.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@xvaara xvaara requested review from Copilot, dwgray and VividLemon and removed request for dwgray June 3, 2025 12:47
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes SSR issues in BTabs by correcting id/index assignment and improving the tab registration flow.

  • Updated the registerTab signature and usage to return a string id.
  • Modified tab id fallback logic and active tab selection in BTabs.vue.
  • Adjusted BTabs and BTab component logic to sync computed ids with registered ids.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/bootstrap-vue-next/src/utils/keys.ts Updated registerTab signature to return a string id.
packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue Improved SSR id/index handling and active tab resolution logic.
packages/bootstrap-vue-next/src/components/BTabs/BTab.vue Updated computed id and registration logic to align with new tab id assignment.
Comments suppressed due to low confidence (1)

packages/bootstrap-vue-next/src/components/BTabs/BTabs.vue:473

  • The variable name 'idx2' is ambiguous; consider renaming it to a more descriptive name such as 'matchingTabIndex'.
const idx2 = tabsInternal.value.findIndex((t) => t.value.internalId === tab.value.internalId)

} else if (activeIndex.value === -1 && !activeId.value) {
activeIndex.value = 0
updateInitialActiveId = true
} else if (activeIndex.value === -1 && !activeId.value && !isChildActive.value) {
Copy link
Preview

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

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

The multiple else-if conditions for determining the active tab make the control flow hard to follow; consider refactoring or adding inline comments to clarify the logic.

Copilot uses AI. Check for mistakes.

Copy link

pkg-pr-new bot commented Jun 3, 2025

bsvn-vite-ts

npm i https://pkg.pr.new/bootstrap-vue-next/bootstrap-vue-next@2726
npm i https://pkg.pr.new/bootstrap-vue-next/bootstrap-vue-next/@bootstrap-vue-next/nuxt@2726

commit: 22c6139

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.

BTabs don't have a first tab open until the "nextTick", causing the fade transition to trigger.
1 participant