naja-js / naja

Modern AJAX library for Nette Framework
https://naja.js.org
MIT License
109 stars 15 forks source link

View Transitions #381

Closed mira-kovar closed 1 year ago

mira-kovar commented 1 year ago

Feature Request

It would be nice to have support of View Transitions API when snippert replace its content.

Somewhere in SnippetHandler::updateSnippet insted of snippet.innerHTML = content; by documentation change to something like document.startViewTransition(() => snippet.innerHTML = content);. And ofcourse prepend and append...

Or please is there some way to hook/bypass snippet content change?

jiripudil commented 1 year ago

Hi, I guess you could use the beforeUpdate event and wrap the snippet update operation. I'm writing this off the top of my head, so it's not guaranteed to work as is, but the idea is there:

naja.snippetHandler.addEventListener('beforeUpdate', (event) => {
    const originalOperation = event.detail.operation;
    event.detail.changeOperation((snippet, content) => document.startViewTransition(() => originalOperation(snippet, content)));
});
mira-kovar commented 1 year ago

Hi, thanks. Nice trick.