I have read a lot of similar articles like the following items:
And I have read many of their answers, but it doesn't help me to solve my problem.
I have a navbar component, which has a bars icon on it. whenever a user clicks on it, it should show the sidebar component and in case the user clicks outside the sidebar component, it should be on hidden state (like first).
Here is what I have implemented:
navbar.js
import React, {Component} from 'react';
import {Icon} from 'antd';
import Sidebar from '../sidebar/sidebar.js';
import '../../css/navbar.css';
class Navbar extends Component {
    constructor(props){
        super(props);
        this.state = {
            sidebarVisible: false
        }
        this.sidebarShow = this.sidebarShow.bind(this);
        this.sidebarHide = this.sidebarHide.bind(this);
    }
    sidebarShow(){
        this.setState({
            sidebarVisible: true
        })
        document.addEventListener('click', this.sidebarHide);
    }
    sidebarHide(){
        this.setState({
            sidebarVisible: false
        })
        document.removeEventListener('click', this.sidebarHide)
    }
    render (){
        return (
            <div className="nav-container">
                <div className="bar" onClick={() => this.sidebarShow()} >
                    <Icon type="bars"/>
                </div>
                <div className="nav-logo">
                    خفت کتاب
                </div>
                {this.state.sidebarVisible ? <Sidebar/> : null}
            </div>
        )
    }
}
export default Navbar
sidebar.js
import React, { Component } from 'react'
import {Icon} from 'antd';
import '../../css/sidebar.css'
class Sidebar extends Component {
    render(){
        return (
            <div className="sidebar">
                <div className="sidebar-user">
                    <div className="sidebar-profile">
                        <img src={require('../../images/personal.jpg')}/>
                    </div>
                    <div className="sidebar-welcome">
                        مصطفی قدیمی
                    </div>
                </div>
                <div className="sidebar-active sidebar-elements">
                    <div className="sidebar-icon">
                        <Icon type="home" />
                    </div>
                    <div className="sidebar-title">
                        خانه
                    </div>
                </div>
                <div className="sidebar-elements">
                    <div className="sidebar-icon">
                        <Icon type="book" />
                    </div>
                    <div className="sidebar-title">
                        ثبت کتاب
                    </div>
                </div>
                <div className="sidebar-elements">
                    <div className="sidebar-icon">
                        <Icon type="info-circle" />
                    </div>
                    <div className="sidebar-title">
                        درباره ما
                    </div>
                </div>
                <div className="sidebar-elements">
                    <div className="sidebar-icon">
                        <Icon type="mail" />
                    </div>
                    <div className="sidebar-title">
                        تماس با ما
                    </div>
                </div>
            </div>
        )
    }
}
export default Sidebar;
sidebar.css
.sidebar {
    position: fixed;
    width: 250px;
    height: 100vh;
    background-color: #001529;
    z-index: 1;
    top: 0;
    right: 0;
    color: white;
    animation-name: 'sidebar';
    animation-duration: 1s;
    animation-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
}
.sidebar > div {
    padding: 10px;
}
.sidebar-elements {
    background-color: #334454;
    opacity: 0.4;
    display: grid;
    grid-template-columns: 40px auto;
    margin-bottom: 5px;
}
.sidebar-elements:hover {
    opacity: 1;
    transition-duration: .5s;
}
.sidebar-icon {
    display: flex;
    align-content: center;
}
.sidebar-profile {
    margin: auto;
    background-color: #334454;
    width: 150px;
    height: 150px;
    box-sizing: border-box;
    padding: 0;
    border-radius: 50%;
    position: relative;
    overflow: hidden;
}
.sidebar-profile > img {
    display: block;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    height: auto;
    transform: translate(-50%, -50%)
}
.sidebar-welcome {
    margin: auto;
    text-align: center;
    font-size: 14px;
    color: white;
}
.sidebar-active {
    background-color: #1890ff;
    opacity: 1;
}
@keyframes sidebar {
    from {transform: translateX(250px);}
    to {transform: translateX(0);}
}
@-webkit-keyframes sidebar { 
    from {transform: translateX(250px);}
    to {transform: translateX(0);}
}
The main problem of this implementation is whenever the user clicks on click on
sidebarcomponent, it becomes invisible. How to prevent it from hiding? Note: Is there any way to add animation when the user click on outside of the component?
 
     
    