@prismicio/svelte 0.0.6 → 0.0.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [0.0.7](https://github.com/prismicio/prismic-svelte/compare/v0.0.6...v0.0.7) (2023-03-02)
6
+
7
+
8
+ ### Features
9
+
10
+ * create `<PrismicImage>` component ([#5](https://github.com/prismicio/prismic-svelte/issues/5)) ([9b29b58](https://github.com/prismicio/prismic-svelte/commit/9b29b58d771ae0d6cf6960a28d35035c7fff23b0))
11
+
5
12
  ### [0.0.6](https://github.com/prismicio/prismic-svelte/compare/v0.0.5...v0.0.6) (2023-02-24)
6
13
 
7
14
 
@@ -0,0 +1,11 @@
1
+ // We need to polyfill process if it doesn't exist, such as in the browser.
2
+ if (typeof process === "undefined") {
3
+ globalThis.process = { env: {} };
4
+ }
5
+ /**
6
+ * `true` if in the production environment, `false` otherwise.
7
+ *
8
+ * This boolean can be used to perform actions only in development environments,
9
+ * such as logging.
10
+ */
11
+ export const __PRODUCTION__ = process.env.NODE_ENV === "production";
@@ -1,15 +1,91 @@
1
1
  <script>import * as prismicH from "@prismicio/helpers";
2
+ import { __PRODUCTION__ } from "../__PRODUCTION__";
2
3
  export let field;
3
- let className;
4
- export { className as class };
4
+ export let imgixParams = {};
5
+ export let widths = undefined;
6
+ export let pixelDensities = undefined;
7
+ export let width = undefined;
8
+ export let height = undefined;
9
+ export let alt = undefined;
10
+ export let fallbackAlt = undefined;
11
+ const castInt = (input) => {
12
+ if (typeof input === "number" ||
13
+ typeof input === "undefined" ||
14
+ input === null) {
15
+ return input;
16
+ }
17
+ else {
18
+ const parsed = Number.parseInt(input);
19
+ if (Number.isNaN(parsed)) {
20
+ return undefined;
21
+ }
22
+ else {
23
+ return parsed;
24
+ }
25
+ }
26
+ };
27
+ const ar = field.dimensions && field.dimensions.width / field.dimensions.height;
28
+ const castedWidth = castInt(width);
29
+ const castedHeight = castInt(height);
30
+ let resolvedWidth = castedWidth ?? (field.dimensions && field.dimensions.width);
31
+ let resolvedHeight = castedHeight ?? (field.dimensions && field.dimensions.height);
32
+ if (ar &&
33
+ castedWidth != null &&
34
+ (castedHeight == null || typeof castedWidth !== "undefined")) {
35
+ resolvedHeight = castedWidth / ar;
36
+ }
37
+ else if (ar &&
38
+ castedHeight != null &&
39
+ (castedWidth == null || typeof castedWidth !== "undefined")) {
40
+ resolvedWidth = castedHeight * ar;
41
+ }
42
+ if (!__PRODUCTION__) {
43
+ if (widths && pixelDensities) {
44
+ console.warn(`[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`);
45
+ }
46
+ if (typeof alt === "string" && alt !== "") {
47
+ console.warn(`[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="".`);
48
+ }
49
+ if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
50
+ console.warn(`[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="".`);
51
+ }
52
+ }
5
53
  </script>
6
54
 
55
+ <!--
56
+ @component
57
+ Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
58
+
59
+ @example Rendering an Image field:
60
+ ```svelte
61
+ <PrismicImage
62
+ field={document.data.example_image}
63
+ imgixParams={{ sat: -100 }}
64
+ />
65
+ ```
66
+ -->
67
+
7
68
  {#if prismicH.isFilled.image(field)}
8
- {@const { alt, dimensions } = field}
9
- {@const { width, height } = dimensions}
10
- {@const { src, srcset } = prismicH.asImageWidthSrcSet(field)}
69
+ {@const { src, srcset } =
70
+ pixelDensities && !widths
71
+ ? prismicH.asImagePixelDensitySrcSet(field, {
72
+ pixelDensities:
73
+ pixelDensities === "defaults" ? undefined : pixelDensities,
74
+ ...imgixParams,
75
+ })
76
+ : prismicH.asImageWidthSrcSet(field, {
77
+ widths: widths === "defaults" ? undefined : widths,
78
+ ...imgixParams,
79
+ })}
11
80
 
12
- <img {src} {srcset} {alt} {width} {height} class={className} />
81
+ <img
82
+ {src}
83
+ {srcset}
84
+ alt={alt ?? field.alt ?? fallbackAlt}
85
+ width={width || height ? resolvedWidth : field.dimensions.width}
86
+ height={height || width ? resolvedHeight : field.dimensions.height}
87
+ {...$$restProps}
88
+ />
13
89
  {:else}
14
- <img alt={field.alt || ""} class={className} />
90
+ <img alt={field.alt || ""} {...$$restProps} />
15
91
  {/if}
@@ -1,5 +1,5 @@
1
1
  // export { default as PrismicEmbed } from "./PrismicEmbed.svelte";
2
- // export { default as PrismicImage } from "./PrismicImage.svelte";
2
+ export { default as PrismicImage } from "./PrismicImage.svelte";
3
3
  export { default as PrismicLink } from "./PrismicLink.svelte";
4
4
  // export { default as PrismicText } from "./PrismicText.svelte";
5
5
  // export { default as PrismicRichText } from "./PrismicRichText.svelte";
package/index.js CHANGED
@@ -2,8 +2,7 @@
2
2
  export {
3
3
  // Components
4
4
  // PrismicEmbed,
5
- // PrismicImage,
6
- PrismicLink,
5
+ PrismicImage, PrismicLink,
7
6
  // PrismicText,
8
7
  // PrismicRichText,
9
8
  // Slice Zone
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `true` if in the production environment, `false` otherwise.
3
+ *
4
+ * This boolean can be used to perform actions only in development environments,
5
+ * such as logging.
6
+ */
7
+ export declare const __PRODUCTION__: boolean;
@@ -1,9 +1,51 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
+ import * as prismicH from "@prismicio/helpers";
2
3
  import type * as prismicT from "@prismicio/types";
3
4
  declare const __propDef: {
4
5
  props: {
6
+ /**
7
+ * A Prismic Image field.
8
+ */
5
9
  field: prismicT.ImageField;
6
- class: string;
10
+ /**
11
+ * An object of Imgix URL API parameters to transform the image.
12
+ *
13
+ * See: https://docs.imgix.com/apis/rendering
14
+ */
15
+ imgixParams?: Parameters<typeof prismicH.asImageSrc>[1];
16
+ /**
17
+ * Widths used to build a `srcset` value for the Image field.
18
+ *
19
+ * If a `widths` prop is not given or `"defaults"` is passed, the following
20
+ * widths will be used: 640, 750, 828, 1080, 1200, 1920, 2048, 3840.
21
+ *
22
+ * If the Image field contains responsive views, each responsive view can be
23
+ * used as a width in the resulting `srcset` by passing `"thumbnails"` as
24
+ * the `widths` prop.
25
+ */
26
+ widths?: NonNullable<Parameters<typeof prismicH.asImageWidthSrcSet>[1]>["widths"] | "defaults";
27
+ /**
28
+ * Pixel densities used to build a `srcset` value for the Image field.
29
+ *
30
+ * If a `pixelDensities` prop is passed `"defaults"`, the following pixel
31
+ * densities will be used: 1, 2, 3.
32
+ */
33
+ pixelDensities?: NonNullable<Parameters<typeof prismicH.asImagePixelDensitySrcSet>[1]>["pixelDensities"] | "defaults";
34
+ /**
35
+ * Declare an image as decorative by providing `alt=""`.
36
+ *
37
+ * See:
38
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
39
+ */
40
+ alt?: "" | undefined;
41
+ /**
42
+ * Declare an image as decorative only if the Image field does not have
43
+ * alternative text by providing `fallbackAlt=""`.
44
+ *
45
+ * See:
46
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
47
+ */
48
+ fallbackAlt?: "" | undefined;
7
49
  };
8
50
  events: {
9
51
  [evt: string]: CustomEvent<any>;
@@ -13,6 +55,17 @@ declare const __propDef: {
13
55
  export type PrismicImageProps = typeof __propDef.props;
14
56
  export type PrismicImageEvents = typeof __propDef.events;
15
57
  export type PrismicImageSlots = typeof __propDef.slots;
58
+ /**
59
+ * Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
60
+ *
61
+ * @example Rendering an Image field:
62
+ * ```svelte
63
+ * <PrismicImage
64
+ * field={document.data.example_image}
65
+ * imgixParams={{ sat: -100 }}
66
+ * />
67
+ * ```
68
+ */
16
69
  export default class PrismicImage extends SvelteComponentTyped<PrismicImageProps, PrismicImageEvents, PrismicImageSlots> {
17
70
  }
18
71
  export {};
@@ -1,2 +1,3 @@
1
+ export { default as PrismicImage } from "./PrismicImage.svelte";
1
2
  export { default as PrismicLink } from "./PrismicLink.svelte";
2
3
  export { default as SliceZone } from "./SliceZone.svelte";
package/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { PrismicLink, SliceZone, } from "./components";
1
+ export { PrismicImage, PrismicLink, SliceZone, } from "./components";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Svelte plugin, components, and composables to fetch and present Prismic content",
5
5
  "keywords": [
6
6
  "typescript",
@@ -0,0 +1,12 @@
1
+ // We need to polyfill process if it doesn't exist, such as in the browser.
2
+ if (typeof process === "undefined") {
3
+ globalThis.process = { env: {} } as typeof process;
4
+ }
5
+
6
+ /**
7
+ * `true` if in the production environment, `false` otherwise.
8
+ *
9
+ * This boolean can be used to perform actions only in development environments,
10
+ * such as logging.
11
+ */
12
+ export const __PRODUCTION__ = process.env.NODE_ENV === "production";
@@ -1,20 +1,179 @@
1
1
  <script lang="ts">
2
2
  import * as prismicH from "@prismicio/helpers";
3
3
  import type * as prismicT from "@prismicio/types";
4
+ import { __PRODUCTION__ } from "$lib/__PRODUCTION__";
4
5
 
5
- export let field: prismicT.ImageField;
6
+ type PrismicImageProps = {
7
+ /**
8
+ * A Prismic Image field.
9
+ */
10
+ field: prismicT.ImageField;
11
+ /**
12
+ * An object of Imgix URL API parameters to transform the image.
13
+ *
14
+ * See: https://docs.imgix.com/apis/rendering
15
+ */
16
+ imgixParams?: Parameters<typeof prismicH.asImageSrc>[1];
17
+ /**
18
+ * Widths used to build a `srcset` value for the Image field.
19
+ *
20
+ * If a `widths` prop is not given or `"defaults"` is passed, the following
21
+ * widths will be used: 640, 750, 828, 1080, 1200, 1920, 2048, 3840.
22
+ *
23
+ * If the Image field contains responsive views, each responsive view can be
24
+ * used as a width in the resulting `srcset` by passing `"thumbnails"` as
25
+ * the `widths` prop.
26
+ */
27
+ widths?:
28
+ | NonNullable<Parameters<typeof prismicH.asImageWidthSrcSet>[1]>["widths"]
29
+ | "defaults";
30
+ /**
31
+ * Pixel densities used to build a `srcset` value for the Image field.
32
+ *
33
+ * If a `pixelDensities` prop is passed `"defaults"`, the following pixel
34
+ * densities will be used: 1, 2, 3.
35
+ */
36
+ pixelDensities?:
37
+ | NonNullable<
38
+ Parameters<typeof prismicH.asImagePixelDensitySrcSet>[1]
39
+ >["pixelDensities"]
40
+ | "defaults";
41
+ /**
42
+ * Declare an image as decorative by providing `alt=""`.
43
+ *
44
+ * See:
45
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
46
+ */
47
+ alt?: "";
48
+ /**
49
+ * Declare an image as decorative only if the Image field does not have
50
+ * alternative text by providing `fallbackAlt=""`.
51
+ *
52
+ * See:
53
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
54
+ */
55
+ fallbackAlt?: "";
56
+ };
6
57
 
7
- let className: string;
58
+ type $$Props = Omit<svelteHTML.IntrinsicElements["img"], "alt"> &
59
+ PrismicImageProps;
8
60
 
9
- export { className as class };
61
+ export let field: $$Props["field"];
62
+
63
+ export let imgixParams: $$Props["imgixParams"] = {};
64
+
65
+ export let widths: $$Props["widths"] = undefined;
66
+
67
+ export let pixelDensities: $$Props["pixelDensities"] = undefined;
68
+
69
+ export let width: $$Props["width"] = undefined;
70
+
71
+ export let height: $$Props["height"] = undefined;
72
+
73
+ export let alt: $$Props["alt"] = undefined;
74
+
75
+ export let fallbackAlt: $$Props["fallbackAlt"] = undefined;
76
+
77
+ const castInt = (
78
+ input: string | number | null | undefined,
79
+ ): number | undefined | null => {
80
+ if (
81
+ typeof input === "number" ||
82
+ typeof input === "undefined" ||
83
+ input === null
84
+ ) {
85
+ return input;
86
+ } else {
87
+ const parsed = Number.parseInt(input);
88
+
89
+ if (Number.isNaN(parsed)) {
90
+ return undefined;
91
+ } else {
92
+ return parsed;
93
+ }
94
+ }
95
+ };
96
+
97
+ const ar =
98
+ field.dimensions && field.dimensions.width / field.dimensions.height;
99
+
100
+ const castedWidth = castInt(width);
101
+ const castedHeight = castInt(height);
102
+
103
+ let resolvedWidth =
104
+ castedWidth ?? (field.dimensions && field.dimensions.width);
105
+ let resolvedHeight =
106
+ castedHeight ?? (field.dimensions && field.dimensions.height);
107
+
108
+ if (
109
+ ar &&
110
+ castedWidth != null &&
111
+ (castedHeight == null || typeof castedWidth !== "undefined")
112
+ ) {
113
+ resolvedHeight = castedWidth / ar;
114
+ } else if (
115
+ ar &&
116
+ castedHeight != null &&
117
+ (castedWidth == null || typeof castedWidth !== "undefined")
118
+ ) {
119
+ resolvedWidth = castedHeight * ar;
120
+ }
121
+
122
+ if (!__PRODUCTION__) {
123
+ if (widths && pixelDensities) {
124
+ console.warn(
125
+ `[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`,
126
+ );
127
+ }
128
+
129
+ if (typeof alt === "string" && alt !== "") {
130
+ console.warn(
131
+ `[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="".`,
132
+ );
133
+ }
134
+
135
+ if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
136
+ console.warn(
137
+ `[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="".`,
138
+ );
139
+ }
140
+ }
10
141
  </script>
11
142
 
143
+ <!--
144
+ @component
145
+ Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
146
+
147
+ @example Rendering an Image field:
148
+ ```svelte
149
+ <PrismicImage
150
+ field={document.data.example_image}
151
+ imgixParams={{ sat: -100 }}
152
+ />
153
+ ```
154
+ -->
155
+
12
156
  {#if prismicH.isFilled.image(field)}
13
- {@const { alt, dimensions } = field}
14
- {@const { width, height } = dimensions}
15
- {@const { src, srcset } = prismicH.asImageWidthSrcSet(field)}
157
+ {@const { src, srcset } =
158
+ pixelDensities && !widths
159
+ ? prismicH.asImagePixelDensitySrcSet(field, {
160
+ pixelDensities:
161
+ pixelDensities === "defaults" ? undefined : pixelDensities,
162
+ ...imgixParams,
163
+ })
164
+ : prismicH.asImageWidthSrcSet(field, {
165
+ widths: widths === "defaults" ? undefined : widths,
166
+ ...imgixParams,
167
+ })}
16
168
 
17
- <img {src} {srcset} {alt} {width} {height} class={className} />
169
+ <img
170
+ {src}
171
+ {srcset}
172
+ alt={alt ?? field.alt ?? fallbackAlt}
173
+ width={width || height ? resolvedWidth : field.dimensions.width}
174
+ height={height || width ? resolvedHeight : field.dimensions.height}
175
+ {...$$restProps}
176
+ />
18
177
  {:else}
19
- <img alt={field.alt || ""} class={className} />
178
+ <img alt={field.alt || ""} {...$$restProps} />
20
179
  {/if}
@@ -1,6 +1,7 @@
1
1
  <script lang="ts">
2
2
  import * as prismicH from "@prismicio/helpers";
3
3
  import type * as prismicT from "@prismicio/types";
4
+ // import { usePrismic } from "../usePrismic";
4
5
 
5
6
  type PrismicLinkProps = {
6
7
  /**
@@ -1,6 +1,6 @@
1
1
  // export { default as PrismicEmbed } from "./PrismicEmbed.svelte";
2
2
 
3
- // export { default as PrismicImage } from "./PrismicImage.svelte";
3
+ export { default as PrismicImage } from "./PrismicImage.svelte";
4
4
 
5
5
  export { default as PrismicLink } from "./PrismicLink.svelte";
6
6
 
package/src/lib/index.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  export {
4
4
  // Components
5
5
  // PrismicEmbed,
6
- // PrismicImage,
6
+ PrismicImage,
7
7
  PrismicLink,
8
8
  // PrismicText,
9
9
  // PrismicRichText,
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { SliceZone, PrismicLink } from "$lib";
2
+ import { SliceZone, PrismicLink, PrismicImage } from "$lib";
3
3
  import { RichText, CodeSnippet } from "./slices";
4
4
  import type { PageData } from "./$types";
5
5
 
@@ -13,7 +13,14 @@
13
13
 
14
14
  <main>
15
15
  <h1>Welcome to SvelteKit</h1>
16
- <h2>Prismic Link</h2>
16
+ <h2><code>PrismicImage</code></h2>
17
+ <PrismicImage
18
+ field={data.exampleDocument.data.example_image}
19
+ pixelDensities="defaults"
20
+ height="200"
21
+ alt=""
22
+ />
23
+ <h2><code>PrismicLink</code></h2>
17
24
  <PrismicLink
18
25
  data-sveltekit-noscroll
19
26
  data-sveltekit-preload-code
@@ -23,7 +30,7 @@
23
30
  class="doggo"
24
31
  field={data.exampleDocument.data.example_link}>Link</PrismicLink
25
32
  >
26
- <h2>Slice Zone</h2>
33
+ <h2><code>SliceZone</code></h2>
27
34
  <SliceZone slices={data.pageDocument.data.body} {components} />
28
35
  </main>
29
36
 
@@ -32,4 +39,9 @@
32
39
  max-width: min(90%, 800px);
33
40
  margin: auto;
34
41
  }
42
+
43
+ :global(img) {
44
+ /* width: 100%; */
45
+ height: auto;
46
+ }
35
47
  </style>
@@ -3,4 +3,5 @@ import type * as prismicT from "@prismicio/types";
3
3
  export type ExampleDocument = prismicT.PrismicDocument<{
4
4
  example_link: prismicT.LinkField;
5
5
  example_content_relationship: prismicT.ContentRelationshipField;
6
+ example_image: prismicT.ImageField;
6
7
  }>;