Making Electron apps feel native on Mac
Tags: javascript, electron, mac, app
Published: 2021-03-23 19:41:21

---
title: Making Electron apps feel native on Mac
published: true
tags: javascript, electron, mac, app
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xbu4d6fgfpi6t69j8ub.png
---

_This is a repost from my building-in-the-open blog of [Lotus](https://getlotus.app) - app for managing GitHub notifications without stress. Enjoy!_

When I only started thinking about [Lotus](https://getlotus.app), I knew I'd use [Electron](https://www.electronjs.org/). Developers like to joke that you need 64 gigs of RAM to run any Electron app, but I find this next joke much funnier:

> My app uses 0 GB of RAM, because I didn't finish it

I couldn't find the original tweet, but you get the idea. If I had decided to create a native app using Swift, I bet I'd spend several months failing all the time because I have almost no experience with that programming language and then just give up.

So I chose Electron and used my existing skills of building web apps to create an app for Mac. I'm glad I did, because I had a working prototype in just [4 days](https://getlotus.app/4-rounding-up).

However, just because Lotus isn't a native app, it doesn't mean it can't feel like a native app.

Making Electron apps embrace the same standards and patterns of native apps. I've written down everything I know about it so far, hope it will be useful to my fellow Electron developers!

## Windows

### First render

Electron is essentially a web browser underneath, so it needs to load all the HTML, CSS and JavaScript files of your app after window is created. This can take some time, that's why Electron windows show up blank for a fraction of second.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/4495735d-5396-465b-ab6e-436436313e06.gif)

There's a small trick to show a window only after page is loaded:

```js
const {BrowserWindow} = require('electron');

const window = new BrowserWindow({
    show: false
});

window.once('ready-to-show', () => {
    window.show();
});
```

Check out how it looks after applying this change and compare it to the demo above:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/ab5a0f82-2310-437b-b41d-e63c7652951f.gif)

### Restore window position

When you move a window somewhere or resize it, Lotus remembers the new position and dimensions of that window. Next time you launch Lotus, window will be in the exact same position it was the last time and have the same width and height. It's one of those things that are hard to notice, but users have still learned to expect this from native apps.

Thanks to [electron-window-state](https://github.com/mawie81/electron-window-state) it's quite easy to implement for any Electron app.

### Draggable areas

In macOS apps often have a custom titlebar and users expect to be able to drag the entire window by pressing on the empty space there.

Here's a demo of how you can drag the window by pressing anywhere in the top area of the app:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/7f358dba-a5c8-4e9c-a881-423d8edf59ed.gif)

Note how window is not moving when I'm trying to drag by pressing on the "Inbox" label. This is an important detail to keep in mind.

To implement these draggable areas I use two CSS classes:

```css
.drag {
    -webkit-app-region: drag;
}

.no-drag {
    -webkit-app-region: no-drag;
}
```

You can add a `.drag` class to the entire titlebar container element and selectively add `.no-drag` to elements that should prevent the drag interaction. Here's an example:

```html
<div class="drag">
    <h1 class="no-drag">Inbox</h1>
</div>
```

## User interface

### Font

I have to admit that I made it for 5 months before realizing that text in Lotus looks bigger compared to all the other apps I use. Styling in Lotus is powered by [Tailwind](https://tailwindcss.com) and it sets a default font size of 16px. This looks fine on the web, but it certainly stands out inside a desktop app.

[Sindre](https://sindresorhus.com) told me that a default system font size in native apps is 13px, but it didn't look good in Lotus, so I went with 14px as a compromise. Actually, I like it more now!

Tailwind uses [`rem`](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units) unit to define all sizes in its source code, which allowed me to fix the font size issue by adding one line of code.

```css
html {
  font-size: 14px;
}
```

In CSS, `rem` is calculated relatively to the root font size. So in this case, if I'd specify `1rem`, browser would interpret it as `14px`, because that's what I've set above for the entire page.

Also, use system font in your Electron app to make it a good macOS citizen. Tailwind sets it for me by default, but here's how to use a system font if you're not a Tailwind user:

```css
html {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont;
}
```

### Cursor

I literally discovered this a few days ago when Sindre pointed it out to me. Native apps use a default cursor (not the "hand" one) even for buttons and other clickable elements. I completely blocked that out, because I'm so used to setting `cursor: pointer` for interactive elements on the web.

This is simple to fix too:

```css
*, a, button {
    cursor: default;
    user-select: none;
}
```

Pointer (or "hand") cursor should only be used for actual links that lead outside the app.

### Dark mode

This feature needs no introduction, but there's one little-known trick to support dark mode flawlessly in Electron. Let me describe the problem first though.

Lotus has a dark gray background in dark mode and one day when I was resizing its window, I noticed this:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/5f739049-9642-49f7-86a4-b1872d877ce0.gif)

Default background color in Electron window is white. When I'm quickly resizing it, Electron can't resize the page inside as fast as native apps do, which results in these flashes of white background, even though my page has a gray background.

To fix this, set window background color to the same color that's used on the page. Then, update it whenever system switches to/from dark mode.

```js
const {nativeTheme, BrowserWindow} = require('electron');

const darkBackgroundColor = 'black';
const lightBackgroundColor = 'white';

const window = new BrowserWindow({
	backgroundColor: nativeTheme.shouldUseDarkColors
		? darkBackgroundColor
		: lightBackgroundColor
});

nativeTheme.on('updated', () => {
	const backgroundColor = nativeTheme.shouldUseDarkColors
		? darkBackgroundColor
		: lightBackgroundColor;

	window.setBackgroundColor(backgroundColor);
});
```

You won't see any flashes of white background anymore no matter how fast you resize the window.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/dd60188f-acea-462e-90a5-327157ff20d3.gif)

### Unfocused variant of UI elements

Lotus has a sidebar navigation with colorful icons inside each item and a bright purple background for a currently selected page. When Lotus is focused, all colors are displayed as-is:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/fbd104bf-1967-407d-be51-208fb569b1e5.jpg)

But if you click away or switch to some other app, Lotus loses focus and replaces colors with the shades of gray:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/ea674089-df6a-49e5-a9e9-2cfd96cc4873.jpg)

This seems like another small pattern from native apps that is easy to miss. It also requires code in both main and renderer processes to make it work.

In the main process, you need to detect when window is focused or unfocused and pass these events to the renderer process. Because renderer process is basically a browser, the page never loses focus in its "eyes", since it's always visible within Electron window.

```js
window.on('focus', () => {
    window.webContents.send('focus');
});

window.on('blur', () => {
    window.webContents.send('blur');
});
```

Then, in renderer process you need to listen to these messages from the main process by using `ipcRenderer` module.

```js
const {ipcRenderer} = require('electron');

ipcRenderer.on('focus', () => {
	// Change UI state to focused
});

ipcRenderer.on('blur', () => {
	// Change UI state to unfocused
});
```

Lotus is written in React, so I packaged the renderer piece into a handy `useWindowFocus` hook, which I use like this:

```js
const isWindowFocused = useWindowFocus();

return <NavItem className={isWindowFocused ? 'bg-purple' : 'bg-gray'}>…</NavItem>;
```

## Menu

Most Mac apps have a standard menu and Electron apps should have it too.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/43196a90-6700-4b50-be92-358dace35267.jpg)

