-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathprops.ts
More file actions
66 lines (54 loc) · 1.83 KB
/
props.ts
File metadata and controls
66 lines (54 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { computed } from 'vue'
import type { ConfiguredImageProviders, ImageModifiers } from '@nuxt/image'
import { parseSize } from '.'
import { useImage } from '#imports'
export interface BaseImageProps<Provider extends keyof ConfiguredImageProviders> {
// input source
src?: string
// modifiers
format?: string
quality?: string | number
background?: string
fit?: string
modifiers?: Partial<Omit<ImageModifiers, 'format' | 'quality' | 'background' | 'fit'>>
& ('modifiers' extends keyof ConfiguredImageProviders[Provider] ? ConfiguredImageProviders[Provider]['modifiers'] : Record<string, unknown>)
// options
preset?: string
provider?: Provider
sizes?: string | Record<string, any>
densities?: string
preload?: boolean | { fetchPriority: 'auto' | 'high' | 'low' }
// <img> attributes
alt: string
width?: string | number
height?: string | number
crossorigin?: 'anonymous' | 'use-credentials' | boolean
// csp
nonce?: string
}
export const useImageProps = <Provider extends keyof ConfiguredImageProviders>(props: BaseImageProps<Provider>) => {
const $img = useImage()
const providerOptions = computed(() => ({
provider: props.provider,
preset: props.preset,
}))
const normalizedAttrs = computed(() => ({
width: parseSize(props.width),
height: parseSize(props.height),
crossorigin: props.crossorigin === true ? 'anonymous' : props.crossorigin || undefined,
nonce: props.nonce,
alt: props.alt,
}))
const imageModifiers = computed(() => {
return {
...props.modifiers,
width: props.width,
height: props.height,
format: props.format,
quality: props.quality || $img.options.quality,
background: props.background,
fit: props.fit,
} satisfies Partial<ImageModifiers>
})
return { providerOptions, normalizedAttrs, imageModifiers }
}