Skip to content

fix(CNavGroup): retain nested subgroup state when reopening a collapsed parent#460

Merged
mrholek merged 1 commit into
mainfrom
fix/nav-group-nested-subgroup-state
Jun 11, 2026
Merged

fix(CNavGroup): retain nested subgroup state when reopening a collapsed parent#460
mrholek merged 1 commit into
mainfrom
fix/nav-group-nested-subgroup-state

Conversation

@mrholek

@mrholek mrholek commented Jun 11, 2026

Copy link
Copy Markdown
Member

Problem

When a CNavGroup that contains nested sub-groups is collapsed and then re-opened, all of its sub-groups appear closed — their previous open state is lost. This diverges from the vanilla @coreui/coreui navigation (the source of truth) and from @coreui/vue, where the nested sub-groups stay open on reopen.

Root cause

CSidebarNav tracked a single global openChain (the path of ids from the root to the deepest open group) and each CNavGroup derived its visibility from a prefix match against it. Collapsing a parent truncated openChain to the parent's chain, discarding all descendant ids; reopening set it back to just the parent's own chain — so the descendants were forgotten.

A single open path simply cannot represent "parent collapsed, but its children remembered as open".

Fix

Replace the single-path model with a per-level accordion, mirroring vanilla (groupsAutoCollapse) and the Vue implementation:

  • CNavGroupContext now carries { activeId, setActiveId, openBranch } for the current level instead of an ancestor-id chain.
  • CSidebarNav provides the root-level accordion state.
  • Each CNavGroup keeps its own activeChildId for its children. That state lives on the always-mounted component, so collapsing an ancestor never discards it — reopening restores whichever child was open.
  • Opening a group still collapses its siblings (accordion), and controlled mode (visible + onVisibleChange) is unchanged.
  • CNavLink opens its whole ancestor branch via openBranch() when it becomes active (replaces the old setOpenChain(parentChain)).
  • Removed the now-unused internal CSidebarNavContext (not part of the public API).

Tests

  • New: reopening a parent restores the open state of its nested subgroups.
  • New: opening an uncontrolled group collapses its sibling.
  • All existing CNavGroup tests (controlled, locked, active-link-opens-parent, nested default-open) pass unchanged; snapshots unchanged (markup is identical).

No prop changes, so the API tables are unaffected.

@mrholek mrholek merged commit cd08b97 into main Jun 11, 2026
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.

1 participant