Skip to content

remove dependency on imagesloaded - #1306

Open
thet wants to merge 7 commits into
masterfrom
thet/rm-imagesloaded
Open

thet wants to merge 7 commits into
masterfrom
thet/rm-imagesloaded

Conversation

@thet

@thet thet commented Nov 26, 2026

Copy link
Copy Markdown
Member

No description provided.

thet added 7 commits November 26, 2026 02:27
…agesloaded.

With `trigger: auto`, scroll after the page including all images has been
loaded. If the page is already loaded, scroll immediately.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
… changes.

- Equalise immediately on initialization and re-equalise whenever an image
  within the container has been loaded or failed loading. This uses a
  capturing load/error event listener, which also handles images added
  later. The imagesloaded library is not used anymore.

- Fix the MutationObserver callback, which got the list of mutation records
  passed instead of the container and therefore never re-equalised.

- Ignore the style and class changes the equaliser does on the children
  itself, which would otherwise lead to an endless update loop.

- Stop observing when the container was removed from the DOM and do not
  run pending updates on removed containers.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
…listener.

- Re-layout whenever an image within the container has been loaded or
  failed loading. This uses a capturing load/error event listener, which
  also handles images added later. When all images are loaded, do a full
  layout and set the `masonry-ready` class, like before.

- Do not wait for images with `loading="lazy"`, which might only be loaded
  when scrolled into view. imagesloaded did force loading these.

- Fix the `layoutComplete` event listener being added again on each
  `layout` call.

- Add real tests. The previous test had its only assertion commented out.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
…ages with imagesloaded.

Set up the carousel immediately and recalculate its dimensions whenever an
image within has been loaded or failed loading. This uses a capturing
load/error event listener, which also handles images added later. Slick
itself also recalculates on the window load event.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
It is not used anymore. pat-carousel, pat-equaliser, pat-masonry and
pat-scroll now use native browser APIs to react on loaded images.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
@thet
thet requested a review from petschki November 26, 2026 11:14

@petschki petschki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this: one dependency less, and images which are added later are handled too. The two fixes on the way are real (the MutationObserver callback in pat-equaliser, the layoutComplete listener in pat-masonry).

I see no blocker. But there are three changes in behaviour which you should decide on. Points 1 and 3 and the smaller points are from Jest tests and reading the code on this branch. Point 2 I tested in a browser (Chromium 156).

1. pat-equaliser now reacts to mutations in the whole document

The observer is on document.body. Until now its callback did nothing, so this is new behaviour. What I measured:

Change Updates
attribute on an element outside the container 1
class on a nested element in the container 1
class on a direct child of the container 0
mutation outside every 30 ms for one second 0 meanwhile, 1 after it

The debounce keeps the cost small and there is no endless loop. But the filter against the own loop ignores every class and style change on a direct child, also those from other code. When one of the equalised columns is opened by a class on itself, e.g. with pat-collapsible, the heights are not equalised again; when something deeper in it changes, they are.

I would only ignore what the equaliser does itself: height in style and the class equalised.

2. pat-scroll does not wait for images which are loaded later

imagesloaded(document.body) waited for all images which were in the document when the pattern was initialized. Now the pattern scrolls at once when document.readyState is already complete.

I tested this in a browser: a page which is loaded, then a pat-inject link loads a fragment with a pat-scroll with trigger: auto, an image of 1500px height which the server delivers after 3 seconds, and the target below it.

Scrolls after Target after the image is loaded
master about 3.1 s (after the image) 16px below the top, visible
this branch about 0.1 s (at once) 1516px below the top, outside of the window

The image has no width and height attributes; with them the target would not move.

On the first page load it now waits for all resources, so also for iframes and stylesheets.

3. pat-masonry does not wait for lazy images

masonry-ready is set when all images without loading="lazy" are done. That is documented and makes sense to me. The class is not removed again after that; images which are added later only get a quick layout.

Smaller points

  • pat-carousel is set up before the images are loaded. Slick recalculates with setPosition, but until then the carousel can be visible with wrong dimensions.
  • The capturing listener also gets load from iframes, scripts and stylesheets in the container. That only leads to an update which is not needed.
  • The listeners are never removed, and neither is the resize listener of pat-equaliser. The observer of pat-equaliser only disconnects at the next mutation after its container was removed. That was not better before.
  • The first commit "pat-validation: Autoformat." is the same as in #1249. Whichever is merged first takes it along.
  • The description of the PR is empty. The commit messages explain everything well; one sentence on the three changes above would help for the release notes.

Side note: Mockup does not import imagesloaded and has none of the four patterns in its bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants