Replies: 16 comments 14 replies
-
I personally feel that the way the modal pattern is handled in react-router is basically a very inconvenient patch-job. It's a very common pattern for modals/drawers/etc to be visible at the same time as originating content. It was possible to do this with v5 relatively easy. Migrating to v6 isn't easy to begin with. The modal pattern deserves proper attention. // react-router@5
<Route path={path}>
<Content />
<Route path={`${path}/new`}><Overlay /></Route>
</Route> |
Beta Was this translation helpful? Give feedback.
-
This took some times to figure out, hope it will help someone. To get basic modal functionality in v6 you can do something like this: <Routes>
<Route
path=":id/grants/*"
element={
<>
<Content />
<Routes>
<Route path="new" element={<Overlay />} />
</Routes>
</>
}
/>
</Routes> The modal example assumes you want to display modal content full screen when navigating directly to the route. IMO this isn't a typical modal pattern. If I open a side drawer and want to share the link, I don't want that drawer to be full screen when my friend navigates to my link. |
Beta Was this translation helpful? Give feedback.
-
Modals don't have to be fullscreen. I started with the example in an application I'm working on it's working as you'd expect (not full screen). The CSS sets the dimensions and any open space displays the background route. |
Beta Was this translation helpful? Give feedback.
-
@ZachMayry by "full screen" i didn't mean visually full screen. In the example if you navigate directly to |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion so it can go through our new Open Development process. Please upvote the new Proposal if you'd like to see this considered! |
Beta Was this translation helpful? Give feedback.
-
I'm currently using two different sets of routes on my app. They are rendered under a
Now I'm looking to replace that structure with a data-router using |
Beta Was this translation helpful? Give feedback.
-
surely for data routes to be fully functional this needs to be possible? |
Beta Was this translation helpful? Give feedback.
-
I found this example using createBrowserRouter: https://stackblitz.com/edit/github-9lelz2-tdcovz?file=src%2Fmain.tsx. <RouterProvider router={router} /> acts like <BrowserRouter>
<Routes>...</Routes>
</BrowserRouter> in the official example, {state?.backgroundLocation && (
<Routes>
<Route path="/img/:id" element={<Modal />} />
</Routes>
)} is inserted into before </BrowserRouter>. |
Beta Was this translation helpful? Give feedback.
-
I have stumbled into the same issue, using
Passing the location state on the Link element conditionally. |
Beta Was this translation helpful? Give feedback.
-
Is there any clean way to have modal routes with data route API? |
Beta Was this translation helpful? Give feedback.
-
Anyone figured out a way to make this work? |
Beta Was this translation helpful? Give feedback.
-
after much wandering through the documentation, i came to a solution using matchRouters and renderMatches utils functions, but it looks more like bruteforce than a good solution. export const renderRoute = (
routes: RouteObject[],
location: string | Partial<Location>
): React.ReactElement | null => {
const route = matchRoutes(routes, location);
const backgroundPage = renderMatches(route);
return backgroundPage;
}; i keep two router objects (one for modals and one for other routes). export const routes = createRoutesFromElements(
<Route path="/" element={<RootLayout />}>
<Route element={<PagesLayout />}>
<Route index element={<MainPage />} />
<Route path="blogs" element={<BlogsPage />} />
<Route path="blog/:id" element={<BlogPage />} />
<Route path="createPost" element={<CreatePostPage />} />
<Route path="user/:id" element={<UserPage />} />
</Route>
<Route element={<AuthLayout />}>
<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
</Route>
</Route>
);
export const modalsRoutes = createRoutesFromElements(
<Route path="/">
<Route
path="user/:id"
element={
<Modal isModalOpened>
<p>Modal With User</p>
</Modal>
}
/>
<Route
path="login"
element={
<Modal isModalOpened>
<p>Must be logged in</p>
</Modal>
}
/>
</Route>
); then in root layout component function RootLayout() {
const location = useLocation();
if (location.state?.background) {
const backgroundPage = renderRoute(
routes,
location.state.background
);
const modal = renderRoute(modalsRoutes, location.pathname);
return (
<div className={styles.content}>
{backgroundPage}
{modal} {/** portal */}
</div>
);
}
return (
<div className={styles.content}>
<Outlet />
</div>
);
} I haven't done any data router features testing, this solution was made only with the goal of recreating the old functionality of modal routes |
Beta Was this translation helpful? Give feedback.
-
Two ways to make it https://devsday.ru/blog/details/114436 |
Beta Was this translation helpful? Give feedback.
-
have been struggling with the same i want to overlay the modal over the content which is rendered using a context but, the moment modal comes up on a route the outlet data goes off any suggestions or patterns how to fix this i am using routerProvider with createbrowserRouter |
Beta Was this translation helpful? Give feedback.
-
At Unsplash this issue is preventing us from migrating to the new data router. @brophdawg11 This discussion has a lot of upvotes now. What's your stance on this issue? Is this something React Router plans to support again? |
Beta Was this translation helpful? Give feedback.
-
Would also appreciate an update on this, it's an old issue and the 7th most upvoted discussion, so clearly there's interest. It sucks that we have to choose between data router or modal routes. |
Beta Was this translation helpful? Give feedback.
-
What is the new or updated feature that you are suggesting?
Somehow add support for a background location with routes
At first glance I'm not understanding how this modal example would work with the new data routers.
https://github.com/remix-run/react-router/blob/dev/examples/modal/src/App.tsx#L56-L70
If this feature is supported, it would be nice to have the modal example updated
Why should this feature be included?
Allowing modal routes to be on top of background location routes is a feature our application is built on but we would like to use the new data loading features as well.
It would be nice to not have to choose data loading or background locations for modals.
Beta Was this translation helpful? Give feedback.
All reactions