Skip to content

Commit

Permalink
feat: add module option to modify IntersectionObserver options (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Oct 21, 2020
1 parent 0f6e17e commit 9f9b920
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default {
imgix: {
baseURL: 'https://assets.imgix.net'
}
},
intersectOptions: {
rootMargin: '50px'
}
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function imageModule (moduleOptions: ModuleOptions) {

const options: ModuleOptions = {
presets: [],
intersectOptions: {},
providers: {},
...nuxt.options.image,
...moduleOptions
Expand All @@ -36,6 +37,7 @@ function imageModule (moduleOptions: ModuleOptions) {

const pluginOptions = {
defaultProvider: options.defaultProvider,
intersectOptions: options.intersectOptions,
imports: {} as { [name: string]: string },
providers: [] as { name: string, import: string, options: any }[],
presets: options.presets
Expand Down
13 changes: 8 additions & 5 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function processSource (src: string) {
}
}

export function createImage (context, { providers, defaultProvider, presets }: CreateImageOptions) {
export function createImage (context, { providers, defaultProvider, presets, intersectOptions }: CreateImageOptions) {
const presetMap = presets.reduce((map, preset) => {
map[preset.name] = preset
return map
Expand Down Expand Up @@ -96,7 +96,6 @@ export function createImage (context, { providers, defaultProvider, presets }: C
})
}
})

image.getMeta = async (source: string, modifiers: ImageModifiers, options: any = {}) => {
const { src, provider: sourceProvider } = processSource(source)
const provider = getProvider(sourceProvider || options.provider || defaultProvider)
Expand All @@ -115,13 +114,17 @@ export function createImage (context, { providers, defaultProvider, presets }: C
return meta
}

image.$observer = createObserver()
image.$observer = createObserver(intersectOptions)

return image
}

function createObserver () {
const observer = (typeof window !== 'undefined' ? new IntersectionObserver(callback) : {}) as IntersectionObserver
function createObserver (options: object) {
const intersectOptions = {
rootMargin: '50px',
...options
}
const observer = (typeof IntersectionObserver !== 'undefined' ? new IntersectionObserver(callback, intersectOptions) : {}) as IntersectionObserver
const elementCallbackMap = {}
function callback (entries, imgObserver) {
entries.forEach((entry) => {
Expand Down
2 changes: 2 additions & 0 deletions templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createImage } from '~image/image'

<%=Object.entries(options.imports).map(([name, path]) => `import ${name} from '${path}'`).join('\n')%>

const intersectOptions = <%= devalue(options.intersectOptions) %>
const defaultProvider = '<%= options.defaultProvider %>'
const presets = <%= devalue(options.presets) %>
const providers = {}
Expand All @@ -25,6 +26,7 @@ export default function (context, inject) {
defaultProvider,
providers,
presets,
intersectOptions
})

inject('img', image)
Expand Down
2 changes: 1 addition & 1 deletion types/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ModuleOptions {
}
[name: string]: any
}
provider: object;
intersectOptions: object;
}

export const imageModule: Module<ModuleOptions>
1 change: 1 addition & 0 deletions types/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CreateImageOptions {
}
presets: ImagePreset[]
defaultProvider: string
intersectOptions: object
}

export interface $Image {
Expand Down

0 comments on commit 9f9b920

Please sign in to comment.