DEV Community

Cover image for Create A Menu For Your Gatsby Website Without Libs

Create A Menu For Your Gatsby Website Without Libs

David Dal Busco
Creator of DeckDeckGo | Organizer of the Ionic Zürich Meetup
Originally published at Medium Updated on ・3 min read

I share one trick a day until the end of the COVID-19 quarantine in Switzerland, April 19th 2020. Twenty-one days left until hopefully better days.

I have developed my personal website with Gatsby but without using any templates or any design libraries. No particular reason, I just like in my personal projects to restrict the use of dependencies when I can. Thereby, I had to build my own simple menu for the navigation which I’m sharing with you today.

Menu Component

The menu is nothing else than a component which contains a state to reflect its status, being open or close. Its only particularity is the fact that we have to expose a function open() in order to be able to trigger its opening from the navigation, in my case, from the hamburger menu in the navigation bar. For such purpose we use Hooks API useImperativeHandle to customize our instance.

import React, { useImperativeHandle, useRef } from "react"

import { Link } from "gatsby"

import styles from "./menu.module.scss"

class Menu extends React.Component {
  constructor(props) {
    this.state = {
      open: false,

  render() {
    return (
        className={`${} ${ ? `${}` : ""}`}
        onClick={() => this.close()}
        onKeyDown={() => this.close()}
        <Link to="/"><h1>Home</h1></Link>
        <Link to="/#portfolio"><h1>Portfolio</h1></Link>
        <Link to="/#about"><h1>About</h1></Link>
        <Link to="/blog"><h1>Blog</h1></Link>
        <Link to="/#contact"><h1>Contact</h1></Link>

  close() {
    this.setState({ open: false })

  open() {
    this.setState({ open: true })

export default React.forwardRef((props, ref) => {
  const menuRef = useRef()

  useImperativeHandle(ref, () => ({
    open() {

  return <Menu ref={menuRef} {...props} />
Enter fullscreen mode Exit fullscreen mode

For the styling, I use CSS modules in the above example. The menu is as a fixed div which covers the all screen and is per default not visible. With the help of the state, I apply a style to modify its opacity with a small transition to make the menu appears a bit smoothly.

.menu {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background: #ffffff;
  z-index: 1031;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  visibility: hidden;
  opacity: 0;

  transition: all 0.35s;

  cursor: pointer;

  &.open {
    visibility: visible;
    opacity: 1;

  a {
    pointer-events: all;
Enter fullscreen mode Exit fullscreen mode

Integrating The Menu

To use the menu in the navigation, I have integrated in its related component. The only particularity I had to develop is assigning a reference in order to be able call the method open() from an onClick function.

Note that as the menu inherits a fixed style, it can be integrated at any level of the DOM.

import React from "react"

import Menu from "./menu"

class Navigation extends React.Component {

  render() {
    return (
        <button onClick={() => this.toggleMenu()}>
          Open Menu

        <Menu ref={el => (this.childMenu = el)} />

  toggleMenu() {

export default Navigation
Enter fullscreen mode Exit fullscreen mode

That’s it, nothing more, nothing less, we have developed a custom menu for our website without any JavaScript dependencies 😁.


I like to use Gatsby for website. It has many advantages and I think it is also cool for me that it uses React as I like on a weekly basis to not stick to the same technologies. A bit of Angular, a bit of React, a bit of Web Components with StencilJS, a bit of vanilla JavaScript, everything is only fun 😉.

Stay home, stay safe!


Cover photo by Catherine Heath on Unsplash

Discussion (2)

ywroh profile image

I am currently making an ecommerce site with gatsby tailwind, and I was able to open a modal menu using the information you provided. Thank you.

daviddalbusco profile image
David Dal Busco Author

Awesome!!! My pleasure and thank your for the feedback 😃