Skip to content

Commit 9f3d687

Browse files
committed
feat: add fallbackImageSrc
1 parent c763af1 commit 9f3d687

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ A fallback component which will be rendered if a link preview could not be gener
7777

7878
<hr />
7979

80+
### `fallbackImageSrc` (string)
81+
82+
A fallback image src/URL which will be used if there was no image found for the URL.
83+
8084
### `showLoader?` (boolean)
8185

8286
Whether you want to display the default loading skeleton or not.

src/assets/img-placeholder.jpg

22.7 KB
Loading

src/components/LinkPreview/LinkPreview.stories.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ storiesOf('LinkPreview', module)
5050
margin='30px auto'
5151
/>
5252
))
53-
.add('Placeholder image', () => <LinkPreview url='https://google.com' width='30vw' />)
53+
.add('Fallback image', () => (
54+
<LinkPreview
55+
url='https://google.com'
56+
width='30vw'
57+
fallbackImageSrc='https://www.aljazeera.com/wp-content/uploads/2021/08/2019-12-07T000000Z_879038429_RC2LQD9L67FQ_RTRMADP_3_SOCCER-SPAIN-FCB-RCD-REPORT.jpg?resize=770%2C513'
58+
/>
59+
))
5460
.add('Using custom fetcher', () => (
5561
<LinkPreview url='stripe.com' fetcher={customFetcher} fallback={<div>Fallback</div>} />
5662
));

src/components/LinkPreview/LinkPreview.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './linkPreview.scss';
44
import Skeleton from './Skeleton';
55

66
const proxyLink = 'https://rlp-proxy.herokuapp.com/v2?url=';
7+
const placeholderImg = 'https://github.com/Dhaiwat10/react-link-preview/blob/master/src/assets/img-placeholder.jpg';
78

89
function isValidResponse(res: any): res is APIResponse {
910
return (
@@ -34,6 +35,7 @@ export interface LinkPreviewProps {
3435
customLoader?: JSX.Element[] | JSX.Element | null;
3536
openInNewTab?: boolean;
3637
fetcher?: (url: string) => Promise<APIResponse | null>;
38+
fallbackImageSrc?: string;
3739
}
3840

3941
export interface APIResponse {
@@ -63,6 +65,7 @@ export const LinkPreview: React.FC<LinkPreviewProps> = ({
6365
customLoader = null,
6466
openInNewTab = true,
6567
fetcher,
68+
fallbackImageSrc = placeholderImg,
6669
}) => {
6770
const _isMounted = useRef(true);
6871
const [metadata, setMetadata] = useState<APIResponse | null>();
@@ -147,7 +150,7 @@ export const LinkPreview: React.FC<LinkPreviewProps> = ({
147150
style={{
148151
borderTopLeftRadius: borderRadius,
149152
borderTopRightRadius: borderRadius,
150-
backgroundImage: `url(${image})`,
153+
backgroundImage: `url(${image || fallbackImageSrc})`,
151154
height: imageHeight,
152155
}}
153156
className='Image'

stylelint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
extends: 'stylelint-config-standard',
3-
ignoreFiles: ['src/**/*.snap'],
3+
ignoreFiles: ['src/**/*.snap', 'src/assets/*.jpg'],
44
rules: {
55
'value-keyword-case': null,
66
},

0 commit comments

Comments
 (0)