react-portabletext icon indicating copy to clipboard operation
react-portabletext copied to clipboard

TypeScript usage?

Open selbekk opened this issue 2 years ago • 7 comments

I would love to have some docs showing how this is supposed to be used with TypeScript!

selbekk avatar Mar 02 '22 09:03 selbekk

Agreed. Anything in particular you're curious about?

The demo has some typescript code examples that might be useful in the meantime :)

rexxars avatar Mar 02 '22 19:03 rexxars

Well, mostly how I should type the block content I pass in :) I see in the demo app there's a repo called @portabletext/types - would be great to have a link to this in the docs, for instance :)

selbekk avatar Mar 02 '22 19:03 selbekk

I struggled with this for a good while. I'm still not sure I fully understand it, that being said I have auto completion so I'm happy. I also had a pretty simple case, in that I wanted to style my text blocks with Chraka.

Example type definitions I create, I'm sure there is a better way to do this with the included types but I couldn't figure it out.

type Block = {
  _key: string;
  _type: string;
}
type BlockChild = Block & {
  marks: string[];
  text: string;
}
export type BlockDefault = Block & {
  children: BlockChild[];
  style: string;
}

//My object shape in sanity.
type Author = SanityDocument & {
  name: string;
  picture: string;
  bio: BlockDefault;
  image: Image;
  projects: any[];
}

In my component I pass bio to <PortableText /> pass in which custom component to render for the type block.

<PortableText
            value={bio}
            components={{
              types: {
                block: PortableBioBlock,
              },
            }}
          />

My custom component PortableBioBlock looks like this:

import { Text } from "@chakra-ui/react";
import type { PortableTextTypeComponent } from "@portabletext/react";
import { BlockDefault } from "src/@types/Post";

export const PortableBioBlock: PortableTextTypeComponent<BlockDefault> = ({
  children,
}) => {
  return (
    <Text fontSize={"1.2rem"} letterSpacing={1.3} lineHeight={1.4}>
      {children}
    </Text>
  );
};

I'm not sure if this is any help, or if there are ways I could improve it

swalker326 avatar Mar 23 '22 05:03 swalker326

// @ts-ignore is your friend.

eldoy avatar Jun 15 '22 05:06 eldoy

This is how I did it:

import type { PortableTextBlock } from '@portabletext/types'

export type Recipe = {
  _id: string
  title: string
  tags?: [{ title: string; slug: { current: string } }]
  slug: { current: string }
  body: [PortableTextBlock] // <-- 🎉
  mainImage: {
    asset: {
      url: string
    }
  }
}

ottob avatar Aug 29 '22 11:08 ottob

I did something along these lines which worked nice for our use-case:

interface SanityResponse extends SanityDocument {
    beskrivelse: string;
    embeddedInnhold: PortableTextBlock[];
    maal: string;
    tittel: string;
}
function MyComponent() {
    const data: SanityResponse[] = await sanity.fetch(
        '*[_type == "Aktivitet"]'
    );
    return (
        <PortableText
            value={data.map(({ embeddedInnhold: innhold }) => ({ innhold }))}
            components={portableTextComponents}
        />
    );
}

I can write a simple PR updating the readme on simple typescript use/gotchas if preferable :)

tanettrimas avatar Oct 18 '22 14:10 tanettrimas

We just added a "typing portable text" section to the README, let me know if it is helpful https://github.com/portabletext/react-portabletext#typing-portable-text

snorrees avatar May 31 '23 11:05 snorrees