@prismicio/react 2.0.0 → 2.0.3-debug.1

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.
@@ -1,4 +1,4 @@
1
- import * as React from "react";
1
+ import { useEffect } from "react";
2
2
 
3
3
  /**
4
4
  * Props for `<PrismicToolbar>`.
@@ -27,10 +27,10 @@ export const PrismicToolbar = ({
27
27
  type = "new",
28
28
  }: PrismicToolbarProps): null => {
29
29
  const src = `https://static.cdn.prismic.io/prismic.js?repo=${repositoryName}${
30
- type === "new" ? "&type=new" : ""
30
+ type === "new" ? "&new=true" : ""
31
31
  }`;
32
32
 
33
- React.useEffect(() => {
33
+ useEffect(() => {
34
34
  const existingScript = document.querySelector(`script[src="${src}"]`);
35
35
 
36
36
  if (!existingScript) {
@@ -45,7 +45,7 @@ export const PrismicToolbar = ({
45
45
 
46
46
  document.body.appendChild(script);
47
47
  }
48
- }, [repositoryName, type]);
48
+ }, [repositoryName, type, src]);
49
49
 
50
50
  return null;
51
51
  };
package/src/SliceZone.tsx CHANGED
@@ -1,7 +1,10 @@
1
1
  import * as React from "react";
2
+ import { useEffect, useMemo } from "react";
2
3
  import * as prismicT from "@prismicio/types";
4
+ import type { PascalCase } from "type-fest";
3
5
 
4
6
  import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
7
+ import { pascalCase } from "./lib/pascalCase";
5
8
 
6
9
  /**
7
10
  * The minimum required properties to represent a Prismic Slice for the
@@ -118,7 +121,7 @@ export const TODOSliceComponent = __PRODUCTION__
118
121
  : <TSlice extends SliceLike, TContext>({
119
122
  slice,
120
123
  }: SliceComponentProps<TSlice, TContext>): JSX.Element | null => {
121
- React.useEffect(() => {
124
+ useEffect(() => {
122
125
  console.warn(
123
126
  `[SliceZone] Could not find a component for Slice type "${slice.slice_type}"`,
124
127
  slice,
@@ -148,7 +151,7 @@ type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
148
151
  /**
149
152
  * The name of the Slice.
150
153
  */
151
- sliceName: TSlice["slice_type"];
154
+ sliceName: PascalCase<TSlice["slice_type"]>;
152
155
 
153
156
  /**
154
157
  * The index of the Slice in the Slice Zone.
@@ -240,7 +243,7 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
240
243
  defaultComponent = TODOSliceComponent,
241
244
  context = {} as TContext,
242
245
  }: SliceZoneProps<TSlice, TContext>): JSX.Element => {
243
- const renderedSlices = React.useMemo(() => {
246
+ const renderedSlices = useMemo(() => {
244
247
  return slices.map((slice, index) => {
245
248
  let Comp = (components[slice.slice_type as keyof typeof components] ||
246
249
  defaultComponent) as SliceComponentType<TSlice, TContext>;
@@ -249,7 +252,7 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
249
252
  if (resolver) {
250
253
  const resolvedComp = resolver({
251
254
  slice,
252
- sliceName: slice.slice_type,
255
+ sliceName: pascalCase(slice.slice_type),
253
256
  i: index,
254
257
  });
255
258
 
@@ -270,7 +273,7 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
270
273
  />
271
274
  );
272
275
  });
273
- }, [components, context, defaultComponent, slices]);
276
+ }, [components, context, defaultComponent, slices, resolver]);
274
277
 
275
278
  return <>{renderedSlices}</>;
276
279
  };
@@ -0,0 +1,12 @@
1
+ import type { PascalCase } from "type-fest";
2
+
3
+ export const pascalCase = <Input extends string>(
4
+ input: Input,
5
+ ): PascalCase<Input> => {
6
+ const camelCased = input.replace(/(?:-|_)(\w)/g, (_, c) => {
7
+ return c ? c.toUpperCase() : "";
8
+ });
9
+
10
+ return (camelCased[0].toUpperCase() +
11
+ camelCased.slice(1)) as PascalCase<Input>;
12
+ };
@@ -1,4 +1,4 @@
1
- import * as React from "react";
1
+ import { useContext } from "react";
2
2
 
3
3
  import { PrismicContext, PrismicContextValue } from "./PrismicProvider";
4
4
 
@@ -9,5 +9,5 @@ import { PrismicContext, PrismicContextValue } from "./PrismicProvider";
9
9
  * @returns The closest `<PrismicProvider>` context value.
10
10
  */
11
11
  export const usePrismicContext = (): PrismicContextValue => {
12
- return React.useContext(PrismicContext) || {};
12
+ return useContext(PrismicContext) || {};
13
13
  };
@@ -1,6 +1,6 @@
1
1
  import type * as prismic from "@prismicio/client";
2
2
 
3
- import * as React from "react";
3
+ import { useEffect } from "react";
4
4
 
5
5
  import { usePrismicContext } from "./usePrismicContext";
6
6
  import {
@@ -83,7 +83,7 @@ export const usePrismicPreviewResolver = (
83
83
  const [resolvedURL] = result;
84
84
  const { navigate } = args;
85
85
 
86
- React.useEffect(() => {
86
+ useEffect(() => {
87
87
  if (resolvedURL && navigate) {
88
88
  navigate(resolvedURL);
89
89
  }
@@ -1,6 +1,6 @@
1
1
  import type * as prismic from "@prismicio/client";
2
2
 
3
- import * as React from "react";
3
+ import { useEffect, useMemo, useReducer } from "react";
4
4
 
5
5
  import { PrismicClientHookState } from "./types";
6
6
  import { usePrismicClient } from "./usePrismicClient";
@@ -133,11 +133,11 @@ export const useStatefulPrismicClientMethod = <
133
133
 
134
134
  const client = usePrismicClient(explicitClient || lastArgExplicitClient);
135
135
 
136
- const [state, dispatch] = React.useReducer<
136
+ const [state, dispatch] = useReducer<
137
137
  React.Reducer<StateMachineState<TData>, StateMachineAction<TData>>
138
138
  >(reducer, initialState);
139
139
 
140
- React.useEffect(
140
+ useEffect(
141
141
  () => {
142
142
  // Used to prevent dispatching an action if the hook was cleaned up.
143
143
  let didCancel = false;
@@ -186,7 +186,7 @@ export const useStatefulPrismicClientMethod = <
186
186
  ],
187
187
  );
188
188
 
189
- return React.useMemo(
189
+ return useMemo(
190
190
  () => [
191
191
  state.data,
192
192
  {