Skip to content

Commit bc832c9

Browse files
adding FigmaImage component
1 parent e5e7cfa commit bc832c9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

content/FigmaImageSources.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"https://www.figma.com/design/GCvY3Qv8czRgZgvl1dG6lp/Primer-Web?node-id=26998-2424&t=eruKCwLaYv3FX2Vu-4": "https://user-images.githubusercontent.com/97200987/176713532-b80d0221-fc01-4bbc-bb5d-0a152bf127d2.png"
3+
}

content/components/avatar.mdx

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {Box, Text} from '@primer/react'
1313
import ComponentLayout from '~/src/layouts/component-layout'
1414
export default ComponentLayout
1515
import {AccessibilityLink} from '~/src/components/accessibility-link'
16+
import {FigmaImage} from '~/src/components/FigmaImage'
1617

1718
<img
1819
width="960"
@@ -37,7 +38,7 @@ Avatars start at 16px and increment by base-4 until 32px. At 32px, the scale swi
3738
src="https://user-images.githubusercontent.com/586552/207139770-429f7327-c3e5-4e38-aa58-185a03d82304.png"
3839
/>
3940

40-
<img
41+
<FigmaImage
4142
width="960"
4243
alt="Demo"
4344
src="https://www.figma.com/design/GCvY3Qv8czRgZgvl1dG6lp/Primer-Web?node-id=26998-2424&t=eruKCwLaYv3FX2Vu-4"

src/components/figma-image.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import Images from '../../content/FigmaImageSources.json';
3+
4+
5+
type FigmaImageProps = React.ImgHTMLAttributes<HTMLImageElement> & {
6+
src: string
7+
}
8+
9+
export const FigmaImage: React.FC<FigmaImageProps> = ({src, ...props}) => {
10+
// check for missing prop
11+
if(src === undefined) throw new Error("src is required on FigmaImage component");
12+
// get real image url
13+
const realImageSrc = Images[src]
14+
if (realImageSrc === undefined) throw new Error(`Image with src ${src} not found in FigmaImageSources.json`);
15+
// return image component
16+
return (<img src={realImageSrc} {...props}/>)
17+
}

0 commit comments

Comments
 (0)