Skip to content
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

Determine if child route or not (breadcrumbs + nav) #21

Open
smblee opened this issue Jun 24, 2022 · 2 comments
Open

Determine if child route or not (breadcrumbs + nav) #21

smblee opened this issue Jun 24, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@smblee
Copy link

smblee commented Jun 24, 2022

For nav/breadcrumb purpose, it would be really nice to reverse check if a given URL is

There's probably edge cases here to consider since breadcrumbs are complicated, but at the least if we can have an utility fn to:

  1. Given pathname, determine the actual route
  2. Given two routes (or two pathnames), check if one is a child of another.

The use case would be for a nav item to determine whether the item should be selected or not.

rough sketch of the contract:

url.pathname = '/settings/account';

isChildOf(AppRoute.settings(), url.pathname) => true
isChildOf(AppRoute.settings(), AppRoute.settings.account()) =>  true
isChildOf(AppRoute.settings(), AppRoute.settings.hello({ hey: 123 })) => true
isChildOf('/settings', AppRoute.settings.account()) => true
isChildOf('/settings', '/settings/account') => true

isChildOf(AppRoute.settings(), '/randompage') => false
...

Edit: on a second thought, this just seems like some notion of URL parsing and i am sure such libraries exist already.

Maybe something we can benefit from ts-route side is since we already have more definition than raw strings ("nested"-ness with children prop, various param + names, etc.) we could leverage that for something?

@smblee smblee changed the title Determine if child route or not Determine if child route or not (breadcrumbs + nav) Jun 24, 2022
@smblee
Copy link
Author

smblee commented Jun 24, 2022

const isChildOf = (maybeParentPathname: string, maybeChildPathname: string) => {
	// since / is a parent of all pathnames, do explicit matching.
	if (maybeParentPathname === '/') {
		return maybeParentPathname === maybeChildPathname;
	}
	return maybeChildPathname.startsWith(maybeParentPathname);
};

using this for now which works (for now) since our nav bar items are all at first level.

@alszczep alszczep added the enhancement New feature or request label May 22, 2023
@alszczep
Copy link

Hi, it looks like something that we should be able to integrate into the library. We'll soon check what can be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants