Streamline custom image props

This commit is contained in:
Sam Becker 2025-03-14 23:14:32 -05:00
parent fd89e2b48a
commit b13b286b3f
8 changed files with 25 additions and 21 deletions

View File

@ -2,7 +2,10 @@
import ImageSmall from '@/components/image/ImageSmall'; import ImageSmall from '@/components/image/ImageSmall';
import Spinner from '@/components/Spinner'; import Spinner from '@/components/Spinner';
import { getExtensionFromStorageUrl } from '@/platforms/storage'; import {
getIdFromStorageUrl,
getExtensionFromStorageUrl,
} from '@/platforms/storage';
import { clsx } from 'clsx/lite'; import { clsx } from 'clsx/lite';
import { FaRegCircleCheck } from 'react-icons/fa6'; import { FaRegCircleCheck } from 'react-icons/fa6';
import { pathForAdminUploadUrl } from '@/app/paths'; import { pathForAdminUploadUrl } from '@/app/paths';
@ -46,6 +49,7 @@ export default function AdminUploadsTable({
'scale-90', 'scale-90',
)}> )}>
<ImageSmall <ImageSmall
title={getIdFromStorageUrl(url)}
src={url} src={url}
alt={url} alt={url}
aspectRatio={3.0 / 2.0} aspectRatio={3.0 / 2.0}

View File

@ -1,8 +1,8 @@
import { IMAGE_QUALITY } from '@/app/config'; import { IMAGE_QUALITY } from '@/app/config';
import { IMAGE_WIDTH_LARGE, ImageProps } from '.'; import { IMAGE_WIDTH_LARGE, CustomImageProps } from '.';
import ImageWithFallback from './ImageWithFallback'; import ImageWithFallback from './ImageWithFallback';
export default function ImageLarge(props: ImageProps) { export default function ImageLarge(props: CustomImageProps) {
const { const {
aspectRatio, aspectRatio,
blurCompatibilityMode, blurCompatibilityMode,

View File

@ -1,7 +1,7 @@
import { IMAGE_WIDTH_MEDIUM, ImageProps } from '.'; import { IMAGE_WIDTH_MEDIUM, CustomImageProps } from '.';
import ImageWithFallback from './ImageWithFallback'; import ImageWithFallback from './ImageWithFallback';
export default function ImageMedium(props: ImageProps) { export default function ImageMedium(props: CustomImageProps) {
const { const {
aspectRatio, aspectRatio,
blurCompatibilityMode, blurCompatibilityMode,

View File

@ -1,7 +1,7 @@
import { IMAGE_WIDTH_SMALL, ImageProps } from '.'; import { IMAGE_WIDTH_SMALL, CustomImageProps } from '.';
import ImageWithFallback from './ImageWithFallback'; import ImageWithFallback from './ImageWithFallback';
export default function ImageSmall(props: ImageProps) { export default function ImageSmall(props: CustomImageProps) {
const { const {
aspectRatio, aspectRatio,
blurCompatibilityMode, blurCompatibilityMode,

View File

@ -9,14 +9,14 @@ import { useCallback, useEffect, useRef, useState } from 'react';
export default function ImageWithFallback(props: ImageProps & { export default function ImageWithFallback(props: ImageProps & {
blurCompatibilityLevel?: 'none' | 'low' | 'high' blurCompatibilityLevel?: 'none' | 'low' | 'high'
imgClassName?: string classNameImage?: string
}) { }) {
const { const {
className, className,
classNameImage = 'object-cover h-full',
priority, priority,
blurDataURL, blurDataURL,
blurCompatibilityLevel = 'low', blurCompatibilityLevel = 'low',
imgClassName = 'object-cover h-full',
...rest ...rest
} = props; } = props;
@ -67,8 +67,8 @@ export default function ImageWithFallback(props: ImageProps & {
return ( return (
<div <div
className={clsx( className={clsx(
className,
'flex relative', 'flex relative',
className,
)} )}
> >
{(showFallback || shouldDebugImageFallbacks) && {(showFallback || shouldDebugImageFallbacks) &&
@ -87,8 +87,8 @@ export default function ImageWithFallback(props: ImageProps & {
...rest, ...rest,
src: blurDataURL, src: blurDataURL,
className: clsx( className: clsx(
imgClassName,
getBlurClass(), getBlurClass(),
classNameImage,
), ),
}} /> }} />
: <div className={clsx( : <div className={clsx(
@ -100,7 +100,7 @@ export default function ImageWithFallback(props: ImageProps & {
...rest, ...rest,
ref: imgRef, ref: imgRef,
priority, priority,
className: imgClassName, className: classNameImage,
onLoad, onLoad,
onError, onError,
}} /> }} />

View File

@ -1,3 +1,6 @@
import { ComponentProps } from 'react';
import ImageWithFallback from './ImageWithFallback';
// Height determined by intrinsic photo aspect ratio // Height determined by intrinsic photo aspect ratio
export const IMAGE_WIDTH_SMALL = 50; export const IMAGE_WIDTH_SMALL = 50;
// Height determined by intrinsic photo aspect ratio // Height determined by intrinsic photo aspect ratio
@ -5,13 +8,10 @@ export const IMAGE_WIDTH_MEDIUM = 300;
// Height determined by intrinsic photo aspect ratio // Height determined by intrinsic photo aspect ratio
export const IMAGE_WIDTH_LARGE = 1000; export const IMAGE_WIDTH_LARGE = 1000;
export interface ImageProps { export type CustomImageProps = Omit<
ComponentProps<typeof ImageWithFallback>,
'blurCompatibilityLevel'
> & {
aspectRatio: number aspectRatio: number
blurCompatibilityMode?: boolean blurCompatibilityMode?: boolean
className?: string
imgClassName?: string
src: string
alt: string
blurDataURL?: string
priority?: boolean
} }

View File

@ -180,7 +180,7 @@ export default function PhotoLarge({
> >
<ImageLarge <ImageLarge
className={clsx(arePhotosMatted && 'h-full')} className={clsx(arePhotosMatted && 'h-full')}
imgClassName={clsx(arePhotosMatted && classNameImage={clsx(arePhotosMatted &&
'object-contain w-full h-full')} 'object-contain w-full h-full')}
alt={altTextForPhoto(photo)} alt={altTextForPhoto(photo)}
src={photo.url} src={photo.url}

View File

@ -67,7 +67,7 @@ export default function PhotoMedium({
blurDataURL={photo.blurData} blurDataURL={photo.blurData}
blurCompatibilityMode={doesPhotoNeedBlurCompatibility(photo)} blurCompatibilityMode={doesPhotoNeedBlurCompatibility(photo)}
className="flex object-cover w-full h-full" className="flex object-cover w-full h-full"
imgClassName="object-cover w-full h-full" classNameImage="object-cover w-full h-full"
alt={altTextForPhoto(photo)} alt={altTextForPhoto(photo)}
priority={priority} priority={priority}
/> />