Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trongate-mx): added mx-transition to enable ViewTransitions #210

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion public/css/trongate.css
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,48 @@ div.modal-body p.text-center {
.modal-footer { flex-direction: column-reverse; }
.modal-footer button:last-child { margin: 0 0 0 0.3em; }
p.button-container { display: flex; flex-direction: column-reverse; }
}
}


/* Turn cross-document view-transitions on */
/* Note that this at-rule is all that is needed to create the default cross-fade animation */

@view-transition {
navigation: auto;
}

/* Customize the default animation behavior */

::view-transition-group(root) {
animation-duration: 0.5s;
}

::view-transition-old(root) {
animation: slideSwipeOut 0.6s ease-in-out forwards;
}

::view-transition-new(root) {
animation: slideSwipeIn 0.6s ease-in-out forwards;
}

@keyframes slideSwipeOut {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(-100%);
}
}

@keyframes slideSwipeIn {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
65 changes: 62 additions & 3 deletions public/js/trongate-mx.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ let trongateMXOpeningModal = false;
} else {
console.warn(`Function ${cleanFunctionName} not found`);
}
},

viewTransition(element, callback) {
if (element.getAttribute('mx-transition') !== null && document.startViewTransition) {
document.startViewTransition(() => {
callback(element);
});
} else {
callback(element);
}
}
};

Expand Down Expand Up @@ -375,7 +385,9 @@ let trongateMXOpeningModal = false;
Animation.initAnimateSuccess(targetEl, http, element);
} else {
Modal.initAttemptCloseModal(targetEl, http, element);
this.updateContent(targetEl, http, element, event);
Utils.viewTransition(element, () => {
this.updateContent(targetEl, http, element, event);
});
}
}

Expand Down Expand Up @@ -1323,12 +1335,16 @@ let trongateMXOpeningModal = false;

const events = ['click', 'dblclick', 'change', 'submit', 'keyup', 'keydown', 'focus', 'blur', 'input'];
events.forEach(eventType => {
// Avoid conflicts and stale listeners by removing and re-adding event listeners
document.body.removeEventListener(eventType, Main.handleTrongateMXEvent);
document.body.addEventListener(eventType, Main.handleTrongateMXEvent);
});

document.querySelectorAll('[mx-trigger*="load"]').forEach(Dom.handlePageLoadedEvents);
Main.attemptInitPolling();
window.addEventListener('popstate', Main.handlePopState);
window.addEventListener('popstate', (event) => {
Utils.viewTransition(event.target, Main.handlePopState);
});
},

async handleTrongateMXEvent(event) {
Expand Down Expand Up @@ -1577,6 +1593,47 @@ let trongateMXOpeningModal = false;
}
}

let mxTransitionAbortController = new AbortController();
const parser = new DOMParser();

function handleMxTransitionClick(event) {
if (event.target.hasAttribute('mx-transition') === null) {
return;
}

function replaceDocument(html) {
const doc = parser.parseFromString(html, 'text/html');

document.title = doc.title;

document.documentElement.innerHTML = doc.documentElement.innerHTML;

Main.initializeTrongateMX();
}

if (!!event.target.href) {
event.preventDefault();
// abort other pending, in-flight requests
mxTransitionAbortController.abort();
mxTransitionAbortController = new AbortController();

Utils.viewTransition(event.target, ({ href }) => {
fetch(href, {
headers: {
'X-View-Transition': true,
},
signal: mxTransitionAbortController.signal
})
.then(response => response.text())
.then(html => {
replaceDocument(html);

window.history.pushState({}, '', href);
});
});
}
}

// Initialize Trongate MX when the DOM is loaded
document.addEventListener('DOMContentLoaded', Main.initializeTrongateMX);

Expand All @@ -1586,6 +1643,8 @@ let trongateMXOpeningModal = false;

document.addEventListener("click", (event) => {
handleMxModalClick(event);

handleMxTransitionClick(event);
});

// Establish the target element when mouse down event happens.
Expand All @@ -1604,7 +1663,7 @@ let trongateMXOpeningModal = false;
openModal: Modal.openModal
};


})(window);

const _mxOpenModal = function(modalId) {
Expand Down