A React component that renders beautiful, fully-customizable link previews.
Install the package:
yarn add @dhaiwat10/react-link-preview
npm install @dhaiwat10/react-link-preview
Import and render the preview:
import { LinkPreview } from '@dhaiwat10/react-link-preview';
const Home = () => {
return <LinkPreview url='https://www.youtube.com/watch?v=dQw4w9WgXcQ' width='400px' />;
};
If the component renders nothing, it means that no metadata could be scraped for the URL. Provide a fallback
component if you don't want to render null
.
This package uses a Heroku proxy (open-source) to get around CORS issues. The public proxy receives a lot of traffic (+ there is a rate limit) and is not recommended for production use.
- Please fork the proxy repo and host your own copy of it.
- You can then use the
customFetcher
prop to pass a fetcher function that fetches data from your proxy. TheLinkPreview
will now use your proxy as the data source. More details below.
You can pass the following props to the LinkPreview
component.
The URL for which you want to generate the link preview.
A function that takes in a url
& fetches data from a proxy/server. The function should return a Promise that resolves to an object with the following structure:
{
title: string | null;
description: string | null;
image: string | null;
siteName: string | null;
hostname: string | null;
}
You can use any data-source as you like as long as the customFetcher
function returns a Promise that resolves to an object with the above structure.
A fallback component which will be rendered if a link preview could not be generated.
A fallback image src/URL which will be used if there was no image found for the URL.
The src that will be used for the image regardless of what the data source returns.
Whether you want to display the default loading skeleton or not.
Custom loader that will be displayed in place of the default loader.
Background color of the card.
Color of the primary text (title).
Color of the secondary text (description, URL, domain name).
Color of the border of the card.
Any custom CSS class that you want to apply to the card.
Width of the card.
Height of the card.
Margin around the card.
Length of the description in the card. (number of characters)
Border radius of the card.
Height of the image.
Alignment of the text.
Where the link is opened (new tab or current tab).
Whether the placeholder image is displayed in case no image could be scraped.
Props marked with ? are optional.