Skip to content

Commit

Permalink
feat: media queries js to ts (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiclasNorin authored Nov 23, 2023
1 parent bc53051 commit ca37e67
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 91 deletions.
5 changes: 3 additions & 2 deletions source/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import DynamicSidebar from './dynamicSidebar';
import Navbar from './navbar';
import Table from './table';
import Segment from './segment';
import ContainerMediaQuery from './mediaQuery';
import {initializePagination} from './pagination';
import ResizeByChildren from './resizeByChildren';
import KeepInViewPort from './keepInViewPort';
Expand All @@ -46,6 +45,8 @@ import {initializeOpenStreetMaps} from './openStreetMap';
import {setupCopy} from './copy';
import { initializeCompressed } from './compressed';
import {DeviceDetect} from './deviceDetect';
import {initializeComponentResize} from './resizeMediaQuery';
import {initializeResizeMediaQuery} from './resizeMediaQuery';

expandSection();
setScrollbarCSS();
Expand All @@ -67,7 +68,6 @@ const NotificationInstance = new Notification();
const NotificationDocInstance = new NotificationDoc();
const SidebarInstance = new Sidebar();
const NavbarInstance = new Navbar();
const ContainerMediaQueryInstance = new ContainerMediaQuery();
const KeepInViewPortInstance = new KeepInViewPort();
const ResizeByChildrenInstance = new ResizeByChildren();
const ButtonToggleContentInstance = new ButtonToggleContent();
Expand All @@ -80,6 +80,7 @@ initializeMenus();
initializeSlider();
initializeCompressed();
setupCopy();
initializeResizeMediaQuery();
selectComponentObserverInstance.observe();

const tables = document.querySelectorAll('.c-table');
Expand Down
89 changes: 0 additions & 89 deletions source/js/mediaQuery.js

This file was deleted.

87 changes: 87 additions & 0 deletions source/js/resizeMediaQuery.ts
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,
});
});
}

0 comments on commit ca37e67

Please sign in to comment.