Skip to content

Commit 329ddba

Browse files
committed
added top & bottom positions + some cleanups
1 parent 42c7210 commit 329ddba

File tree

6 files changed

+108
-78
lines changed

6 files changed

+108
-78
lines changed

examples/App.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,31 @@ import { OffCanvas, OffCanvasMenu, OffCanvasBody } from "react-offcanvas";
66
import styles from "./styles.css";
77

88
export default class App extends Component {
9-
componentWillMount() {
10-
// sets the initial state
11-
this.setState({
12-
isMenuOpened: false
13-
});
9+
state = {
10+
isMenuOpened: false,
11+
}
12+
13+
toggle = () => {
14+
this.setState(state => ({ isMenuOpened: !state.isMenuOpened }));
1415
}
1516

1617
render() {
1718
return (
1819
<OffCanvas
1920
width={300}
21+
height={300}
2022
transitionDuration={300}
2123
isMenuOpened={this.state.isMenuOpened}
22-
position={"right"}
23-
effect={"overlay"}
24+
position={"top"}
25+
effect={"parallax"}
2426
>
2527
<OffCanvasBody
2628
className={styles.bodyClass}
2729
style={{ fontSize: "30px" }}
2830
>
2931
<p>This is the main body container.</p>
3032
<p>
31-
<a href="#" onClick={this.handleClick.bind(this)}>
33+
<a href="#" onClick={this.toggle}>
3234
Click here
3335
</a>{" "}
3436
to toggle the menu.
@@ -43,7 +45,7 @@ export default class App extends Component {
4345
<li>Link 4</li>
4446
<li>Link 5</li>
4547
<li>
46-
<a href="#" onClick={this.handleClick.bind(this)}>
48+
<a href="#" onClick={this.toggle}>
4749
Toggle Menu
4850
</a>
4951
</li>
@@ -52,9 +54,4 @@ export default class App extends Component {
5254
</OffCanvas>
5355
);
5456
}
55-
56-
handleClick() {
57-
// toggles the menu opened state
58-
this.setState({ isMenuOpened: !this.state.isMenuOpened });
59-
}
6057
}

examples/bundle.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/OffCanvas.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import React from "react";
44

5-
let OffCanvas = ({
5+
const OffCanvas = ({
66
width = 250,
7+
height = 250,
78
transitionDuration = 250,
89
isMenuOpened = false,
910
position = "left",
@@ -14,6 +15,7 @@ let OffCanvas = ({
1415
const transferProps = element => {
1516
return React.cloneElement(element, {
1617
width,
18+
height,
1719
transitionDuration,
1820
isMenuOpened,
1921
position,
@@ -22,7 +24,7 @@ let OffCanvas = ({
2224
};
2325

2426
// transfer the props from OffCanvas to the child
25-
let offCanvasChildren = React.Children.map(children, transferProps);
27+
const offCanvasChildren = React.Children.map(children, transferProps);
2628

2729
return <div>{offCanvasChildren}</div>;
2830
};

src/OffCanvasBody.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import React, { PropTypes } from "react";
44

5-
let OffCanvasBody = ({
5+
const OffCanvasBody = ({
66
width = 250,
7+
height = 250,
78
transitionDuration = 250,
89
isMenuOpened = false,
910
position = "left",
@@ -12,41 +13,49 @@ let OffCanvasBody = ({
1213
className,
1314
style
1415
}) => {
15-
// closed state style
16-
let translateX = position === "left" ? 0 : 0;
17-
let closedStyle = {
16+
// translateOpen map
17+
const translateOpen = {
18+
left: {
19+
overlay: "0 0",
20+
parallax: width / 2 + "px, 0",
21+
push: width + "px, 0",
22+
},
23+
right: {
24+
overlay: "0 0",
25+
parallax: -width / 2 + "px, 0",
26+
push: -width + "px, 0",
27+
},
28+
top: {
29+
overlay: "0 0",
30+
parallax: "0, " + height / 2 + "px",
31+
push: "0, " + height + "px",
32+
},
33+
bottom: {
34+
overlay: "0 0",
35+
parallax: "0, " + -height / 2 + "px",
36+
push: "0, " + -height + "px",
37+
},
38+
}[position][effect];
39+
40+
const currentStyle = Object.assign({
1841
transitionDuration: transitionDuration + "ms",
19-
transform: "translate(" + translateX + "px, 0px)",
42+
transform: "translate(" + (isMenuOpened ? translateOpen : "0, 0") +")",
2043
backfaceVisibility: "hidden"
21-
};
22-
23-
// open state style
24-
let translateOpenX = position === "left" ? width : -1 * width;
25-
translateOpenX = effect === "parallax" ? translateOpenX / 2 : translateOpenX;
26-
translateOpenX = effect === "overlay" ? 0 : translateOpenX;
27-
28-
let openStyle = {
29-
transform: "translate(" + translateOpenX + "px, 0px)"
30-
};
31-
32-
// create current state styles
33-
let currStyle = Object.assign({}, closedStyle);
34-
if (isMenuOpened) {
35-
currStyle = Object.assign({}, currStyle, openStyle);
36-
}
44+
}, style);
3745

3846
return (
39-
<div style={{ ...currStyle, ...style }} className={className}>
47+
<div style={currentStyle} className={className}>
4048
{children}
4149
</div>
4250
);
4351
};
4452

4553
OffCanvasBody.propTypes = {
4654
width: PropTypes.number,
55+
height: PropTypes.number,
4756
transitionDuration: PropTypes.number,
4857
isMenuOpened: PropTypes.bool,
49-
position: PropTypes.oneOf(["left", "right"]),
58+
position: PropTypes.oneOf(["left", "right", "top", "bottom"]),
5059
effect: PropTypes.oneOf(["push", "parallax", "overlay"]),
5160
style: PropTypes.object
5261
};

src/OffCanvasMenu.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import React, { PropTypes } from "react";
44

5-
let OffCanvasMenu = ({
5+
const OffCanvasMenu = ({
66
width = 250,
7+
height = 250,
78
transitionDuration = 250,
89
isMenuOpened = false,
910
position = "left",
@@ -12,46 +13,67 @@ let OffCanvasMenu = ({
1213
className,
1314
style
1415
}) => {
15-
console.log(position, effect);
16-
// closed state style
17-
let left = position === "left" ? -1 * width + "px" : "auto";
18-
let right = position === "left" ? "auto" : -1 * width + "px";
19-
let translateX = position === "left" ? -1 * width : 0;
20-
let closedStyle = {
16+
// position styles map
17+
const { left, right, top, bottom, translateClose, translateOpen } = {
18+
left: {
19+
left: -width + "px",
20+
right: "auto",
21+
top: 0,
22+
bottom: "auto",
23+
translateClose: -width + "px, 0",
24+
translateOpen: width + "px, 0",
25+
},
26+
right: {
27+
left: "auto",
28+
right: -width + "px",
29+
top: 0,
30+
bottom: "auto",
31+
translateClose: "0, 0",
32+
translateOpen: -width + "px, 0",
33+
},
34+
top: {
35+
left: 0,
36+
right: "auto",
37+
top: -height + "px",
38+
bottom: "auto",
39+
translateClose: "0, " + -height + "px",
40+
translateOpen: "0, " + height + "px",
41+
},
42+
bottom: {
43+
left: 0,
44+
right: "auto",
45+
top: "auto",
46+
bottom: -height + "px",
47+
translateClose: "0, 0",
48+
translateOpen: "0, " + -height + "px",
49+
},
50+
}[position];
51+
52+
const currentStyle = Object.assign({
2153
width: width + "px",
2254
position: "fixed",
23-
top: "0px",
24-
left: left,
25-
right: right,
26-
transform: "translate(" + translateX + "px, 0px)",
55+
top,
56+
bottom,
57+
left,
58+
right,
59+
transform: "translate(" + (isMenuOpened ? translateOpen : translateClose) + ")",
2760
transitionDuration: transitionDuration + "ms",
2861
backfaceVisibility: "hidden"
29-
};
30-
31-
// open state style
32-
let translateOpenX = position === "left" ? width : -1 * width;
33-
let openStyle = {
34-
transform: "translate(" + translateOpenX + "px, 0px)"
35-
};
36-
37-
// create current state styles
38-
let currStyle = Object.assign({}, closedStyle);
39-
if (isMenuOpened) {
40-
currStyle = Object.assign({}, currStyle, openStyle);
41-
}
62+
}, style);
4263

4364
return (
44-
<div style={{ ...currStyle, ...style }} className={className}>
65+
<div style={currentStyle} className={className}>
4566
{children}
4667
</div>
4768
);
4869
};
4970

5071
OffCanvasMenu.propTypes = {
5172
width: PropTypes.number,
73+
height: PropTypes.number,
5274
transitionDuration: PropTypes.number,
5375
isMenuOpened: PropTypes.bool,
54-
position: PropTypes.oneOf(["left", "right"]),
76+
position: PropTypes.oneOf(["left", "right", "top", "bottom"]),
5577
effect: PropTypes.oneOf(["push", "parallax", "overlay"]),
5678
style: PropTypes.object
5779
};

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
'use strict';
1+
"use strict";
22

3-
export OffCanvas from './OffCanvas';
4-
export OffCanvasBody from './OffCanvasBody';
5-
export OffCanvasMenu from './OffCanvasMenu';
3+
export { default as OffCanvas } from './OffCanvas';
4+
export { default as OffCanvasBody } from './OffCanvasBody';
5+
export { default as OffCanvasMenu } from './OffCanvasMenu';

0 commit comments

Comments
 (0)