Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Suppers Object fit cover #10

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/xrias/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ekwoka/x-rias",
"version": "1.2.1",
"version": "1.2.2",
"description": "A simple Alpine Directive for use with Cloudinary Fetch API for handling Responsive Images",
"author": {
"name": "Eric Kwoka",
Expand Down
37 changes: 25 additions & 12 deletions packages/xrias/src/resizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
const debounceObserver = (
fn: (elements: Set<HTMLElement>, observer: ResizeObserver) => void,
const debounceObserver = <T extends Element = HTMLElement>(
fn: (elements: Set<T>, observer: ResizeObserver) => void,
delay: number,
): ResizeObserverCallback => {
let timer: number;
const allElements = new Set<HTMLElement>();
let timer: number | NodeJS.Timeout | null;
const allElements = new Set<T>();
return function (
entries: ResizeObserverEntry[],
observer: ResizeObserver,
): void {
if (timer) clearTimeout(timer);
entries.forEach(({ target }) => allElements.add(target as HTMLElement));
entries.forEach(({ target }) => allElements.add(target as T));
timer = setTimeout(() => {
fn(allElements, observer);
timer = null;
}, delay);
};
};

const observerCB = (els: Set<HTMLElement>) => {
const observerCB = (els: Set<HTMLImageElement>) => {
els.forEach((el) => resize(el));
els.clear();
};

const resize = (el: HTMLElement) => {
let sizes = el.offsetWidth;
let parent = el.parentNode as HTMLElement;
while (sizes < 100 && parent) {
sizes = parent.offsetWidth;
const resize = (el: HTMLImageElement) => {
const setSize = (width: number) =>
el.setAttribute('sizes', (width | 0) + 'px');
let width = 0;
let height = 0;
let parent: HTMLElement | null = el;
while (width < 100 && parent) {
width = parent.offsetWidth;
height = parent.offsetHeight;
parent = parent.parentNode as HTMLElement;
}
el.setAttribute('sizes', sizes + 'px');
const objectFit = getComputedStyle(el).objectFit;
if (objectFit !== 'cover') return setSize(width);
const imageRatio =
Number(el.getAttribute('width')) / Number(el.getAttribute('height'));
if (!imageRatio) return setSize(width);
const displayedRatio = width / height;
if (displayedRatio >= imageRatio + 0.1) return setSize(width);
const newWidth = height * imageRatio;
setSize(newWidth);
};

export const observer = new ResizeObserver(debounceObserver(observerCB, 800));
8 changes: 4 additions & 4 deletions size.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
},
"xrias": {
"minified": {
"pretty": "1.67 kB",
"raw": 1669
"pretty": "1.86 kB",
"raw": 1856
},
"brotli": {
"pretty": "818 B",
"raw": 818
"pretty": "909 B",
"raw": 909
}
},
"xrouter": {
Expand Down
Loading