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

🎨 Server-side rendering of wp-show in PHP #100

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ec1f9e5
Hardcode transitions initially
DAreRodz Oct 19, 2022
f123b76
Merge branch 'main-wp-directives-plugin' into wp-directives/animate-t…
luisherranz Oct 20, 2022
148580b
Add initial block
luisherranz Oct 20, 2022
4487079
Add different transition cases
DAreRodz Oct 20, 2022
0c30ee3
Add reactive state
luisherranz Oct 20, 2022
7a5ed15
Use a different transition tag for each content
DAreRodz Oct 20, 2022
42e43fe
Merge branch 'wp-directives/animate-transitions' into wpmoviesdemo
SantosGuillamot Oct 20, 2022
7d86ee4
Handle scroll inside startTransition
DAreRodz Oct 20, 2022
ea06205
Merge branch 'wp-directives/animate-transitions' into wpmoviesdemo
SantosGuillamot Oct 20, 2022
66058b8
Add favorite post blocks
SantosGuillamot Oct 20, 2022
8562977
Fix scroll when there are no client transitions
DAreRodz Oct 20, 2022
384729e
Add default values to favorites blocks
SantosGuillamot Oct 20, 2022
1d659fa
Minor fixes
luisherranz Oct 20, 2022
2351426
Merge remote-tracking branch 'origin/wp-directives/animate-transition…
luisherranz Oct 20, 2022
2e06a0a
Fix image url
luisherranz Oct 20, 2022
e017eda
Fix image class
luisherranz Oct 20, 2022
2a57a7f
Save and restore favorites
DAreRodz Oct 20, 2022
403a195
Preserve the client component in the DOM
luisherranz Oct 21, 2022
34961c7
Remove transitions
luisherranz Nov 3, 2022
28a67eb
Add tabs block
luisherranz Nov 3, 2022
26da281
Add initial SSR for wp-show
luisherranz Nov 3, 2022
668f530
Add state to wpx in PHP
luisherranz Nov 3, 2022
5529474
Add attributes back to the HTML
luisherranz Nov 3, 2022
e217625
Rename ssr.mjs
luisherranz Nov 3, 2022
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
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@babel/preset-react",
{
"throwIfNamespace": false
}
]
]
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"version": "0.1.0",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"main": "build/index.js",
"scripts": {
"build": "webpack --mode=production",
"start": "webpack --mode=development --watch",
"dev": "npm start",
"test": "jest",
"test:watch": "jest --watch",
"plugin-zip": "wp-scripts plugin-zip",
"wp-env": "wp-env"
"wp-env": "wp-env",
"ssr": "node ssr.mjs"
},
"prettier": {
"useTabs": true,
Expand Down Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"@preact/signals": "^1.1.2",
"hpq": "^1.3.0",
"preact": "^10.10.6"
"preact": "^10.10.6",
"ultrahtml": "^0.4.0"
}
}
15 changes: 15 additions & 0 deletions src/blocks/favorites-number/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "bhe/favorites-number",
"version": "0.1.0",
"title": "BHE - Favorites Number",
"category": "text",
"icon": "heart",
"description": "",
"textdomain": "bhe",
"editorScript": "file:./index.js",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
19 changes: 19 additions & 0 deletions src/blocks/favorites-number/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import '@wordpress/block-editor';
import { useBlockProps } from '@wordpress/block-editor';

const Edit = () => {
return (
<div {...useBlockProps()}>
<img
draggable="false"
role="img"
class="emoji"
alt=":heart:"
src="https://s.w.org/images/core/emoji/14.0.0/svg/2764.svg"
/>
<span>5</span>
</div>
);
};

export default Edit;
8 changes: 8 additions & 0 deletions src/blocks/favorites-number/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerBlockType } from '@wordpress/blocks';
import Edit from './edit';
import './style.scss';

registerBlockType('bhe/favorites-number', {
edit: Edit,
save: () => null,
});
13 changes: 13 additions & 0 deletions src/blocks/favorites-number/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div wp-effect:restore="actions.favorites.restore" wp-effect:save="actions.favorites.save">
<img
class="emoji"
alt=":heart:"
src="https://s.w.org/images/core/emoji/14.0.0/svg/1f90d.svg"
wp-bind:src="selectors.favorites.isFavoritePostsEmpty"
/>
<span
wp-bind:children="state.favorites.count"
>
0
</span>
</div>
4 changes: 4 additions & 0 deletions src/blocks/favorites-number/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.wp-block-bhe-favorites-number {
border: 1px solid rgb(209, 21, 21);
position: relative;
}
48 changes: 48 additions & 0 deletions src/blocks/favorites-number/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import wpx from '../../runtime/wpx';
import { deepMerge } from '../../runtime/utils';

wpx({
state: {
favorites: {
posts: [],
count: ({ state }) => state.favorites.posts.length,
},
},
selectors: {
favorites: {
isPostIncluded: ({ state, context: { post } }) =>
`https://s.w.org/images/core/emoji/14.0.0/svg/${
state.favorites.posts.includes(post.id) ? '2764' : '1f90d'
}.svg`,
isFavoritePostsEmpty: ({ state }) =>
`https://s.w.org/images/core/emoji/14.0.0/svg/${
state.favorites.posts.length !== 0 ? '2764' : '1f90d'
}.svg`,
},
},
actions: {
favorites: {
togglePost: ({ state, context }) => {
const index = state.favorites.posts.findIndex(
(post) => post === context.post.id
);
if (index === -1) state.favorites.posts.push(context.post.id);
else state.favorites.posts.splice(index, 1);
},
save: ({ state }) => {
localStorage.setItem(
'wpmoviesdemo.favorites',
JSON.stringify(state.favorites)
);
},
restore: ({ state }) => {
deepMerge(
state.favorites,
JSON.parse(
localStorage.getItem('wpmoviesdemo.favorites')
) || {}
);
},
},
},
});
15 changes: 15 additions & 0 deletions src/blocks/post-favorite/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "bhe/post-favorite",
"version": "0.1.0",
"title": "BHE - Post Favorite",
"category": "text",
"icon": "heart",
"description": "",
"textdomain": "bhe",
"editorScript": "file:./index.js",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
18 changes: 18 additions & 0 deletions src/blocks/post-favorite/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import '@wordpress/block-editor';
import { useBlockProps } from '@wordpress/block-editor';

