-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc53051
commit ca37e67
Showing
3 changed files
with
90 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
/* eslint-disable class-methods-use-this */ | ||
/** | ||
* Component Image | ||
*/ | ||
class ResizeMediaQuery { | ||
private prefixName: string; | ||
private element: HTMLElement; | ||
private defaultBreakpoints: Object; | ||
private resizeClass: string | null; | ||
|
||
constructor(element: HTMLElement) { | ||
|
||
// Class vars | ||
this.prefixName = "--size-"; | ||
this.defaultBreakpoints = {xs: 384, sm: 576, md: 768, lg: 960, xl: 1200}; | ||
this.element = element; | ||
this.resizeClass = null; | ||
|
||
let resizeClass = element.getAttribute('data-observe-resizes'); | ||
if (resizeClass !== '') { | ||
this.resizeClass = resizeClass; | ||
} | ||
|
||
// Init only if browser supports ResizeObserver | ||
if ('ResizeObserver' in self) { | ||
this.resizeObserver(); | ||
} | ||
} | ||
|
||
private resizeObserver () { | ||
new ResizeObserver((entries) => { | ||
entries.forEach((entry) => { | ||
this.handleResize(entry); | ||
}); | ||
}).observe(this.element); | ||
} | ||
|
||
private handleResize(entry: ResizeObserverEntry) { | ||
// If breakpoints are defined on the observed element, | ||
|
||
const element = entry.target as HTMLElement; | ||
const breakpoints = element.dataset.breakpoints ? | ||
JSON.parse(element.dataset.breakpoints) : | ||
this.defaultBreakpoints; | ||
|
||
// Update the matching breakpoints on the observed element. | ||
Object.keys(breakpoints).forEach((breakpoint) => { | ||
const minWidth = breakpoints[breakpoint]; | ||
if (entry.contentRect.width >= minWidth) { | ||
element.classList.add((this.resizeClass ?? element.classList[0]) + this.prefixName + breakpoint); | ||
} else { | ||
element.classList.remove((this.resizeClass ?? element.classList[0]) + this.prefixName + breakpoint); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export function initializeResizeMediaQuery() { | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const elements = document.querySelectorAll('[data-observe-resizes]'); | ||
|
||
if (elements.length) { | ||
elements.forEach((element) => { | ||
new ResizeMediaQuery(element as HTMLElement); | ||
}); | ||
} | ||
|
||
const observer = new MutationObserver(mutationsList => { | ||
mutationsList.forEach(mutation => { | ||
const elements = mutation.addedNodes; | ||
if (elements?.length) { | ||
elements.forEach(element => { | ||
if (element?.nodeType === Node.ELEMENT_NODE && (element as HTMLElement).matches('[data-observe-resizes]')) { | ||
new ResizeMediaQuery(element as HTMLElement); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe(document.body, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
}); | ||
} |