Showing Offline Message0:00
It's inevitable, but at some point in time, we will be faced with an offline device. And as a User, that can be very frustrating to be offline and not really know it until you start clicking or tapping on something and then nothing works. So ideally, we would provide users some information when their device is offline, and we can do so with the wire:offline attribute. The most basic usage is just applying that attribute to an element. By default, this element will be hidden and it will only appear when the device is offline. So we are currently online. If we work offline, we can see our message, you are offline. But I want to provide a little more from a visual standpoint, like for example, the
Styling Links When Offline0:47
If we work offline, we can see our message, you are offline. But I want to provide a little more from a visual standpoint, like for example, the links for the articles. We can provide a visual cue to change the color of those links, and we can do so by using the class modifier, which also has a remove modifier, the idea being that we can specify the classes that we want to remove from the element when we are offline. So in this case, I'm going to remove both the color and the hover effect, so that now when we go offline, not only do we get a message saying you are offline, but now we have this nice visual cue leading the user to think, huh, something is definitely not right. But if we move on over to the search box, we can see that we can interact with that.
Disabling Inputs Offline1:28
nice visual cue leading the user to think, huh, something is definitely not right. But if we move on over to the search box, we can see that we can interact with that. And I think a better solution would be to disable this input element when we are offline. So once again, we can use wire:offline, and we have an attribute modifier, ATTR. And then we just specify the attribute that we want to apply to that element when we are offline. So once again, whenever the browser detects that it is offline, we have our message, we have the color change for our links. But now as I move the mouse over the search box, and I attempt to focus the search box, I am unable to do so because it is disabled.
Restoring UI Online2:08
But now as I move the mouse over the search box, and I attempt to focus the search box, I am unable to do so because it is disabled. But the moment that the browser detects that, hey, we are back online, then everything returns back to normal. Our message goes away, the links all have their appropriate color and hover effect, and I can come over to the search box, and I can interact with it. Now there are a couple of other modifiers, like for example, if we can add an attribute, it almost goes without saying that we can remove an attribute. So that if the browser detects that we are offline, then we can remove an attribute from an element.
Adding/Removing Classes Attributes2:43
So that if the browser detects that we are offline, then we can remove an attribute from an element. And likewise, if we can remove a CSS class, then it almost goes without saying that we can add a CSS class. All we have to do is use the class modifier, assign whatever classes that we want, and then when the browser detects it is offline, those classes are applied to the element. So the offline feature is very simple and straightforward. The most basic usage is to just apply the wire:offline attribute to an element, and when the browser detects that it is offline, that element will be displayed. But you can also use that attribute to add or remove classes, and add or remove attributes.
when the browser detects that it is offline, that element will be displayed. But you can also use that attribute to add or remove classes, and add or remove attributes. You'll just need to use the class and attribute modifiers to get your desired results.
