confirm_form.twig 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. {# Reusable confirm-then-submit form. Renders a button that opens a small Alpine
  2. modal asking the user to confirm before posting to `action`. The form's
  3. hidden inputs include the CSRF token and any extra inputs in `extra_fields`.
  4. Args:
  5. - action (string) : POST URL.
  6. - label (string) : button text and modal title prefix.
  7. - description (string) : line of explanatory text in the modal.
  8. - extra_fields (array) : optional name->value hidden inputs (for "next").
  9. - btn_class (string) : optional override for the trigger-button classes. #}
  10. {% set _btn_class = btn_class|default('rounded-md border border-red-300 bg-white px-2 py-1 text-xs font-medium text-red-700 hover:bg-red-50 dark:border-red-700 dark:bg-slate-900 dark:text-red-300 dark:hover:bg-slate-800') %}
  11. <div x-data="toggle" class="inline-block">
  12. <button type="button"
  13. x-on:click="show()"
  14. class="{{ _btn_class }}">{{ label }}</button>
  15. <div x-show="open" x-cloak
  16. class="fixed inset-0 z-50 flex items-center justify-center bg-slate-900/60 px-4">
  17. <div x-on:click.outside="hide()"
  18. class="w-full max-w-sm rounded-2xl border border-slate-200 bg-white p-6 shadow-lg dark:border-slate-800 dark:bg-slate-900">
  19. <h2 class="text-base font-semibold">{{ label }}?</h2>
  20. <p class="mt-2 text-sm text-slate-600 dark:text-slate-400">{{ description }}</p>
  21. <form method="post" action="{{ action }}" class="mt-4 flex justify-end gap-2">
  22. <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
  23. {% for k, v in extra_fields|default({}) %}
  24. <input type="hidden" name="{{ k }}" value="{{ v }}">
  25. {% endfor %}
  26. <button type="button" x-on:click="hide()"
  27. class="rounded-md border border-slate-300 px-3 py-1.5 text-sm hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">Cancel</button>
  28. <button type="submit"
  29. class="rounded-md bg-red-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-red-500">Confirm</button>
  30. </form>
  31. </div>
  32. </div>
  33. </div>