diff options
author | 2022-03-07 15:36:22 -0600 | |
---|---|---|
committer | 2022-03-07 15:36:22 -0600 | |
commit | f18ee36dc0abdc5c8ec87734de7962966d16fe65 (patch) | |
tree | c01a7034186cb0bbe5e1d042f4a5dd09bad21ed5 /packages/webapi/src/lib/MediaQueryList.ts | |
parent | 10a9c3412b4f6e8607687a74eafdb150d3222047 (diff) | |
download | astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.gz astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.zst astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.zip |
Add `@astrojs/webapi` package (#2729)@astrojs/webapi@0.11.0
* chore: add @astrojs/webapi
* chore: update package.json
* fix: update file case
* fix: remove lowercase file
* chore: update tests to use mocha
* chore: update LICENSE
Diffstat (limited to 'packages/webapi/src/lib/MediaQueryList.ts')
-rw-r--r-- | packages/webapi/src/lib/MediaQueryList.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/webapi/src/lib/MediaQueryList.ts b/packages/webapi/src/lib/MediaQueryList.ts new file mode 100644 index 000000000..44edd89be --- /dev/null +++ b/packages/webapi/src/lib/MediaQueryList.ts @@ -0,0 +1,37 @@ +import * as _ from './utils' + +export class MediaQueryList extends EventTarget { + get matches(): boolean { + return _.internalsOf(this, 'MediaQueryList', 'matches').matches + } + + get media(): string { + return _.internalsOf(this, 'MediaQueryList', 'media').media + } +} + +_.allowStringTag(MediaQueryList) + +export const initMediaQueryList = (target: Target, exclude: Set<string>) => { + if (exclude.has('MediaQueryList') || exclude.has('matchMedia')) return + + const EventTarget = target.EventTarget || globalThis.EventTarget + const MediaQueryList = target.MediaQueryList || globalThis.MediaQueryList + + target.matchMedia = function matchMedia(media: string) { + const mql = Object.setPrototypeOf(new EventTarget(), MediaQueryList.prototype) as MediaQueryList + + _.INTERNALS.set(mql, { + matches: false, + media, + }) + + return mql + } +} + +interface Target extends Record<any, any> { + matchMedia: { + (media: string): MediaQueryList + } +} |