React/Material-UIでheaderを実装④(colorを変更)


./components/ui/Header.js
import { AppBar, Toolbar } from "@material-ui/core";
import React from "react";
import useScrollTrigger from "@material-ui/core/useScrollTrigger";

function ElevationScroll(props) {
  const { children, window } = props;

  const trigger = useScrollTrigger({
    disableHysteresis: true,
    threshold: 0,
    target: window ? window() : undefined,
  });

  return React.cloneElement(children, {
    elevation: trigger ? 4 : 0,
  });
}

const Header = () => {
  return (
    <ElevationScroll>
      <AppBar color="primary">
        <Toolbar>Header</Toolbar>
      </AppBar>
    </ElevationScroll>
  );
};

export default Header;

./components/ui/Header.js
import { AppBar, Toolbar } from "@material-ui/core";
import React from "react";
import useScrollTrigger from "@material-ui/core/useScrollTrigger";

function ElevationScroll(props) {
  const { children, window } = props;

  const trigger = useScrollTrigger({
    disableHysteresis: true,
    threshold: 0,
    target: window ? window() : undefined,
  });

  return React.cloneElement(children, {
    elevation: trigger ? 4 : 0,
  });
}

const Header = () => {
  return (
    <ElevationScroll>
      <AppBar color="secondary">
        <Toolbar>Header</Toolbar>
      </AppBar>
    </ElevationScroll>
  );
};

export default Header;