@prismicio/vue 3.0.0-alpha.3 → 3.0.0-beta.0

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.
@@ -20,9 +20,11 @@ import { __PRODUCTION__ } from "../lib/__PRODUCTION__";
20
20
  import { usePrismic } from "../usePrismic";
21
21
 
22
22
  /**
23
- * The minimum required properties to represent a Prismic Slice for the `<SliceZone />` component.
23
+ * The minimum required properties to represent a Prismic Slice for the
24
+ * `<SliceZone />` component.
24
25
  *
25
- * If using Prismic's REST API, use the `Slice` export from `@prismicio/types` for a full interface.
26
+ * If using Prismic's REST API, use the `Slice` export from `@prismicio/types`
27
+ * for a full interface.
26
28
  *
27
29
  * @typeParam TSliceType - Type name of the Slice
28
30
  */
@@ -34,44 +36,57 @@ export type SliceLike<TSliceType extends string = string> = Pick<
34
36
  /**
35
37
  * A looser version of the `SliceZone` type from `@prismicio/types` using `SliceLike`.
36
38
  *
37
- * If using Prismic's REST API, use the `SliceZone` export from `@prismicio/types` for the full type.
39
+ * If using Prismic's REST API, use the `SliceZone` export from
40
+ * `@prismicio/types` for the full type.
38
41
  *
39
42
  * @typeParam TSlice - The type(s) of slices in the Slice Zone
40
43
  */
41
44
  export type SliceZoneLike<TSlice extends SliceLike> = readonly TSlice[];
42
45
 
43
46
  /**
44
- * Vue props for a component rendering content from a Prismic Slice using the `<SliceZone />` component.
47
+ * Vue props for a component rendering content from a Prismic Slice using the
48
+ * `<SliceZone />` component.
45
49
  *
46
50
  * @typeParam TSlice - The type(s) of slices in the Slice Zone
47
- * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made available to all Slice components
51
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
52
+ * available to all Slice components
48
53
  */
49
54
  export type SliceComponentProps<
50
55
  TSlice extends SliceLike = SliceLike,
51
56
  TContext = unknown,
52
57
  > = {
53
- /** Slice data for this component. */
58
+ /**
59
+ * Slice data for this component.
60
+ */
54
61
  slice: TSlice;
55
62
 
56
- /** The index of the Slice in the Slice Zone. */
63
+ /**
64
+ * The index of the Slice in the Slice Zone.
65
+ */
57
66
  index: number;
58
67
 
59
- /** All Slices from the Slice Zone to which the Slice belongs. */
68
+ /**
69
+ * All Slices from the Slice Zone to which the Slice belongs.
70
+ */
60
71
  // TODO: We have to keep this list of Slices general due to circular
61
72
  // reference limtiations. If we had another generic to determine the full
62
73
  // union of Slice types, it would include TSlice. This causes TypeScript to
63
74
  // throw a compilation error.
64
75
  slices: SliceZoneLike<SliceLike>;
65
76
 
66
- /** Arbitrary data passed to `<SliceZone />` and made available to all Slice components. */
77
+ /**
78
+ * Arbitrary data passed to `<SliceZone />` and made available to all Slice components.
79
+ */
67
80
  context: TContext;
68
81
  };
69
82
 
70
83
  /**
71
- * Native Vue props for a component rendering content from a Prismic Slice using the `<SliceZone />` component.
84
+ * Native Vue props for a component rendering content from a Prismic Slice using
85
+ * the `<SliceZone />` component.
72
86
  *
73
87
  * @typeParam TSlice - The type(s) of slices in the Slice Zone
74
- * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made available to all Slice components
88
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
89
+ * available to all Slice components
75
90
  */
76
91
  export type DefineComponentSliceComponentProps<
77
92
  TSlice extends SliceLike = SliceLike,
@@ -96,36 +111,38 @@ export type DefineComponentSliceComponentProps<
96
111
  };
97
112
 
98
113
  /**
99
- * Gets native Vue props for a component rendering content from a Prismic Slice using the `<SliceZone />` component. Props are: `["slice", "index", "slices", "context"]`
114
+ * Gets native Vue props for a component rendering content from a Prismic Slice
115
+ * using the `<SliceZone />` component. Props are: `["slice", "index", "slices",
116
+ * "context"]`
100
117
  *
101
- * @param propsHint - An optional array of prop names used for the sole purpose of having a visual hint of which props are made available to the slice, this parameters doesn't have any effect
102
- *
103
- * @returns Props object to use with {@link defineComponent}
118
+ * @example Defining a new slice component:
104
119
  *
105
- * @typeParam TSlice - The type(s) of slices in the Slice Zone
106
- * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made available to all Slice components
107
- *
108
- * @example
109
- * Defining a new slice component:
110
- *
111
- * ```
120
+ * ```javascript
112
121
  * import { getSliceComponentProps } from "@prismicio/vue";
113
122
  *
114
123
  * export default {
115
- * props: getSliceComponentProps(),
124
+ * props: getSliceComponentProps(),
116
125
  * };
117
126
  * ```
118
127
  *
119
- * @example
120
- * Defining a new slice component with visual hint:
128
+ * @example Defining a new slice component with visual hint:
121
129
  *
122
- * ```
130
+ * ```javascript
123
131
  * import { getSliceComponentProps } from "@prismicio/vue";
124
132
  *
125
133
  * export default {
126
- * props: getSliceComponentProps(["slice", "index", "slices", "context"]),
134
+ * props: getSliceComponentProps(["slice", "index", "slices", "context"]),
127
135
  * };
128
136
  * ```
137
+ *
138
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
139
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
140
+ * available to all Slice components
141
+ * @param propsHint - An optional array of prop names used for the sole purpose
142
+ * of having a visual hint of which props are made available to the slice,
143
+ * this parameters doesn't have any effect
144
+ *
145
+ * @returns Props object to use with {@link defineComponent}
129
146
  */
130
147
  export const getSliceComponentProps = <
131
148
  TSlice extends SliceLike = SliceLike,
@@ -170,7 +187,8 @@ export type SliceComponentType<
170
187
  /**
171
188
  * This Slice component can be used as a reminder to provide a proper implementation.
172
189
  *
173
- * This is also the default Vue component rendered when a component mapping cannot be found in `<SliceZone />`.
190
+ * This is also the default Vue component rendered when a component mapping
191
+ * cannot be found in `<SliceZone />`.
174
192
  */
175
193
  export const TODOSliceComponent = __PRODUCTION__
176
194
  ? ((() => null) as FunctionalComponent<SliceComponentProps>)
@@ -201,7 +219,8 @@ export const TODOSliceComponent = __PRODUCTION__
201
219
  }) as SliceComponentType);
202
220
 
203
221
  /**
204
- * A record of Slice types mapped to Vue components. Each components will be rendered for each instance of their Slice type.
222
+ * A record of Slice types mapped to Vue components. Each components will be
223
+ * rendered for each instance of their Slice type.
205
224
  *
206
225
  * @typeParam TSlice - The type(s) of slices in the Slice Zone
207
226
  * @typeParam TContext - Arbitrary data made available to all Slice components
@@ -226,26 +245,20 @@ export type SliceZoneComponents<
226
245
  };
227
246
 
228
247
  /**
229
- * Gets an optimized record of Slice types mapped to Vue components. Each components will be rendered for each instance of their Slice type.
248
+ * Gets an optimized record of Slice types mapped to Vue components. Each
249
+ * components will be rendered for each instance of their Slice type.
230
250
  *
231
- * @param components - {@link SliceZoneComponents}
232
- *
233
- * @returns A new optimized record of {@link SliceZoneComponents}
234
- *
235
- * @typeParam TSlice - The type(s) of slices in the Slice Zone
236
- * @typeParam TContext - Arbitrary data made available to all Slice components
237
- *
238
- * @remarks This is essentially an helper function to ensure {@link markRaw} is correctly applied on each components, improving performances.
251
+ * @remarks
252
+ * This is essentially an helper function to ensure {@link markRaw} is correctly
253
+ * applied on each components, improving performances.
254
+ * @example Defining a slice components:
239
255
  *
240
- * @example
241
- * Defining a slice components:
242
- *
243
- * ```
244
- * import { getSliceZoneComponents } from "@prismicio/vue";
256
+ * ```javascript
257
+ * import { defineSliceZoneComponents } from "@prismicio/vue";
245
258
  *
246
259
  * export default {
247
260
  * data() {
248
- * components: getSliceZoneComponents({
261
+ * components: defineSliceZoneComponents({
249
262
  * foo: Foo,
250
263
  * bar: defineAsyncComponent(
251
264
  * () => new Promise((res) => res(Bar)),
@@ -255,8 +268,14 @@ export type SliceZoneComponents<
255
268
  * }
256
269
  * };
257
270
  * ```
271
+ *
272
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
273
+ * @typeParam TContext - Arbitrary data made available to all Slice components
274
+ * @param components - {@link SliceZoneComponents}
275
+ *
276
+ * @returns A new optimized record of {@link SliceZoneComponents}
258
277
  */
259
- export const getSliceZoneComponents = <
278
+ export const defineSliceZoneComponents = <
260
279
  TSlice extends SliceLike = SliceLike,
261
280
  TContext = unknown,
262
281
  >(
@@ -281,6 +300,43 @@ export const getSliceZoneComponents = <
281
300
  return result;
282
301
  };
283
302
 
303
+ /**
304
+ * Arguments for a `<SliceZone>` `resolver` function.
305
+ */
306
+ export type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
307
+ /**
308
+ * The Slice to resolve to a Vue component..
309
+ */
310
+ slice: TSlice;
311
+
312
+ /**
313
+ * The name of the Slice.
314
+ */
315
+ sliceName: TSlice["slice_type"];
316
+
317
+ /**
318
+ * The index of the Slice in the Slice Zone.
319
+ */
320
+ i: number;
321
+ };
322
+
323
+ /**
324
+ * A function that determines the rendered Vue component for each Slice in the
325
+ * Slice Zone. If a nullish value is returned, the component will fallback to
326
+ * the `components` or `defaultComponent` props to determine the rendered component.
327
+ *
328
+ * @deprecated Use the `components` prop instead.
329
+ * @param args - Arguments for the resolver function.
330
+ *
331
+ * @returns The Vue component to render for a Slice.
332
+ */
333
+ export type SliceZoneResolver<
334
+ TSlice extends SliceLike = SliceLike,
335
+ TContext = unknown,
336
+ > = (
337
+ args: SliceZoneResolverArgs<TSlice>,
338
+ ) => SliceComponentType<TSlice, TContext> | string | undefined | null;
339
+
284
340
  /**
285
341
  * Props for `<SliceZone />`.
286
342
  *
@@ -291,26 +347,45 @@ export type SliceZoneProps<
291
347
  TSlice extends SliceLike = SliceLike,
292
348
  TContext = unknown,
293
349
  > = {
294
- /** List of Slice data from the Slice Zone. */
350
+ /**
351
+ * List of Slice data from the Slice Zone.
352
+ */
295
353
  slices: SliceZoneLike<TSlice>;
296
354
 
297
- /** A record mapping Slice types to Vue components. */
298
- components: SliceZoneComponents;
355
+ /**
356
+ * A record mapping Slice types to Vue components.
357
+ */
358
+ components?: SliceZoneComponents;
299
359
 
300
- /** Arbitrary data made available to all Slice components. */
360
+ /**
361
+ * A function that determines the rendered Vue component for each Slice in the
362
+ * Slice Zone.
363
+ *
364
+ * @deprecated Use the `components` prop instead.
365
+ * @param args - Arguments for the resolver function.
366
+ *
367
+ * @returns The Vue component to render for a Slice.
368
+ */
369
+ resolver?: SliceZoneResolver<TSlice, TContext>;
370
+
371
+ /**
372
+ * Arbitrary data made available to all Slice components.
373
+ */
301
374
  context?: TContext;
302
375
 
303
376
  /**
304
- * A component or a functional component rendered if a component mapping from the `components` prop cannot be found.
305
- *
306
- * @remarks Components will be rendered using the {@link SliceComponentProps} interface.
377
+ * A component or a functional component rendered if a component mapping from
378
+ * the `components` prop cannot be found.
307
379
  *
380
+ * @remarks
381
+ * Components will be rendered using the {@link SliceComponentProps} interface.
308
382
  * @defaultValue The Slice Zone default component provided to `@prismicio/vue` plugin if configured, otherwise `null` when `process.env.NODE_ENV === "production"` else {@link TODOSliceComponent}.
309
383
  */
310
384
  defaultComponent?: SliceComponentType<TSlice, TContext>;
311
385
 
312
386
  /**
313
- * An HTML tag name, a component, or a functional component used to wrap the output. The Slice Zone is not wrapped by default.
387
+ * An HTML tag name, a component, or a functional component used to wrap the
388
+ * output. The Slice Zone is not wrapped by default.
314
389
  */
315
390
  wrapper?: string | ConcreteComponent;
316
391
  };
@@ -329,7 +404,13 @@ export const SliceZoneImpl = /*#__PURE__*/ defineComponent({
329
404
  },
330
405
  components: {
331
406
  type: Object as PropType<SliceZoneComponents>,
332
- required: true,
407
+ default: undefined,
408
+ required: false,
409
+ },
410
+ resolver: {
411
+ type: Function as PropType<SliceZoneResolver>,
412
+ default: undefined,
413
+ required: false,
333
414
  },
334
415
  context: {
335
416
  type: null,
@@ -357,14 +438,28 @@ export const SliceZoneImpl = /*#__PURE__*/ defineComponent({
357
438
 
358
439
  const renderedSlices = computed(() => {
359
440
  return props.slices.map((slice, index) => {
360
- const component =
361
- slice.slice_type in props.components
441
+ let component =
442
+ props.components && slice.slice_type in props.components
362
443
  ? props.components[slice.slice_type]
363
444
  : props.defaultComponent ||
364
445
  options.components?.sliceZoneDefaultComponent ||
365
446
  TODOSliceComponent;
366
447
 
448
+ // TODO: Remove `resolver` in v3 in favor of `components`.
449
+ if (props.resolver) {
450
+ const resolvedComponent = props.resolver({
451
+ slice,
452
+ sliceName: slice.slice_type,
453
+ i: index,
454
+ });
455
+
456
+ if (resolvedComponent) {
457
+ component = resolvedComponent;
458
+ }
459
+ }
460
+
367
461
  const p = {
462
+ key: `${slice.slice_type}-${index}`,
368
463
  slice,
369
464
  index,
370
465
  context: props.context,
@@ -23,7 +23,7 @@ export type {
23
23
  export {
24
24
  getSliceComponentProps,
25
25
  TODOSliceComponent,
26
- getSliceZoneComponents,
26
+ defineSliceZoneComponents,
27
27
  SliceZoneImpl,
28
28
  SliceZone,
29
29
  } from "./SliceZone";
@@ -33,6 +33,8 @@ export type {
33
33
  SliceComponentType,
34
34
  SliceLike,
35
35
  SliceZoneComponents,
36
+ SliceZoneResolverArgs,
37
+ SliceZoneResolver,
36
38
  SliceZoneLike,
37
39
  SliceZoneProps,
38
40
  } from "./SliceZone";