GithubHelp home page GithubHelp logo

tusheer / react-tweet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vercel/react-tweet

0.0 0.0 0.0 441 KB

Embedded and static tweets for React applications

Home Page: https://static-tweet.vercel.app

License: MIT License

Shell 0.04% JavaScript 2.23% TypeScript 68.11% CSS 26.79% HTML 2.83%

react-tweet's Introduction

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} />
)

react-tweet's People

Contributors

lfades avatar github-actions[bot] avatar tywayne avatar stefkors avatar leerob avatar joulev avatar jaredpalmer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.