Skip to content

fix(nav): allow out-of-bounds or unset activeIndex without throwing - #154

Merged
treeder merged 2 commits into
mainfrom
fix/nav-bar-active-index
Oct 30, 2026
Merged

treeder merged 2 commits into
mainfrom
fix/nav-bar-active-index

Conversation

@treeder

@treeder treeder commented Oct 30, 2026

Copy link
Copy Markdown
Member

Summary

Fixes Uncaught (in promise) Error: NavigationBar: activeIndex is out of bounds. when md-nav-bar or md-nav-rail has no active tab (e.g., active-index="-1" or an empty/unset active-index attribute) or updates before slotted tab items are populated.

Key Changes

  • Remove the out-of-bounds error throw in NavigationBar.onActiveIndexChange so that out-of-bounds or negative activeIndex values cleanly mark all tabs inactive.
  • Default activeIndex to -1 and add an attribute converter so empty/missing/non-numeric active-index values (such as active-index="" when interpolating undefined) resolve to -1 instead of 0 or NaN.
  • Add tabs to NavigationBar.properties and attach @slotchange to keep slotted tab items synchronized in both NavigationBar and Rail.
  • Add safe optional chaining in handleKeydown and tabsElement.

Verification

  • Verified onActiveIndexChange with -1, empty active-index, and valid indices 0..n on md-nav-bar and md-nav-rail.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-nav-bar-active-index-material.treeder.workers.dev (commit f7db644)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://79234a63-material.treeder.workers.dev f7db644 2026-10-30T03:02:21.495Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://ce323f75-material.treeder.workers.dev f8abf2d 2026-10-30T01:20:34.138Z Visit the dashboard ↗

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This pull request updates NavigationBar (md-nav-bar) and Rail (md-nav-rail) to support an unselected state by changing the default activeIndex from 0 to -1. It introduces a custom attribute converter to parse active-index, eliminates the runtime error thrown when activeIndex is out of bounds, and adds defensive guards and optional chaining to keyboard navigation handlers. In addition, slot change listeners are connected to trigger layout recalculation dynamically, and the documentation has been kept in sync with the new default.

🔍 General Feedback

  • Removing the unhandled exception in onActiveIndexChange when this.tabs[value] is undefined is a great fix; previously, empty tab arrays or negative indices would immediately crash during component lifecycle execution.
  • The defensive checks and optional chaining (?.focus()) added to handleKeydown significantly improve resilience when focus is outside the tabs or when the bar contains no items.
  • The documentation updates in nav/README.md accurately document -1 as the new default for unselected states.
📊 Token Usage & Cost Efficiency
Metric Value
Input Tokens (uncached) 16,466
Input Tokens (cached) 434,764 (⚡ 96.2% cached)
PR Comments History Tokens 532
Thinking / Reasoning Tokens 32,385
Output Tokens 688
Total Session Tokens 484,835
Cost (uncached input) $0.0127
Cost (cached input) $0.0326
Cost (thinking / reasoning) $0.1214
Cost (response output) $0.0026
Estimated Total Cost $0.1694

Gemini 3.8 Flash: introductory rate $0.75/$3.75 per 1M applied; reverts to $1.5/$7.5 after 2026-12-31.
Gemini 3.8 Flash: standard tier; batch and flex are half again, priority is higher.
Cache reads are priced here, but context-cache STORAGE, where the model charges for it separately per token-hour, is not included, so the figure can run slightly low.

Comment thread nav/bar.js Resolved
Comment thread nav/bar.js Resolved

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This pull request refactors and fixes active index handling in <md-nav-bar> and <md-nav-rail>. It introduces a default activeIndex of -1 (indicating no active selection) with a custom attribute converter, safeguards navigation event dispatching, and removes the out-of-bounds error throw in onActiveIndexChange. In addition, child tab management is streamlined across <md-nav-bar> and <md-nav-rail> with slotchange listeners and shared reactive property definitions, and documentation tables in nav/README.md have been updated accordingly.

✅ Resolved Items from Prior Reviews

  • Converted tabs in NavigationBar.properties to internal reactive state (state: true) and cleaned up redundant property re-declarations in Rail to prevent exposing tabs as an observed HTML attribute. (nav/bar.js:20)
  • Added a guard (this.activeIndex >= 0 && this.tabs[this.activeIndex]) to the navigation-bar-activated event dispatch in updated() so it only fires when an active destination item exists. (nav/bar.js:24)

🔍 General Feedback

  • Defaulting activeIndex to -1 and using activeIndexConverter provides a clean way to represent an unselected state across navigation bars and rails.
  • Removing the out-of-bounds Error throw in onActiveIndexChange prevents runtime crashes when tabs are dynamically populated or cleared.
  • Spreading ...NavigationBar.properties in Rail nicely eliminates property duplication while keeping reactive configurations in sync.
📊 Token Usage & Cost Efficiency
Metric Value
Input Tokens (uncached) 16,849
Input Tokens (cached) 434,764 (⚡ 96.1% cached)
PR Comments History Tokens 978
Thinking / Reasoning Tokens 22,564
Output Tokens 406
Total Session Tokens 475,561
Cost (uncached input) $0.0134
Cost (cached input) $0.0326
Cost (thinking / reasoning) $0.0846
Cost (response output) $0.0015
Estimated Total Cost $0.1321

Gemini 3.8 Flash: introductory rate $0.75/$3.75 per 1M applied; reverts to $1.5/$7.5 after 2026-12-31.
Gemini 3.8 Flash: standard tier; batch and flex are half again, priority is higher.
Cache reads are priced here, but context-cache STORAGE, where the model charges for it separately per token-hour, is not included, so the figure can run slightly low.

@treeder
treeder merged commit dc30b3f into main Oct 30, 2026
2 checks passed
@treeder
treeder deleted the fix/nav-bar-active-index branch October 30, 2026 03:17
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