Javascript – How to apply transitions to full page in material-ui and keep AppBar fixed

cssjavascriptmaterial-uireactjs

How can I keep the material-ui AppBar fixed at top if it's wrapped together with page contents in a transition? Obviously, I'd like the AppBar to transition (e.g. Zoom or Slide) in with the rest of the page contents.

I first discovered this when trying to use the material-ui Dialog component in fullScreen mode. If the page contents has height enough to create a scrollbar, the AppBar in the Dialog scrolls off the page, even if it's position is set to fixed.

To reproduce issue, go here (https://codesandbox.io/s/2471jyxxkr) and click the OPEN FULLSCREEN DIALOG button, and then scroll down. Or alternatively go here (https://codesandbox.io/s/lrxv6w43y9) and scroll down. How do I fix the AppBar at top instead of scrolling?

One possible workaround would be to conditionally render an AppBar for the duration of the transition, but then hide that one and show one outside the transition after the transition has completed. But that seems like a hack.

I'd be happy to use an npm module that handles nice page transitions.

I found one attempt with a pre-v1 version of Material-Ui here (https://www.npmjs.com/package/material-ui-fullscreen-dialog-transition-fix), but I'd like something tha works with > v1, if possible.

Also this looks nice, but it says it does not fully support React Router v4.
https://github.com/trungdq88/react-router-page-transition (although maybe it supports it well enough https://github.com/trungdq88/react-router-page-transition#using-with-react-router-4)

See also https://github.com/mui-org/material-ui/issues/12096

Best Answer

You can use react-router to address this scenario:

as an example:

<BrowserRouter>
 <div className={classes.root}>
  <AppBar position="static" color="default">
    <Tabs
      value={this.state.value}
      onChange={this.handleChange}
      indicatorColor="primary"
      textColor="primary"
      fullWidth
    >
      <Tab label="Item One" component={Link} to="/one" />
      <Tab label="Item Two" component={Link} to="/two" />
    </Tabs>
  </AppBar>

  <Switch>
    <Route path="/one" component={PageShell(ItemOne)} />
    <Route path="/two" component={PageShell(ItemTwo)} />
  </Switch>
</div>

here appbar is fixed on top and other pages are changed as you try to route.

I have used react-addons-css-transition-group for transitions with routes and it is described in this article: https://blog.logrocket.com/routes-animation-transitions-in-react-router-v4-9f4788deb964

please find the animation index.css file in demo

I wrapped pages from a HOC to make routing easier ( PageShell componet)

here is a working example:https://codesandbox.io/s/04p1v46qww

hope this is what you are looking for.

Related Topic