Building a Menu Bar Application with Electron.js and React

Written by

Share this post:

Building a Menu Bar Application with Electron.js and React

This tutorial will guide you through the process of creating a menu bar application using Electron.js and React.

Electron.js, a cross-platform framework, empowers developers to build desktop applications using web technologies. Combined with React, another powerful JavaScript library, the pair can be harnessed to create a menu bar application that's functional, intuitive, and versatile.

In this comprehensive guide, we'll delve deep into the process of creating a menu bar application using Electron.js and React. We will also discuss how to use Electron's built-in modules to communicate between processes and persist data, and how to customize the menu bar application to suit your needs.

Tables Of Contents

  • Understanding Electron.js
  • Electron.js Processes
  • Setting up the Electron.js Project
  • Basics of Electron.js
  • Generating a Window with BrowserWindow
  • Creating a Tray Object
  • Implementing Data Persistence with electron-store
  • Launching the Application at Startup
  • Inter-Process Communication (IPC) in Electron.js
  • Customizing the Menu Bar
  • Distributing the Application
  • Wrapping up

Understanding Electron.js

Electron.js is built on Node.js and Chromium, providing the ability to develop desktop applications using HTML, CSS, and JavaScript. The applications created are cross-platform, meaning they can run on Windows, macOS, and Linux.

Electron.js Processes

Electron.js operates on two processes - main and renderer. The main process is akin to the server-side of a traditional client-server relationship, managing windows and handling system events. The renderer process, analogous to the client-side, controls what is displayed inside the windows.

These two processes need to communicate but are not inherently aware of each other. To bridge this gap, Electron.js provides the ipcMain and ipcRenderer modules, which function as EventEmitters.

Setting up the Electron.js Project

Starting a project with Electron.js and React can be daunting. To simplify the process, you can use a boilerplate, such as the one provided by FrontTribe, a frontend development agency renowned for its expert-certified developers and extensive expertise in web app and mobile app development.

Basics of Electron.js

To get started, you need to import the necessary modules. Let's begin with the basic imports to the main.js file, which is the entry point for our Electron application.

Generating a Window with BrowserWindow API

The BrowserWindow API allows the creation of a browser window with the specified options. We create a function called createMainWindow and define a BrowserWindow function within it.

Here, we declare the mainWindow variable outside the createMainWindow function, as we will need to access it globally later. The BrowserWindow instance is created only after the app is ready. Therefore, we must use the ready event, which is emitted when Electron has finished initializing:

At this point, we have only created BrowserWindow and specified what to display. However, we don't have any renderer process running. To start a renderer process, you can run npm run client to start a server on localhost port 3000, which is loaded by the loadURL method in development mode.

Creating a Tray Object

Next, we want to create a Tray object, which displays the specified icon in the menu bar. To do this, we create a class named TrayGenerator that bundles all the functionalities inside.

The TrayGenerator class will contain the following functions:

  • getWindowPosition - Calculates the x and y coordinates of BrowserWindow to position it relative to the Tray icon
  • showWindow - Sets the position of the mainWindow object using the getWindowPosition() function
  • toggleWindow - Toggles the visibility of the mainWindow object
  • rightClickMenu - Creates an array of options for the context menu when the user right clicks on the Tray icon
  • createTray - Creates a Tray object and adds event listeners for click and right-click events

Implementing Data Persistence with Electron-Store

Data persistence becomes crucial when you need to store user preferences or application state. A useful npm package called electron-store can be used to persist data on the main process.

Launching the Application at Startup

We can use electron-store to store a user preference, such as "Launch at startup". To do this, install and import the electron-store package and then create a store with a schema in main.js.

Inter-Process Communication (IPC) in Electron.js

Inter-Process Communication (IPC) is a system that allows different processes to communicate with each other. In Electron.js, IPC is achieved using the ipcMain and ipcRenderer modules. These modules allow sending and receiving messages across processes using event listeners and emitters.

Implementing IPC

In our React application, we'll create a basic counter where you can increase or decrease a number by clicking the buttons. To do this, we need to send the updated counter value from the renderer process to the main process using IPC.

Customizing the Menu Bar

Electron.js allows you to customize the menu bar to fit your application's needs. You can create custom menu items to perform specific actions, such as minimize, maximize, or close the window, or open a specific menu action.

Once you've created a custom menu item, you can add it to the menu bar using the menuBar.add() method.

menuBar.add(minimizeMenuItem);

Distributing the Application

After implementing your application, the final step is to distribute it. You can use a package like electron-builder to distribute your Electron app. If you're using a boilerplate, you simply need to run npm run build to create a production build of your React app, and then run npm run dist to distribute your Electron app.

Wrapping up

In this guide, we've thoroughly explored the process of building a menu bar application using Electron.js and React, a powerful combination that allows developers to create cross-platform desktop apps using web technologies. We've also discussed the use of Electron's built-in IPC modules for process communication and data persistence, as well as customization of the menu bar.

Building a menu bar application with Electron.js and React can seem daunting at first. But with proper understanding and the right tools, it can be a rewarding experience. Keep learning and experimenting, and you'll soon master the art of building robust and dynamic desktop applications with Electron.js and React.

Are you ready to take your frontend development skills to the next level? Visit FrontTribe today to learn more about their expert-certified developers and extensive expertise in web app and mobile app development.