A Modal is a window overlaid on either the primary window or another dialog window. Content behind a modal dialog is inert, meaning that users cannot interact with it.
NativeBase exports a Modal Compound component:
Modal: The wrapper that provides context for its children.
Modal.Content: The container for the modal dialog's content.
Modal.Header: The header that labels the modal dialog.
Modal.Footer: The footer that houses the modal actions.
Modal.Body: The wrapper that houses the modal's main content.
Modal.CloseButton: The button that closes the modal.
import { Modal } from 'native-base';
const Example = () => {
const [showModal, setShowModal] = useState(false);
return <>
<Button onPress={() => setShowModal(true)}>Button</Button>
<Modal isOpen={showModal} onClose={() => setShowModal(false)}>
<Modal.Content maxWidth="400px">
<Modal.CloseButton />
<Modal.Header>Modal Title</Modal.Header>
<Modal.Body>
Sit nulla est ex deserunt exercitation anim occaecat. Nostrud
ullamco deserunt aute id consequat veniam incididunt duis in sint
irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit
officia tempor esse quis. Sunt ad dolore quis aute consequat. Magna
exercitation reprehenderit magna aute tempor cupidatat consequat
elit dolor adipisicing. Mollit dolor eiusmod sunt ex incididunt
cillum quis. Velit duis sit officia eiusmod Lorem aliqua enim
ullamco deserunt aute id consequat veniam incididunt duis in sint
irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit
officia tempor esse quis. Sunt ad dolore quis aute consequat. Magna
exercitation reprehenderit magna aute tempor cupidatat consequat
elit dolor adipisicing. Mollit dolor eiusmod sunt ex incididunt
cillum quis. Velit duis sit officia eiusmod Lorem aliqua enim
exercitation reprehenderit magna aute tempor cupidatat consequat
elit dolor adipisicing. Mollit dolor eiusmod sunt ex incididunt
cillum quis. Velit duis sit officia eiusmod Lorem aliqua enim
ullamco deserunt aute id consequat veniam incididunt duis in sint
irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit
officia tempor esse quis. Sunt ad dolore quis aute consequat. Magna
exercitation reprehenderit magna aute tempor cupidatat consequat
elit dolor adipisicing. Mollit dolor eiusmod sunt ex incididunt
cillum quis. Velit duis sit officia eiusmod Lorem aliqua enim
</Modal.Body>
<Modal.Footer>
<Button.Group variant="ghost" space={2}>
<Button>LEARN MORE</Button>
<Button onPress={() => {
setShowModal(false);
}}>
ACCEPT
</Button>
</Button.Group>
</Modal.Footer>
</Modal.Content>
</Modal>
</>;
};
You can pass size prop to NativeBase Modal , it can take sm, md, lg, full that maps to 60%, 75%, 90%, 100%, or a string or a numerical width of the Modal.
function Example() {
const [modalVisible, setModalVisible] = React.useState(false);
const [size, setSize] = React.useState("md");
const handleSizeClick = newSize => {
setSize(newSize);
setModalVisible(!modalVisible);
};
return <>
<Modal isOpen={modalVisible} onClose={setModalVisible} size={size}>
<Modal.Content>
<Modal.CloseButton />
<Modal.Header>Modal Title</Modal.Header>
<Modal.Body>
Sit nulla est ex deserunt exercitation anim occaecat. Nostrud
ullamco deserunt aute id consequat veniam incididunt duis in sint
irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit
officia tempor esse quis. Sunt ad dolore quis aute consequat. Magna
exercitation reprehenderit magna aute tempor cupidatat consequat
elit dolor adipisicing. Mollit dolor eiusmod sunt ex incididunt
cillum quis. Velit duis sit officia eiusmod Lorem aliqua enim
laboris do dolor eiusmod. Et mollit incididunt nisi consectetur esse
laborum eiusmod pariatur
</Modal.Body>
<Modal.Footer>
<Button.Group variant="ghost" space={2}>
<Button>SAVE</Button>
<Button onPress={() => {
setModalVisible(!modalVisible);
}} colorScheme="muted">
CLOSE
</Button>
</Button.Group>
</Modal.Footer>
</Modal.Content>
</Modal>
<Stack direction={{
base: "column",
md: "row"
}} space={2}>
{["sm", "md", "lg", "full"].map(size => {
return <Button onPress={() => handleSizeClick(size)} key={size}>{`Open ${size} Modal`}</Button>;
})}
</Stack>
</>;
}
intialFocusRef and finalFocusRef Example
function Example() {
const [modalVisible, setModalVisible] = React.useState(false);
const initialRef = React.useRef(null);
const finalRef = React.useRef(null);
return <>
<Modal isOpen={modalVisible} onClose={setModalVisible} initialFocusRef={initialRef} finalFocusRef={finalRef}>
<Modal.Content>
<Modal.CloseButton />
<Modal.Header>Want to set focus somewhere?</Modal.Header>
<Modal.Body>
The below input will get focus upon opening of the Modal
<Input mt={4} ref={initialRef} placeholder="I'll recieve focus on Modal's opening" />
</Modal.Body>
<Modal.Footer>
<Button.Group variant="ghost" space={2}>
<Button>SAVE</Button>
<Button onPress={() => {
setModalVisible(!modalVisible);
}} colorScheme="secondary">
CLOSE
</Button>
</Button.Group>
</Modal.Footer>
</Modal.Content>
</Modal>
<Button onPress={() => {
setModalVisible(!modalVisible);
}}>
Open Modal
</Button>
<Input mt={4} ref={finalRef} placeholder="I'll receive focus on close" _light={{
placeholderTextColor: "blueGray.700"
}} _dark={{
placeholderTextColor: "blueGray.100"
}} />
</>;
}
function Example() {
const [modalVisible, setModalVisible] = React.useState(false);
return <>
<Modal isOpen={modalVisible} onClose={setModalVisible} avoidKeyboard>
<Modal.Content>
<Modal.CloseButton />
<Modal.Header>KeyboardAvoidView Modal</Modal.Header>
<Modal.Body>
Try typing something in the Input.
<Input mt={4} placeholder="Click here..." />
</Modal.Body>
<Modal.Footer>
<Button.Group variant="ghost" space={2}>
<Button>SAVE</Button>
<Button onPress={() => {
setModalVisible(!modalVisible);
}} colorScheme="secondary">
CLOSE
</Button>
</Button.Group>
</Modal.Footer>
</Modal.Content>
</Modal>
<VStack space={2}>
<Button onPress={() => {
setModalVisible(!modalVisible);
}}>
Open Modal
</Button>
<Text>
Open modal and focus on the input element to see the effect.
</Text>
</VStack>
</>;
}
const Example = () => {
const [placement, setPlacement] = useState(undefined);
const [open, setOpen] = useState(false);
const openModal = placement => {
setOpen(true);
setPlacement(placement);
};
return <>
<Stack direction={{
base: "column",
md: "row"
}} space={2}>
<Button onPress={() => openModal("top")}>Top</Button>
<Button onPress={() => openModal("bottom")}>Bottom</Button>
<Button onPress={() => openModal("center")}>Center</Button>
<Button onPress={() => openModal("left")}>Left</Button>
<Button onPress={() => openModal("right")}>Right</Button>
</Stack>
<Modal isOpen={open} onClose={() => setOpen(false)} mt={12}>
<Modal.Content maxWidth="400px" {...styles[placement]}>
<Modal.CloseButton />
<Modal.Header>Modal Title</Modal.Header>
<Modal.Body>
Sit nulla est ex deserunt exercitation anim occaecat. Nostrud
ullamco deserunt aute id consequat veniam incididunt duis in sint
irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit
</Modal.Body>
<Modal.Footer>
<Button.Group variant="ghost" space={2}>
<Button>LEARN MORE</Button>
<Button onPress={() => {
setOpen(false);
}}>
ACCEPT
</Button>
</Button.Group>
</Modal.Footer>
</Modal.Content>
</Modal>
</>;
};
const styles = {
top: {
marginBottom: "auto",
marginTop: 0
},
bottom: {
marginBottom: 0,
marginTop: "auto"
},
left: {
marginLeft: 0,
marginRight: "auto"
},
right: {
marginLeft: "auto",
marginRight: 0
},
center: {}
};
Tip: If you want a specifically aligned Modal, pass justifyContent and alignItems to Modal.
Uses React Native ARIA
which follows the
.
Moves focus to the next tabbable element inside the dialog. If focus is on the last tabbable element inside the dialog, moves focus to the first tabbable element inside the dialog.
Moves focus to the previous tabbable element inside the dialog. If focus is on the first tabbable element inside the dialog, moves focus to the last tabbable element inside the dialog.
Modal implements
,
,
,
,
,
,
,
,
,
,
,
,
If true, the modal will open. Useful for controllable state behaviour
Callback invoked when the modal is closed
If true, the modal will be opened by default
The ref of element to receive focus when the modal opens.
The ref of element to receive focus when the modal closes.
If true and the keyboard is opened, the modal will move up equvivalent to the keyboard height.
If true, the modal will close when the overlay is clicked
If true, the modal will close when Escape key is pressed
If true, a backdrop element is visible
If true, a backdrop element is visible
Props applied on Overlay.
Modal.Header,
Modal.Footer and
Modal.Body composes the
component.
Modal.CloseButton composes the
.
NativeBase ships with a default theme for each component. Check out the default theme of the modal
.
Info:
We can easily extend the modal component theme using extendTheme function as described in the documentation .