It's fairly simple to set it up by using [`Menu`](https://www.electronjs.org/docs/api/menu) class provided by Electron. Here are some useful links to get you started quicker and create a standard macOS menu immediately:

- [electron-boilerplate](https://github.com/sindresorhus/electron-boilerplate/blob/main/menu.js)
- [electron-default-menu](https://github.com/carter-thaxton/electron-default-menu)

I opted to create a custom menu in Lotus, because I needed a lot of custom items in there. Which also brings us to the next tip.

## Shortcuts

It's still somewhat rare to spot proper keyboard shortcuts in web apps, but they're a first-class citizen in native ones. It's really simple to add them in Electron, so you literally don't have any excuse not to! First, add a custom menu item, then use an `accelerator` property to configure a shortcut that's going to trigger that item.

```js
{
    label: 'Refresh',
    accelerator: 'CmdOrCtrl+R',
    click: () => {
        // User clicked on the menu item or pressed ⌘R
    }
}
```

It may sound weird at first that a menu item is required for a shortcut to work, but keep in mind that users often browse the app's menu first and only then learn which shortcuts it has.

In Lotus I created a separate menu section for actions related to managing a notification that's currently displayed with a shortcut assigned to each action:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/e104746c-a17d-440e-acb1-37d5a8800b41.jpg)

## Undo / redo

This is another feature that web apps often miss. It's interesting that we always expect native apps to allow us to undo or redo any action, but we don't have the same expectation on the web. Anyway, make sure to add this to your Electron app sooner or later, it's going to significantly up your native-app game.

Undo / redo was a complicated feature to develop and I had to rewrite it multiple times, but I think I've landed at an implementation that is abstract enough to be reused and open-sourced later.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/95241231-3229-4e32-980b-31f130c4ed0a.gif)

## Preferences

I made the mistake of showing "Preferences" page just like all other pages in the sidebar navigation before, but now Lotus has a separate native-like window. It even animates as you switch between tabs! Time well spent for sure.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/2cf61627-fa1c-4d55-b05a-cfbae16f2db0.gif)

There's also no need to add any button in the UI to open preferences window, because all native macOS apps follow the same pattern of adding "Preferences" item to the menu and using a <kbd>⌘,</kbd> shortcut to open it.

![](https://buttondown.s3.us-west-2.amazonaws.com/images/e7e097ba-6ed1-489a-85a2-7fdeb21e976d.jpg)

Bad news here, there's nothing I could find to create preferences window quickly, so you'll need to code it yourself.

## Offline support

Unless your app absolutely can't function without an internet connection, it should gracefully degrade to an offline-first experience by syncing changes when a connection becomes available. I actually almost finished implementing offline support in Lotus, even though it depends on external data from GitHub API.

Here's a few tips on how Lotus works offline:

- No data is fetched from GitHub API until a connection becomes available.
- Startup is instant, because Lotus doesn't even attempt to fetch new notifications.
- You can still mark notifications as read, reply later and perform all other actions. Lotus will put these in a queue and sync these changes to GitHub when you go online in an order those actions were performed.
- Embedded github.com webviews obviously can't work, so I show a fallback UI instead:

![](https://buttondown.s3.us-west-2.amazonaws.com/images/6e45ec33-72b0-4e05-bab2-590a75b0176f.jpg)

Hope this deep dive into Electron UX was interesting and useful for you!

What other details or patterns would make Electron apps feel more native? Anything I missed?