2.1.0 • Published 10 months ago

@adoratorio/medusa v2.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
10 months ago

Medusa

A simple utility to easy handle multiple IntersectionObserver

Installation

# Install package
npm install @adoratorio/medusa

Browser Compatibility

If you want to support the browsers that don't support the Itersection Observer you have to change your idea or, at least, don't use this utils 🥳.

Usage

Since this package has a pkg.module field, it's highly recommended to import it as an ES6 module with some bundlers like webpack or rollup:

import Medusa from '@adoratorio/medusa';

const medusa = new Medusa(/* { ...medusaOptions  } */);

Available options

MedusaOptions

Definition:

ParameterTypeDefaultDescription
observersArray<ObserverConfig>[]Array to fill with observers custom configurations
debugbooleanfalseSet it to true if you want messages in console

Medusa observer

When you want to add a new observer in Medusa, you have to create a new configuration object with a specific structure where only the property id is required.

ObserverConfig interface:

interface ObserverConfig = {
  id : string,
  viewport : null | Document | HTMLElement,
  nodes : Array<Element>,
  threshold : number,
  offsets: string,
  emitGlobal : boolean,
  emitByNode : boolean,
  mode : MODE,
  callback : Function,
};

Definition:

ParameterTypeDefaultDescription
idstringrequiredThe Observer identifier.
viewportHTMLElementnullThe element that is used as the viewport for checking visibility of the target.
nodesArray<Element>[]All nodes you want to observe.
thresholdnumber0numbers which indicate at what percentage of the target's visibility, a float value between (0, 1).You can use:• a float number • Medusa.THRESHOLD.BEARLY (0.0)• Medusa.THRESHOLD.HALF (0.5)• Medusa.THRESHOLD.FULL (1.0)
offsetsstring'0px 0px 0px 0px'Margin around the root. Can have values similar to the CSS margin property
emitGlobalbooleanfalseIf it's true, Medusa emit the intersection custom event on the window
emitByNodebooleanfalseIf it's true, Medusa emit the intersection custom event on the node that intersect the viewport
ModestringMeduse.MODE.DEFAULTParameter that permit to change how many time the callback is execute.You can use:• Medusa.MODE.DEFAULT or 'default': trigger the callback every time the element intersect the viewport threshold.Medusa.MODE.ONCE or 'once': trigger the callback the only once.Medusa.MODE.BYPIXELS or 'byPixel': trigger the callback every pixel when the element observed is in viewport.
callbackfunction(e, o) => {}A function that is executed whenever an element intersect the viewport threshold that you set in the options. You have the access to the single entry and the istance of the observer.

Properties

observers

You can access to all the observers added by the property observers.

Medusa.observers : Map<string, InternalObserver>;

If you want a specific InternalObserver you can do like this:

Medusa.observers.get('observerId');

ObserverConfig interface:

interface InternalObserver = {
  id : string,
  observerInstance : null | IntersectionObserver,
  observedNodes : Map<number, MedusaElement>,
  emitGlobal : boolean,
  emitByNode : boolean,
  mode : MODE,
  callback : Function,
};

InternalObserver definition:

ParameterTypeDescription
idstringThe InternalObserver identifier.
observerInstanceIntersectionObserverThe IntersectionObserver instance.
observedNodesMap<number, MedusaElement>A Map of element observed by the IntersectionObserver. The number is the element unique id.
emitGlobalbooleanIf it's true, Medusa emit the intersection custom event on the window
emitByNodebooleanIf it's true, Medusa emit the intersection custom event on the node that intersect the viewport
ModestringParameter that permit to change how many time the callback is execute.
callbackfunctionA function that is executed whenever an element intersect the viewport threshold that you set in the options. You have the access to the single entry and the istance of the observer.

Methods

addObserver

To add a new observer you have to create a specific object with the ObserverConfig structure and then you have to pass it to the method.

Medusa.addObserver(configurations : Array<PartialObserverConfig> | PartialObserverConfig);

removeObserver

To remove a specific observer you have to know its id and then pass it to the method.

Medusa.removeObserver('observerId' : string);

clearObserver

Call it if you want to remove all observed nodes from a specific observer providing its id to the method.

Medusa.clearObserver('observerId' : string);

clearAllObservers

Call it if you want to remove all observed nodes from all observers.

Medusa.clearAllObservers();

observe

To observe single node or an array of nodes, you have to pass the observerId of the observer already created and the node/nodes that you want to add.

Medusa.observe('observerId' : string, elToAdd : Element | Array<Element>);

unobserve

To unobserve a single node or an array of nodes, you have to pass the observerId of the observer and the node/nodes that you want to remove.

Medusa.unobserve('observerId' : string, elToRemove : Element | Array<Element>);

Events

When a new Observer is created you can choose if Medusa can emit an event on two different targets:

EventArgumentsDescription
medusa-${observerId}eventIf you set emitGlobal property to true will emit a golbal event on the window when the callback is triggered, on the other hand, if you set emitByNode property to true in the configuration object Medusa will emit an event on the IntersectionObserver entry.target.

Argument details:

event.detail = {
  node : Element, // node observed that intersect the viewport previously defined
  isIn : boolean, // if the element observed is in viewport or not
  entry : IntersectionObserverEntry, // the IntersectionObserver entry
}
2.1.0

10 months ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago