Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support untyped or partially typed Slices in <SliceZone> #154

Merged
merged 2 commits into from Jun 8, 2022

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Jun 1, 2022

Types of changes

  • Chore (a non-breaking change which is related to package maintenance)
  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR changes <SliceZone> to accept untyped or partially typed Slices. The changes affect both the provided Slices (the slices prop) and components (the components prop).

It effectively removes any type checking between the slices and components props.

It also defaults Slice components' slice prop to any rather than SliceLike. This was done to support Prismic's Rest API and GraphQL API with the same set of types.

Developers can still provide types to their API responses via @prismicio/client's type parameters and Slice components via @prismicio/react's SliceComponentProps. This PR does not include breaking changes.

While these changes may seem like a step backwards, it broadens the library's support to include projects with insufficient type data. Today, projects with insufficient types is common. This can happen when Slice components are properly typed, but the API data is not. This is even the case in Prismic-provided integrations like Slice Simulator.

Rather than force developers to hand-type their projects, the library has been updated to gracefully support untyped or partially typed data and components.

For more details on what prompted this change, see prismicio/prismic-types#34.


Component Type Examples

<SliceZone> will accept any of these typed components.

Fully typing a Slice component for Prismic's Rest API:

import { SliceComponentProps } from "@prismicio/react";
import * as prismicT from "@prismicio/types";

type ImageSlice = prismicT.SharedSlice<
  "image",
  prismicT.SharedSliceVariation<
    "default",
    {
      image: prismicT.ImageField;
    },
    never
  >
>;

const Image = ({ slice }: SliceComponentProps<ImageSlice>) => {
  return <section>{/* implementation */}</section>;
};

Fully typing a Slice component for Prismic's GraphQL API using a generated type (e.g. from GraphQL Code Generator):

import { SliceComponentProps } from "@prismicio/react";

// A type generated from a tool like GraphQL Code Generator
import { ImageSlice } from "../types.generated";

const Image = ({ slice }: SliceComponentProps<ImageSlice>) => {
  return <section>{/* implementation */}</section>;
};

Partially typing a Slice component (restricted to Prismic's Rest API):

import { SliceComponentProps, SliceLikeRestV2 } from "@prismicio/react";

const Image = ({ slice }: SliceComponentProps<SliceLikeRestV2>) => {
  return <section>{/* implementation */}</section>;
};

Partially typing a Slice component (restricted to Prismic's GraphQL API):

import { SliceComponentProps, SliceLikeGraphQL } from "@prismicio/react";

const Image = ({ slice }: SliceComponentProps<SliceLikeGraphQL>) => {
  return <section>{/* implementation */}</section>;
};

Partially typing a Slice component (any API):

import { SliceComponentProps } from "@prismicio/react";

// `slice` is typed as `any`
const Image = ({ slice }: SliceComponentProps) => {
  return <section>{/* implementation */}</section>;
};

Checklist:

  • My change requires an update to the official documentation.
  • All TSDoc comments are up-to-date and new ones have been added where necessary.
  • All new and existing tests are passing.

馃Β

@codecov-commenter
Copy link

Codecov Report

Merging #154 (c328833) into master (1eb04d8) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #154   +/-   ##
=======================================
  Coverage   91.61%   91.61%           
=======================================
  Files          18       18           
  Lines         334      334           
  Branches       84       84           
=======================================
  Hits          306      306           
  Misses          5        5           
  Partials       23       23           
Impacted Files Coverage 螖
src/SliceZone.tsx 84.00% <100.00%> (酶)

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 1eb04d8...c328833. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Jun 1, 2022

size-limit report 馃摝

Path Size
dist/index.js 5.63 KB (0%)
dist/index.cjs 7.16 KB (0%)

@angeloashmore angeloashmore requested a review from lihbr June 1, 2022 03:26
Copy link
Member

@lihbr lihbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor one maybe, gg!

src/SliceZone.tsx Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants