default navbar: Handle case where multiple pages have same url segment

This commit is contained in:
Jakob Pinterits
2024-10-16 16:54:57 +02:00
parent 2091aedfdb
commit 43d0a3f695
3 changed files with 15 additions and 16 deletions

View File

@@ -147,15 +147,12 @@ class DefaultRootComponent(component.Component):
)
# Which page is currently active?
# Prepare the URL
segments = self.session.active_page_url.parts
# Special case: Not deep enough
if len(segments) <= 1:
# This won't match anything because it's not a valid URL segment"
current_page_url = "///"
else:
current_page_url = segments[1]
try:
active_page = self.session.active_page_instances[0]
except IndexError:
# Special case: No page is active. Use a value that won't match any
# page
active_page = None
# Add navigation
#
@@ -174,7 +171,7 @@ class DefaultRootComponent(component.Component):
pages.add(
NavButton(
page,
is_current=page.url_segment == current_page_url,
is_current=page is active_page,
)
)

View File

@@ -55,9 +55,10 @@ class Navbar(rio.Component):
# Which page is currently active? This will be used to highlight the
# correct navigation button.
#
# `active_page_instances` contains the same `rio.Page` instances
# that you've passed the app during creation. Since multiple pages
# can be active at a time (e.g. /foo/bar/baz), this is a list.
# `active_page_instances` contains `rio.ComponentPage` instances
# that that are created during app creation. Since multiple pages
# can be active at a time (e.g. /foo/bar/baz), this is a list rather
# than just a single page.
active_page = self.session.active_page_instances[1]
active_page_url_segment = active_page.url_segment
except IndexError:

View File

@@ -27,9 +27,10 @@ class Navbar(rio.Component):
# Which page is currently active? This will be used to highlight the
# correct navigation button.
#
# `active_page_instances` contains the same `rio.ComponentPage`
# instances that you've passed the app during creation. Since multiple
# pages can be active at a time (e.g. /foo/bar/baz), this is a list.
# `active_page_instances` contains `rio.ComponentPage` instances that
# that are created during app creation. Since multiple pages can be
# active at a time (e.g. /foo/bar/baz), this is a list rather than just
# a single page.
active_page = self.session.active_page_instances[0]
active_page_url_segment = active_page.url_segment