이번 협업을 진행하면서 사수님이 추천해주셔서 React Spring 사용해보고 굉장히 편리하고 다루기 쉬워서 글로 간단하게 정리 해놓고자 한다.
$ npm install --save react-spring
import React, { Component } from 'react';
import Modal1 from './Modal1';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = { openModal: false };
}
handleOpenModal = () => {
this.setState({ openModal: true });
};
handleCloseModal = () => {
this.setState({ openModal: false });
};
render() {
return (
<Modal1>
<div>모달내용</div>
<button onClick={this.handleCloseModal}>닫기</button>
</Modal1>
);
}
}
export default App;
import React, { Component } from 'react';
import './Modal1.css';
class Modal1 extends Component {
render() {
const { openModal } = this.props;
return (
<>
{openModal && (
<div className='over_lay'>
<div className='modal_container'>{this.props.children}</div>
</div>
)}
</>
);
}
}
export default Modal1;
.over_lay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(45, 45, 45, 0.6);
backdrop-filter: blur(30px);
z-index: 9;
}
.modal_container {
position: fixed;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
top: 320px;
width: 812px;
left: 500px;
height: 302px;
border-radius: 20px;
background-color: rgba(216, 216, 216, 0.7);
padding-top: 40px;
padding-bottom: 70px;
}
import React from 'react';
import './Background.css';
export default class Background extends React.PureComponent {
render() {
return (
<div className='main' onClick={this.toggle}>
<div
style={{
...this.props.parentStyle
}}
></div>
</div>
);
}
}
.main > div {
cursor: pointer;
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-weight: 800;
font-size: 10em;
background-image: url(https://images.unsplash.com/photo-1532386236358-a33d8a9434e3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60);
background-size: cover;
}
import React, { Component } from 'react';
import './App.css';
import { Spring } from 'react-spring/renderprops';
import Modal1 from './Modal1';
import Background from './Background';
class App extends Component {
constructor(props) {
super(props);
this.state = { openModal: false };
}
handleOpenModal = () => {
this.setState({ openModal: true });
};
handleCloseModal = () => {
this.setState({ openModal: false });
};
render() {
const { openModal } = this.state;
return (
<>
<button onClick={this.handleOpenModal}>open</button>
<Modal1 openModal={openModal} handleCloseModal={this.handleCloseModal}>
<div className='membership_header'>
<div>모달내용</div>
<button onClick={this.handleCloseModal}>닫기</button>
</div>
</Modal1>
{openModal ? (
<Spring
from={{ transform: 'scale(1.0)' }}
to={{ transform: 'scale(0.8)' }}
>
{props => <Background parentStyle={props} />}
</Spring>
) : (
<Spring
from={{ transform: 'scale(0.8)' }}
to={{ transform: 'scale(1.0)' }}
>
{props => <Background parentStyle={props} />}
</Spring>
)}
</>
);
}
}
export default App;