1.0.11 • Published 2 years ago

sm-modal v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

sm-modal

npm.io npm.io npm.io npm.io

This library does not provide any UI, but instead offers a convenient way to render modal components defined elsewhere.

Demo

Table of Contents

Install

npm install --save sm-modal

Usage

Use Modal component

import { useState } from 'react'
import { Modal } from 'sm-modal'

const HomePage: React.FC = () => {
  const [showModal, setShowModal] = useState < boolean > false

  const handleShowModal = () => {
    setShowModal(true)
  }

  return (
    <div>
      <h1>Home Page</h1>
      <button onClick={handleShowModal}>Show modal</button>
      <Modal show={showModal} onClose={() => setShowModal(false)}>
        <div>Modal content</div>
      </Modal>
    </div>
  )
}

export default HomePage

Use ModalProvider to provide modal context:

import React from 'react'
import ReactDOM from 'react-dom'
import { ModalProvider } from 'sm-modal'
import App from './App'

ReactDOM.render(
  <ModalProvider>
    <App />
  </ModalProvider>,
  document.getElementById('root')
)

Call useModal with the dialog component of your choice.

import { useModal } from 'sm-modal'

const HomePage: React.FC = () => {
  const { showModal } = useModal()

  const handleShowModal = () => {
    showModal(<div>Modal content</div>)
  }

  return (
    <div>
      <h1>Home Page</h1>
      <button onClick={handleShowModal}>Show modal</button>
    </div>
  )
}

export default HomePage

Advance

Custom config of ModalProvider

import React from 'react'
import { ModalProvider } from 'sm-modal'
import HomePage from './pages/HomePage'

function App() {
  return (
    <ModalProvider
      configs={{
        cancelText: 'Cancel',
        closable: true,
        closableWithEsc: true,
        maskClosable: true,
        okText: 'Submit',
        title: 'Custom title',
        width: '500px',
        zIndex: 10
      }}
    >
      <HomePage />
    </ModalProvider>
  )
}

export default App

License

MIT © pcsaovang

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago