@prismicio/react
Version:
React components and hooks to fetch and present Prismic content
120 lines (119 loc) • 5.3 kB
TypeScript
import { Slice } from "@prismicio/client";
import { ComponentType, FC, ReactNode } from "react";
//#region src/SliceZone.d.ts
/**
* Returns the type of a `SliceLike` type.
*
* @typeParam TSlice - The slice from which the type will be extracted.
*/
type ExtractSliceType<TSlice extends SliceLike> = TSlice extends Slice ? TSlice["slice_type"] : TSlice extends SliceLikeGraphQL ? TSlice["type"] : never;
/**
* The minimum required properties to represent a Prismic slice from the Prismic Content API for the
* `mapSliceZone()` helper.
*
* @typeParam SliceType - Type name of the slice.
*/
type SliceLikeRestV2<TSliceType extends string = string> = Pick<Slice<TSliceType>, "id" | "slice_type">;
/**
* The minimum required properties to represent a Prismic slice from the Prismic GraphQL API for the
* `mapSliceZone()` helper.
*
* @typeParam SliceType - Type name of the slice.
*/
type SliceLikeGraphQL<TSliceType extends string = string> = {
type: Slice<TSliceType>["slice_type"];
};
/**
* The minimum required properties to represent a Prismic slice for the `mapSliceZone()` helper.
*
* If using Prismic's Content API, use the `Slice` export from `@prismicio/client` for a full
* interface.
*
* @typeParam SliceType - Type name of the slice.
*/
type SliceLike<TSliceType extends string = string> = (SliceLikeRestV2<TSliceType> | SliceLikeGraphQL<TSliceType>) & {
/**
* If `true`, this slice has been modified from its original value using a mapper and
* `@prismicio/client`'s `mapSliceZone()`.
*
* @internal
*/
__mapped?: true;
};
/**
* A looser version of the `SliceZone` type from `@prismicio/client` using `SliceLike`.
*
* If using Prismic's Content API, use the `SliceZone` export from `@prismicio/client` for the full
* type.
*
* @typeParam TSlice - The type(s) of a slice in the slice zone.
*/
type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];
/**
* React props for a component rendering content from a Prismic slice using the `<SliceZone>`
* component.
*
* @typeParam TSlice - The slice passed as a prop.
* @typeParam TContext - Arbitrary data passed to `<SliceZone>` and made available to all slice
* components.
*/
type SliceComponentProps<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
/** Slice data for this component. */slice: TSlice; /** The index of the slice in the slice zone. */
index: number; /** All slices from the slice zone to which the slice belongs. */
slices: SliceZoneLike<TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2>; /** Arbitrary data passed to `<SliceZone>` and made available to all slice components. */
context: TContext;
};
/**
* A React component to be rendered for each instance of its slice.
*
* @typeParam TSlice - The type(s) of a slice in the slice zone.
* @typeParam TContext - Arbitrary data made available to all slice components.
*/
type SliceComponentType<TSlice extends SliceLike = any, TContext = unknown> = ComponentType<SliceComponentProps<TSlice, TContext>>;
/**
* A record of slice types mapped to a React component. The component will be rendered for each
* instance of its slice.
*
* @deprecated This type is no longer used by `@prismicio/react`. Prefer using
* `Record<string, SliceComponentType<any>>` instead.
* @typeParam TSlice - The type(s) of a slice in the slice zone.
* @typeParam TContext - Arbitrary data made available to all slice components.
*/
type SliceZoneComponents<TSlice extends SliceLike = SliceLike, TContext = unknown> = { [SliceType in ExtractSliceType<TSlice>]: SliceComponentType<Extract<TSlice, SliceLike<SliceType>> extends never ? SliceLike : Extract<TSlice, SliceLike<SliceType>>, TContext> };
/**
* React props for the `<SliceZone>` component.
*
* @typeParam TSlice - The type(s) of a slice in the slice zone.
* @typeParam TContext - Arbitrary data made available to all slice components.
*/
type SliceZoneProps<TContext = unknown> = {
/** List of slice data from the slice zone. */slices?: SliceZoneLike; /** A record mapping slice types to React components. */
components?: Record<string, ComponentType<any>>; /** The React component rendered if a component mapping from the `components` prop cannot be found. */
defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>; /** Arbitrary data made available to all slice components. */
context?: TContext;
};
/**
* This slice component can be used as a reminder to provide a proper implementation.
*
* This is also the default React component rendered when a component mapping cannot be found in
* `<SliceZone>`.
*/
declare const TODOSliceComponent: <TSlice extends SliceLike>({
slice
}: {
slice: TSlice;
}) => ReactNode;
/**
* Renders slices in a slice zone as React components.
*
* @example
* ```tsx
* <SliceZone slices={page.data.slices} components={components} />;
* ```
*
* @see Learn how to create slices, use slice variations, and display slices: {@link https://prismic.io/docs/slices}
*/
declare const SliceZone: FC<SliceZoneProps>;
//#endregion
export { SliceComponentProps, SliceComponentType, SliceLike, SliceLikeGraphQL, SliceLikeRestV2, SliceZone, SliceZoneComponents, SliceZoneLike, SliceZoneProps, TODOSliceComponent };
//# sourceMappingURL=SliceZone.d.ts.map