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

AppBar offset component when it's fixed #16844

Closed
2 tasks done
rhuanbarreto opened this issue Aug 1, 2019 · 9 comments · Fixed by #17896
Closed
2 tasks done

AppBar offset component when it's fixed #16844

rhuanbarreto opened this issue Aug 1, 2019 · 9 comments · Fixed by #17896
Labels
component: app bar This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. hacktoberfest https://hacktoberfest.digitalocean.com/

Comments

@rhuanbarreto
Copy link
Contributor

On material design react components repo they have a component that helps when the top bar is fixed:

Use the <TopAppBarFixedAdjust /> component to give your content top-padding, so it isn't hidden on page render.

I've made one based on material UI that helps, so you could integrate it.

import React from "react";
import { makeStyles } from "@material-ui/styles";

const useStyles = makeStyles(theme => ({
  offset: {
    ...theme.mixins.toolbar,
    flexGrow: 1
  }
}))

export default function AppBarOffset() {
  const classes = useStyles();
  return <div className={classes.offset} />;
}

Use as:

<AppBar position="fixed" />
<AppBarOffset />
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.
@oliviertassinari
Copy link
Member

@rhuanbarreto Do you have an example where the app bar fails to live up its expectations?

@rhuanbarreto
Copy link
Contributor Author

When the appbar is fixed, you always need this offset, so the content initially doesn't get under the bar. In material UI website main page this is made by a div with a class called jss3 where it adds a paddingTop of 64px. But this value is variable and depends on the viewport. So it's not straightforward to do this. So this offset must be set programatically.

Link for codesanbox showing the problem: https://codesandbox.io/s/material-demo-ejxmq. There is a text hidden under the bar.
Link for codesandbox with the solution using the code I used: https://codesandbox.io/s/material-demo-dn4mr

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 1, 2019

What's wrong with the Toolbar component?

import React from "react";
import {
  Box,
  CssBaseline,
  AppBar,
  Toolbar,
  Typography,
  Button,
  IconButton,
} from "@material-ui/core";
import MenuIcon from "@material-ui/icons/Menu";

export default function ButtonAppBar() {
  return (
    <div>
      <CssBaseline />
      <AppBar position="fixed">
        <Toolbar>
          <Box mr={2}>
            <IconButton edge="start" color="inherit" aria-label="menu">
              <MenuIcon />
            </IconButton>
          </Box>
          <Typography variant="h6">News</Typography>
          <Box flexGrow={1} />
          <Button color="inherit">Login</Button>
        </Toolbar>
      </AppBar>
      <Toolbar />
      Text to be shown.
    </div>
  );
}

https://codesandbox.io/s/material-demo-bnlpz

@rhuanbarreto
Copy link
Contributor Author

Great! Using the <Toolbar /> does the trick. So it's a documentation problem. This should be better documented, so people could know better how to implement fixed toolbars.

@oliviertassinari oliviertassinari added component: app bar This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation labels Aug 1, 2019
@oliviertassinari
Copy link
Member

I agree about the documentation aspect. It's the same pattern we use in these demos: https://material-ui.com/components/app-bar/#scrolling. What do you think of a ## Placement section on this page? The usage of Toolbar is one valid approach, it's not the only one. theme.mixins.toolbar can be useful too for people who don't want to create a new DOM node.

@oliviertassinari oliviertassinari added the good first issue Great for first contributions. Enable to learn the contribution process. label Aug 1, 2019
@rhuanbarreto
Copy link
Contributor Author

I think this could be good! This will probably help lot's of people once a fixed appbar is very common usage. And explaining all the ways to do this is the best that can be done.

@joshwooding joshwooding added the hacktoberfest https://hacktoberfest.digitalocean.com/ label Sep 30, 2019
@adeelibr
Copy link
Contributor

Can I document this feature for usage of & theme.mixins.toolbar for Appbar position fixed demo? 😅

@oliviertassinari
Copy link
Member

@adeelibr Yes, I think that it would be great to document the placement issue. For instance, I have seen @jlengstorf hit by this problem in a recent video.

@sami-lab
Copy link

How to use Two appbar in same page?
I have condition where i have one appbar on top
and other on bottom while content scroll between them.

The Proposed solution is only for one AppBar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: app bar This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. hacktoberfest https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants