@parhelia/react 0.0.0-stage → 1.0.12951
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +106 -2
- package/package.json +40 -4
- package/src/bridge-client.d.ts +8 -0
- package/src/bridge-client.js +29 -0
- package/src/bridge.d.ts +25 -0
- package/src/bridge.js +37 -0
- package/src/index.d.ts +18 -0
- package/src/index.js +20 -0
- package/src/picture.d.ts +20 -0
- package/src/picture.js +62 -0
- package/src/refresh-hook.d.ts +15 -0
- package/src/refresh-hook.js +27 -0
- package/src/slot.d.ts +22 -0
- package/src/slot.js +34 -0
package/LICENSE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parhelia/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The React bindings of [`@parhelia/head`](https://www.npmjs.com/package/@parhelia/head), for a React rendering head on any framework: Next.js (through
|
|
4
|
+
`@parhelia/next`), TanStack Start, React Router, plain Vite SSR. Plain JavaScript, no build step.
|
|
5
|
+
|
|
6
|
+
| Export | What it is for |
|
|
7
|
+
|---|---|
|
|
8
|
+
| `ParheliaBridge` | loads the editing bridge after hydration when the editor frames the head directly |
|
|
9
|
+
| `ParheliaBridgeScript` | renders the bridge's `<script>` on the server, on the editor's requests |
|
|
10
|
+
| `ParheliaSlot` | marks a slot for the editor, also when it is empty |
|
|
11
|
+
| `useParheliaRefresh` | the in-place refresh as a hook |
|
|
12
|
+
| `ParheliaPicture` | an image cropped by the browser, marked for the editor's cropper |
|
|
13
|
+
|
|
14
|
+
Everything else a head needs is `@parhelia/head`'s: the server check (`@parhelia/head/server`), the rendered identity, rich
|
|
15
|
+
text, a CMS's markers, the build values and the Vite adapter (`@parhelia/head/vite`). The constants these components' props
|
|
16
|
+
refer to (`PARHELIA_BRIDGE_PATH`, `EMPTY_SLOT_ATTRIBUTE`, the bridge's helpers) are re-exported here.
|
|
17
|
+
|
|
18
|
+
## The editing bridge: `ParheliaBridge` or `ParheliaBridgeScript`
|
|
19
|
+
|
|
20
|
+
A head the editor frames directly (its own URL, another origin than the editor's) loads Parhelia's editing bridge itself: a
|
|
21
|
+
cross-origin frame cannot be given the bridge by its parent. Below Parhelia's rendering proxy the editor injects it, and
|
|
22
|
+
neither component is needed. One implementation, two ways to load it:
|
|
23
|
+
|
|
24
|
+
- **`ParheliaBridge`**, a client component: adds the script after hydration, only in a frame of another origin on
|
|
25
|
+
`?parhelia=1`. It needs no request, and the bridge never touches markup React is still hydrating.
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
<ParheliaBridge />
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- **`ParheliaBridgeScript`**: renders the `<script>` where it is rendered, so it is in the server's HTML. Use it where the
|
|
32
|
+
head's server knows the request and wants the script in the HTML: a CSP nonce, `integrity`. It renders on every request
|
|
33
|
+
with `?parhelia=1`, whatever frames the page (a server cannot see that); in a same-origin frame the editor then injects
|
|
34
|
+
nothing. Server components can import it (no `"use client"`).
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
// App Router page or layout: the page's searchParams, the response's nonce
|
|
38
|
+
<ParheliaBridgeScript searchParams={await searchParams} nonce={nonce} />
|
|
39
|
+
// a fetch handler or route: the request URL; or decide yourself with `enabled`
|
|
40
|
+
<ParheliaBridgeScript requestUrl={request.url} />
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Both load the bridge from `/parhelia/editor-bridge/v1/parhelia-bridge.js` on the Parhelia origin (`origin`, default
|
|
44
|
+
`parheliaOrigin()`) and name that origin as the bridge's parent (`data-parhelia-parent-origin`): an HTTPS editor framing an
|
|
45
|
+
HTTP head sends no referrer to derive the parent from. When another origin serves the bridge (a CMS authoring host that
|
|
46
|
+
proxies Parhelia's routes), pass it as `bridgeOrigin` (environment fallback `PARHELIA_BRIDGE_ORIGIN`, then
|
|
47
|
+
`NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN`): it only serves the script and is never named as the parent. Both take `nonce`,
|
|
48
|
+
`integrity`, `crossOrigin`, `referrerPolicy` and `id`. The origins come from trusted configuration, never from the query
|
|
49
|
+
string or a parent-window message; allow the origin that serves the script in the head's CSP `script-src`.
|
|
50
|
+
|
|
51
|
+
`shouldRenderParheliaBridgeScript({ searchParams | requestUrl })` is the request check alone (for a non-React render), and
|
|
52
|
+
`parheliaBridgeScriptProps(options)` the script's attributes. `@parhelia/editor-bridge-react`, which had
|
|
53
|
+
`ParheliaBridgeScript` before, is a deprecated re-export of it.
|
|
54
|
+
|
|
55
|
+
## Empty slots: `ParheliaSlot`
|
|
56
|
+
|
|
57
|
+
A Live Preview site tags its fields only, and an empty slot (a reference-array field such as a page's `topSection`) renders
|
|
58
|
+
no field: the editor knows the slot from the page's store but not where it is on the page, so the canvas offers no drop
|
|
59
|
+
zone for it. Wrap each slot's rendering; in Parhelia's frames it renders the slot's markers around it and, when the slot is
|
|
60
|
+
empty, an empty block (`emptyMinHeight`, default `6rem`) where the editor draws the drop zone; in the editor's preview, which
|
|
61
|
+
draws no drop zones, the bridge collapses that block. The blocks inside are still found by their own tags. Outside
|
|
62
|
+
Parhelia's frames it renders the children alone.
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { isParheliaFrameRequest } from '@parhelia/head';
|
|
66
|
+
import { ParheliaSlot } from '@parhelia/react';
|
|
67
|
+
|
|
68
|
+
const inParhelia = isParheliaFrameRequest(useRouter().query);
|
|
69
|
+
|
|
70
|
+
<ParheliaSlot name="topSection" enabled={inParhelia}>
|
|
71
|
+
{topSection?.map((entry) => <ComponentResolver key={entry.sys.id} componentProps={entry} />)}
|
|
72
|
+
</ParheliaSlot>;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`name` is the slot's key as Parhelia names it: the field's API id for a slot of the page being edited. `description` (the
|
|
76
|
+
label, default `name`) is optional.
|
|
77
|
+
|
|
78
|
+
## In-place refresh: `useParheliaRefresh`
|
|
79
|
+
|
|
80
|
+
The editor calls `window.requestRefresh(newUri?)` after a save. Mount the hook in Parhelia's editing frames with how the head
|
|
81
|
+
reads the page again and a stable string of what it rendered; it reads again once a second until that string changed and
|
|
82
|
+
settled, or the deadline passed:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
// Next.js Pages Router: getServerSideProps runs again (a fresh query); App Router: router.refresh();
|
|
86
|
+
// TanStack Router: router.invalidate()
|
|
87
|
+
useParheliaRefresh({ signature: JSON.stringify(pageProps.page), refetch: () => router.replace(withFreshQuery(), undefined, { scroll: false }) });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A head that runs another React than the one this package resolves (linked from a checkout) calls
|
|
91
|
+
`installParheliaRefresh({ refetch, signature: () => latest, deadlineMs })` from `@parhelia/head/refresh` in its own effect: it
|
|
92
|
+
imports no React, and its answer removes the hook again.
|
|
93
|
+
|
|
94
|
+
## Cropped pictures: `ParheliaPicture`
|
|
95
|
+
|
|
96
|
+
Renders what `contentfulPictureSources` (`@parhelia/head/contentful`) computed: the picture's CSS in a `<style>`, a clipping box
|
|
97
|
+
of the crop's shape, and a `<picture>` whose sources and image carry their variant (`data-variant`) and the crop they apply
|
|
98
|
+
(`data-parhelia-crop`). Put the field's annotations on the box (`boxProps`), so the editor measures the visible box:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import { contentfulPictureSources } from '@parhelia/head/contentful';
|
|
102
|
+
import { ParheliaPicture } from '@parhelia/react';
|
|
103
|
+
|
|
104
|
+
<ParheliaPicture picture={contentfulPictureSources(asset, crops, variants)} alt={asset.description} boxProps={fieldTags} />;
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
A head that runs another React than the one this package resolves renders the same data with its own elements.
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.12951",
|
|
4
|
+
"description": "The React bindings of @parhelia/head for a React rendering head on any framework: the editing bridge (ParheliaBridge after hydration, ParheliaBridgeScript on the server), ParheliaSlot for empty slots and the in-place refresh hook (useParheliaRefresh). Next.js heads use @parhelia/next.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"default": "./src/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src/*.js",
|
|
20
|
+
"src/*.d.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"scripts": {
|
|
26
|
+
"check-types": "tsc --noEmit -p tsconfig.json",
|
|
27
|
+
"test": "vitest run"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@parhelia/head": "1.0.12951"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "*",
|
|
37
|
+
"@types/react": "19.2.18",
|
|
38
|
+
"jsdom": "^30.0.1",
|
|
39
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
40
|
+
"vitest": "^4.1.11"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ParheliaBridgeLoadOptions } from "@parhelia/head";
|
|
2
|
+
|
|
3
|
+
export { shouldLoadParheliaBridge } from "@parhelia/head";
|
|
4
|
+
|
|
5
|
+
export type ParheliaBridgeProps = ParheliaBridgeLoadOptions;
|
|
6
|
+
|
|
7
|
+
/** Loads Parhelia's editing bridge after hydration when the editor frames this head directly; renders nothing. */
|
|
8
|
+
export declare function ParheliaBridge(props?: ParheliaBridgeProps): null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* `ParheliaBridge` — loads Parhelia's editing bridge (`./bridge.js`) after hydration, as next/script's afterInteractive
|
|
4
|
+
* would, so the bridge never touches markup React is still hydrating. Only in a frame of another origin: a proxied head is
|
|
5
|
+
* framed from the editor's own origin, which injects the bridge itself, so nothing is loaded there.
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect } from "react";
|
|
8
|
+
|
|
9
|
+
import { loadParheliaBridge } from "@parhelia/head";
|
|
10
|
+
|
|
11
|
+
export { shouldLoadParheliaBridge } from "@parhelia/head";
|
|
12
|
+
|
|
13
|
+
/** @param {import("./bridge-client.js").ParheliaBridgeProps} [props] */
|
|
14
|
+
export function ParheliaBridge({
|
|
15
|
+
origin,
|
|
16
|
+
parheliaOrigin,
|
|
17
|
+
bridgeOrigin,
|
|
18
|
+
queryParam = "parhelia",
|
|
19
|
+
id,
|
|
20
|
+
nonce,
|
|
21
|
+
crossOrigin,
|
|
22
|
+
integrity,
|
|
23
|
+
referrerPolicy,
|
|
24
|
+
} = {}) {
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
loadParheliaBridge({ origin, parheliaOrigin, bridgeOrigin, queryParam, id, nonce, crossOrigin, integrity, referrerPolicy });
|
|
27
|
+
}, [origin, parheliaOrigin, bridgeOrigin, queryParam, id, nonce, crossOrigin, integrity, referrerPolicy]);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
package/src/bridge.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
|
|
3
|
+
import type { ParheliaBridgeScriptAttributes, ParheliaBridgeScriptProps } from "@parhelia/head";
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
PARHELIA_BRIDGE_PATH,
|
|
7
|
+
PARHELIA_EDITOR_MODE_ATTRIBUTE,
|
|
8
|
+
PARHELIA_EDITING_SELECTOR,
|
|
9
|
+
parheliaBridgeScriptProps,
|
|
10
|
+
resolveParheliaBridgeUrl,
|
|
11
|
+
shouldRenderParheliaBridgeScript,
|
|
12
|
+
type ParheliaBridgeActivationOptions,
|
|
13
|
+
type ParheliaBridgeCrossOrigin,
|
|
14
|
+
type ParheliaBridgeReferrerPolicy,
|
|
15
|
+
type ParheliaBridgeScriptAttributes,
|
|
16
|
+
type ParheliaBridgeScriptOptions,
|
|
17
|
+
type ParheliaBridgeScriptProps,
|
|
18
|
+
type ParheliaBridgeSearchParams,
|
|
19
|
+
} from "@parhelia/head";
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/** Renders the bridge's `<script>` on the editor's requests (`?parhelia=1`), where it is rendered (on the server). */
|
|
23
|
+
export declare function ParheliaBridgeScript(
|
|
24
|
+
props?: ParheliaBridgeScriptProps,
|
|
25
|
+
): ReactElement<ParheliaBridgeScriptAttributes, "script"> | null;
|
package/src/bridge.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parhelia's editing bridge for a head the editor frames DIRECTLY (a cross-origin frame cannot be given the bridge by its
|
|
3
|
+
* parent; a proxied head is framed from the editor's own origin, which injects the bridge itself). One implementation,
|
|
4
|
+
* two ways to load it:
|
|
5
|
+
*
|
|
6
|
+
* - `ParheliaBridgeScript` renders the `<script>` where it is rendered (on the server), on a request with `?parhelia=1`.
|
|
7
|
+
* Use it where the server knows the request (a page's `searchParams`, a route's `request.url`) and the script belongs
|
|
8
|
+
* in the HTML: a nonce from the response's CSP, `integrity`. The server cannot see how the page is framed, so it also
|
|
9
|
+
* renders in a same-origin frame, where the editor then injects nothing (it skips a document that has the bridge).
|
|
10
|
+
* - `ParheliaBridge` (`./bridge-client.js`) adds the same script after hydration, only in a cross-origin frame on
|
|
11
|
+
* `?parhelia=1`: it needs no request, and the bridge never touches markup React is still hydrating.
|
|
12
|
+
*
|
|
13
|
+
* Both build the script with `parheliaBridgeScriptProps`. No "use client" here: a server component imports
|
|
14
|
+
* `ParheliaBridgeScript`, the predicate and the constants as themselves, not as client references.
|
|
15
|
+
*/
|
|
16
|
+
import { createElement } from "react";
|
|
17
|
+
|
|
18
|
+
import { parheliaBridgeScriptProps, shouldRenderParheliaBridgeScript } from "@parhelia/head";
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
PARHELIA_BRIDGE_PATH,
|
|
22
|
+
PARHELIA_EDITOR_MODE_ATTRIBUTE,
|
|
23
|
+
PARHELIA_EDITING_SELECTOR,
|
|
24
|
+
parheliaBridgeScriptProps,
|
|
25
|
+
resolveParheliaBridgeUrl,
|
|
26
|
+
shouldRenderParheliaBridgeScript,
|
|
27
|
+
} from "@parhelia/head";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The bridge's `<script>` on the editor's requests (`?parhelia=1`), rendered where the component is (on the server).
|
|
31
|
+
* @param {import("./bridge.js").ParheliaBridgeScriptProps} [props]
|
|
32
|
+
*/
|
|
33
|
+
export function ParheliaBridgeScript({ enabled, queryParam, queryValue, requestUrl, searchParams, ...script } = {}) {
|
|
34
|
+
if (!shouldRenderParheliaBridgeScript({ enabled, queryParam, queryValue, requestUrl, searchParams })) return null;
|
|
35
|
+
const props = parheliaBridgeScriptProps(script);
|
|
36
|
+
return props && createElement("script", props);
|
|
37
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ParheliaBridgeScript,
|
|
3
|
+
parheliaBridgeScriptProps,
|
|
4
|
+
resolveParheliaBridgeUrl,
|
|
5
|
+
shouldRenderParheliaBridgeScript,
|
|
6
|
+
PARHELIA_BRIDGE_PATH,
|
|
7
|
+
PARHELIA_EDITOR_MODE_ATTRIBUTE,
|
|
8
|
+
PARHELIA_EDITING_SELECTOR,
|
|
9
|
+
type ParheliaBridgeActivationOptions,
|
|
10
|
+
type ParheliaBridgeScriptAttributes,
|
|
11
|
+
type ParheliaBridgeScriptOptions,
|
|
12
|
+
type ParheliaBridgeScriptProps,
|
|
13
|
+
type ParheliaBridgeSearchParams,
|
|
14
|
+
} from "./bridge.js";
|
|
15
|
+
export { ParheliaBridge, shouldLoadParheliaBridge, type ParheliaBridgeProps } from "./bridge-client.js";
|
|
16
|
+
export { ParheliaSlot, EMPTY_SLOT_ATTRIBUTE, type ParheliaSlotProps } from "./slot.js";
|
|
17
|
+
export { useParheliaRefresh, type ParheliaRefreshOptions } from "./refresh-hook.js";
|
|
18
|
+
export { ParheliaPicture, type ParheliaPictureProps } from "./picture.js";
|
package/src/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/react` — the React bindings of `@parhelia/head`, for a React rendering head on any framework: the editing
|
|
3
|
+
* bridge (`ParheliaBridge` after hydration, `ParheliaBridgeScript` rendered on the server), `ParheliaSlot`, the in-place
|
|
4
|
+
* refresh as a hook (`useParheliaRefresh`) and `ParheliaPicture` (an image the browser crops). Everything else a head needs
|
|
5
|
+
* (the server check, the identity, rich text, a CMS's markers, the build values) is `@parhelia/head`'s; the constants these
|
|
6
|
+
* components' props refer to are re-exported here. A Next.js head uses `@parhelia/next`, which re-exports both. Plain JavaScript, no build step.
|
|
7
|
+
*/
|
|
8
|
+
export {
|
|
9
|
+
ParheliaBridgeScript,
|
|
10
|
+
parheliaBridgeScriptProps,
|
|
11
|
+
resolveParheliaBridgeUrl,
|
|
12
|
+
shouldRenderParheliaBridgeScript,
|
|
13
|
+
PARHELIA_BRIDGE_PATH,
|
|
14
|
+
PARHELIA_EDITOR_MODE_ATTRIBUTE,
|
|
15
|
+
PARHELIA_EDITING_SELECTOR,
|
|
16
|
+
} from "./bridge.js";
|
|
17
|
+
export { ParheliaBridge, shouldLoadParheliaBridge } from "./bridge-client.js";
|
|
18
|
+
export { ParheliaSlot, EMPTY_SLOT_ATTRIBUTE } from "./slot.js";
|
|
19
|
+
export { useParheliaRefresh } from "./refresh-hook.js";
|
|
20
|
+
export { ParheliaPicture } from "./picture.js";
|
package/src/picture.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import type { CroppedPicture } from "@parhelia/head/picture-crops";
|
|
3
|
+
|
|
4
|
+
export interface ParheliaPictureProps {
|
|
5
|
+
/** What a CMS helper computed (for Contentful, `contentfulPictureSources` of `@parhelia/head/contentful`); nothing renders for none. */
|
|
6
|
+
picture: CroppedPicture | null | undefined;
|
|
7
|
+
alt?: string;
|
|
8
|
+
/** The clipping box's class. */
|
|
9
|
+
className?: string;
|
|
10
|
+
/** Attributes of the clipping box: the field's annotations, so the editor measures the visible box. */
|
|
11
|
+
boxProps?: Record<string, unknown>;
|
|
12
|
+
loading?: "eager" | "lazy";
|
|
13
|
+
fetchPriority?: "high" | "low" | "auto";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* An image shown with the crop the browser applies: the picture's rules in a `<style>`, a clipping box of the crop's shape,
|
|
18
|
+
* and a `<picture>` whose sources and image carry their variant and crop for Parhelia's editor.
|
|
19
|
+
*/
|
|
20
|
+
export declare function ParheliaPicture(props: ParheliaPictureProps): ReactElement | null;
|
package/src/picture.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ParheliaPicture` — an image shown with a crop the browser applies (picture crops, docs/contentful-picture-crops-plan.md
|
|
3
|
+
* §4.7): a box of the crop's shape that clips the whole image, with a `<source>` per variant the head switches by media
|
|
4
|
+
* query. It renders what a CMS helper computed (a `CroppedPicture`; for Contentful, `contentfulPictureSources` of
|
|
5
|
+
* `@parhelia/head/contentful`): the picture's rules in a `<style>`, the clipping box, and each source and the image marked
|
|
6
|
+
* with its variant (`data-variant`) and the crop it applied (`data-parhelia-crop`), which Parhelia's editor reads to open
|
|
7
|
+
* the cropper on what the page shows.
|
|
8
|
+
*
|
|
9
|
+
* The field's annotations go on the box (`boxProps`), so the editor measures the visible box, not the larger image inside it.
|
|
10
|
+
* Renders nothing for no picture (an asset without a file or size): the head shows its own empty state then.
|
|
11
|
+
*/
|
|
12
|
+
import { createElement, Fragment } from "react";
|
|
13
|
+
|
|
14
|
+
import { PICTURE_CROP_ATTRIBUTE } from "@parhelia/head/picture-crops";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {{ picture: import("@parhelia/head/picture-crops").CroppedPicture | null | undefined, alt?: string,
|
|
18
|
+
* className?: string, boxProps?: Record<string, unknown>, loading?: "eager" | "lazy",
|
|
19
|
+
* fetchPriority?: "high" | "low" | "auto" }} props
|
|
20
|
+
*/
|
|
21
|
+
export function ParheliaPicture({ picture, alt = "", className, boxProps, loading, fetchPriority }) {
|
|
22
|
+
if (!picture) return null;
|
|
23
|
+
/** @param {import("@parhelia/head/picture-crops").CroppedPictureElement} element */
|
|
24
|
+
const marks = (element) => ({ "data-variant": element.variant, [PICTURE_CROP_ATTRIBUTE]: element.crop });
|
|
25
|
+
return createElement(
|
|
26
|
+
Fragment,
|
|
27
|
+
null,
|
|
28
|
+
// Unescaped: React escapes a <style>'s text on the server, which the browser would not decode inside <style>.
|
|
29
|
+
createElement("style", { dangerouslySetInnerHTML: { __html: picture.css } }),
|
|
30
|
+
createElement(
|
|
31
|
+
"div",
|
|
32
|
+
{ ...boxProps, className, ...picture.box },
|
|
33
|
+
createElement(
|
|
34
|
+
"picture",
|
|
35
|
+
null,
|
|
36
|
+
...picture.sources.map((source) =>
|
|
37
|
+
createElement("source", {
|
|
38
|
+
key: source.media,
|
|
39
|
+
media: source.media,
|
|
40
|
+
srcSet: source.srcset,
|
|
41
|
+
sizes: source.sizes,
|
|
42
|
+
width: source.width,
|
|
43
|
+
height: source.height,
|
|
44
|
+
...marks(source),
|
|
45
|
+
}),
|
|
46
|
+
),
|
|
47
|
+
createElement("img", {
|
|
48
|
+
key: "img",
|
|
49
|
+
src: picture.img.src,
|
|
50
|
+
srcSet: picture.img.srcset,
|
|
51
|
+
sizes: picture.img.sizes,
|
|
52
|
+
width: picture.img.width,
|
|
53
|
+
height: picture.img.height,
|
|
54
|
+
alt,
|
|
55
|
+
loading,
|
|
56
|
+
fetchPriority,
|
|
57
|
+
...marks(picture.img),
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ParheliaRefreshOptions {
|
|
2
|
+
/** A stable string of what the page rendered (its data); the loop stops once it changed and read the same twice. */
|
|
3
|
+
signature?: string;
|
|
4
|
+
/** Reads the page again: the editor's `newUri` when it named one (every read of one request gets the same). */
|
|
5
|
+
refetch: (newUri?: string) => unknown;
|
|
6
|
+
/** How long one request keeps reading again (default 8000). */
|
|
7
|
+
deadlineMs?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The in-place canvas refresh (`installParheliaRefresh`) while the component is mounted: `window.requestRefresh(newUri?)` reads
|
|
12
|
+
* the page again through `refetch` until `signature` changed and settled, or the deadline passed. Mount it in Parhelia's
|
|
13
|
+
* editing frames only.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useParheliaRefresh(options: ParheliaRefreshOptions): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useParheliaRefresh` — the in-place canvas refresh (`@parhelia/head/refresh`) as a React hook: installed while the component
|
|
3
|
+
* is mounted (mount it in Parhelia's editing frames only). `refetch` and `signature` are read through refs: a router hands out a
|
|
4
|
+
* new object after its own navigation, which must not tear the loop down.
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useRef } from "react";
|
|
7
|
+
|
|
8
|
+
import { installParheliaRefresh } from "@parhelia/head/refresh";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {{ signature?: string, refetch: (newUri?: string) => unknown, deadlineMs?: number }} options
|
|
12
|
+
*/
|
|
13
|
+
export function useParheliaRefresh({ signature, refetch, deadlineMs }) {
|
|
14
|
+
const refetchRef = useRef(refetch);
|
|
15
|
+
refetchRef.current = refetch;
|
|
16
|
+
const signatureRef = useRef(signature);
|
|
17
|
+
signatureRef.current = signature;
|
|
18
|
+
useEffect(
|
|
19
|
+
() =>
|
|
20
|
+
installParheliaRefresh({
|
|
21
|
+
refetch: newUri => refetchRef.current(newUri),
|
|
22
|
+
signature: () => signatureRef.current,
|
|
23
|
+
deadlineMs,
|
|
24
|
+
}),
|
|
25
|
+
[deadlineMs],
|
|
26
|
+
);
|
|
27
|
+
}
|
package/src/slot.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/** The attribute of the block an empty slot renders in Parhelia's frames; its value is the slot's key. */
|
|
4
|
+
export { EMPTY_SLOT_ATTRIBUTE } from "@parhelia/head";
|
|
5
|
+
|
|
6
|
+
export interface ParheliaSlotProps {
|
|
7
|
+
/** The slot's key as Parhelia names it: the field's API id for a slot of the page being edited (`topSection`). */
|
|
8
|
+
name: string;
|
|
9
|
+
/** The slot's label in the editor (default: `name`). */
|
|
10
|
+
description?: string;
|
|
11
|
+
/** True in Parhelia's frames only (`isParheliaFrameRequest(router.query)`); otherwise the children render alone. */
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
/** The height of the block an empty slot renders in Parhelia's frames (default `6rem`). */
|
|
14
|
+
emptyMinHeight?: string | number;
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Marks a slot for Parhelia's editor in its frames, also when it holds nothing (an empty slot renders an empty block, the
|
|
20
|
+
* place of its drop zone). Outside Parhelia's frames it renders the children alone.
|
|
21
|
+
*/
|
|
22
|
+
export declare function ParheliaSlot(props: ParheliaSlotProps): ReactElement;
|
package/src/slot.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ParheliaSlot` — marks one of the page's slots (a Contentful reference-array field such as a page's `topSection`) for
|
|
3
|
+
* Parhelia's editor, also when it holds nothing.
|
|
4
|
+
*
|
|
5
|
+
* A Live Preview site tags its fields only, and an empty slot renders no field: the editor then knows the slot from the
|
|
6
|
+
* page's store but not where it is on the page, so the canvas offers no drop zone for it. In Parhelia's frames this
|
|
7
|
+
* renders Parhelia's own slot markers around the slot's rendering (`data-placeholder-start` / `-end`, inert scripts) and,
|
|
8
|
+
* for an empty slot, an empty block of `emptyMinHeight` between them: the slot's place on the page, where the editor
|
|
9
|
+
* draws its drop zone. The components inside are still found by their own tags. Outside Parhelia's frames (`enabled`
|
|
10
|
+
* false) it renders the children alone, exactly as without it.
|
|
11
|
+
*
|
|
12
|
+
* `name` is the slot's key as Parhelia names it: the field's API id for a slot of the page being edited.
|
|
13
|
+
*/
|
|
14
|
+
import { Children, createElement, Fragment } from "react";
|
|
15
|
+
|
|
16
|
+
import { EMPTY_SLOT_MIN_HEIGHT, emptySlotAttributes, slotEndAttributes, slotStartAttributes } from "@parhelia/head";
|
|
17
|
+
|
|
18
|
+
export { EMPTY_SLOT_ATTRIBUTE } from "@parhelia/head";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {{ name: string, description?: string, enabled?: boolean, emptyMinHeight?: string | number,
|
|
22
|
+
* children?: import("react").ReactNode }} props
|
|
23
|
+
*/
|
|
24
|
+
export function ParheliaSlot({ name, description, enabled, emptyMinHeight = EMPTY_SLOT_MIN_HEIGHT, children }) {
|
|
25
|
+
if (!enabled) return createElement(Fragment, null, children);
|
|
26
|
+
const empty = Children.toArray(children).length === 0;
|
|
27
|
+
return createElement(
|
|
28
|
+
Fragment,
|
|
29
|
+
null,
|
|
30
|
+
createElement("script", slotStartAttributes(name, description)),
|
|
31
|
+
empty ? createElement("div", { ...emptySlotAttributes(name), style: { minHeight: emptyMinHeight } }) : children,
|
|
32
|
+
createElement("script", slotEndAttributes(name)),
|
|
33
|
+
);
|
|
34
|
+
}
|