importWhenVisible
Use this function to import component when an element is visible.
Usage
js
import { importWhenVisible } from '@studiometa/js-toolkit'; importWhenVisible(() => import('./components/Component.js'), 'Component');Parameters
importFn(() => Promise<Base>): the function to import componentsselector(string): the name of the component or a CSS selector, the same logic as components resolution will be usedparent(Base?): the parent Base instance used to query theselector, if not specifiedselectorwill be searched in the whole document.observerOptions(IntersectionObserverInit?): options for theIntersectionObserverinstance
Returns
Promise<Base>: a promise resolving to the the component's class
Examples
Import the class when the root element of the component is visible:
js
import { Base, importWhenVisible } from '@studiometa/js-toolkit'; class App extends Base { static config = { name: 'App', components: { Component: (app) => importWhenVisible( () => import('./components/Component.js'), 'Component', app, ), }, }; }Import the class when the #import-component-trigger element, which can be anywhere in the DOM, is visible:
js
import { Base, importWhenVisible } from '@studiometa/js-toolkit'; class App extends Base { static config = { name: 'App', components: { Component: () => importWhenVisible( () => import('./components/Component.js'), '#import-component-trigger', ), }, }; }Import the class when the app's btn ref is visible:
js
import { Base, importWhenVisible } from '@studiometa/js-toolkit'; class App extends Base { static config = { name: 'App', refs: ['btn'], components: { Component: (app) => importWhenVisible( () => import('./components/Component.js'), app.$refs.btn, ), }, }; }