Created
September 18, 2016 16:50
-
-
Save gheoan/73f899ea32784b57300bd700019ebb32 to your computer and use it in GitHub Desktop.
Wrapper for Media Queries Level 4: Interaction Media Features
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
/** | |
* Wrapper for Media Queries Level 4: Interaction Media Features | |
*/ | |
export class PointingDevice { | |
static pointer: string; | |
static hover: string; | |
static anyPointer: string; | |
static anyHover: string; | |
} | |
function set(property: string, mediaQueryListEvent: MediaQueryList) { | |
const { matches, media } = mediaQueryListEvent; | |
// only set if the media query matches | |
if (matches) { | |
PointingDevice[property] = media.substring(media.indexOf(':') + 1, media.lastIndexOf(')')); | |
} | |
} | |
function init(mediaQueryLists: MediaQueryList[], property: string) { | |
mediaQueryLists.forEach((mediaQueryList: MediaQueryList) => { | |
// set the initial value | |
set(property, mediaQueryList); | |
// listen the media query, and set the value on change | |
mediaQueryList.addListener((mediaQueryListEvent) => set(property, mediaQueryListEvent)); | |
}); | |
} | |
const match = window.matchMedia; | |
new Map([ | |
[ | |
'pointer', | |
[ | |
match('(pointer:none)'), | |
match('(pointer:coarse)'), | |
match('(pointer:fine)'), | |
], | |
], | |
[ | |
'hover', | |
[ | |
match('(hover:none)'), | |
match('(hover:on-demand)'), | |
match('(hover:hover)'), | |
], | |
], | |
[ | |
'anyPointer', | |
[ | |
match('(any-pointer:none)'), | |
match('(any-pointer:coarse)'), | |
match('(any-pointer:fine)'), | |
], | |
], | |
[ | |
'anyHover', | |
[ | |
match('(any-hover:none)'), | |
match('(any-hover:on-demand)'), | |
match('(any-hover:hover)'), | |
], | |
], | |
]).forEach(init); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment