Skip to content

fix(tabs): honor hash-linked nav tabs#12303

Open
tarunvashishth wants to merge 2 commits intopatternfly:mainfrom
tarunvashishth:tabs-should-work-with-url
Open

fix(tabs): honor hash-linked nav tabs#12303
tarunvashishth wants to merge 2 commits intopatternfly:mainfrom
tarunvashishth:tabs-should-work-with-url

Conversation

@tarunvashishth
Copy link
Copy Markdown

@tarunvashishth tarunvashishth commented Mar 28, 2026

Closes #12094

Summary by CodeRabbit

  • New Features
    • Navigation tabs now auto-select the active tab from the URL hash, letting users bookmark/share links to specific tab views; hidden or disabled tabs are ignored and non-nav tabs continue to ignore the hash.
  • Tests
    • Added tests covering hash-based selection for controlled and uncontrolled tabs, skipped hidden/disabled tabs, and non-nav behavior.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

Walkthrough

Derives an initial active tab from the URL hash for nav-style Tabs by parsing each tab's href hash and matching it to window.location.hash, skipping hidden/disabled tabs; introduces initialActiveKey and updates uncontrolled/controlled selection and click/update flows accordingly.

Changes

Cohort / File(s) Summary
Tabs component
packages/react-core/src/components/Tabs/Tabs.tsx
Added DOM-dependent helpers to parse tab href hashes and compute an initialActiveKey from window.location.hash when isNav/component="nav" is used; updated uncontrolled/controlled active-key initialization and logic to respect and clear initialActiveKey after user-driven selection.
Tabs tests
packages/react-core/src/components/Tabs/__tests__/Tabs.test.tsx
Added hash-based nav selection test suite: sets/clears window.location.hash, verifies hash-derived initial selection for nav tabs (controlled and uncontrolled), ensures hidden/disabled/aria-disabled tabs are skipped, and confirms non-nav Tabs ignore the hash. Updated React imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: enabling tabs to honor URL hash for navigation tab selection.
Linked Issues check ✅ Passed The PR implements hash-based tab selection for nav tabs, directly addressing issue #12094's requirement that tabs specified by URL fragment are selected on page load.
Out of Scope Changes check ✅ Passed All changes are scoped to implementing hash-based nav tab selection: DOM-dependent helpers, initial state logic, click/update control flow, and comprehensive tests covering controlled/uncontrolled scenarios.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@patternfly-build
Copy link
Copy Markdown
Collaborator

patternfly-build commented Mar 28, 2026

Copy link
Copy Markdown

@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: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/react-core/src/components/Tabs/Tabs.tsx`:
- Around line 194-198: Seed the uncontrolledActiveKey state using the
hash-derived key so initial selection matches shownKeys: set
uncontrolledActiveKey to hashActiveKey ?? this.props.defaultActiveKey (mirroring
shownKeys logic that uses hashActiveKey ?? ...). Update the initializer that
defines uncontrolledActiveKey in the Tabs component so it references
hashActiveKey, and ensure any later render-time hash derivation logic is removed
or not used to override user interactions after mount (preserve user changes to
uncontrolledActiveKey afterwards).
- Around line 145-156: Tests are missing for getTabHashActiveKey to verify
hash-based selection; add unit tests in the Tabs spec that set
window.location.hash (use a beforeEach to assign a hash and afterEach to clear
it) and assert getTabHashActiveKey (via rendering Tabs or importing the function
if exported) returns the expected eventKey when a child TabElement has matching
href, and does not return keys for hidden/disabled tabs or when component/isNav
conditions are unmet; target the getTabHashActiveKey logic and exercise
Tabs/TabsProps behavior with TabElement props (href, isHidden, isDisabled,
isAriaDisabled) so hash-based navigation is covered.
- Around line 559-560: The code currently recalculates hashActiveKey each render
(getTabHashActiveKey) and then uses it to set localActiveKey, which causes the
URL hash to override user clicks; instead, remove the per-render usage of
getTabHashActiveKey and seed uncontrolledActiveKey from the hash only on initial
mount/constructor (where you already compute it around line ~186), or use the
existing initial shownKeys/uncontrolledActiveKey initialization to incorporate
the hash-derived key; update Tabs constructor/initialization to set
uncontrolledActiveKey from getTabHashActiveKey once and ensure handleTabClick
updates uncontrolledActiveKey (not the URL hash) so subsequent renders respect
the user's selection rather than recomputing hashActiveKey each render.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3544653a-d87d-4a92-8b53-8011dd08fcc6

📥 Commits

Reviewing files that changed from the base of the PR and between e8f083e and 7e3c952.

📒 Files selected for processing (1)
  • packages/react-core/src/components/Tabs/Tabs.tsx

Comment on lines +145 to +156
const getTabHashActiveKey = ({ children, component, isNav }: Pick<TabsProps, 'children' | 'component' | 'isNav'>) => {
if (!canUseDOM || !(component === TabsComponent.nav || isNav) || !window.location.hash) {
return undefined;
}

return Children.toArray(children)
.filter((child): child is TabElement => isValidElement(child))
.filter(({ props }) => !props.isHidden)
.find(
({ props }) => !props.isDisabled && !props.isAriaDisabled && getHashFromHref(props.href) === window.location.hash
)?.props.eventKey;
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if Tabs tests have any hash-related setup
rg -n "location.hash|window.location" packages/react-core/src/components/Tabs/__tests__/

Repository: patternfly/patternfly-react

Length of output: 53


Add test coverage for the getTabHashActiveKey function with hash-based navigation.

The new getTabHashActiveKey function accesses window.location.hash and should be tested to ensure the hash-based tab selection works correctly and prevent regressions. Currently, no hash-related test setup exists in the Tabs tests, while the Nav component tests demonstrate the pattern with window.location.hash setup in a beforeEach block.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-core/src/components/Tabs/Tabs.tsx` around lines 145 - 156,
Tests are missing for getTabHashActiveKey to verify hash-based selection; add
unit tests in the Tabs spec that set window.location.hash (use a beforeEach to
assign a hash and afterEach to clear it) and assert getTabHashActiveKey (via
rendering Tabs or importing the function if exported) returns the expected
eventKey when a child TabElement has matching href, and does not return keys for
hidden/disabled tabs or when component/isNav conditions are unmet; target the
getTabHashActiveKey logic and exercise Tabs/TabsProps behavior with TabElement
props (href, isHidden, isDisabled, isAriaDisabled) so hash-based navigation is
covered.

Copy link
Copy Markdown

@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.

🧹 Nitpick comments (1)
packages/react-core/src/components/Tabs/__tests__/Tabs.test.tsx (1)

122-129: Restore previous hash instead of forcing empty cleanup.

Line 128 always sets window.location.hash = ''. Prefer restoring the pre-test value for better isolation if surrounding tests ever rely on a non-empty hash.

♻️ Suggested tweak
 describe('hash-based nav selection', () => {
+  let previousHash = '';
+
   beforeEach(() => {
+    previousHash = window.location.hash;
     window.location.hash = '#/items/2';
   });

   afterEach(() => {
-    window.location.hash = '';
+    window.location.hash = previousHash;
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-core/src/components/Tabs/__tests__/Tabs.test.tsx` around lines
122 - 129, The afterEach cleanup currently forces window.location.hash = ''
which can break other tests; capture the original hash in the beforeEach (e.g.,
store it in a local variable like originalHash) and in afterEach restore
window.location.hash = originalHash so the test suite returns the location state
to what it was; update the beforeEach/afterEach surrounding the
describe('hash-based nav selection') block to use that stored value.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/react-core/src/components/Tabs/__tests__/Tabs.test.tsx`:
- Around line 122-129: The afterEach cleanup currently forces
window.location.hash = '' which can break other tests; capture the original hash
in the beforeEach (e.g., store it in a local variable like originalHash) and in
afterEach restore window.location.hash = originalHash so the test suite returns
the location state to what it was; update the beforeEach/afterEach surrounding
the describe('hash-based nav selection') block to use that stored value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2c4b6d9f-e34c-4693-9b87-f4036d496206

📥 Commits

Reviewing files that changed from the base of the PR and between 7e3c952 and 9122887.

📒 Files selected for processing (2)
  • packages/react-core/src/components/Tabs/Tabs.tsx
  • packages/react-core/src/components/Tabs/__tests__/Tabs.test.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react-core/src/components/Tabs/Tabs.tsx

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.

Bug - Tabs - Tabs linked to nav elements does not highlight url from anchor

2 participants