Skip to main content

8.33 Reusing Backdrop

Fix (add letter s)

SideDrawer.module.css

SideDrawer.module.css
.SideDrawer {
position: fixed;
width: 28px;
min-width: 280px;
height: 100%;
left: 0;
top: 0;
z-index: 200;
background-color: #fff;
padding: 32px 16px;
box-sizing: border-box;
transition: transform 0.3s ease-out;
}

@media (min-width: 500px) {
.SideDrawer {
display: none;
}
}

.Open {
transform: translateX(0);
}

.Close {
transform: translateX(-100%);
}

.Logo {
height: 11%;
margin-bottom: 32px;
}

Converting functional to class and editing

Layout.js

Layout.js
import React, { Component, Fragment } from "react";
import SideDrawer from "../Navigation/SideDrawer/SideDrawer";
import Toolbar from "../Navigation/Tollbar/Toolbar";
import classes from "./Layout.module.css";

class Layout extends Component {
state = {
showSideDrawer: true,
};
sideDrawerClosedHandler = () => {
this.setState({ showSideDrawer: false });
};

render() {
return (
<Fragment>
<Toolbar />
<SideDrawer
closed={this.sideDrawerClosedHandler}
open={this.state.showSideDrawer}
/>
<main className={classes.Content}>{this.props.children}</main>
</Fragment>
);
}
}
export default Layout;

SideDrawer.js

SideDrawer.js
import React, { Fragment } from "react";
import Logo from "../../Logo/Logo";
import Backdrop from "../../UI/Backdrop/Backdrop";
import NavigationItems from "../NavigationItems/NavigationItems";
import classes from "./SideDrawer.module.css";

const sideDrawer = (props) => {
let attachedClasses = [classes.SideDrawer, classes.Close];
if (props.open) {
attachedClasses = [classes.SideDrawer, classes.Open];
}
return (
<Fragment>
<Backdrop show={props.open} clicked={props.closed} />
<div className={attachedClasses.join(" ")}>
<div className={classes.Logo}>
<Logo />
</div>
<nav>
<NavigationItems />
</nav>
</div>
</Fragment>
);
};

export default sideDrawer;