Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cssVariables.cjs

This file was deleted.

21 changes: 6 additions & 15 deletions src/components/Media/Image/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
'use client'

import type { MediaProps } from '@components/Media/types'
import type { StaticImageData } from 'next/image'

import { standardSizes } from '@utilities/image-sizes'
import NextImage from 'next/image'
import React, { useState } from 'react'
import React from 'react'

import type { Props } from '../types'

import cssVariables from '../../../../cssVariables.cjs'
import classes from './index.module.scss'

const { breakpoints } = cssVariables

export const Image: React.FC<Props> = (props) => {
export const Image: React.FC<MediaProps> = (props) => {
const {
alt: altFromProps,
fill,
Expand All @@ -27,12 +24,13 @@ export const Image: React.FC<Props> = (props) => {
width: widthFromProps,
} = props

const [isLoading, setIsLoading] = useState(true)
const [isLoading, setIsLoading] = React.useState(true)

let width: null | number | undefined = widthFromProps
let height: null | number | undefined = heightFromProps
let alt = altFromProps
let src: null | StaticImageData | string | undefined = srcFromProps
const sizes = sizesFromProps || standardSizes

const hasDarkModeFallback =
resource?.darkModeFallback &&
Expand All @@ -47,13 +45,6 @@ export const Image: React.FC<Props> = (props) => {
src = resource.url
}

// NOTE: this is used by the browser to determine which image to download at different screen sizes
const sizes =
sizesFromProps ||
Object.entries(breakpoints)
.map(([, value]) => `(max-width: ${value}px) ${value}px`)
.join(', ')

const baseClasses = [
isLoading && classes.placeholder,
classes.image,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Media/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { StaticImageData } from 'next/image'
import type { TypedUploadCollection, UploadCollectionSlug } from 'payload'
import type { ElementType, Ref } from 'react'

export interface Props {
export interface MediaProps {
alt?: string
className?: string
fill?: boolean // for NextImage only
Expand Down
26 changes: 26 additions & 0 deletions src/utilities/image-sizes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable */
/* perfectionist/sort-objects complaining about order of breakpoints */

/**
* Formats object literals into strings that conform
* to Next.js's `sizes` prop for the Image component.
*
* The utility function `stringifyBreakpoints({size: number})`
* can be imported to create custom breakpoints for specific
* `Image` components if needed.
*
*/

type Breakpoint = {
[size: string]: number
}

export const stringifyBreakpoints = <B extends Breakpoint>(
breakpoints: B
): string => {
return Object.entries(breakpoints)
.map(([, width]) => `(max-width: ${width}px) ${width}px`)
.join(', ')
}

export const standardSizes = stringifyBreakpoints({s: 768, m: 1024, l: 1440})