Skip to content

AppBar disregards className prop - HideOnScroll is the reason #8142

Closed

Description

What you were expecting:

Adding a className prop to the AppBar should add the provided class to the actual DOM element.

What happened instead:
The provided class is not present in the root DOM element of AppBar. Consequently, the styled mui utility cannot be used with the default AppBar since the injected className prop is ignored.

Related code:
In the example below I create a StyledAppBar with the styled utility and use it in place of AppBar. The color of the AppBar should be black. Also, inspecting the DOM reveals that no class related to my styling is added to the root DOM element (<header>) of the AppBar.

https://codesandbox.io/s/dazzling-thunder-hdd1ed

// part of the code in sandbox
const MyAppBar = memo((props) => (
  <StyledAppBar {...props} userMenu={<MyUserMenu />} />
));

const StyledAppBar = styled(AppBar, { name: "MyAppBar" })({ // a class containing 'MyAppBar' string should appear in the root element of AppBar
  backgroundColor: "black !important"
});

Other information:

Inspecting the GitHub repo, I see that AppBar internally uses a wrapper container. That container is the root react element of the AppBar component and the one that receives the className prop (<Container className={className}>).

The container component though defaults to HideOnScroll, which, in turn, ignores and does not pass down the className property to its children elements

// HideOnScroll implementation
export const HideOnScroll = (props: HideOnScrollProps) => {
    const { children } = props;
    const trigger = useScrollTrigger();
    return (
        <Slide appear={false} direction="down" in={!trigger}> // no className used or passed down
            {children}
        </Slide>
    );
};

So the AppBar default implementation renders a <HideOnScrollclassName={props.className}> which ignores the provided className altogether.

Environment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions