
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
styled-react-modal
Advanced tools
Styled React Modal is a modal implementation built with styled-components. It uses the latest React 16.x features and exposes a familiar, easy to use API.
npm i -s styled-react-modal
Add the <ModalProvider>
component near the top of your application's tree.
import React, { Component } from 'react'
import { ModalProvider } from 'styled-react-modal'
...
export default class App extends Component {
render () {
return (
<ThemeProvider theme={theme}>
<ModalProvider>
<FancyModalButton />
</ModalProvider>
</ThemeProvider>
)
}
}
Use the <Modal>
component.
import Modal from 'styled-react-modal'
...
const StyledModal = Modal.styled`
width: 20rem;
height: 20rem;
display: flex;
align-items: center;
justify-content: center;
background-color: ${props => props.theme.colors.white};
`
class FancyModalButton extends Component {
constructor (props) {
super(props)
this.state = {
isOpen: false,
}
this.toggleModal = this.toggleModal.bind(this)
}
toggleModal (e) {
this.setState({ isOpen: !this.state.isOpen })
}
render () {
return (
<div>
<button onClick={this.toggleModal}>Click me</button>
<StyledModal
isOpen={this.state.isOpen}
onBackgroundClick={this.toggleModal}
onEscapeKeydown={this.toggleModal}>
<span>I am a modal!</span>
<button onClick={this.toggleModal}>Close me</button>
</StyledModal>
</div>
)
}
}
<ModalProvider>
Modal
(Default)
Modal.styled(styles)
<Modal>
<BaseModalBackground>
<ModalProvider>
Sets the root portal where <Modal>
s will be rendered.
Props
backgroundComponent
] (Component): A styled component to be used as the default modal background. If not provided, library defaults will be used.Example:
import { ModalProvider } from 'styled-react-modal'
const SpecialModalBackground = styled.div`
display: flex;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 30;
background-color: green;
`
export default class App extends Component {
render () {
return (
<ThemeProvider theme={theme}>
<ModalProvider backgroundComponent={SpecialModalBackground}>
<FancyModalButton />
</ModalProvider>
</ThemeProvider>
)
}
}
Modal.styled(styles)
Factory method that accepts a tagged template literal and returns a <Modal>
component with styles included.
Arguments
styles
(Tagged Template Literal): styled-components compatible css styles.Example:
const StyledModal = Modal.styled`
width: 20rem;
height: 20rem;
display: flex;
align-items: center;
justify-content: center;
background-color: ${props => props.theme.colors.white};
`
<Modal>
Renders its children in a modal when open, nothing when not open.
Props
isOpen
(Boolean): A boolean that indicates whether the modal is to be open or closed.onBackgroundClick
] (Function): A function that is called when the modal background is clicked.onEscapeKeydown
] (Function): A function that is called when the escape key is pressed while the modal is open.Example:
import Modal from 'styled-react-modal'
class FancyModalButton extends Component {
constructor (props) {
super(props)
this.state = {
isOpen: false,
}
this.toggleModal = this.toggleModal.bind(this)
}
toggleModal (e) {
this.setState({ isOpen: !this.state.isOpen })
}
render () {
return (
<div>
<button onClick={this.toggleModal}>Click me</button>
<Modal isOpen={this.state.isOpen}>
<span>I am a modal!</span>
<button onClick={this.toggleModal}>Close me</button>
</Modal>
</div>
)
}
}
<BaseModalBackground>
A convenience base component for making default background styles with <ModalProvider>
.
Example:
const SpecialModalBackground = styled(BaseModalBackground)`
background-color: green;
`
FAQs
A React modal built with styled-components.
We found that styled-react-modal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.