-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pdc-frontend): improve the image component & enable figcaption
- [x] fix the broken image - [x] improve the image component implementation - [x] enable figcaption
- Loading branch information
1 parent
d19262e
commit 943b9c3
Showing
9 changed files
with
114 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.utrecht-img { | ||
block-size: auto; | ||
max-inline-size: 100%; | ||
} | ||
|
||
.utrecht-figure { | ||
--utrecht-figure-margin-inline-start: 0; | ||
--utrecht-figure-margin-inline-end: 0; | ||
--utrecht-figure-margin-block-start: var(--utrecht-space-block-md); | ||
--utrecht-figure-margin-block-end: var(--utrecht-space-block-md); | ||
|
||
margin-block-end: var(--utrecht-figure-margin-block-end); | ||
margin-block-start: var(--utrecht-figure-margin-block-start); | ||
margin-inline-end: var(--utrecht-figure-margin-inline-end); | ||
margin-inline-start: var(--utrecht-figure-margin-inline-start); | ||
} | ||
|
||
.utrecht-figure__figcaption { | ||
--utrecht-figure--figcaption-color: var(--utrecht-color-grey-40); | ||
|
||
color: var(--utrecht-figure--figcaption-color); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import classNames from 'classnames/bind'; | ||
import Image from 'next/image'; | ||
import React from 'react'; | ||
import styles from './index.module.scss'; | ||
|
||
const css = classNames.bind(styles); | ||
|
||
export interface ImgProps | ||
extends Omit<React.DetailedHTMLProps<React.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, 'ref'> {} | ||
export const Img = (props: ImgProps & { 'data-figcaption'?: null }) => { | ||
const { height, width, src, alt, title } = props; | ||
|
||
if (width && height && src) { | ||
const imgElement = ( | ||
<Image | ||
className={css('utrecht-img')} | ||
src={src} | ||
alt={alt || ''} | ||
title={title} | ||
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw" | ||
width={width as number} | ||
height={height as number} | ||
/> | ||
); | ||
|
||
if (props['data-figcaption']) { | ||
return ( | ||
<figure className={css('utrecht-figure')}> | ||
{imgElement} | ||
<figcaption className={css('utrecht-figure__figcaption')}>{props['data-figcaption']}</figcaption> | ||
</figure> | ||
); | ||
} | ||
|
||
return imgElement; | ||
} | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const buildImgURL = (src: string) => { | ||
if (!process.env.STRAPI_PUBLIC_URL) { | ||
throw new Error('`STRAPI_PUBLIC_URL` is required to construct the image URL in the Markdown component.'); | ||
} | ||
const url = new URL(process.env.STRAPI_PUBLIC_URL); | ||
// if we need to support a different image-provider-upload, we can use the following approach | ||
// just rename env variable | ||
// if (process.env.DEPLOY_TO_VERCEL && Boolean(process.env.DEPLOY_TO_VERCEL)) { | ||
// return src; | ||
// } | ||
return `${url?.origin}${src}`; | ||
}; |