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

Commit 86daac7

Browse files
committed
Add media hydration
1 parent 36bf77c commit 86daac7

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/gutenberg-packages/frontend.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ class GutenbergBlock extends HTMLElement {
2929
);
3030
const innerBlocks = this.querySelector("gutenberg-inner-blocks");
3131
const Comp = blockTypes.get(blockType);
32-
const hydrationTechnique = this.getAttribute("data-gutenberg-hydrate");
33-
32+
const technique = this.getAttribute("data-gutenberg-hydrate");
33+
const media = this.getAttribute("data-gutenberg-media");
34+
const hydrationOptions = {
35+
technique,
36+
media,
37+
};
3438
hydrate(
3539
<EnvContext.Provider value="frontend">
3640
<Comp
@@ -45,7 +49,7 @@ class GutenbergBlock extends HTMLElement {
4549
</Comp>
4650
</EnvContext.Provider>,
4751
this,
48-
hydrationTechnique
52+
hydrationOptions
4953
);
5054
});
5155
}

src/gutenberg-packages/wordpress-blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const save =
1010
data-gutenberg-block-type={name}
1111
data-gutenberg-attributes={JSON.stringify(attributes)}
1212
data-gutenberg-block-props={JSON.stringify(blockProps)}
13-
data-gutenberg-hydrate="view"
13+
data-gutenberg-hydrate="idle"
1414
>
1515
<Comp blockProps={blockProps} attributes={attributes}>
1616
<gutenberg-inner-blocks>

src/gutenberg-packages/wordpress-element.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,22 @@ export const useState = (init) =>
3535
export const useEffect = (...args) =>
3636
useBlockEnvironment() !== "save" ? useReactEffect(...args) : noop;
3737

38-
export const hydrate = (container, element, technique) => {
38+
export const hydrate = (container, element, hydrationOptions) => {
39+
const { technique, media } = hydrationOptions || {};
40+
const cb = () => {
41+
ReactHydrate(container, element);
42+
};
3943
switch (technique) {
44+
case "media":
45+
if (media) {
46+
const mql = matchMedia(media);
47+
if (mql.matches) {
48+
cb();
49+
} else {
50+
mql.addEventListener("change", cb, { once: true });
51+
}
52+
}
53+
break;
4054
// Hydrate the element when is visible in the viewport.
4155
// https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
4256
case "view":
@@ -45,7 +59,7 @@ export const hydrate = (container, element, technique) => {
4559
if (!entry.isIntersecting) continue;
4660
// As soon as we hydrate, disconnect this IntersectionObserver.
4761
io.disconnect();
48-
ReactHydrate(container, element);
62+
cb();
4963
break; // break loop on first match
5064
}
5165
});
@@ -54,14 +68,13 @@ export const hydrate = (container, element, technique) => {
5468
case "idle":
5569
// Safari does not support requestIdleCalback, we use a timeout instead. https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback
5670
if ("requestIdleCallback" in window) {
57-
window.requestIdleCallback(() => {
58-
ReactHydrate(container, element);
59-
});
71+
window.requestIdleCallback(cb);
6072
} else {
61-
setTimeout(ReactHydrate(container, element), 200);
73+
setTimeout(cb, 200);
6274
}
6375
break;
76+
// Hydrate this component immediately.
6477
default:
65-
ReactHydrate(container, element);
78+
cb();
6679
}
6780
};

0 commit comments

Comments
 (0)