@parhelia/react 1.0.12903 → 1.0.13030
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/README.md +17 -3
- package/package.json +2 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +5 -4
- package/src/picture.d.ts +20 -0
- package/src/picture.js +62 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @parhelia/react
|
|
2
2
|
|
|
3
|
-
The React bindings of [`@parhelia/head`](
|
|
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
4
|
`@parhelia/next`), TanStack Start, React Router, plain Vite SSR. Plain JavaScript, no build step.
|
|
5
5
|
|
|
6
6
|
| Export | What it is for |
|
|
@@ -9,6 +9,7 @@ The React bindings of [`@parhelia/head`](../head/README.md), for a React renderi
|
|
|
9
9
|
| `ParheliaBridgeScript` | renders the bridge's `<script>` on the server, on the editor's requests |
|
|
10
10
|
| `ParheliaSlot` | marks a slot for the editor, also when it is empty |
|
|
11
11
|
| `useParheliaRefresh` | the in-place refresh as a hook |
|
|
12
|
+
| `ParheliaPicture` | an image cropped by the browser, marked for the editor's cropper |
|
|
12
13
|
|
|
13
14
|
Everything else a head needs is `@parhelia/head`'s: the server check (`@parhelia/head/server`), the rendered identity, rich
|
|
14
15
|
text, a CMS's markers, the build values and the Vite adapter (`@parhelia/head/vite`). The constants these components' props
|
|
@@ -72,8 +73,7 @@ const inParhelia = isParheliaFrameRequest(useRouter().query);
|
|
|
72
73
|
```
|
|
73
74
|
|
|
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
|
|
75
|
-
label, default `name`) is optional.
|
|
76
|
-
`docs/migration/w12-cfsite-starter-changes.md`).
|
|
76
|
+
label, default `name`) is optional.
|
|
77
77
|
|
|
78
78
|
## In-place refresh: `useParheliaRefresh`
|
|
79
79
|
|
|
@@ -91,3 +91,17 @@ A head that runs another React than the one this package resolves (linked from a
|
|
|
91
91
|
`installParheliaRefresh({ refetch, signature: () => latest, deadlineMs })` from `@parhelia/head/refresh` in its own effect: it
|
|
92
92
|
imports no React, and its answer removes the hook again.
|
|
93
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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13030",
|
|
4
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
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"publishConfig": {
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"test": "vitest run"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@parhelia/head": "1.0.
|
|
30
|
+
"@parhelia/head": "1.0.13030"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": ">=18"
|
package/src/index.d.ts
CHANGED
|
@@ -15,3 +15,4 @@ export {
|
|
|
15
15
|
export { ParheliaBridge, shouldLoadParheliaBridge, type ParheliaBridgeProps } from "./bridge-client.js";
|
|
16
16
|
export { ParheliaSlot, EMPTY_SLOT_ATTRIBUTE, type ParheliaSlotProps } from "./slot.js";
|
|
17
17
|
export { useParheliaRefresh, type ParheliaRefreshOptions } from "./refresh-hook.js";
|
|
18
|
+
export { ParheliaPicture, type ParheliaPictureProps } from "./picture.js";
|
package/src/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
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
|
|
4
|
-
* refresh as a hook (`useParheliaRefresh`)
|
|
5
|
-
* CMS's markers, the build values) is `@parhelia/head`'s; the constants these
|
|
6
|
-
* here. A Next.js head uses `@parhelia/next`, which re-exports both. Plain JavaScript, no build step.
|
|
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
7
|
*/
|
|
8
8
|
export {
|
|
9
9
|
ParheliaBridgeScript,
|
|
@@ -17,3 +17,4 @@ export {
|
|
|
17
17
|
export { ParheliaBridge, shouldLoadParheliaBridge } from "./bridge-client.js";
|
|
18
18
|
export { ParheliaSlot, EMPTY_SLOT_ATTRIBUTE } from "./slot.js";
|
|
19
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
|
+
}
|