How to Add Hover and Focus States to a React Native Mac App

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Building React Native Apps for Mac course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Building React Native Apps for Mac, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Building React Native Apps for Mac

There are other details that make the desktop experience different from mobiles . Namely, we want to give the user more feedback whenever the mouse is used or whenever any element is highlighted. One of the things we need to take care of are hover states. Unfortunately, React Native macOS still doesn't have very support for many of the actions that you would expect, keyword events and hover states and other details. So, we're going to work around some of these issues by using undocumented APIs. This API is my change in the future, but they might also establish themselves. So we're just going to use them for now. So we're going to start by using hover states. I'm going to go into my button component. And inside of the button component, we have the touchable opacity component, right? And so this touchable opacity component has a few undocumented properties that we will take advantage of. One of them is the onMouseEnter prop, which takes a function. And I'm just going to do something like set is overt to true. The other function that we need is onMouseLeave. And I'm going to use this as false. So I need some internal state into my component, right? And to tell it, it's currently being hovered. So I'm going to use a hook for that. It's a simple, simple US state hook, and it's going to start as false. Now you can see because these are undocumented and they're not really part of React Native, of the pure types of React Native, that we're going to have to ignore the type error for now, right? We're just going to tell the TypeScript compiler, don't worry about it. We know it doesn't exist on the types. We're still going to use it. And then we're going to use our small trick that we did before to change our styles. Let me change this into our CW function. And what else do we need to change? Oops. This one, this one. And I'm just going to say to make my life a little bit easier. Let me think about this. So I'm going to change the font size is OK. So I'm going to say my font is VOTE whenever my button is hovered. So now if I go to my application and I hover over my button, you can see the font getting bold. Now this is not perfect because, like I said, React Native Maco has still not super polished around this event. So for example, if I would have another element on top of my button, it would still trigger the on hover state. Right now React Native Maco has, it doesn't do any bubbling. It doesn't prevent that if there is any element on top of this element, then it doesn't trigger the on mouse enter option. So let's say I put another view in here. I'm going to make this absolutely position. This is just to show you guys what this does. And I'm going to say this is a background. What does this show? OK, great. So I'm going to make this cover it a little bit here. Great. So here you can see, even though that there is an element or I can actually move this because right now this is inside of my touchable opacity. So it makes sense. But if I move this outside of it and I am just going to let me think about this . I'm going to pass the right zero. OK, perfect. Right, so now you can see my touchable opacity is the one that has the on mouse enter and mouse leaf. And I have a view on top of this touchable opacity. So in theory, you would expect that the on mouse enter or leaf function wouldn 't trigger, but it does. Right, unfortunately, it is one of the things that currently not working. Maybe in the future it will get solved. But for now we just kind of have to live with it. So make sure whenever you have any overlay things will look a little bit broken . So try to make your overlays not overlap any element that might have a hover state. It's unfortunate, but it is what it is. There is one more detail that we're going to take a look into. So depending on your macro settings, let me just show it to you. Sometimes or some users might have keyboard navigation on. So that means whenever I am on my application and I click on a button or just let me hear, for example, right, I could use my keyboard to highlight different UI elements. But sometimes you might have a hover in box, which makes messes with your design. Let's just say that. Right. So you might want to disable this. So in order to do this, we're going to use another undocumented API. I'm going to go into my books container and on my touchable opacity for my books, I'm going to add the enable focus ring prop and I'm going to set it to false. Now again, because this is undocumented and there is no way to type check it. I'm just going to disable it like that. And now you see it's not visible anymore. Right. So there's a ton of little details like this that you might have to take a look to think about how your app is interacting on a desktop and what are the differences of it interacting on the mobile device. But these are just two examples that should kind of give you an idea what's important to keep an eye out for.