Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Burger menu using bulma on React not working

I am new to react. Im trying to make a navigation in my header. i use bulma css :

"bulma": "^0.7.4",

And i import bulma file like this:

import 'bulma/css/bulma.css';

It is working for the most of the css but not with burger menu, the menu is not collapsed when i click on the burger button

Here is my header code:

import React,{Component} from 'react'
import {NavLink} from 'react-router-dom';
import style from './style.css';

class Header extends Component{

    render(){
        return (
            <React.Fragment>
                <nav className="navbar is-fixed-top" role="navigation" aria-label="main navigation">
                    <div className="navbar-brand">
                        <a href="/" className="navbar-item" to="/">
                        <img src="" alt="" width={112} height={28}/>
                        </a>

                        <a role="button" className="navbar-burger burger " aria-label="menu" aria-expanded="false" data-target="mainNavbar">
                        <span aria-hidden="true"></span>
                        <span aria-hidden="true"></span>
                        <span aria-hidden="true"></span>
                        </a>
                    </div>

                    <div  id="mainNavbar" className="navbar-menu">

                        <div className="navbar-start">
                            <NavLink exact activeClassName={style.active} to="/"  className="navbar-item">
                                Home
                            </NavLink>

                            <NavLink activeClassName={style.active} to="/films"  className="navbar-item">
                                Films
                            </NavLink>
                        </div>

                    </div>
                    </nav>  
            </React.Fragment>
        );
    }
}


export default Header;

The

data-target

attribute on the burger button is not triggering the menu. Pleas let me know what im doing wrong. Thanks

like image 485
rizesky Avatar asked Mar 06 '19 04:03

rizesky


People also ask

Does Bulma React work?

Designed for mobile first and built using the Flexbox methodology, the open source CSS framework Bulma provides ready-to-use frontend components to build responsive web interfaces without JavaScript, making it ideal to use with React.


2 Answers

Bulma doesn't have a built in toggle event you have to create it manually by adding "is-active" class in "navbar-burger" and "navbar-menu" here I have achieved that with React Hooks

 const [isActive, setisActive] = React.useState(false);
 <nav className="navbar" role="navigation" aria-label="main navigation">
        <div className="navbar-brand">
          <a
            onClick={() => {
              setisActive(!isActive);
            }}
            role="button"
            className={`navbar-burger burger ${isActive ? "is-active" : ""}`}
            aria-label="menu"
            aria-expanded="false"
            data-target="navbarBasicExample"
          >
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
          </a>
        </div>
        <div
          id="navbarBasicExample"
          className={`navbar-menu ${isActive ? "is-active" : ""}`}
        >
          <div className="navbar-start">
            <a className="navbar-item">Home</a>
            <a className="navbar-item">Documentation</a>
          </div>
        </div>
   </nav>
like image 125
Sjs Ahd Avatar answered Sep 24 '22 22:09

Sjs Ahd


As stated in another answer, additional js is required to make Bulma's burger menu work. Below is how I did it. The key in toggleBurgerMenu. Also, toggleBurgerMenu needs to be specified in the onClick event of each navbar-item so that the burger menu closes after a menu item is clicked.

import React from 'react';
import { Link } from 'react-router-dom';

const Navigation = (props) => {
  function toggleBurgerMenu() {
    document.querySelector('.navbar-menu').classList.toggle('is-active');
  }

  return (
    <nav className="navbar" role="navigation" aria-label="main navigation">
      <div className="navbar-brand">
        <Link to="/" className="navbar-item">React App</Link>

        <a role="button" className="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasic"
          onClick={toggleBurgerMenu}>
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
        </a>
      </div>

      <div id="navbarBasic" className="navbar-menu">
        <div className="navbar-start">
          <Link to="/about" className="navbar-item" onClick={toggleBurgerMenu}>About</Link>
          <Link to="/contact" className="navbar-item" onClick={toggleBurgerMenu}>Contact</Link>
          <Link to="/notes" className="navbar-item" onClick={toggleBurgerMenu}>Notes</Link>
        </div>
      </div>
    </nav>
  );
}

export default Navigation;
like image 39
datchung Avatar answered Sep 21 '22 22:09

datchung