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

Add wp-init and other small adjustments #225

Merged
Merged
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
1 change: 0 additions & 1 deletion src/runtime/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const csnMetaTagItemprop = 'wp-client-side-navigation';
export const directivePrefix = 'data-wp-';
20 changes: 15 additions & 5 deletions src/runtime/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,27 @@ export default () => {
const contextValue = useContext(context);
Object.values(effect).forEach((path) => {
useSignalEffect(() => {
evaluate(path, { context: contextValue });
return evaluate(path, { context: contextValue });
});
});
});

// data-wp-init.[name]
directive('init', ({ directives: { init }, context, evaluate }) => {
const contextValue = useContext(context);
Object.values(init).forEach((path) => {
useEffect(() => {
return evaluate(path, { context: contextValue });
}, []);
});
});

// data-wp-on.[event]
directive('on', ({ directives: { on }, element, evaluate, context }) => {
const contextValue = useContext(context);
Object.entries(on).forEach(([name, path]) => {
element.props[`on${name}`] = (event) => {
evaluate(path, { event, context: contextValue });
return evaluate(path, { event, context: contextValue });
};
});
});
Expand Down Expand Up @@ -86,9 +96,9 @@ export default () => {
: name;

useEffect(() => {
// This seems necessary because Preact doesn't change the class names
// on the hydration, so we have to do it manually. It doesn't need
// deps because it only needs to do it the first time.
// This seems necessary because Preact doesn't change the class
// names on the hydration, so we have to do it manually. It doesn't
// need deps because it only needs to do it the first time.
if (!result) {
element.ref.current.classList.remove(name);
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import registerDirectives from './directives';
import { init } from './hydration';
export { store } from './store';

/**
* Initialize the Interactivity API.
*/
document.addEventListener('DOMContentLoaded', async () => {
registerDirectives();
await init();
// eslint-disable-next-line no-console
console.log('Interactivity API started');
});
registerDirectives();
init();
4 changes: 1 addition & 3 deletions src/runtime/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deepSignal } from 'deepsignal';
const isObject = (item) =>
item && typeof item === 'object' && !Array.isArray(item);

export const deepMerge = (target, source) => {
const deepMerge = (target, source) => {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
Expand Down Expand Up @@ -36,8 +36,6 @@ const getSerializedState = () => {
const rawState = getSerializedState();
export const rawStore = { state: deepSignal(rawState) };

if (typeof window !== 'undefined') window.store = rawStore;

export const store = ({ state, ...block }) => {
deepMerge(rawStore, block);
deepMerge(rawState, state);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// For wrapperless hydration of document.body.
// For wrapperless hydration.
// See https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c
export const createRootFragment = (parent, replaceNode) => {
replaceNode = [].concat(replaceNode);
Expand Down