prismicio / prismic-react

React components and hooks to fetch and present Prismic content
https://prismic.io/docs/technologies/homepage-reactjs
Apache License 2.0
153 stars 40 forks source link

feat(SliceZone): add support for mapped Slice Zones created with `@prismicio/client`'s `unstable_mapSliceZone()` #185

Closed angeloashmore closed 10 months ago

angeloashmore commented 10 months ago

Types of changes

Description

This PR adds support for @prismicio/client's unstable_mapSliceZone() to <SliceZone>.

unstable_mapSliceZone() requires loosening <SliceZone>'s types because mapSliceZone() may return arbitrary data.

unstable_mapSliceZone() adds a __mapped property used to detect mapped Slices. <SliceZone> detects the __mapped property and automatically spreads the Slice's properties as props. An unmapped Slice will continue to provide the standard { slice, index, slices, context } props.

Example

const mappedSliceZone = await prismic.unstable_mapSliceZone(
  page.data.slices,
  {
    foo: () => ({ foo: "bar" }),
    bar: () => ({ baz: "qux" }),
  }
);

function Page() {
  return (
    <SliceZone
      slices={mappedSliceZone}
      components={{
        // These Slices receive their mapped values as props.
        foo: ({ foo }) => null /* Return some component. */,
        bar: ({ baz }) => null /* Return some component. */,

        // Since Slices with type "baz" are not mapped, the component receives the standard Slice props.
        baz: ({ slice, index, slices, context }) =>
          null /* Return some component. */,
      }}
    />
  );
}

Checklist:

🦆

github-actions[bot] commented 10 months ago

size-limit report 📦

Path Size
./dist/index.cjs 8 KB (+0.77% 🔺)
./dist/index.js 6.03 KB (+1.3% 🔺)
./dist/react-server.cjs 7.2 KB (+0.91% 🔺)
./dist/react-server.js 5.56 KB (+1.36% 🔺)
codecov-commenter commented 10 months ago

Codecov Report

Merging #185 (aa2d187) into master (10e25c5) will increase coverage by 0.11%. The diff coverage is 100.00%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master     #185      +/-   ##
==========================================
+ Coverage   99.56%   99.68%   +0.11%     
==========================================
  Files          20       20              
  Lines        2540     2555      +15     
  Branches      129       93      -36     
==========================================
+ Hits         2529     2547      +18     
+ Misses         11        8       -3     
Impacted Files Coverage Δ
src/SliceZone.tsx 100.00% <100.00%> (+0.90%) :arrow_up: