Skip to content

Instantly share code, notes, and snippets.

@edeoliv
Created June 27, 2025 11:36
Show Gist options
  • Save edeoliv/efd6911eff0b28172032fa7b06d0aacb to your computer and use it in GitHub Desktop.
Save edeoliv/efd6911eff0b28172032fa7b06d0aacb to your computer and use it in GitHub Desktop.
Toggle with modal confirmation
<?php
....
//In your boot method on Service Provider or custom method add
Toggle::macro('registerDynamicActions', function (
string $confirmationActionName,
string $toggleStateName,
string $modalHeading = 'Confirm Action',
string $modalButton = 'Yes',
string $modalCancelButton = 'No'
): static {
$this->afterStateUpdated(function (?bool $state, Toggle $component, $livewire) use ($confirmationActionName): void {
if ($state && method_exists($livewire, 'mountFormComponentAction')) {
$livewire->mountFormComponentAction($component->getStatePath(), $confirmationActionName);
}
});
$this->registerActions([
Action::make($confirmationActionName)
->requiresConfirmation()
->modalHeading($modalHeading)
->modalSubmitActionLabel($modalButton)
->modalCancelAction(false)
->closeModalByEscaping(false)
->closeModalByClickingAway(false)
->extraModalFooterActions(fn(Action $action): array => [
$action->makeModalSubmitAction($modalCancelButton, arguments: ['cancel' => true]),
])
->action(function (array $arguments, Set $set, Toggle $component) use ($toggleStateName): void {
$cancel = data_get($arguments, 'cancel', false);
if ($cancel) {
$set($toggleStateName, false);
}
}),
]);
return $this;
});
//Toggle::make('notify_users')->registerDynamicActions('notify_users_confirmation','notify_users', 'Confirm Notification','Send Notification','Cancel')
//Name of the confirmation action,Toggle Status Name,Modal title,Confirmation button text,Cancellation button text
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment