Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/with-typescript/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,22 @@ export const WithSliceZone = (): ReactNode => {
/>
)
}

// Rendering a Slice Zone with a specific context contract looks like this.
// Note that the `context` prop is required when the `SliceZone` is supplied with a generic.
type RichContext = { name: string; age: number };
export const WithContextRichSliceZone = (): ReactNode => {
return (
<SliceZone<RichContext>
slices={page.data.slices}
components={{
hero: () => <div>Hero Slice</div>,
call_to_action: () => <div>Call to Action Slice</div>,
}}
context={{
name: "Bo",
age: 25,
}}
/>
)
}
7 changes: 3 additions & 4 deletions src/SliceZone.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Slice } from "@prismicio/client"
import { DEV } from "esm-env"
import type { ComponentType, FC, ReactNode } from "react"
import type { ComponentType, ReactNode } from "react"

/**
* Returns the type of a `SliceLike` type.
Expand Down Expand Up @@ -150,8 +150,7 @@ export type SliceZoneProps<TContext = unknown> = {
defaultComponent?: ComponentType<SliceComponentProps<any, TContext>>

/** Arbitrary data made available to all slice components. */
context?: TContext
}
} & (unknown extends TContext ? { context?: TContext } : { context: TContext })

/**
* This slice component can be used as a reminder to provide a proper implementation.
Expand Down Expand Up @@ -190,7 +189,7 @@ export const TODOSliceComponent = <TSlice extends SliceLike>({
*
* @see Learn how to create slices, use slice variations, and display slices: {@link https://prismic.io/docs/slices}
*/
export const SliceZone: FC<SliceZoneProps> = (props) => {
export const SliceZone = <T = never>(props: SliceZoneProps<T>): ReactNode | Promise<ReactNode> => {
const { slices = [], components = {}, defaultComponent, context = {} } = props

const renderedSlices = slices.map((slice, index) => {
Expand Down