Skip to content

Commit

Permalink
feat: img src external query params (#128)
Browse files Browse the repository at this point in the history
* feat: add `srcExternalQueryParams`

* feat: rename to `customizedSize`

* feat: update image docs

* fix: remove dependency

* feat: update variable name
  • Loading branch information
Vibes-INS authored Dec 15, 2021
1 parent 5f95701 commit 9987186
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
31 changes: 31 additions & 0 deletions apps/mibao-ui-docs/src/app/pages/image.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default {
crossOrigin: {
options: ['', 'anonymous', 'use-credentials'],
control: { type: 'select' }
},
customizedFixedSize: {
options: ['favicon', 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'xxx-large'],
control: { type: 'select' }
}
},
parameters: {
Expand Down Expand Up @@ -68,3 +72,30 @@ export const AsyncLoadingImage: Story = (args) => {
</MibaoProvider>
)
}

export const SrcExternalQueryParams: Story = (args) => {
return (
<MibaoProvider>
<MibaoImage
width="300px"
customizedSize={{
fixed: 'large'
}}
{...args}
/>
<MibaoImage
width="300px"
customizedSize={{
lambda: '10x10'
}}
{...args}
/>
</MibaoProvider>
)
}

SrcExternalQueryParams.args = {
src: 'https://oss.jinse.cc/production/e5a85cb5-bdd3-40c5-94de-c3335a704ab8.jpg',
customizedLambdaSize: '10x10',
customizedFixedSize: 'large'
}
17 changes: 14 additions & 3 deletions libs/mibao-ui/src/lib/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import { useState, useMemo, useEffect, useCallback, ReactNode } from 'react'
import FALLBACK_SRC from '../../../assets/images/fallback.svg'
import { addParamsToUrl, disableImageContext, getImagePreviewUrl } from '../../utils'

export type CustomizedImageSize = 'favicon' | 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' | 'x-large' | 'xx-large' | 'xxx-large'

export interface ImageProps extends ChakraImageProps {
loader?: ReactNode
srcQueryParams?: Record<string, string | number>
disableContextMenu?: boolean
resizeScale?: number // Specifies the shortest edge of the target zoom graph.
resizeScale?: number // OSS: Specifies the shortest edge of the target zoom graph.
webp?: boolean
containerProps?: BoxProps
customizedSize?: {
fixed?: CustomizedImageSize
lambda?: string
}
}

export const Image: React.FC<ImageProps> = ({
Expand All @@ -27,6 +33,7 @@ export const Image: React.FC<ImageProps> = ({
loader,
webp,
src,
customizedSize,
...props
}) => {
const { fallbackSrc = FALLBACK_SRC } = props
Expand All @@ -40,15 +47,19 @@ export const Image: React.FC<ImageProps> = ({

const imageSrc = useMemo(() => {
if (!src) return src
const url = addParamsToUrl(src, srcQueryParams ?? {})
const url = addParamsToUrl(src, {
...srcQueryParams,
...customizedSize?.fixed ? { size: customizedSize.fixed } : {},
...customizedSize?.lambda ? { size: customizedSize.lambda } : {}
})
if (resizeScale) {
return getImagePreviewUrl(url, {
size: resizeScale,
webp
})
}
return url
}, [resizeScale, src, webp, srcQueryParams])
}, [src, srcQueryParams, customizedSize, resizeScale, webp])

useEffect(() => {
if (src) {
Expand Down

1 comment on commit 9987186

@vercel
Copy link

@vercel vercel bot commented on 9987186 Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.