[Glitch] Add UJS to buttons

Port 100b20f290 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Echo
2025-12-02 11:35:29 +01:00
committed by Claire
parent ac926baa74
commit 1faaa9706a

View File

@@ -5,20 +5,26 @@ export function setupLinkListeners() {
// We don't want to target links with data-confirm here, as those are handled already. // We don't want to target links with data-confirm here, as those are handled already.
on('click', 'a[data-method]:not([data-confirm])', handleMethodLink); on('click', 'a[data-method]:not([data-confirm])', handleMethodLink);
// We also want to target buttons with data-confirm that are not inside forms.
on('click', ':not(form) button[data-confirm]:not([form])', handleConfirmLink);
} }
function handleConfirmLink(event: MouseEvent) { function handleConfirmLink(event: MouseEvent) {
const anchor = event.currentTarget; const target = event.currentTarget;
if (!(anchor instanceof HTMLAnchorElement)) { if (
!(target instanceof HTMLAnchorElement) &&
!(target instanceof HTMLButtonElement)
) {
return; return;
} }
const message = anchor.dataset.confirm; const message = target.dataset.confirm;
if (!message || !window.confirm(message)) { if (!message || !window.confirm(message)) {
event.preventDefault(); event.preventDefault();
return; return;
} }
if (anchor.dataset.method) { if (target.dataset.method) {
handleMethodLink(event); handleMethodLink(event);
} }
} }