const Edit = () => {
return (
<div {...useBlockProps()}>
<img
draggable="false"
role="img"
class="emoji"
alt=":heart:"
src="https://s.w.org/images/core/emoji/14.0.0/svg/2764.svg"
/>
</div>
);
};

export default Edit;
8 changes: 8 additions & 0 deletions src/blocks/post-favorite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerBlockType } from '@wordpress/blocks';
import Edit from './edit';
import './style.scss';

registerBlockType('bhe/post-favorite', {
edit: Edit,
save: () => null,
});
15 changes: 15 additions & 0 deletions src/blocks/post-favorite/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
$post = get_post();
$wrapper_attributes = get_block_wrapper_attributes();
?>

<wp-context data='{"post": {"id": <?= $post->ID ?>}}'>
<img
<?= $wrapper_attributes ?>
wp-on:click="actions.favorites.togglePost"
class="emoji"
alt=":heart:"
src="https://s.w.org/images/core/emoji/14.0.0/svg/1f90d.svg"
wp-bind:src="selectors.favorites.isPostIncluded"
/>
</wp-context>
4 changes: 4 additions & 0 deletions src/blocks/post-favorite/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.wp-block-bhe-post-favorite {
width: 1rem;
cursor: pointer;
}
15 changes: 15 additions & 0 deletions src/blocks/tabs/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "bhe/tabs",
"version": "0.1.0",
"title": "BHE - Tabs",
"category": "text",
"icon": "tab",
"description": "Tabs",
"textdomain": "bhe",
"editorScript": "file:./index.js",
"editorStyle": "file:./style.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
7 changes: 7 additions & 0 deletions src/blocks/tabs/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useBlockProps } from '@wordpress/block-editor';

const Edit = () => {
return <div {...useBlockProps()}>I'm a tab</div>;
};

export default Edit;
8 changes: 8 additions & 0 deletions src/blocks/tabs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerBlockType } from '@wordpress/blocks';
import Edit from './edit';
import './style.scss';

registerBlockType('bhe/tabs', {
edit: Edit,
save: () => null,
});
11 changes: 11 additions & 0 deletions src/blocks/tabs/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
wpx([
'state' => [
'show' => true,
'dontShow' => false,
],
]); ?>

<h3>The tabs!</h3>
<wp-show when="state.show"><div>I should be shown</div></wp-show>
<wp-show when="state.dontShow"><div>I should not be shown</div></wp-show>
2 changes: 2 additions & 0 deletions src/blocks/tabs/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.wp-block-bhe-tabs {
}
7 changes: 6 additions & 1 deletion src/runtime/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { component } from './hooks';

export default () => {
const WpContext = ({ children, data, context: { Provider } }) => {
const signals = useMemo(() => deepSignal(JSON.parse(data)), []);
const signals = useMemo(() => deepSignal(JSON.parse(data)), [data]);
return <Provider value={signals}>{children}</Provider>;
};
component('wp-context', WpContext);

const WpShow = ({ children }) => {
return children;
};
component('wp-show', WpShow);
};
20 changes: 11 additions & 9 deletions src/runtime/deepsignal.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { signal } from '@preact/signals';
import { knownSymbols, shouldWrap } from './utils';

const proxyToSignals = new WeakMap();
const objToProxy = new WeakMap();
const returnSignal = /^\$/;

export const deepSignal = (obj) => new Proxy(obj, handlers);
export const options = { returnSignal: /^\$/ };

const handlers = {
get(target, prop, receiver) {
if (typeof prop === 'symbol' && knownSymbols.has(prop))
return Reflect.get(target, prop, receiver);
const shouldReturnSignal = returnSignal.test(prop);
const key = shouldReturnSignal ? prop.replace(returnSignal, '') : prop;
const returnSignal = options.returnSignal.test(prop);
const key = returnSignal
? prop.replace(options.returnSignal, '')
: prop;
if (!proxyToSignals.has(receiver))
proxyToSignals.set(receiver, new Map());
const signals = proxyToSignals.get(receiver);
if (!signals.has(key)) {
let val = Reflect.get(target, key, receiver);
if (typeof val === 'object' && val !== null && shouldWrap(val))
val = new Proxy(val, handlers);
if (typeof val === 'object' && val !== null) {
if (!objToProxy.has(val))
objToProxy.set(val, new Proxy(val, handlers));
val = objToProxy.get(val);
}
signals.set(key, signal(val));
}
return returnSignal ? signals.get(key) : signals.get(key).value;
},

set(target, prop, val, receiver) {
let internal = val;
if (typeof val === 'object' && val !== null && shouldWrap(val)) {
if (typeof val === 'object' && val !== null) {
if (!objToProxy.has(val))
objToProxy.set(val, new Proxy(val, handlers));
internal = objToProxy.get(val);
Expand Down
Loading