npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nekochan0122/react-tweet

v2.0.7

Published

Embedded and static tweet for React applications.

Downloads

1

Readme

react-tweet

Embedded and static tweet for React applications.

Installation

Install react-tweet using your package manager of choice:

pnpm add react-tweet
yarn add react-tweet
npm install react-tweet

Now follow the usage instructions for your framework or builder:

Choosing a theme

The prefers-color-scheme CSS media feature is used to select the theme of the tweet.

Toggling theme manually

The closest data-theme attribute on a parent element can determine the theme of the tweet. You can set it to light or dark, like so:

<div data-theme="dark">
  <Tweet id="1629307668568633344" />
</div>

Alternatively, a parent with the class light or dark will also work:

<div className="dark">
  <Tweet id="1629307668568633344" />
</div>

Updating the theme

In CSS Modules, you can use the :global selector to update the CSS variables used by themes:

.my-class :global(.react-tweet-theme) {
  --tweet-body-font-size: 1rem;
}

For Global CSS the usage of :global is not necessary.

API Reference

Tweet

import { Tweet } from 'react-tweet'

Fetches and renders the tweet. It accepts the following props:

  • id - string: the tweet ID. For example in https://twitter.com/chibicode/status/1629307668568633344 the tweet ID is 1629307668568633344. This is the only required prop.
  • fallback - ReactNode: The fallback component to render while the tweet is loading. Defaults to TweetSkeleton.
  • onError - (error?: any) => any: The returned error will be sent to the TweetNotFound component.
  • components - TweetComponents: Components to replace the default tweet components. See the custom tweet components section for more details.

If the environment where Tweet is used does not support React Server Components then it will work with SWR instead and the tweet will be fetched from https://react-tweet.vercel.app/api/tweet/:id, which is CORS friendly.

We recommend adding your own API route to fetch the tweet in production. You can do it by using the apiUrl prop:

<Tweet apiUrl={id && `/api/tweet/${id}`} />

Note: apiUrl does nothing if the Tweet is rendered in a server component because it can fetch directly from Twitter's CDN.

To see it in action go to: /apps/next-app/pages/dark/swr/[tweet].tsx. And here's a good example of how to setup your own API route: /apps/vite-app/api/tweet/[tweet].ts.

EmbeddedTweet

import { EmbeddedTweet } from 'react-tweet'

Renders a tweet. It accepts the following props:

  • tweet - Tweet: the tweet data, as returned by getTweet. Required.
  • components - TweetComponents: Components to replace the default tweet components. See the custom tweet components section for more details.

TweetSkeleton

import { TweetSkeleton } from 'react-tweet'

A tweet skeleton useful for loading states.

TweetNotFound

import { TweetNotFound } from 'react-tweet'

A tweet not found component. It accepts the following props:

  • error - any: the error that was thrown when fetching the tweet. Not required.

getTweet

import { getTweet } from 'react-tweet/api'

function getTweet(id: string): Promise<Tweet | undefined>

Fetches and returns the tweet data. It accepts the following params:

  • id - string: the tweet ID. For example in https://twitter.com/chibicode/status/1629307668568633344 the tweet ID is 1629307668568633344. This is the only required prop.

If a tweet is not found it returns undefined.

Custom tweet components

Default components used by Tweet and EmbeddedTweet can be replaced by passing a components prop. It extends the TweetComponents type exported from react-tweet:

type TweetComponents = {
  TweetNotFound?: (props: Props) => JSX.Element
  AvatarImg?: (props: AvatarImgProps) => JSX.Element
  MediaImg?: (props: MediaImgProps) => JSX.Element
}

For example, to replace the default img tag used for the avatar and media with next/image you can do the following:

// tweet-components.tsx
import Image from 'next/image'
import type { TweetComponents } from 'react-tweet'

export const components: TweetComponents = {
  AvatarImg: (props) => <Image {...props} />,
  MediaImg: (props) => <Image {...props} fill unoptimized />,
}

And then pass the components to Tweet or EmbeddedTweet:

import { components } from './tweet-components'

const MyTweet = ({ id }: { id: string }) => (
  <Tweet id={id} components={components} />
)