Class Module<StaticProps, ChangeableProps, CallbacksTypes>

A base class for modules that handle responsive properties, event listeners, and custom callbacks. This class provides a structure for modules that can dynamically respond to viewport changes, property mutations, and various lifecycle events.

Example

interface IStaticProps extends NModule.IStaticProps {
staticName: 'My name';
}

interface IChangeableProps extends NModule.IChangeableProps {
weight: number;
height: number;
}

interface ICallbacks extends NModule.ICallbacksTypes {}

const module = new Module<IStaticProps, IChangeableProps, ICallbacks>({
staticName: 'My name',
weight: 70,
height: 175,
}, false);

module.addResponsiveProps({
breakpoint: 'viewport_phone',
settings: {
weight: 80,
},
});

module.init();

module.addCallback('propsMutate', () => console.log('mutate props'));
module.addCallback('destroy', () => console.log('destroy'));

Type Parameters

Hierarchy

Constructors

Accessors

Methods

  • Adds a custom callback to the module.

    Type Parameters

    • T extends string | number | symbol

    Parameters

    • target: T

      The event type to listen for (e.g., 'propsChange', 'destroy').

    • action: TAction<CallbacksTypes[T]>

      The function to execute when the event is triggered.

    • settings: ISettings = {}

      Additional settings for the callback.

    Returns IAddedCallback

  • Adds a DOM event listener that will be automatically removed when the module is destroyed.

    Type Parameters

    • Target extends keyof HTMLElementEventMap

    • Listener extends ((event) => void)

    Parameters

    • element: Document | Element | Window

      The target element for the event listener.

    • target: Target

      The event type to listen for (e.g., 'click', 'resize').

    • callback: Listener

      The callback function to execute when the event is triggered.

    • Optional options: boolean | AddEventListenerOptions

      Additional options for the event listener.

    Returns (() => void)

      • (): void
      • Returns void

  • Adds responsive property rules to the module. This must be done before initialization.

    Parameters

    • rules: IResponsive<ChangeableProps>

      The responsive property rules to be added.

    Returns void

  • Adds a viewport callback that will be automatically removed when the module is destroyed.

    Parameters

    • target: keyof IViewportCallbackTypes

      The viewport target (e.g., width or height).

    • action: (() => void)

      The callback function to execute when the viewport target changes.

        • (): void
        • Returns void

    • data: undefined | ISettings = {}

      Additional data for the callback.

    Returns void

  • Updates the changeable properties of the module.

    Parameters

    • props: Partial<ChangeableProps>

      The properties to be updated.

    Returns void

  • Destroys the module, cleaning up resources, callbacks, and event listeners.

    Returns void

  • Initializes the module. Calls the internal _init method and marks the module as initialized.

    Returns void