Criando um Modal em ReactJS
20768 단어 reactjavascripttypescriptmodal
Muitos escolhem a segunda opção, as vezes pela facilidade de estar pronto, outros por falta de conhecimento para desenvolver o próprio componente. 예를 들어 ReactJS를 간단하게 비교하기로 결정했습니다.
import React, { useState, useEffect } from 'react'
import ReactFrom from 'react-dom'
import './modal.css'
import React, { useState, useEffect } from 'react'
import ReactDom from 'react-dom'
import './modal.css'
interface ModalProps {
isShowing: boolean;
toggle: () => void;
}
const Modal: React.FC<ModalProps> = ({ isShowing, toggle, children }) => {
useEffect(() => {
const listner = function (e: KeyboardEvent ) {
if (e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27) {
e.preventDefault();
e.stopPropagation();
isShowing && toggle();
}
}
window.addEventListener('keyup', listner)
return (() => {
window.removeEventListener('keyup', listner)
})
}, [isShowing, toggle])
return (
isShowing ? ReactDom.createPortal(
<div className="modal-overlay">
<div className="modal-wrapper">
<div className="modal">
{children}
</div>
</div>
</div>, document.body
) : null
)
}
interface ModalHeaderProps {
toggle: () => void;
}
export const ModalHeader: React.FC<ModalHeaderProps> = ({ toggle, children }) => (
<div className="modal-header">
{children || 'Title'}
<button
className="modal-button-close"
data-dismiss="modal"
aria-label="Close"
onClick={toggle}
>
×
</button>
</div>
)
export const ModalBody: React.FC = ({ children }) => (
<div className="modal-body">
{children}
</div>
)
export const ModalFooter: React.FC = ({ children }) => (
<div className="modal-footer">
{children}
</div>
)
export const useModal = () => {
const [isShowing, setIsShowing] = useState(false);
function toggle() {
setIsShowing(!isShowing);
}
return {
isShowing,
toggle,
}
}
export default Modal;
.modal-overlay {
position: fixed;
top: 0;
left: 0;
z-index: 1040;
width: 100vw;
height: 100vh;
background-color: rgba(0,0,0,0.5);
}
.modal-wrapper {
position: fixed;
top: 0;
left: 0;
z-index: 1050;
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
outline: 0;
}
.modal {
z-index: 100;
background: white;
position: relative;
margin:20px auto;
border-radius: 3px;
max-width: 800px;
padding: 8px;
border-radius: 4px;
}
.modal-header {
display: flex;
justify-content: space-between;
border-bottom: 1px solid #ddd;
padding: 8px 0;
font-weight: bold;
}
.modal-button-close {
background: transparent;
border: none;
cursor: pointer;
font-size: 16px;
}
.modal-body {
padding: 8px 0;
}
.modal-footer {
display: flex;
flex-direction: row;
justify-content: flex-end;
padding: 8px 0;
}
Agora para usar o componente criado é simples
import React from 'react';
import Modal, {
ModalHeader,
ModalBody,
ModalFooter,
useModal
} from './components/Modal'
function App() {
const { isShowing, toggle } = useModal();
return (
<div>
<button onClick={toggle}>
Modal
</button>
<Modal {...{isShowing, toggle}}>
<ModalHeader {...{toggle}}>
My Title
</ModalHeader>
<ModalBody>
Hello World!
</ModalBody>
<ModalFooter>
<button onClick={toggle}>
Cancel
</button>
</ModalFooter>
</Modal>
</div>
);
}
export default App;
링크 do github https://github.com/renatoosaka/useModal
Reference
이 문제에 관하여(Criando um Modal em ReactJS), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/renatoosaka/criando-um-modal-em-reactjs-54g0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)