Skip to content

Commit

Permalink
feat(modal): allow modal trigger to load a remote URL in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
philschanely authored and FuturaExtraBold committed Sep 8, 2021
1 parent deeff31 commit 8e971a6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/sage-system/lib/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Sage.modal = (function() {
function initTrigger(el) {
el.addEventListener("click", function(evt) {
let modalId = evt.target.getAttribute(SELECTOR_MODALTRIGGER) || evt.target.parentElement.getAttribute(SELECTOR_MODALTRIGGER);
openModal(modalId);
openModal(modalId, el);
});
}

Expand All @@ -57,13 +57,15 @@ Sage.modal = (function() {
if (keyNum === 27) dispatchCloseAll();
}

function openModal(modalId) {
function openModal(modalId, trigger) {
let modal = document.querySelector(`[${SELECTOR_MODAL}="${modalId}"]`);
let focusableEls = modal.querySelectorAll(SELECTOR_FOCUSABLE_ELEMENTS);

dispatchOpenEvents(modal);

if(modal.hasAttribute(SELECTOR_MODAL_REMOTE_URL)) {
if(trigger.hasAttribute(SELECTOR_MODAL_REMOTE_URL)) {
fetchModalContent(modal, trigger.dataset.jsRemoteUrl);
} else if (modal.hasAttribute(SELECTOR_MODAL_REMOTE_URL)) {
fetchModalContent(modal);
}

Expand Down Expand Up @@ -126,9 +128,12 @@ Sage.modal = (function() {
removeModalContents(el);
}

function fetchModalContent(el) {
function fetchModalContent(el, url) {
if (!url) {
url = el.getAttribute(SELECTOR_MODAL_REMOTE_URL);
}

let elContainer = el.querySelector(`[${SELECTOR_MODAL_CONTAINER}]`);
let url = el.getAttribute(SELECTOR_MODAL_REMOTE_URL);
const xhr = new XMLHttpRequest();

xhr.addEventListener("load", (evt) => {
Expand Down

0 comments on commit 8e971a6

Please sign in to comment.