@parhelia/react 0.0.0-stage → 1.0.12903
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 +92 -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 +17 -0
- package/src/index.js +19 -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,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parhelia/react
|
|
2
|
+
|
|
3
|
+
The React bindings of [`@parhelia/head`](../head/README.md), 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
|
+
|
|
13
|
+
Everything else a head needs is `@parhelia/head`'s: the server check (`@parhelia/head/server`), the rendered identity, rich
|
|
14
|
+
text, a CMS's markers, the build values and the Vite adapter (`@parhelia/head/vite`). The constants these components' props
|
|
15
|
+
refer to (`PARHELIA_BRIDGE_PATH`, `EMPTY_SLOT_ATTRIBUTE`, the bridge's helpers) are re-exported here.
|
|
16
|
+
|
|
17
|
+
## The editing bridge: `ParheliaBridge` or `ParheliaBridgeScript`
|
|
18
|
+
|
|
19
|
+
A head the editor frames directly (its own URL, another origin than the editor's) loads Parhelia's editing bridge itself: a
|
|
20
|
+
cross-origin frame cannot be given the bridge by its parent. Below Parhelia's rendering proxy the editor injects it, and
|
|
21
|
+
neither component is needed. One implementation, two ways to load it:
|
|
22
|
+
|
|
23
|
+
- **`ParheliaBridge`**, a client component: adds the script after hydration, only in a frame of another origin on
|
|
24
|
+
`?parhelia=1`. It needs no request, and the bridge never touches markup React is still hydrating.
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
<ParheliaBridge />
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- **`ParheliaBridgeScript`**: renders the `<script>` where it is rendered, so it is in the server's HTML. Use it where the
|
|
31
|
+
head's server knows the request and wants the script in the HTML: a CSP nonce, `integrity`. It renders on every request
|
|
32
|
+
with `?parhelia=1`, whatever frames the page (a server cannot see that); in a same-origin frame the editor then injects
|
|
33
|
+
nothing. Server components can import it (no `"use client"`).
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
// App Router page or layout: the page's searchParams, the response's nonce
|
|
37
|
+
<ParheliaBridgeScript searchParams={await searchParams} nonce={nonce} />
|
|
38
|
+
// a fetch handler or route: the request URL; or decide yourself with `enabled`
|
|
39
|
+
<ParheliaBridgeScript requestUrl={request.url} />
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Both load the bridge from `/parhelia/editor-bridge/v1/parhelia-bridge.js` on the Parhelia origin (`origin`, default
|
|
43
|
+
`parheliaOrigin()`) and name that origin as the bridge's parent (`data-parhelia-parent-origin`): an HTTPS editor framing an
|
|
44
|
+
HTTP head sends no referrer to derive the parent from. When another origin serves the bridge (a CMS authoring host that
|
|
45
|
+
proxies Parhelia's routes), pass it as `bridgeOrigin` (environment fallback `PARHELIA_BRIDGE_ORIGIN`, then
|
|
46
|
+
`NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN`): it only serves the script and is never named as the parent. Both take `nonce`,
|
|
47
|
+
`integrity`, `crossOrigin`, `referrerPolicy` and `id`. The origins come from trusted configuration, never from the query
|
|
48
|
+
string or a parent-window message; allow the origin that serves the script in the head's CSP `script-src`.
|
|
49
|
+
|
|
50
|
+
`shouldRenderParheliaBridgeScript({ searchParams | requestUrl })` is the request check alone (for a non-React render), and
|
|
51
|
+
`parheliaBridgeScriptProps(options)` the script's attributes. `@parhelia/editor-bridge-react`, which had
|
|
52
|
+
`ParheliaBridgeScript` before, is a deprecated re-export of it.
|
|
53
|
+
|
|
54
|
+
## Empty slots: `ParheliaSlot`
|
|
55
|
+
|
|
56
|
+
A Live Preview site tags its fields only, and an empty slot (a reference-array field such as a page's `topSection`) renders
|
|
57
|
+
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
|
|
58
|
+
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
|
|
59
|
+
empty, an empty block (`emptyMinHeight`, default `6rem`) where the editor draws the drop zone; in the editor's preview, which
|
|
60
|
+
draws no drop zones, the bridge collapses that block. The blocks inside are still found by their own tags. Outside
|
|
61
|
+
Parhelia's frames it renders the children alone.
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { isParheliaFrameRequest } from '@parhelia/head';
|
|
65
|
+
import { ParheliaSlot } from '@parhelia/react';
|
|
66
|
+
|
|
67
|
+
const inParhelia = isParheliaFrameRequest(useRouter().query);
|
|
68
|
+
|
|
69
|
+
<ParheliaSlot name="topSection" enabled={inParhelia}>
|
|
70
|
+
{topSection?.map((entry) => <ComponentResolver key={entry.sys.id} componentProps={entry} />)}
|
|
71
|
+
</ParheliaSlot>;
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`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
|
|
75
|
+
label, default `name`) is optional. The Contentful Marketing starter does this (P11,
|
|
76
|
+
`docs/migration/w12-cfsite-starter-changes.md`).
|
|
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.
|
|
2
93
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.12903",
|
|
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.12903"
|
|
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,17 @@
|
|
|
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";
|
package/src/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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` and the in-place
|
|
4
|
+
* refresh as a hook (`useParheliaRefresh`). Everything else a head needs (the server check, the identity, rich text, a
|
|
5
|
+
* CMS's markers, the build values) is `@parhelia/head`'s; the constants these components' props refer to are re-exported
|
|
6
|
+
* 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";
|
|
@@ -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
|
+
}
|