Skip to content

Commit

Permalink
Merge pull request #314 from uyupun/refactor/248_image
Browse files Browse the repository at this point in the history
ImageコンポーネントにJSDocを追加
  • Loading branch information
takashi0602 authored Oct 19, 2023
2 parents 3350307 + 08deba5 commit 54ced8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/components/base/Image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type Props = {
className?: string;
/**
* 画像の読み込みを遅延させるかどうか
* ビューポート外にある画像: true
* ビューポート内にある画像: false
*/
isLazy?: boolean;
isLazy: boolean;
/**
* 画像の横幅
*/
Expand Down Expand Up @@ -61,7 +63,15 @@ const getSource = (sources: ImageSource[]): ImageSource | null => {
return sortedSources[0];
};

const Image: FC<Props> = ({ sources, alt, className, isLazy = true, height, width }) => {
/**
* ビューポートのどこで使用するかによって isLazy の値を以下のように変更する
* ビューポート外にある画像: true
* ビューポート内にある画像: false
* また decoding の値は常に auto とし、ブラウザに任せる
*
* ref: https://zenn.dev/ixkaito/articles/deep-dive-into-decoding
*/
const Image: FC<Props> = ({ sources, alt, className, isLazy, height, width }) => {
const sourceForImgElement = getSource(sources);
if (sourceForImgElement === null) return <></>;

Expand All @@ -82,7 +92,7 @@ const Image: FC<Props> = ({ sources, alt, className, isLazy = true, height, widt
<img
alt={alt}
className={className}
decoding="async"
decoding="auto"
height={sourceForImgElement.isDesktop === true ? height.desktop : height.mobile}
loading={isLazy ? 'lazy' : 'eager'}
src={sourceForImgElement.srcset}
Expand Down
3 changes: 3 additions & 0 deletions src/components/common/ImageLink/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const ImageLink: Story = {
<CommonImageLink
height={height}
href={pagesPath.$url()}
isLazy={false}
sources={sources}
text="ゼウスくん"
width={width}
Expand All @@ -71,6 +72,7 @@ export const ImageLink: Story = {
<CommonImageLink
height={height}
href="https://example.com/"
isLazy={false}
rel="noreferrer noopener"
sources={sources}
target="_blank"
Expand All @@ -85,6 +87,7 @@ export const ImageLink: Story = {
<CommonImageLink
height={height}
href="https://example.com/"
isLazy={true}
rel="noreferrer noopener"
sources={sources}
target="_blank"
Expand Down

0 comments on commit 54ced8f

Please sign in to comment.