Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Experiment: Add view transitions API directive #241

12 changes: 10 additions & 2 deletions src/runtime/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ export default () => {
if (clientSideNavigation && link !== false) {
element.props.onclick = async (event) => {
event.preventDefault();

// Fetch the page (or return it from cache).
await navigate(href);
if (
document.startViewTransition &&
link.viewTransitionsAPI
) {
document.startViewTransition(async () => {
await navigate(href);
luisherranz marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
await navigate(href);
}

// Update the scroll, depending on the option. True by default.
if (link?.scroll === 'smooth') {
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ const RecursivePriorityLevel = ({
);

const props = { ...originalProps, children };
const directiveArgs = { directives, props, element, context, evaluate };
const directiveArgs = {
directives,
props,
element,
context,
evaluate,
store,
};

for (const d in directives) {
const wrapper = directiveMap[d]?.(directiveArgs);
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import registerDirectives from './directives';
import { init } from './router';
export { store } from './store';
export { navigate } from './router';
export { directive } from './hooks';
export * from 'preact/hooks';

/**
* Initialize the Interactivity API.
Expand Down
4 changes: 2 additions & 2 deletions wp-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ function wp_directives_add_wp_link_attribute( $block_content ) {
) {
$w->set_attribute(
'data-wp-link',
'{ "prefetch": true, "scroll": false }'
'{ "prefetch": true, "viewTransitionsAPI": true, "scroll": false }'
);
} else {
$w->set_attribute( 'data-wp-link', '{ "prefetch": true }' );
$w->set_attribute( 'data-wp-link', '{ "prefetch": true, "viewTransitionsAPI": true, "scroll": false }' );
SantosGuillamot marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down