🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)
🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)

🔧 Programmierung 🕛 vor 3 Jahren 3 Min Lesezeit
0

Media Queries in JS/TS done right

↗ Quelle (dev.to)
🗣️ Stimme:

Recently, I had a situation that involved listening to the screen width of an application.

There are a lot of methods of achieving this, but I wanted something simple, reusable & generic that did not impact performance too much.



I thought that listening to resize event on window and checking the innerWidth is way too slow especially since everything related to layout forces a API and it seemed like that was the best solution (it is also supported in all of the major browsers).



The only thing left to do, was to implement a simple, reusable & generic solution that uses this underneath. Here's a helpful MDN link that got me started.



What I wanted was to be able to subscribe to different media queries from different components across the application, without creating new listeners.



The solution I came up with, was to create a static class (well, a class with static methods and variables), that would keep a list of queries & subscriptions for those queries (and a list of listener refs so it can be easily cleaned up).




CODE
export interface IQueryList {
[key: string]: {
query: MediaQueryList;
subscribers: Array<(matchesQuery: boolean) => void>;
};
}

interface IEventListener {
[key: string]: (event: MediaQueryListEvent) => void;
}

export class MediaQueryListener {
private static _subscribers: IQueryList = {};
private static _eventListenersRefs: IEventListener = {};

static subscribe() {
//...
}
static unsubscribe() {
//...
}
}






This class would expose 2 public methods: subscribe and unsubscribe.



The subscribe method takes a callback function that receives a boolean parameter representing if the query matches or not, and the actual query in a string format (eg: '(max-width: 768px)').

It then checks if there is already a subscription made for this query from some other place:



  • if it is, it only adds the callback to the list of subscribers for that query.

  • if it's not, it adds the query to the list, with the only subscriber being the passed callback, then it starts to listen to changes for the newly created query.

Eitherway, it also calls the callback instantly with the current query match.




CODE
static subscribe = (cb: (matchesQuery: boolean) => void, query: string) => {
if (!this._subscribers[query]) {
this._subscribers[query] = {
query: window.matchMedia(query),
subscribers: [ cb ],
};

// keep the refs so we can remove the listeners in unsubscribe
this._eventListenersRefs[query] = event => {
this._subscribers[query]
.subscribers
.forEach(subscriber => subscriber(event.matches));
};

// create the listener only on first subscription for the query
this._subscribers[query]
.query
.addEventListener('change', this._eventListenersRefs[query]);
} else {
this._subscribers[query].subscribers.push(cb);
}

// Call the callback with the current value of the media query
cb(window.matchMedia(query).matches);
};






The unsubscribe method receives the same callback function & query in a string format, and removes the callback from the subscribers of that query. It also checks if there are no more subscribers left, and removes the media query listener so there are no hanging listeners left.




CODE
static unsubscribe = (cb: (matchesQuery: boolean) => void, query: string) => {
if (!this._subscribers[query]) {
return;
}

this._subscribers[query].subscribers = this._subscribers[query].subscribers.filter(subscriber => subscriber !== cb);

if (this._subscribers[query].subscribers.length === 0) {
this._subscribers[query].query.removeEventListener('change', this._eventListenersRefs[query]);

delete this._subscribers[query];
delete this._eventListenersRefs[query];
}
};






As I said, it's a basic wrapper on top of matchMedia api. There are probably multiple packages out there that achieve the same thing in a way or another, I did not research specifically for those packages in advance before creating my solution since I liked the idea of writing one.



Thanks for reading the article !

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Modify Windows Support Phone Number with PowerShell
1 Quelle
Die Zukunft des Einkaufens: Warum wir ein neues Kapitel aufschlagen (und wie du es mitschreiben kannst)
1 Quelle
ZDE Podcast 251: Wie sieht digitales Instore Marketing 2026 aus, Amit Chatterjee?
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Media Queries in JS/TS done right

Thematisch verwandte Begriffe: Media, Queries, JSTS, done · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...