Creating a web-based application admin, of course, We don’t forget to add Block UI components to the application. Likewise, when I was learning React JS, when I arrived at the HTTP Request session, a system needed a part to block the user from accessing other events or actions while waiting for the HTTP request process to finish.
Under the title of the article, I will share how to create a simple Block Page UI without the help of a plugin. Why make it yourself when the Block UI plugin is easily found on repositories like npmjs?
A little story … I spent all day searching and using the BlockUI React plugin that fit the criteria that I was looking for. After being tested several times when getting a plugin that is good enough, some bugs might be inconvenient for me to find a solution.
If you can make it simpler, get up, and work on it so that the results are more satisfying. At least for self-satisfaction. LOL Okay, just start the tutorial.
Table of Contents
How to make JS React Block UI
Create a new component named BlockUI.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import React from "react"; import "./BlockUI.css"; const BlockUI = props => { if (!props.blocking) { return ""; } else { return ( <div className="block-ui-container"> <div className="block-ui-overlay" /> <div className="block-ui-message-container"> <div className="block-ui-message"> <h4>{props.title}</h4> <div className="loading-indicator"> <svg id="indicator" viewBox="0 0 100 100"> <circle id="circle" cx="50" cy="50" r="45" /> </svg> </div> </div> </div> </div> ); } }; BlockUI.defaultProps = { blocking: false, title: "Loading" }; export default BlockUI; |
Create a new CSS file with the name BlockUI.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | .loading-indicator { text-align: center; } .block-ui { position: relative; min-height: 3em; } .block-ui-container { position: absolute; z-index: 1010; top: 0; right: 0; bottom: 0; left: 0; height: 100%; width: 100%; min-height: 2em; cursor: wait; overflow: hidden; } .block-ui-container:focus { outline: none; } .block-ui-overlay { width: 100%; height: 100%; opacity: 0.75; filter: alpha(opacity=50); background-color: rgb(184, 184, 184); } .block-ui-message-container { position: absolute; top: 50%; left: 0; right: 0; text-align: center; transform: translateY(-50%); z-index: 10001; } .block-ui-message { color: #333; background: none; z-index: 1011; } #indicator { width: 100px; height: 100px; position: absolute; top: calc(50% - 50px); left: calc(50% - 50px); animation: spin 1s linear infinite; } #circle { fill: none; stroke: rgba(255, 255, 255, 0.5); stroke-linecap: round; stroke-width: 4; animation: draw 3s ease-in-out infinite; } @keyframes draw { 0% { stroke-dasharray: 20, 282.6; } 50% { stroke-dasharray: 200, 282.6; } 100% { stroke-dasharray: 20, 282.6; } } @keyframes spin { to { transform: rotate(360deg); } } |
You can change the loading bar with your desire.
Easy way to block page :
1 | <BlockUI blocking=true /> |
The property :
- blocking: default value is false. To activate the BlockUI component, please set blocking to true.
- title: default value, “Loading.” To make a title on the active Block UI overlay.
Example:
The following example is the use of the ReactJS Block Page UI to make API requests using Axios ReactJS.
1 2 3 4 5 6 7 8 9 10 | this.setState({ blocking: true }); axios.get("https://5ezbs.sse.codesandbox.io/user").then(res => { console.log(res.data); this.setState({ blocking: false, user: res.data[0] }); }); |
Here is the full code sample above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import React from "react"; import "./styles.css"; import BlockUI from "./BlockUI/BlockUI"; import axios from "axios"; class App extends React.Component { state = { blocking: false, user: {} }; callApiHandler = () => { this.setState({ blocking: true }); axios.get("https://5ezbs.sse.codesandbox.io/user").then(res => { console.log(res.data); this.setState({ blocking: false, user: res.data[0] }); }); }; render() { return ( <div className="App"> <h1>Example simple block UI</h1> <button onClick={this.callApiHandler}>test block</button> <h4>Result</h4> {Object.keys(this.state.user).length > 0 ? ( <div> Name : {this.state.user.name} <br /> Age : {this.state.user.age} <br /> <img src={this.state.user.image} alt={this.state.user.name} /> </div> ) : ( "" )} <BlockUI blocking={this.state.blocking} /> </div> ); } } export default App; |
Run tests and see the result.
To try it directly, please play with the following playground.
Conclusion
Using the blockui component above is easy way to block the user when system is proccessing or initializing
For simple ui components, you should make it yourself without the help of an external plugin. Why? The application will be lighter, and the system will run according to your wishes.
Thus my short tutorial on how to create a React block UI component . Read the other article about react blog here
Leave a Reply