Is there any way to currently create a React Native app that runs in the background within the macOS menu bar?
React Native solves this problem by offering React Native for Windows + macOS framework, enabling developers to build cross-platform desktop applications for Windows and macOS using only a single codebase. This cross-platform development ability provides React Native support for the Windows SDK and the macOS 10.14 SDK.
To create a menu we will use material design library i.e. react-native-paper. Menu contains a list of options that appear temporarily. In our project, we will create a button and on click of that button, menu will appear.
The question is not recent, but I found it searching for this topic and there is a recent relevant update: with the new desktop efforts from Microsoft, this is now feasible, although still tricky.
Credits to ospfranco for the great work.
The only way to do it is by editing your AppDelegate
class and initialize the status button label and its popover content with the Root View content from the React Native application. It's also possible to customize its size, appearance and the button at the bar, but only in Swift code.
func applicationDidFinishLaunching(_ aNotification: Notification) {
let jsCodeLocation: URL
jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "tempomat", initialProperties: nil, launchOptions: nil)
let rootViewController = NSViewController()
rootViewController.view = rootView
popover = NSPopover()
popover.contentSize = NSSize(width: 700, height: 800)
popover.animates = true
popover.behavior = .transient
popover.contentViewController = rootViewController
statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(60))
if let button = self.statusBarItem.button {
button.action = #selector(togglePopover(_:))
button.title = "Tempomat"
}
}
References:
React Native MacOS (Microsoft): https://github.com/microsoft/react-native-macos
Sample code: https://github.com/ospfranco/react-native-macos-menubar-template
Blog post: https://ospfranco.github.io/post/2020/05/23/how-to-make-a-react-native-menu-bar-app-for-mac-os/
As of now, the best way to do this is using the Electron platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With