old-mediaquerylist.ts 611 B

12345678910111213141516171819202122
  1. // Safari < 14 does not have mql.addEventListener(), but uses the old spec mql.addListener()
  2. let oMatchMedia = window.matchMedia;
  3. type MqlListener = (this: MediaQueryList, ev: MediaQueryListEvent) => any;
  4. window.matchMedia = (mediaQueryString) => {
  5. let mql = oMatchMedia(mediaQueryString);
  6. if (!mql.addEventListener) {
  7. // @ts-ignore
  8. mql.addEventListener = (type: string, listener: MqlListener) => {
  9. mql.addListener(listener);
  10. };
  11. // @ts-ignore
  12. mql.removeEventListener = (type: string, listener: MqlListener) => {
  13. mql.removeListener(listener);
  14. };
  15. }
  16. return mql;
  17. };