Skip to main content

useModal

The useModal hook returns the ModalComponent component to render the Modal and openModal function to open the ModalComponent programatically.

Importing the hook


import { useModal } from 'repopup';

Basic Usage


import React from 'react';
import { useModal , RepopupProvider } from 'repopup';
import ReactDOM from 'react-dom';

const App = () => {
const { ModalComponent: Modal, openModal } = useModal({
title: 'React Hooks are ❤️❤️❤️❤️❤️'
});
return (
<React.Fragment>
<Button onClick={() => openModal()}>Open</Button>
<Modal />
</React.Fragment>
);
};

ReactDOM.render(
<RepopupProvider>
<App />
</RepopupProvider>,
,document.querySelector('#app'));

Full Usage

import React from 'react';
import { useModal , RepopupProvider } from 'repopup';
import ReactDOM from 'react-dom';

const App = () => {
const { ModalComponent: Modal, openModal } = useModal({
icon: 'question',
title: 'Do you want to save the changes ?',
showConfirmButton: true,
onConfirm: () => console.log('Saved!!!'),
confirmButtonText: 'Save',
showDenyButton: true,
denyButtonText: `Don't Save`,
showCancelButton: true,
onCancel: 'close'
});
return (
<React.Fragment>
<Button onClick={() => openModal()}>Open</Button>
<Modal />
</React.Fragment>
);
};

ReactDOM.render(
<RepopupProvider>
<App />
</RepopupProvider>,
,document.querySelector('#app'));


Function Signature


function useModal(
options: UseModalArgs,
): {ModalComponent:JSX.Element,openModal: () => void}

Options

OptionTypeDescriptionDefault
backgroundstringPopup window background (CSS background property).'#ffffff'
cancelButtonTextstringUse this to change the text on the "Cancel"-button.Cancel
childrenAfterDefaultContentReactNode | nullUse this to place children after default content of the popupnull
childrenBeforeDefaultContentReactNode | nullUse this to place children before default content of the popupnull
closeOnBackdropClickbooleanWheather or not to close the popup on clicking the backdroptrue
colorstringColor for title and content'#000000'
confirmButtonTextstringUse this to change the text on the "Confirm"-button.'Confirm'
customClassIModalCustomClassType;A custom CSS class for the popup{}
denyButtonTextstringUse this to change the text on the "Deny"-button.'Deny'
iconsuccess| error| info| warning| question| nullvarinat of a icon to be shownnull
onCancelReact.MouseEventHandler<HTMLButtonElement> | undefined | 'close'Use this as a click handler for the "Cancel"-button.() => {}
onConfirmReact.MouseEventHandler<HTMLButtonElement> | undefined | 'close'Use this as a click handler for the "Confirm"-button() => {}
onDenyReact.MouseEventHandler<HTMLButtonElement> | undefined| 'close'Use this as a click handler for the "Deny"-button.() => {}
onSuccessReact.MouseEventHandler<HTMLButtonElement> | undefined| 'close'Use this as a click handler for the "Success"-button.() => {}
placement'top'| 'center' | 'bottom'| 'center-start'| 'bottom-start'| 'top-start'| 'top-end'| 'bottom-end'| 'center-end'Use this to change Popup window positioncenter
showCancelButtonbooleanIf set to true, a "Cancel"-button will be shown.false
showConfirmButtonbooleanIf set to true, a "Confirm"-button will be shown.false
showDenyButtonbooleanIf set to true, a "Deny"-button will be shown.false
showSuccessButtonbooleanIf set to true, a "Success"-button will be shown.false
subtitlestringUsed for a description for the popup.
successButtonTextstringUse this to change the text on the "Success"-button.Ok
titlestringUsed for the title of the popup