Skip to content

Commit

Permalink
Merge pull request #26 from BKWLD/deprecate-enums
Browse files Browse the repository at this point in the history
Deprecate enums
  • Loading branch information
weotch authored Sep 21, 2023
2 parents c29b849 + 240f13b commit 9bcd311
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/NextVisual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
collectDataAttributes,
} from '@react-visual/react'

import { NextVisualProps, ObjectFit } from './types/nextVisualTypes'
import { NextVisualProps } from './types/nextVisualTypes'

// Render a Sanity image via Next/Image
export default function NextVisual(
Expand All @@ -24,7 +24,7 @@ export default function NextVisual(
aspect,
width,
height,
fit = ObjectFit.Cover,
fit = 'cover',
position,
priority,
sizes,
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import NextVisual from './NextVisual'
export default NextVisual
export { NextVisualProps, ObjectFit } from './types/nextVisualTypes'
export {
NextVisualProps,
ObjectFit,
ObjectFitOption,
} from './types/nextVisualTypes'
5 changes: 4 additions & 1 deletion packages/next/src/types/nextVisualTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type NextVisualProps = {
aspect?: number // An explict aspect ratio
width?: number | string
height?: number | string
fit?: ObjectFit
fit?: ObjectFitOption | ObjectFit
position?: string

priority?: boolean
Expand All @@ -23,6 +23,9 @@ export type NextVisualProps = {
style?: CSSProperties
}

export type ObjectFitOption = 'cover' | 'contain'

// Deprecated
export enum ObjectFit {
Cover = 'cover',
Contain = 'contain',
Expand Down
1 change: 1 addition & 0 deletions packages/sanity-next/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SanityNextVisual from './SanityNextVisual'
export default SanityNextVisual
export { SanityNextVisualProps } from './types/sanityNextVisualTypes'
export { ObjectFitOption } from '@react-visual/next'
export { makeImageUrl, makeImageBuilder, makeFileUrl } from './lib/urlBuilding'
8 changes: 4 additions & 4 deletions packages/sanity-next/src/lib/urlBuilding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getFileAsset, type SanityFileSource } from '@sanity/asset-utils'
import type { ImageUrlBuilder } from '@sanity/image-url/lib/types/builder'
import type { ImageLoader, ImageLoaderProps } from 'next/image'
import type { SanityImageSource } from '@sanity/image-url/lib/types/types'
import { ObjectFit } from '@react-visual/next'
import { ObjectFitOption } from '@react-visual/next'

// Access ENV vars
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID as string,
Expand All @@ -16,7 +16,7 @@ const imageBuilder = createImageUrlBuilder({ projectId, dataset })
type imageUrlBuildingOptions = {
width?: number
height?: number
fit?: ObjectFit
fit?: ObjectFitOption
}

// Helper for making image URLs
Expand All @@ -30,7 +30,7 @@ export function makeImageUrl(

// Add common conventions when building URLs to images
export function makeImageBuilder(source: SanityImageSource, {
width, height, fit = ObjectFit.Contain
width, height, fit = 'contain'
}: imageUrlBuildingOptions = {}): ImageUrlBuilder {

// Open up builder
Expand All @@ -39,7 +39,7 @@ export function makeImageBuilder(source: SanityImageSource, {
// Map the ObjectFit values to Sanity image CDN equivalents. The default
// is 'max'.
// https://www.sanity.io/docs/image-urls#fit-45b29dc6f09f
builder = builder.fit(fit == ObjectFit.Cover ? 'min' : 'max')
builder = builder.fit(fit == 'cover' ? 'min' : 'max')

// Conditionally add dimensions
if (width) builder = builder.width(width)
Expand Down

0 comments on commit 9bcd311

Please sign in to comment.