@dani94 What does the code look like in your x-modal component that listens for events to actually open the modal? You should be able to get a reference to the triggering element there.
For example, I use Bootstrap and its Modal component a lot. I have forms in my application to delete records:
<x-form :action="route('product.destroy', compact('product'))" method="DELETE" x-confirm="Are you sure you want to delete this product?">
<button type="submit">Delete</button>
</form>
I then have an Alpine plugin (what you see as the x-confirm directive) that intercepts submissions and instead shows a modal:
import { Alpine as TAlpine } from 'alpine-js';
import Modal from 'bootstrap';
export default function (Alpine) {
Alpine.directive('confirm', function (el, { expression }) {
el.addEventListener('submit', function (event) {
event.preventDefault();
const modal = Modal.getOrCreateInstance('confirm-delete-modal');
modal.querySelector('form').setAttribute('action', event.target.getAttribute('action'));
modal.querySelector('.modal-body-message').textContent = expression;
modal.show();
});
});
}
It intercepts submit events, finds the #confirm-delete-modal on the page, and then sets the form’s action attribute to the original form’s action attribute value, and replaces the contents of .modal-body-message with the text that was in the x-confirm expression.
The confirm delete modal is then just a modal with a form, a confirmation message, and submit button:
<div aria-hidden="true" aria-labelledby="confirm-delete-modal-label" class="fade modal" id="confirm-delete-modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<header class="modal-header">
<h1 class="fs-5 modal-title" id="confirm-delete-modal-label">Confirm delete</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</header>
<div class="modal-body">
<p class="mb-0 modal-body-message"></p>
</div>
<footer class="modal-footer">
<x-form action="#" method="DELETE">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No, keep</button>
<button type="submit" class="btn btn-danger">Yes, delete</button>
</x-form>
</footer>
</div>
</div>
</div>