solid-router icon indicating copy to clipboard operation
solid-router copied to clipboard

Allow useMatch take RouteDefinition object instead of path.

Open minht11 opened this issue 4 years ago • 1 comments

Basically I want to know which route is currently active outside of <Routes/>, so I can for example change nav bar styles on specific page, currently this is hard to do correctly. So my proposed solution is to allow useMatch take RouteDefinition object so this works:

const LIST_ROUTE = {
  path: '/list',
  component: List,
  children: [
    { path: '/' },
    { path: '/apples' },
    { path: '/oranges' },
    { path: '/banana' },
  ],
}

const routes: RouteDefinition[] = [
  {
    path: '/',
    component: Home,
  },
  LIST_ROUTE,
]

export const App = () => {
  const Routes = useRoutes(routes)
  const isListRoute = useMatch(LIST_ROUTE)

  return (
    <div>
      <Routes />
      <BottomNavBar hide={isListRoute()} />
    </div>
  )
}

Alternative today is to use

const isListRoute = useMatch(() => '/list/*') // Matches everything and is not a correct solution
// or way more complicated
const isListRoute = useMatch(() => '/list/')
const isListAppleRoute = useMatch(() => '/list/apple') 
const isListOrangeRoute = useMatch(() => '/list/orange') 
const isListBananaRoute = useMatch(() => '/list/banana') 
const isListRouteActive= createMemo(() => isListRoute() && isListAppleRoute() && isListOrangeRoute() && isListBananaRoute())

There might be some other way to solve this like having <Routes /> fire change event with matched route id.

minht11 avatar Aug 28 '21 09:08 minht11

I think that this API is a bit awkward and I'd probably prefer something like this

useMatch('/list', { matchChildren: true })
useMatch('/list', { matchChildren: true, matchChildrenOnly: true })

See also this issue https://github.com/solidjs/solid-router/issues/127 According to the original proposal it could be

useMatch(path`/list/${['apple', 'orange', 'banana']}`)

Or the way I'd prefer, something like

useMatch('/list/(apple|orange|banana)')

orenelbaum avatar Dec 07 '22 20:12 orenelbaum