Socket
Book a DemoInstallSign in
Socket

@terrahq/dyn-import

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@terrahq/dyn-import

Javascript library for to improve performance, it is tought to be used on scroll or on click event. This is benefitial when you want to load a modal on click, trigger something on scroll,etc.

unpublished
latest
npmnpm
Version
0.0.22
Version published
Maintainers
2
Created
Source

@terrahq/dyn-import

Javascript library for to improve performance, it is tought to be used on scroll or on click event. This is benefitial when you want to load a modal on click, trigger something on scroll,etc.

It also tought to fire only once if you have multiple elements.

Usage

Install

    npm install @terrahq/dyn-import

Import

import DynamicImportEvent from "@terrahq/dyn-import";

Example on Click

let clickDataElements = document.querySelectorAll(".js--trigger-button");
if (clickDataElements) {
    clickDataElements.forEach((element, index) => {
        let result: any = false;
        new DynamicImportEvent({
            type: {
                fireEvent: "click",
                clickElement: element,
            },
            callback: async () => {
                if(!result){
                    // @ts-ignore
                    result =  import("./modules/Bar.js").then((x) => {
                    document.querySelectorAll(".js--foo").forEach((element, index) => {
                        new x.default({ });
                    });
                });
                }else{
                    // Once the class has been initialized, use the funcions of the dynamic imported Class. In this case (InjectContent Class)
                    result.init()
                }
            },
        });
    });
}

Example on Scroll

if (document.querySelector(".js--bar")) {
    const FooModuleInstance = new DynamicImportEvent({
        type: {
            fireEvent: "scroll",
            distance: 200,
        },
        callback:() => {
            import("./modules/Foo.js").then((x) => {
                document.querySelectorAll(".js--trigger").forEach((element, index) => {
                    new x.default({
                        triggerCardElement: element,
                    });
                });
            });
        },
    });
}

Example on Observer

let observerDataElements = document.querySelectorAll(".js--observer-data");
if (observerDataElements) {
    observerDataElements.forEach(element => {
        let result:any = '';
        new DynamicImportEvent({
            type: {
                fireEvent: "observer",
                element: element,
                rootMargin : element.getAttribute("data-root")
            },
            callback: async () => {
                // Checks if the import class has been fired
                if(!result){
                    // @ts-ignore
                    result = await import("./modules/ChangeElement").then((x) => {
                        return  new x.default({
                            element : element
                        })
                    }).catch(()=> console.log('there was a problem'))
                }else{
                    result.init({
                        element : element
                    })
                }
            },
        });
    });
}

<div class="c--cta-a js--observer-data" data-root="100px">
    ....
</div>

Options

Option NameTypeDefaultDesc
fireEventstringcould be "scroll" or "click",
distancenumber100if "scroll" is defined, it is expected to add how much the user has to scroll from top of the page
clickElementsHTMLCollectionif "click" is defined, it is expected to know which triggers are responsable of fire the event.
fireOncebooleantrueif "true" the callback will be executed just one time.
elementHTMLCollectionif "observer" is defined, it is expected to know which triggers are responsable of fire the event.
rootMarginstringif "observer" is defined, the html has a data-root attribute use it, else use the 100px default value

Methods

There is a public method to destroy the class & remove event listeners

FooModuleInstance.destroy();

License

FAQs

Package last updated on 11 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts