@prismicio/react 3.3.0-canary.67d00d1 → 3.3.0-pr.249.d18f450

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.
@@ -8,6 +8,25 @@ type PrismicToolbarProps = {
8
8
  * repository URL is `my-repo.prismic.io`.
9
9
  */
10
10
  repositoryName: string;
11
+ /**
12
+ * Called when the Prismic toolbar triggers a preview update. This happens
13
+ * when a preview session starts or the previewed content changes.
14
+ *
15
+ * The callback receives the full `CustomEvent`, giving access to
16
+ * `event.detail.ref` (the preview ref) and `event.preventDefault()` to
17
+ * cancel the toolbar's default reload behavior.
18
+ */
19
+ onPreviewUpdate?: (event: CustomEvent<{
20
+ ref: string;
21
+ }>) => void;
22
+ /**
23
+ * Called when the Prismic toolbar triggers a preview end. This happens when
24
+ * a preview session is closed.
25
+ *
26
+ * The callback receives the full `CustomEvent`, giving access to
27
+ * `event.preventDefault()` to cancel the toolbar's default reload behavior.
28
+ */
29
+ onPreviewEnd?: (event: CustomEvent<null>) => void;
11
30
  };
12
31
  /**
13
32
  * Renders the Prismic Toolbar script to support draft previews.
@@ -1 +1 @@
1
- {"version":3,"file":"PrismicToolbar.d.ts","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":[],"mappings":";;;;KAMY,mBAAA;EAAA;AAmBZ;;;;;;;;;;;;;;;;cAAa,gBAAgB,GAAG"}
1
+ {"version":3,"file":"PrismicToolbar.d.ts","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":[],"mappings":";;;;KAMY,mBAAA;EAAA;AAsCZ;;;;;;;;;;;;4BAvB2B;;;;;;;;;;yBASH;;;;;;;;;;;;;cAcX,gBAAgB,GAAG"}
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { useEffect } from "react";
3
+ import { useEffect, useRef } from "react";
4
4
  import { getToolbarSrc } from "@prismicio/client";
5
5
 
6
6
  //#region src/PrismicToolbar.tsx
@@ -16,8 +16,12 @@ import { getToolbarSrc } from "@prismicio/client";
16
16
  * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
17
17
  */
18
18
  const PrismicToolbar = (props) => {
19
- const { repositoryName } = props;
19
+ const { repositoryName, onPreviewUpdate, onPreviewEnd } = props;
20
20
  const src = getToolbarSrc(repositoryName);
21
+ const onPreviewUpdateRef = useRef(onPreviewUpdate);
22
+ onPreviewUpdateRef.current = onPreviewUpdate;
23
+ const onPreviewEndRef = useRef(onPreviewEnd);
24
+ onPreviewEndRef.current = onPreviewEnd;
21
25
  useEffect(() => {
22
26
  if (!document.querySelector(`script[src="${src}"]`)) {
23
27
  const script = document.createElement("script");
@@ -29,6 +33,12 @@ const PrismicToolbar = (props) => {
29
33
  document.body.appendChild(script);
30
34
  }
31
35
  }, [repositoryName, src]);
36
+ useEffect(() => {
37
+ const controller = new AbortController();
38
+ window.addEventListener("prismicPreviewUpdate", (event) => onPreviewUpdateRef.current?.(event), { signal: controller.signal });
39
+ window.addEventListener("prismicPreviewEnd", (event) => onPreviewEndRef.current?.(event), { signal: controller.signal });
40
+ return () => controller.abort();
41
+ }, []);
32
42
  return null;
33
43
  };
34
44
 
@@ -1 +1 @@
1
- {"version":3,"file":"PrismicToolbar.js","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":["\"use client\";\n\nimport { type FC, useEffect } from \"react\";\nimport { getToolbarSrc } from \"@prismicio/client\";\n\n/** Props for `<PrismicToolbar>`. */\nexport type PrismicToolbarProps = {\n\t/**\n\t * The name of the Prismic repository. For example, `\"my-repo\"` if the\n\t * repository URL is `my-repo.prismic.io`.\n\t */\n\trepositoryName: string;\n};\n\n/**\n * Renders the Prismic Toolbar script to support draft previews.\n *\n * @example\n *\n * ```tsx\n * <PrismicToolbar repositoryName=\"my-repo\" />;\n * ```\n *\n * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}\n */\nexport const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {\n\tconst { repositoryName } = props;\n\n\tconst src = getToolbarSrc(repositoryName);\n\n\tuseEffect(() => {\n\t\tconst existingScript = document.querySelector(`script[src=\"${src}\"]`);\n\n\t\tif (!existingScript) {\n\t\t\tconst script = document.createElement(\"script\");\n\t\t\tscript.src = src;\n\t\t\tscript.defer = true;\n\n\t\t\t// Used to distinguish the toolbar element from other elements.\n\t\t\tscript.dataset.prismicToolbar = \"\";\n\t\t\tscript.dataset.repositoryName = repositoryName;\n\n\t\t\t// Disable Happy DOM `<script>` evaluation during tests.\n\t\t\t// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.\n\t\t\tscript._evaluateScript = false;\n\n\t\t\tdocument.body.appendChild(script);\n\t\t}\n\t}, [repositoryName, src]);\n\n\treturn null;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAyBA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EAAE,mBAAmB;CAE3B,MAAM,MAAM,cAAc,eAAe;AAEzC,iBAAgB;AAGf,MAAI,CAFmB,SAAS,cAAc,eAAe,IAAI,IAAI,EAEhD;GACpB,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,UAAO,MAAM;AACb,UAAO,QAAQ;AAGf,UAAO,QAAQ,iBAAiB;AAChC,UAAO,QAAQ,iBAAiB;AAIhC,UAAO,kBAAkB;AAEzB,YAAS,KAAK,YAAY,OAAO;;IAEhC,CAAC,gBAAgB,IAAI,CAAC;AAEzB,QAAO"}
1
+ {"version":3,"file":"PrismicToolbar.js","names":[],"sources":["../src/PrismicToolbar.tsx"],"sourcesContent":["\"use client\";\n\nimport { type FC, useEffect, useRef } from \"react\";\nimport { getToolbarSrc } from \"@prismicio/client\";\n\n/** Props for `<PrismicToolbar>`. */\nexport type PrismicToolbarProps = {\n\t/**\n\t * The name of the Prismic repository. For example, `\"my-repo\"` if the\n\t * repository URL is `my-repo.prismic.io`.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * Called when the Prismic toolbar triggers a preview update. This happens\n\t * when a preview session starts or the previewed content changes.\n\t *\n\t * The callback receives the full `CustomEvent`, giving access to\n\t * `event.detail.ref` (the preview ref) and `event.preventDefault()` to\n\t * cancel the toolbar's default reload behavior.\n\t */\n\tonPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void;\n\n\t/**\n\t * Called when the Prismic toolbar triggers a preview end. This happens when\n\t * a preview session is closed.\n\t *\n\t * The callback receives the full `CustomEvent`, giving access to\n\t * `event.preventDefault()` to cancel the toolbar's default reload behavior.\n\t */\n\tonPreviewEnd?: (event: CustomEvent<null>) => void;\n};\n\n/**\n * Renders the Prismic Toolbar script to support draft previews.\n *\n * @example\n *\n * ```tsx\n * <PrismicToolbar repositoryName=\"my-repo\" />;\n * ```\n *\n * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}\n */\nexport const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {\n\tconst { repositoryName, onPreviewUpdate, onPreviewEnd } = props;\n\n\tconst src = getToolbarSrc(repositoryName);\n\n\tconst onPreviewUpdateRef = useRef(onPreviewUpdate);\n\tonPreviewUpdateRef.current = onPreviewUpdate;\n\n\tconst onPreviewEndRef = useRef(onPreviewEnd);\n\tonPreviewEndRef.current = onPreviewEnd;\n\n\tuseEffect(() => {\n\t\tconst existingScript = document.querySelector(`script[src=\"${src}\"]`);\n\n\t\tif (!existingScript) {\n\t\t\tconst script = document.createElement(\"script\");\n\t\t\tscript.src = src;\n\t\t\tscript.defer = true;\n\n\t\t\t// Used to distinguish the toolbar element from other elements.\n\t\t\tscript.dataset.prismicToolbar = \"\";\n\t\t\tscript.dataset.repositoryName = repositoryName;\n\n\t\t\t// Disable Happy DOM `<script>` evaluation during tests.\n\t\t\t// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.\n\t\t\tscript._evaluateScript = false;\n\n\t\t\tdocument.body.appendChild(script);\n\t\t}\n\t}, [repositoryName, src]);\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\n\t\twindow.addEventListener(\n\t\t\t\"prismicPreviewUpdate\",\n\t\t\t(event) =>\n\t\t\t\tonPreviewUpdateRef.current?.(\n\t\t\t\t\tevent as CustomEvent<{ ref: string }>,\n\t\t\t\t),\n\t\t\t{ signal: controller.signal },\n\t\t);\n\n\t\twindow.addEventListener(\n\t\t\t\"prismicPreviewEnd\",\n\t\t\t(event) =>\n\t\t\t\tonPreviewEndRef.current?.(event as CustomEvent<null>),\n\t\t\t{ signal: controller.signal },\n\t\t);\n\n\t\treturn () => controller.abort();\n\t}, []);\n\n\treturn null;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AA4CA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EAAE,gBAAgB,iBAAiB,iBAAiB;CAE1D,MAAM,MAAM,cAAc,eAAe;CAEzC,MAAM,qBAAqB,OAAO,gBAAgB;AAClD,oBAAmB,UAAU;CAE7B,MAAM,kBAAkB,OAAO,aAAa;AAC5C,iBAAgB,UAAU;AAE1B,iBAAgB;AAGf,MAAI,CAFmB,SAAS,cAAc,eAAe,IAAI,IAAI,EAEhD;GACpB,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,UAAO,MAAM;AACb,UAAO,QAAQ;AAGf,UAAO,QAAQ,iBAAiB;AAChC,UAAO,QAAQ,iBAAiB;AAIhC,UAAO,kBAAkB;AAEzB,YAAS,KAAK,YAAY,OAAO;;IAEhC,CAAC,gBAAgB,IAAI,CAAC;AAEzB,iBAAgB;EACf,MAAM,aAAa,IAAI,iBAAiB;AAExC,SAAO,iBACN,yBACC,UACA,mBAAmB,UAClB,MACA,EACF,EAAE,QAAQ,WAAW,QAAQ,CAC7B;AAED,SAAO,iBACN,sBACC,UACA,gBAAgB,UAAU,MAA2B,EACtD,EAAE,QAAQ,WAAW,QAAQ,CAC7B;AAED,eAAa,WAAW,OAAO;IAC7B,EAAE,CAAC;AAEN,QAAO"}
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "3.3.0-canary.67d00d1";
2
+ var version = "3.3.0-pr.249.d18f450";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/react",
3
- "version": "3.3.0-canary.67d00d1",
3
+ "version": "3.3.0-pr.249.d18f450",
4
4
  "description": "React components and hooks to fetch and present Prismic content",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { type FC, useEffect } from "react";
3
+ import { type FC, useEffect, useRef } from "react";
4
4
  import { getToolbarSrc } from "@prismicio/client";
5
5
 
6
6
  /** Props for `<PrismicToolbar>`. */
@@ -10,6 +10,25 @@ export type PrismicToolbarProps = {
10
10
  * repository URL is `my-repo.prismic.io`.
11
11
  */
12
12
  repositoryName: string;
13
+
14
+ /**
15
+ * Called when the Prismic toolbar triggers a preview update. This happens
16
+ * when a preview session starts or the previewed content changes.
17
+ *
18
+ * The callback receives the full `CustomEvent`, giving access to
19
+ * `event.detail.ref` (the preview ref) and `event.preventDefault()` to
20
+ * cancel the toolbar's default reload behavior.
21
+ */
22
+ onPreviewUpdate?: (event: CustomEvent<{ ref: string }>) => void;
23
+
24
+ /**
25
+ * Called when the Prismic toolbar triggers a preview end. This happens when
26
+ * a preview session is closed.
27
+ *
28
+ * The callback receives the full `CustomEvent`, giving access to
29
+ * `event.preventDefault()` to cancel the toolbar's default reload behavior.
30
+ */
31
+ onPreviewEnd?: (event: CustomEvent<null>) => void;
13
32
  };
14
33
 
15
34
  /**
@@ -24,10 +43,16 @@ export type PrismicToolbarProps = {
24
43
  * @see Learn how to set up preview functionality and the toolbar's role in preview sessions: {@link https://prismic.io/docs/previews}
25
44
  */
26
45
  export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
27
- const { repositoryName } = props;
46
+ const { repositoryName, onPreviewUpdate, onPreviewEnd } = props;
28
47
 
29
48
  const src = getToolbarSrc(repositoryName);
30
49
 
50
+ const onPreviewUpdateRef = useRef(onPreviewUpdate);
51
+ onPreviewUpdateRef.current = onPreviewUpdate;
52
+
53
+ const onPreviewEndRef = useRef(onPreviewEnd);
54
+ onPreviewEndRef.current = onPreviewEnd;
55
+
31
56
  useEffect(() => {
32
57
  const existingScript = document.querySelector(`script[src="${src}"]`);
33
58
 
@@ -48,5 +73,27 @@ export const PrismicToolbar: FC<PrismicToolbarProps> = (props) => {
48
73
  }
49
74
  }, [repositoryName, src]);
50
75
 
76
+ useEffect(() => {
77
+ const controller = new AbortController();
78
+
79
+ window.addEventListener(
80
+ "prismicPreviewUpdate",
81
+ (event) =>
82
+ onPreviewUpdateRef.current?.(
83
+ event as CustomEvent<{ ref: string }>,
84
+ ),
85
+ { signal: controller.signal },
86
+ );
87
+
88
+ window.addEventListener(
89
+ "prismicPreviewEnd",
90
+ (event) =>
91
+ onPreviewEndRef.current?.(event as CustomEvent<null>),
92
+ { signal: controller.signal },
93
+ );
94
+
95
+ return () => controller.abort();
96
+ }, []);
97
+
51
98
  return null;
52
99
  };