In a multi-page Dash app I created a Navbar
dynamically based on the pages that the app has
def navbar():
return dbc.Navbar(
dbc.Container(
[
dbc.NavbarToggler(id='navbar-toggler', n_clicks=0),
dbc.Collapse(
dbc.Nav(
[
dbc.NavItem(
dbc.NavLink(
page['title'],
id=page["relative_path"].split("/")[-1],
href=page["relative_path"]
)
) for page in dash.page_registry.values()
],
),
id='navbar-collapse',
navbar=True
),
]
),
)
The Navbar
automatically collapses on small screens so I added the following callback
@callback(
Output('navbar-collapse', 'is_open'),
Input('navbar-toggler', 'n_clicks'),
State('navbar-collapse', 'is_open'),
)
def toggle_navbar_collapse(n, is_open):
if n:
return not is_open
return is_open
Now, in order to save one click on small screens, I’d like to collapse the Navbar
whenever one of the Navlink
is pressed. I thought of using pattern matching callbacks, however I’m not sure how to adapt one of the examples in that page. I tried to do something like this
@callback(
Output('navbar-collapse', 'is_open', allow_duplicate=True),
Input({'type': 'navbar-link', 'index': ALL}, 'n_clicks'),
State('navbar-collapse', 'is_open'),
prevent_initial_call=True
)
def toggle_navbar_collapse_from_navlink(navlink_clicks, is_open):
if is_open:
return False
but this is never triggered.
I just want to trigger this callback every time any of the NavLink
is pressed without having to write an individual callback per NavLink
, obviously.