@prismicio/next 0.1.6-alpha.1 → 0.1.7

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,3 @@
1
- import * as React from "react";
2
1
  import Image, { ImageProps, ImageLoaderProps } from "next/image";
3
2
  import { buildURL, ImgixURLParams } from "imgix-url-builder";
4
3
  import * as prismicH from "@prismicio/helpers";
@@ -7,6 +6,20 @@ import * as prismicT from "@prismicio/types";
7
6
  import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
8
7
  import { devMsg } from "./lib/devMsg";
9
8
 
9
+ const castInt = (input: string | number | undefined): number | undefined => {
10
+ if (typeof input === "number" || typeof input === "undefined") {
11
+ return input;
12
+ } else {
13
+ const parsed = Number.parseInt(input);
14
+
15
+ if (Number.isNaN(parsed)) {
16
+ return undefined;
17
+ } else {
18
+ return parsed;
19
+ }
20
+ }
21
+ };
22
+
10
23
  /**
11
24
  * Creates a `next/image` loader for Imgix, which Prismic uses, with an optional
12
25
  * collection of default Imgix parameters.
@@ -32,7 +45,7 @@ const imgixLoader = (args: ImageLoaderProps): string => {
32
45
 
33
46
  export type PrismicNextImageProps = Omit<
34
47
  ImageProps,
35
- "src" | "alt" | "width" | "height"
48
+ "src" | "alt" | "width" | "height" | "layout"
36
49
  > & {
37
50
  /**
38
51
  * The Prismic Image field or thumbnail to render.
@@ -62,7 +75,14 @@ export type PrismicNextImageProps = Omit<
62
75
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
63
76
  */
64
77
  fallbackAlt?: "";
65
- };
78
+ } & (
79
+ | ({
80
+ layout?: "intrinsic" | "fixed";
81
+ } & Pick<ImageProps, "width" | "height">)
82
+ | {
83
+ layout: "responsive" | "fill";
84
+ }
85
+ );
66
86
 
67
87
  /**
68
88
  * React component that renders an image from a Prismic Image field or one of
@@ -77,7 +97,6 @@ export type PrismicNextImageProps = Omit<
77
97
  *
78
98
  * @returns A responsive image component using `next/image` for the given Image
79
99
  * field.
80
- *
81
100
  * @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image
82
101
  */
83
102
  export const PrismicNextImage = ({
@@ -85,7 +104,7 @@ export const PrismicNextImage = ({
85
104
  imgixParams = {},
86
105
  alt,
87
106
  fallbackAlt,
88
- layout,
107
+ layout = "intrinsic",
89
108
  ...restProps
90
109
  }: PrismicNextImageProps) => {
91
110
  if (!__PRODUCTION__) {
@@ -108,12 +127,34 @@ export const PrismicNextImage = ({
108
127
 
109
128
  if (prismicH.isFilled.imageThumbnail(field)) {
110
129
  const src = buildURL(field.url, imgixParams);
130
+ const ar = field.dimensions.width / field.dimensions.height;
131
+
132
+ let resolvedWidth = field.dimensions.width;
133
+ let resolvedHeight = field.dimensions.height;
134
+
135
+ if (
136
+ (layout === "intrinsic" || layout === "fixed") &&
137
+ ("width" in restProps || "height" in restProps)
138
+ ) {
139
+ const castedWidth = castInt(restProps.width);
140
+ const castedHeight = castInt(restProps.height);
141
+
142
+ if (castedWidth) {
143
+ resolvedWidth = castedWidth;
144
+ } else {
145
+ if (castedHeight) {
146
+ resolvedWidth = ar * castedHeight;
147
+ }
148
+ }
149
+
150
+ resolvedHeight = resolvedWidth / ar;
151
+ }
111
152
 
112
153
  return (
113
154
  <Image
114
155
  src={src}
115
- width={layout === "fill" ? undefined : field.dimensions.width}
116
- height={layout === "fill" ? undefined : field.dimensions.height}
156
+ width={layout === "fill" ? undefined : resolvedWidth}
157
+ height={layout === "fill" ? undefined : resolvedHeight}
117
158
  alt={alt ?? (field.alt || fallbackAlt)}
118
159
  loader={imgixLoader}
119
160
  layout={layout}
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
- import { PrismicToolbar } from "@prismicio/react";
3
2
  import { useRouter } from "next/router";
3
+ import Script from "next/script";
4
4
 
5
5
  import { getPrismicPreviewCookie } from "./lib/getPrismicPreviewCookie";
6
6
  import { getPreviewCookieRepositoryName } from "./lib/getPreviewCookieRepositoryName";
@@ -164,7 +164,10 @@ export function PrismicPreview({
164
164
  return (
165
165
  <>
166
166
  {children}
167
- <PrismicToolbar repositoryName={repositoryName} />
167
+ <Script
168
+ src={`https://static.cdn.prismic.io/prismic.js?repo=${repositoryName}&new=true`}
169
+ strategy="lazyOnload"
170
+ />
168
171
  </>
169
172
  );
170
173
  }