-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NavLink Update for Active #504
base: master
Are you sure you want to change the base?
Conversation
…utomatically: - options for `active` are now `boolean | 'partial' | 'exact'` - partial will care if the pathname starts with the href - exact will display if the pathname is an exact match - true / false will disable the matching and set true and false respectively
Thanks @BSd3v This works great! Will target merging after dmc V1 is released (and after dash 3 is released). To do:
DocstringsI think the dosctring could be a little more clear. Perhaps something like:
I like the example included in the dbc docstring, but it would be better to include it in the docs instead:
Handle hash or query stringsDo you think there is a way to accommodate a link where the href includes a # or a query string? dmc.NavLink(label="page 1", href="/page-1#subject-1", active="exact"), Example AppHere's a good sample app for the release announcement and the docs. This demos nested links using the import dash
import dash_mantine_components as dmc
from dash import Dash, _dash_renderer, html
_dash_renderer._set_react_version("18.2.0")
app = Dash(external_stylesheets=dmc.styles.ALL, use_pages=True, pages_folder="")
dash.register_page("home", path="/", layout=html.Div("I'm home"))
dash.register_page("page1", path="/page-1", layout=html.Div("Info about page 1 subjects"))
dash.register_page("page1s1", path="/page-1/sub-1", layout=html.Div("page 1 subject 1"))
dash.register_page("page1s2", path="/page-1/sub-2", layout=html.Div("page 1 subject 2"))
component = dmc.Box([
dmc.NavLink(label="home", href="/", active="exact"),
dmc.NavLink(
label="Page 1",
childrenOffset=28,
href="/page-1",
active="partial",
children=[
dmc.NavLink(label="Subject 1", href="/page-1/sub-1", active="exact"),
dmc.NavLink(label="Subject 2", href="/page-1/sub-2", active="exact"),
],
),
dmc.Divider(mb="lg"),
dash.page_container
])
app.layout = dmc.MantineProvider([component])
if __name__ == "__main__":
app.run(debug=True)
|
… the url, this is an endswith example
const pathnameToActive = pathname => { | ||
setLinkActive( | ||
active === true || | ||
(active === 'exact' && pathname === href) || | ||
(active === 'partial' && pathname.startsWith(href)) || | ||
(active === 'hyperlink' && pathname.endsWith(href)) | ||
); | ||
}; | ||
|
||
const parsePath = (location) => { | ||
if (active === 'hyperlink') { | ||
pathnameToActive(location.href) | ||
} | ||
else { | ||
pathnameToActive(location.pathname) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the endsWith
option so it can pick up query stings and # for scrolling to a position on a page. What do you think about using different variable names to make it more clear?
setLinkActive(
active === true ||
(active === 'exact-path' && location.pathname === href) ||
(active === 'starts-with-path' && location.pathname.startsWith(href)) ||
(active === 'ends-with-url' && fullUrl.endsWith(href))
);
```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I think that would be fine.
Was more taking the lead from dbc on it.
adding support for
NavLink
to set active based upon page navigation automatically:active
are nowboolean | 'partial' | 'exact'
href
closes #365