Open In App

React Suite Modal Accessibility Keyboard Interaction

React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application.

In this article, we’ll learn about React suite Modal Accessibility Keyboard Interaction. The keyboard prop is used to interact with the keyboard which closes the modal using the ‘Esc’ key.



Modal Props:

Modal.Header Props:



Modal.Title Props:

Modal.Footer Props:

Modal.Body Props:

Syntax:

import { Modal } from "rsuite";

export default function App() {
    return (
        <Modal keyboard={false}>
            ...
        </Modal>
    );
}

Creating React Application And Installing Module:

Step 1: Create a React application using the given command:

npm create-react-app projectname

Step 2: After creating your project, move to it using the given command:

cd projectname

Step 3: Now Install the rsuite node package using the given command:

npm install rsuite

Project Structure: Now your project structure should look like the following:

 

Example 1: Below example demonstrates the modal when the keyboard is set to ‘false’.

Note: In the given example, the modal is closed using the close button and not using the ‘Esc’ key.




import { useState } from "react";
import { Button, Modal } from "rsuite";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
    const [open, setOpen] = useState(false);
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Modal Accessibility 
                    Keyboard Interaction
                </h4>
  
                <div style={{ marginTop: 20, width: 400 }}>
                    <div className="modal-container">
                        <Button onClick={handleOpen} 
                            appearance="primary" color="green">
                            Open
                        </Button>
  
                        <Modal open={open} onClose={handleClose} 
                            keyboard={false}>
                            <Modal.Header>
                                <Modal.Title>GeeksforGeeks</Modal.Title>
                            </Modal.Header>
                            <Modal.Body>
                                <div>
                                    <h3>You can't close this Modal 
                                        using ESC key.</h3>
                                </div>
                            </Modal.Body>
                            <Modal.Footer>
                                <Button
                                    onClick={handleClose}
                                    appearance="primary"
                                    color="green"
                                >
                                    Ok
                                </Button>
                                <Button onClick={handleClose} 
                                    appearance="subtle">
                                    Cancel
                                </Button>
                            </Modal.Footer>
                        </Modal>
                    </div>
                </div>
            </div>
        </center>
    );
}

Steps to run the application: Write the below code in the terminal to run the server:

node index.js

Output:

 

Example 2: Below example demonstrates the modal when the keyboard is set to ‘true’.

Note: In the given example, the modal is closed using the ‘Esc’ key.




import { useState } from "react";
import { Button, Modal } from "rsuite";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
    const [open, setOpen] = useState(false);
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Modal Accessibility 
                    Keyboard Interaction
                </h4>
  
                <div style={{ marginTop: 20, width: 400 }}>
                    <div className="modal-container">
                        <Button onClick={handleOpen} 
                            appearance="primary" color="green">
                            Open
                        </Button>
  
                        <Modal open={open} onClose={handleClose} 
                            keyboard={true}>
                            <Modal.Header>
                                <Modal.Title>GeeksforGeeks</Modal.Title>
                            </Modal.Header>
                            <Modal.Body>
                                <div>
                                    <h3>You can close this Modal 
                                        using ESC key.</h3>
                                </div>
                            </Modal.Body>
                            <Modal.Footer>
                                <Button
                                    onClick={handleClose}
                                    appearance="primary"
                                    color="green"
                                >
                                    Ok
                                </Button>
                                <Button onClick={handleClose} 
                                appearance="subtle">
                                    Cancel
                                </Button>
                            </Modal.Footer>
                        </Modal>
                    </div>
                </div>
            </div>
        </center>
    );
}

Output:

 

Reference: https://rsuitejs.com/components/modal/#keyboard-interaction


Article Tags :