Skip to content

Instantly share code, notes, and snippets.

@cpereiraweb
Forked from edeoliv/AppServiceProvider.php
Created June 27, 2025 12:44
Show Gist options
  • Save cpereiraweb/f6a3597cad170578a66820266d94d838 to your computer and use it in GitHub Desktop.
Save cpereiraweb/f6a3597cad170578a66820266d94d838 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