Loading States
cbwire via Livewire uses AJAX requests to invoke component actions on our wire objects. There are cases when this may involve a long-running process, such as completing a cart checkout, and the page may not respond instantly to a user event like a click. cbwire allows you to effortlessly display loading states, such as showing and hiding elements, adding and removing classes, or toggling HTML attributes until the server responds.
Loading States can make your apps feel responsive and user-friendly.

Toggling Elements

Let's take a look at a checkout action that takes too long.
1
// File: ./wires/Cart.cfc
2
component extends="cbwire.models.Component"{
3
4
function checkout(){
5
sleep( 5000 ); // optimize this code yo!
6
}
7
8
function $renderIt(){
9
return this.$renderView( "wires/cart" );
10
}
11
}
Copied!
We can annotate our <div> with wire:loading to display Processing Payment while the checkout action is running.
1
// File: ./views/wires/cart.cfm
2
<div>
3
<button wire:click="checkout">Checkout</button>
4
5
<div wire:loading>
6
Processing Payment...
7
</div>
8
</div>
Copied!
After the checkout action completes, the Processing Payment output will disappear.
👋

Toggling Attributes

1
<div>
2
<button wire:click="checkout" wire:loading.attr="disabled">
3
Checkout
4
</button>
5
</div>
Copied!

Delay Loading State

Sometimes your component actions complete at the speed of light, and the user doesn't have enough time to see the elements you want to be displayed. You can add a .delay modifier to delay showing your HTML elements for 200ms.
1
// File: ./views/wires/cart.cfm
2
<div wire:loading.delay>...</div>
Copied!

Display Property

Loading State elements are set with a CSS property of display: inline-block; by default. You can override this behavior by using various annotation modifiers.
1
// File: ./views/wires/cart.cfm
2
<div wire:loading.flex>...</div>
3
<div wire:loading.grid>...</div>
4
<div wire:loading.inline>...</div>
5
<div wire:loading.table>...</div>
Copied!

Hide During Loading State

If you would like to display an element EXCEPT during a loading state, you can apply the wire:loading.remove annotation.
1
// File: ./views/wires/cart.cfm
2
<div>
3
<button wire:click="checkout">Checkout</button>
4
5
<div wire:loading.remove>
6
Hide Me While Loading...
7
</div>
8
</div>
Copied!
Last modified 2mo ago