Skip to content

Commit 9ef91ed

Browse files
Merge pull request #1 from Codebrahma/feature/RemovingStyledComponents
Feature/removing styled components
2 parents d3a272c + 1326db2 commit 9ef91ed

File tree

10 files changed

+344
-470
lines changed

10 files changed

+344
-470
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,3 @@ Read the full Documentation [here](https://react-flexible-sliding-menu.netlify.c
2929
## Motivation
3030

3131
There are a lot of libraries out there which set out to do the same thing, some accomplished it some don't. But, I always found one or another thing missing in them. Hence, I built this library to fulfill my needs and possibly yours.
32-
33-
## Dependencies
34-
35-
Right now the only main Dependency is [Styled-Components](https://www.styled-components.com/), which I'll soon be replacing with vanilla css to reduce the library size even more.

docs/introduction.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,3 @@ A **React** library which provides **Flexible Sliding Menu** with an easy to use
3232
## Motivation
3333

3434
There are a lot of libraries out there which set out to do the same thing, some accomplished it some don't. But, I always found one or another thing missing in them. Hence, I built this library to fulfill my needs and possibly yours.
35-
36-
## Dependencies
37-
38-
Right now the only main Dependency is [Styled-Components](https://www.styled-components.com/), which I'll soon be replacing with vanilla css to reduce the library size even more.

package-lock.json

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

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-flexible-sliding-menu",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"description": "A React library which provides Flexible Sliding Menu with an easy to use API and a bunch of other awesome features. Also, you get to choose which animation you want for the sliding menu.",
55
"main": "dist/index.js",
66
"homepage": "https://react-flexible-sliding-menu.netlify.com/",
@@ -50,8 +50,7 @@
5050
"react": "^16.12.0"
5151
},
5252
"dependencies": {
53-
"prop-types": "^15.7.2",
54-
"styled-components": "^4.4.1"
53+
"prop-types": "^15.7.2"
5554
},
5655
"devDependencies": {
5756
"@babel/cli": "^7.7.4",

src/CustomProps/width.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This is a factory function (also called a higher-order function)
2+
function createCustomWidthPropType(isRequired) {
3+
// The factory returns a custom prop type
4+
return function(props, propName, componentName) {
5+
const { width } = props;
6+
if (width == null) {
7+
// Prop is missing
8+
if (isRequired) {
9+
// Prop is required but wasn't specified. Throw an error.
10+
throw new Error(
11+
`The prop \`${propName}\` is marked as required in \`${componentName}\`, but its value is \`undefined\`.`
12+
);
13+
}
14+
// Prop is optional. Do nothing.
15+
} else {
16+
const validCSSDimension = /^(\d+|\d*\.\d+)(px|rem|em|%|vw|vh|cm|mm|Q|in|pc|pt|ex|ch|lh|vmin|vmax)$/;
17+
if (!validCSSDimension.test(width)) {
18+
return new Error(
19+
`Invalid prop \`${propName}\` of value \`${width}\` supplied to \`${componentName}\`, expected a valid css length with unit (https://developer.mozilla.org/en-US/docs/Web/CSS/length).`
20+
);
21+
}
22+
}
23+
return null;
24+
};
25+
}
26+
27+
// Using the factory, create two different versions of your prop type
28+
const widthPropType = createCustomWidthPropType(false);
29+
widthPropType.isRequired = createCustomWidthPropType(true);
30+
31+
export default widthPropType;

src/Push/index.jsx

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,71 @@
11
import React, { useEffect, useState } from 'react';
2-
import { PushingDiv } from './styles';
2+
import PropTypes from 'prop-types';
3+
import widthPropType from '../CustomProps/width';
34

4-
const Push = ({ show, children, direction, width }) => {
5-
const [shouldRender, setRender] = useState(show);
5+
const AppContainerForPush = ({ direction, width, menuIsClosing, children }) => {
6+
const appContainerStyles = {
7+
transform: menuIsClosing
8+
? 'translateX(0)'
9+
: `translateX(${direction === 'right' ? '-' : '+'}${width})`,
10+
transition: 'transform 0.3s ease'
11+
};
12+
13+
return <div style={appContainerStyles}>{children}</div>;
14+
};
15+
16+
const Push = ({ direction, width, menuIsClosing, setIsMenuOpen, children }) => {
17+
const [menuIsOpening, setMenuIsOpening] = useState(false);
18+
19+
const menuContainerStyles = {
20+
position: 'fixed',
21+
width,
22+
zIndex: 9999999999,
23+
top: 0,
24+
left: direction === 'right' ? null : 0,
25+
right: direction === 'right' ? 0 : null,
26+
height: '100vh',
27+
background: 'whitesmoke',
28+
transform: menuIsOpening
29+
? 'translateX(0)'
30+
: `translateX(${direction === 'right' ? '+' : '-'}100%)`,
31+
transition: 'transform 0.3s ease'
32+
};
633

734
useEffect(() => {
8-
if (show) setRender(true);
9-
}, [show]);
35+
setMenuIsOpening(true);
36+
}, []);
1037

11-
const onAnimationEnd = () => {
12-
if (!show) setRender(false);
38+
useEffect(() => {
39+
if (menuIsClosing) setMenuIsOpening(false);
40+
}, [menuIsClosing]);
41+
42+
const onTransitionEnd = () => {
43+
if (menuIsClosing) {
44+
setIsMenuOpen(false);
45+
}
1346
};
1447

1548
return (
16-
shouldRender && (
17-
<PushingDiv
18-
show={show}
19-
width={width}
20-
direction={direction}
21-
onAnimationEnd={onAnimationEnd}
22-
>
23-
{children}
24-
</PushingDiv>
25-
)
49+
<div style={menuContainerStyles} onTransitionEnd={onTransitionEnd}>
50+
{children}
51+
</div>
2652
);
2753
};
2854

55+
Push.propTypes = {
56+
direction: PropTypes.oneOf(['left', 'right']).isRequired,
57+
width: widthPropType.isRequired,
58+
menuIsClosing: PropTypes.bool.isRequired,
59+
setIsMenuOpen: PropTypes.func.isRequired,
60+
children: PropTypes.node.isRequired
61+
};
62+
63+
AppContainerForPush.propTypes = {
64+
direction: PropTypes.oneOf(['left', 'right']).isRequired,
65+
width: widthPropType.isRequired,
66+
menuIsClosing: PropTypes.bool.isRequired,
67+
children: PropTypes.node.isRequired
68+
};
69+
70+
export { AppContainerForPush };
2971
export default Push;

src/Push/styles.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/Slide/index.jsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,58 @@
11
import React, { useEffect, useState } from 'react';
2-
import SlidingDiv from './styles';
2+
import PropTypes from 'prop-types';
3+
import widthPropType from '../CustomProps/width';
34

4-
const Slide = ({ show, children, direction, width }) => {
5-
const [shouldRender, setRender] = useState(show);
5+
const Slide = ({
6+
direction,
7+
width,
8+
menuIsClosing,
9+
setIsMenuOpen,
10+
children
11+
}) => {
12+
const [menuIsOpening, setMenuIsOpening] = useState(false);
13+
14+
const menuContainerStyles = {
15+
position: 'fixed',
16+
width,
17+
zIndex: 9999999999,
18+
top: 0,
19+
left: direction === 'right' ? null : 0,
20+
right: direction === 'right' ? 0 : null,
21+
height: '100vh',
22+
background: 'whitesmoke',
23+
transform: menuIsOpening
24+
? 'translateX(0)'
25+
: `translateX(${direction === 'right' ? '+' : '-'}100%)`,
26+
transition: 'transform 0.3s ease'
27+
};
28+
29+
useEffect(() => {
30+
setMenuIsOpening(true);
31+
}, []);
632

733
useEffect(() => {
8-
if (show) setRender(true);
9-
}, [show]);
34+
if (menuIsClosing) setMenuIsOpening(false);
35+
}, [menuIsClosing]);
1036

11-
const onAnimationEnd = () => {
12-
if (!show) setRender(false);
37+
const onTransitionEnd = () => {
38+
if (menuIsClosing) {
39+
setIsMenuOpen(false);
40+
}
1341
};
1442

1543
return (
16-
shouldRender && (
17-
<SlidingDiv
18-
show={show}
19-
width={width}
20-
direction={direction}
21-
onAnimationEnd={onAnimationEnd}
22-
>
23-
{children}
24-
</SlidingDiv>
25-
)
44+
<div style={menuContainerStyles} onTransitionEnd={onTransitionEnd}>
45+
{children}
46+
</div>
2647
);
2748
};
2849

50+
Slide.propTypes = {
51+
direction: PropTypes.oneOf(['left', 'right']).isRequired,
52+
width: widthPropType.isRequired,
53+
menuIsClosing: PropTypes.bool.isRequired,
54+
setIsMenuOpen: PropTypes.func.isRequired,
55+
children: PropTypes.node.isRequired
56+
};
57+
2958
export default Slide;

src/Slide/styles.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)