Skip to content

Commit 239732a

Browse files
committed
Removed the need for styled-components
1 parent eb5cb4f commit 239732a

File tree

3 files changed

+111
-543
lines changed

3 files changed

+111
-543
lines changed

index.js

+56-58
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,74 @@
1-
import React, { useState, useEffect } from 'react'
1+
import React, { useState, useEffect, useLayoutEffect } from 'react'
22

3-
import styled from 'styled-components'
4-
5-
const Container = styled.div`
6-
position: absolute;
7-
top: 0;
8-
bottom: 0;
9-
left: 0;
10-
right: 0;
11-
display: flex;
12-
justify-content: center;
13-
align-items: center;
14-
z-index: 100;
15-
`
16-
17-
const PromptElement = styled.div`
18-
padding: 40px;
19-
background: #fff;
20-
border-radius: 10px;
21-
margin: 20px;
22-
max-width: 400px;
23-
box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.2);
24-
z-index: 100;
25-
cursor: default;
26-
transition: ${(props) => props.animationDuration}ms;
27-
opacity: ${(props) => (props.visible ? 1 : 0)};
28-
transform: ${(props) => (props.visible ? 'scale(1)' : 'scale(0.9)')};
29-
`
3+
const ContainerStyle = {
4+
position: 'absolute',
5+
top: 0,
6+
bottom: 0,
7+
left: 0,
8+
right: 0,
9+
display: 'flex',
10+
justifyContent: 'center',
11+
alignItems: 'center',
12+
zIndex: 100
13+
}
3014

3115
function Popup ({ children, visible, onClose, animationDuration = 100, style }) {
16+
const [firstTimeSetup, setFirstTimeSetup] = useState(true)
3217
const [animationState, setAnimationState] = useState(visible)
33-
3418
const [displayNothing, setDisplayNothing] = useState(!visible)
3519

20+
useLayoutEffect(() => {
21+
if (firstTimeSetup) return
22+
if (displayNothing) return
23+
// Fix to make sure the element has been rendered before we start the animation otherwise react might execute before doing a re-render
24+
window.setTimeout(() => {
25+
setAnimationState(true)
26+
}, 1)
27+
}, [displayNothing])
28+
3629
useEffect(() => {
30+
setFirstTimeSetup(false)
31+
if (firstTimeSetup) return
32+
3733
if (visible) {
38-
// Trigger chained effect
3934
setDisplayNothing(false)
40-
}
41-
42-
if (!visible) {
43-
setAnimationState(visible)
44-
window.setTimeout(() => {
45-
setDisplayNothing(true)
46-
}, animationDuration)
35+
} else {
36+
if (displayNothing === false) {
37+
setAnimationState(false)
38+
window.setTimeout(() => {
39+
setDisplayNothing(true)
40+
}, animationDuration)
41+
}
4742
}
4843
}, [visible, animationDuration])
4944

50-
// Chained effects to be able to animate in from not existing
51-
useEffect(() => {
52-
if (!displayNothing) {
53-
setAnimationState(visible)
54-
}
55-
}, [displayNothing, visible])
56-
5745
if (displayNothing) return null
5846

47+
const PromptStyle = {
48+
padding: 40,
49+
background: '#fff',
50+
borderRadius: 10,
51+
margin: 20,
52+
maxWidth: 400,
53+
boxShadow: '0px 0px 20px 0px rgba(0, 0, 0, 0.2)',
54+
zIndex: 100,
55+
cursor: 'default',
56+
transition: animationDuration + 'ms',
57+
opacity: animationState ? 1 : 0,
58+
transform: animationState ? 'scale(1)' : 'scale(0.9)'
59+
}
60+
5961
return React.createElement(
60-
Container,
61-
{ clickable: animationState, onClick: onClose },
62-
[
63-
React.createElement(
64-
PromptElement,
65-
{
66-
animationDuration: animationDuration,
67-
onClick: (e) => e.stopPropagation(),
68-
style: { ...style },
69-
visible: animationState
70-
},
71-
children
72-
)
73-
]
62+
'div',
63+
{ onClick: onClose, style: ContainerStyle },
64+
React.createElement(
65+
'div',
66+
{
67+
onClick: (e) => e.stopPropagation(),
68+
style: { ...style, ...PromptStyle }
69+
},
70+
children
71+
)
7472
)
7573
}
7674

0 commit comments

Comments
 (0)