# react-example-tweet

> Embed and display Twitter tweets.

A shadcn/ui block from the [@prosekit](https://prosekit.dev) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/prosekit/react-example-tweet).

- Page, with a live preview: https://shadcn.exchange/r/prosekit/react-example-tweet
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/prosekit/react-example-tweet-light-e8900e105895d6ce.png
- Registry item: https://prosekit.dev/r/react-example-tweet.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://prosekit.dev/r/react-example-tweet.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `prosekit`, `react-tweet`
- Registry items: `https://unpkg.com/prosekit-registry/dist/r/react-sample-sample-doc-tweet.json`

## Files

### `registry/src/react/examples/tweet/editor.tsx`

```tsx
'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor, type Extension, type NodeJSON } from 'prosekit/core'
import { defineReactNodeView, ProseKit, useExtension } from 'prosekit/react'
import { useMemo, useState } from 'react'

import { sampleContent } from '../../sample/sample-doc-tweet.ts'

import { defineExtension } from './extension.ts'
import { MethodSelect } from './method-select.tsx'
import { TweetView } from './tweet-view.tsx'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    return createEditor({ extension: defineExtension(), defaultContent })
  }, [defaultContent])

  const [method, setMethod] = useState<'basic' | 'advanced'>('basic')

  const reactTweetView: Extension | null = useMemo(() => {
    if (method === 'basic') {
      return null
    }
    return defineReactNodeView({
      name: 'tweet',
      component: TweetView,
    })
  }, [method])

  useExtension(reactTweetView, { editor })

  return (
    <ProseKit editor={editor}>
      <MethodSelect value={method} onChange={setMethod} />
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
        </div>
      </div>
    </ProseKit>
  )
}
```

### `registry/src/react/examples/tweet/extension.ts`

```ts
import { defineBasicExtension } from 'prosekit/basic'
import { defineNodeSpec, union } from 'prosekit/core'

function defineTweetSpec() {
  return defineNodeSpec({
    name: 'tweet',
    group: 'block',
    attrs: {
      tweetId: { default: null },
    },
    parseDOM: [{
      tag: 'iframe[src^="https://platform.twitter.com/embed/Tweet.html"]',
      getAttrs: (node) => {
        const src = node.getAttribute('src')
        const match = src?.match(/id=([^&]+)/)
        return {
          tweetId: match?.[1] ?? null,
        }
      },
    }],
    toDOM: (node) => {
      return [
        'iframe',
        {
          src: `https://platform.twitter.com/embed/Tweet.html?id=${node.attrs.tweetId}&theme=dark`,
          style: 'height: 300px',
        },
      ]
    },
  })
}

function defineTweet() {
  return union(
    defineTweetSpec(),
  )
}

export function defineExtension() {
  return union(
    defineBasicExtension(),
    defineTweet(),
  )
}

export type EditorExtension = ReturnType<typeof defineExtension>
```

### `registry/src/react/examples/tweet/index.ts`

```ts
export { default as ExampleEditor } from './editor.tsx'
```

### `registry/src/react/examples/tweet/method-select.tsx`

```tsx
'use client'

import { useId } from 'react'

export function MethodSelect(props: {
  value: 'basic' | 'advanced'
  onChange: (value: 'basic' | 'advanced') => void
}) {
  const id = 'id-' + useId()
  const basicId = `${id}-basic`
  const advancedId = `${id}-advanced`

  return (
    <fieldset className="not-content">
      <legend>Select a render method:</legend>

      <div>
        <input
          type="radio"
          id={basicId}
          name={id}
          value="basic"
          checked={props.value === 'basic'}
          onChange={() => props.onChange('basic')}
        />
        <label htmlFor={basicId}>basic</label>
      </div>

      <div>
        <input
          type="radio"
          id={advancedId}
          name={id}
          value="advanced"
          checked={props.value === 'advanced'}
          onChange={() => props.onChange('advanced')}
        />
        <label htmlFor={advancedId}>advanced</label>
      </div>
    </fieldset>
  )
}
```

### `registry/src/react/examples/tweet/tweet-view.tsx`

```tsx
'use client'

import type { ReactNodeViewProps } from 'prosekit/react'
import { Tweet } from 'react-tweet'

export function TweetView({ node }: ReactNodeViewProps) {
  const tweetId = node.attrs.tweetId as string
  return (
    <div className="[&_img]:m-0!">
      <div>
        <strong>
          Rendered in React using library{' '}
          <span>
            <a href="https://github.com/vercel/react-tweet" target="_blank" rel="noopener noreferrer">react-tweet</a>
          </span>
        </strong>
      </div>
      <Tweet id={tweetId} />
    </div>
  )
}
```
