Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@bva/iolazyload
Advanced tools
A component built to utilize intersection observers to implement lazy loading. It is extendable to allow for custom lazy loading of any component.
There are four settings the component will accept:
The success callback will take three parameters:
The error callback takes one parameter:
The error callback will default to the success callback if none is provided - and will just be fired on the DOMContentLoaded
event for each entry that it would have otherwise been bound to.
First, drop the component into your project and import it into wherever you will be performing your lazy loading.
Next, to add lazy loading you'll use one function: setupObservers
.
The default use case is for adding lazy loading to image sources, source sets, and images as backgrounds via CSS. This is done with the following attributes:
src
attribute on the image coming into the viewportbackground-image
style.background-image
style (example to follow). Requires the element to also have a unique data-io-id
attribute (for scoping styles correctly).Here's an example of setting up lazy loading for an image, and also for adding background images to a div. First, we need the markup and then perform the JS setup:
<!-- HTML / Liquid -->
<img io-img-src="www.image.com/my-image.png" io-img-srcset="www.image.com/my-image.png 400w, www.image.com/my-image-big.png 800w" />
<div data-io-id="{{- section.id -}}" io-bg-srcset="www.image.com/my-image.png 0px, www.image.com/my-image-big.png 800px, www.image.com/my-image-extra-big.png 1200px"></div>
/** JS */
import LazyLoad from '@bva/iolazyload';
document.addEventListener('DOMContentLoaded', () => {
LazyLoad.setupObservers();
});
It's possible to add custom callbacks for any element:
<!-- HTML -->
<article data-lazy-article>
...
</article>
/** JS */
import LazyLoad from '@bva/iolazyload';
const displayArticle = (entry) => {
entry.classList.add('loaded');
};
const successCallback = (entry, observer, isIntersecting) => {
if (isIntersecting) {
displayArticle(entry);
observer.unobserve(entry);
}
};
document.addEventListener('DOMContentLoaded', () => {
LazyLoad.setupObservers({
queryString: '[data-lazy-article]',
callback: successCallback,
errorCallback: displayArticle,
margins: '10% 10% 10% 10%'
});
});
Notice we use the observer object, and once the article is loaded then unobserve it so as not to try and keep loading it in everytime it scrolls into view.
The component adds a custom event that is dispatched in the default success callback after an element has had its src
or background style updated when it comes into view.
The event is ioSourceSet
and is bound to the document. The events details.target
property refers to the element that was loaded into view.
Here's an example where we add the class loaded
to an element once the default callback has loaded the element:
document.addEventListener('ioSourceSet', (event) => {
const elementLoaded = event.detail.target;
elementLoaded.classList.add('loaded');
});
FAQs
Intersection Observer based lazy loading component
The npm package @bva/iolazyload receives a total of 0 weekly downloads. As such, @bva/iolazyload popularity was classified as not popular.
We found that @bva/iolazyload demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.