Wirestrap is still in active development

This project is pre-1.0 and may introduce breaking changes at any time without prior notice.

Alert

Programmatic alert dialogs and confirmation prompts. Dialogs are created on the fly and appended to the document body. Trigger them via the $wirestrap.alert Alpine magic helper, Wirestrap.alert in vanilla JS, or from Livewire PHP via the WithWirestrap trait.

Alerts are queued: only one is visible at a time, and each one appears after the previous is dismissed.

Showing an alert

Pass a string for a bare message, or an options object for anything more:

// Alpine — simple string
$wirestrap.alert.show('Changes saved.')

// Alpine — options object
$wirestrap.alert.show({ type: 'success', message: 'Changes saved.' })

Outside of an Alpine context, use Wirestrap.alert.show() directly:

// Vanilla JS
Wirestrap.alert.show('Changes saved.')
Wirestrap.alert.show({ type: 'success', message: 'Changes saved.' })

From a Blade template, call it directly on a button click:

<button x-on:click="$wirestrap.alert.show('Action completed.')">
    Show alert
</button>

Variants

Five built-in variants: primary, success, info, warning, danger. type is just a CSS class on the alert element, so any custom class name works.

$wirestrap.alert.show({ type: 'primary', title: 'Primary' message: 'A new version is available.' })
$wirestrap.alert.show({ type: 'success', title: 'Success' message: 'Changes saved.' })
$wirestrap.alert.show({ type: 'info', title: 'Info' message: 'Your export is ready.' })
$wirestrap.alert.show({ type: 'warning', title: 'Warning' message: 'Unsaved changes will be lost.' })
$wirestrap.alert.show({ type: 'danger', title: 'Danger' message: 'Something went wrong.' })

Auto-dismiss

Set duration (ms) to dismiss the alert automatically. A progress bar animates for the countdown. duration: 0 is the default and keeps the alert open until dismissed.

$wirestrap.alert.show({
    type: 'success',
    message: 'Changes saved.',
    duration: 3000
})

Dismiss behavior

The alert can be dismissed via the close button, a backdrop click, or Escape. All three can be disabled independently. When a blocked dismiss is attempted, the dialog shakes.

$wirestrap.alert.show({
    type: 'warning',
    message: 'You must acknowledge this.',
    backdropDismiss: false,
    escapeDismiss: false,
})

Set url and the dismiss button becomes a real link, for a redirect you offer rather than impose. Because it is an actual <a href>, the browser keeps its usual behavior: the target shows in the status bar, and a middle-click or Ctrl/Cmd-click opens it in a new tab. Any click closes the alert.

$wirestrap.alert.show({
    type: 'success',
    message: 'Record created.',
    url: '/records/42',
    dismissText: 'View record',
})

The link replaces the dismiss button, so it is the only button in the dialog. Leave backdropDismiss and escapeDismiss enabled — they are how the user declines and closes the alert.

Same option from Livewire:

$this->alert(
    type: 'success',
    message: 'Record created.',
    dismissText: 'View record',
    url: route('records.show', $record),
);

From Livewire

Use $this->alert() in any Livewire component that includes the WithWirestrap trait. Multiple calls in the same request are queued and displayed one after the other.

use Wirestrap\Traits\WithWirestrap;

class MyComponent extends Component
{
    use WithWirestrap;

    public function save(): void
    {
        // ...
        $this->alert('success', 'Changes saved.');
        $this->alert(type: 'danger', message: 'Something went wrong.', title: 'Error');
    }
}

Global configuration

Call Wirestrap.alert.configure() once at boot in your app.js to set defaults for every alert. Per-call options always take precedence.

Wirestrap.alert.configure({
    duration: 4000,
    dismissText: 'Close'
})

Confirm dialog

$wirestrap.alert.confirm() shows a confirmation prompt and calls a Livewire method when the user confirms.

Shorthand: message, method name, then any number of params as individual arguments:

<button x-on:click="$wirestrap.alert.confirm('Delete this record?', 'delete')">
    Delete
</button>
{{-- Any number of extra arguments are forwarded as method parameters --}}
<button x-on:click="$wirestrap.alert.confirm('Move item?', 'move', {{ $item->id }}, {{ $target->id }})">
    Move
</button>

Use a full options object when you need to customize the dialog:

<button x-on:click="$wirestrap.alert.confirm({
    type: 'danger',
    title: 'Delete record',
    message: 'This action cannot be undone.',
    method: 'delete',
    params: [{{ $record->id }}],
    confirmText: 'Yes, delete',
    cancelText: 'Cancel',
})">
    Delete
</button>

In vanilla JS, use Wirestrap.alert.confirm.show() and pass the wire instance explicitly — the Alpine auto-resolution is not available outside an Alpine context:

// wire must be passed explicitly (no auto-resolution outside Alpine)
Wirestrap.alert.confirm.show({
    type: 'danger',
    title: 'Delete record',
    message: 'This action cannot be undone.',
    wire: Livewire.find(wireId),
    method: 'delete',
    params: [recordId],
    confirmText: 'Yes, delete',
})

Configure confirm defaults independently via Wirestrap.alert.confirm.configure():

Wirestrap.alert.confirm.configure({
    type: 'warning',
    title: 'Confirm',
    confirmText: 'Yes',
    cancelText: 'No'
})

Redirect dialog

$wirestrap.alert.redirect() shows a blocking alert, then sends the browser to url once the countdown ends. Use it to let the user read a message instead of redirecting them without warning.

Its defaults differ from a plain alert: duration is 2000 so there is always a countdown, and backdropDismiss and escapeDismiss are false, so the dialog blocks the view and shakes on any attempt to close it. The only way out is forward: the dialog carries a link to url, so an impatient user can skip the wait — clicking it performs the same navigation, just earlier.

// Shorthand: message, url, duration
$wirestrap.alert.redirect('Record deleted, redirecting…', '/records', 2000)

// Options object
$wirestrap.alert.redirect({
    type: 'success',
    title: 'Deleted',
    message: 'Record deleted, redirecting…',
    url: '/records',
    duration: 2000,
})

Rename that link with dismissText:

// Custom label on the link
$wirestrap.alert.redirect({
    message: 'Redirecting to your dashboard…',
    url: '/dashboard',
    duration: 8000,
    dismissText: 'Go now',
})

Navigation is a native full page load, so any target works, same origin or not. The alert stays on screen while the destination loads, instead of flashing the page underneath. With duration: 0 there is no countdown at all and the link becomes the only way out, so do not turn both off.

From Livewire, $this->alertRedirect() replaces a plain $this->redirect() when you want the user to read something first:

use Wirestrap\Traits\WithWirestrap;

class MyComponent extends Component
{
    use WithWirestrap;

    public function destroy(Record $record): void
    {
        $record->delete();

        // Informs the user, then navigates once the countdown ends
        $this->alertRedirect(
            url: route('records.index'),
            message: 'Record deleted, redirecting…',
            type: 'success',
        );
    }
}

Redirect defaults are configured independently via Wirestrap.alert.redirect.configure():

Wirestrap.alert.redirect.configure({
    type: 'info',
    duration: 2000,
    dismissText: 'Continue'
})

Options

$wirestrap.alert.show() / Wirestrap.alert.show()

Option Type Default Description
message string '' Alert body text.
type string 'primary' CSS class applied to the alert element. Built-in values: primary, success, info, warning, danger.
title string null Optional header title. Shows a colored icon indicator when set.
duration number 0 Auto-dismiss delay in ms. 0 = persistent.
dismissText string 'OK' Label of the dismiss button.
showDismiss bool true Show the dismiss button.
backdropDismiss bool true Dismiss on backdrop click. Alert shakes if disabled and attempted.
escapeDismiss bool true Dismiss on Escape key. Alert shakes if disabled and attempted.
url string null Turns the dismiss button into a link to this url. Clicking it closes the alert and lets the browser navigate.
No results

$wirestrap.alert.confirm() / Wirestrap.alert.confirm.show()

Option Type Default Description
message string '' Confirmation body text.
method string -- Livewire method name to call on confirm.
params array [] Parameters passed to the method.
type string 'primary' Visual variant.
title string null Optional header title.
duration number 0 Auto-dismiss delay in ms.
confirmText string 'Confirm' Label of the confirm button.
cancelText string 'Cancel' Label of the cancel button.
backdropDismiss bool true Dismiss on backdrop click (counts as cancel).
escapeDismiss bool true Dismiss on Escape key (counts as cancel).
No results

$wirestrap.alert.redirect() / Wirestrap.alert.redirect.show()

Option Type Default Description
message string '' Alert body text.
url string -- Destination. Navigation is a native full page load, so any url works.
duration number 2000 Countdown before navigating, in ms. The progress bar shows the time left.
type string 'success' Visual variant.
title string null Optional header title.
showDismiss bool true Show a link that navigates immediately, skipping the countdown.
dismissText string 'Continue' Label of that link.
backdropDismiss bool false Dismiss on backdrop click. Dismissing cancels the navigation.
escapeDismiss bool false Dismiss on Escape key. Dismissing cancels the navigation.
No results

Wirestrap.alert.configure()

Option Type Default Description
duration number 0 Default auto-dismiss delay in ms.
dismissText string 'OK' Default dismiss button label.
showDismiss bool true Show dismiss button by default.
backdropDismiss bool true Dismiss on backdrop click by default.
escapeDismiss bool true Dismiss on Escape key by default.
No results

Wirestrap.alert.confirm.configure()

Option Type Default Description
type string 'primary' Default visual variant.
title string null Default header title.
duration number 0 Default auto-dismiss delay in ms.
confirmText string 'Confirm' Default confirm button label.
cancelText string 'Cancel' Default cancel button label.
backdropDismiss bool true Dismiss on backdrop click by default.
escapeDismiss bool true Dismiss on Escape key by default.
No results

Wirestrap.alert.redirect.configure()

Option Type Default Description
type string 'success' Default visual variant.
title string null Default header title.
duration number 2000 Default countdown before navigating, in ms.
showDismiss bool true Show the continue link by default.
dismissText string 'Continue' Default continue link label.
backdropDismiss bool false Dismiss on backdrop click by default.
escapeDismiss bool false Dismiss on Escape key by default.
No results