|
1 | 1 | import { compileQuery, matches, type Environment, type EvaluateResult, type SimplePerm } from "media-query-fns"; |
2 | 2 |
|
| 3 | +// TODO: remove `deviceWidth` & `deviceHeight` from it, and only derive those from `width`, `height`, and `dppx` |
3 | 4 | type MediaState = { [key in keyof Environment as key extends `${infer Key}Px` ? Key : key]?: Environment[key] }; |
4 | 5 |
|
5 | 6 | const DEFAULT_ENV: Parameters<typeof matches>[1] = { |
@@ -35,6 +36,58 @@ type Feature = keyof MediaState; |
35 | 36 |
|
36 | 37 | const now = Date.now(); |
37 | 38 |
|
| 39 | +/** |
| 40 | + * Match the renaming done by media-query-fns |
| 41 | + * |
| 42 | + * {@link https://github.com/tbjgolden/media-query-fns/blob/7dae2618b9321f503cbd0a44a202a9190665e80e/lib/matches.ts#L200-L533} |
| 43 | + */ |
| 44 | +const MEDIA_FEATURE_TO_FEATURES = { |
| 45 | + // Same but different casing |
| 46 | + "any-hover": ["anyHover"], |
| 47 | + "any-pointer": ["anyPointer"], |
| 48 | + "color-gamut": ["colorGamut"], |
| 49 | + "color-index": ["colorIndex"], |
| 50 | + "display-mode": ["displayMode"], |
| 51 | + "dynamic-range": ["dynamicRange"], |
| 52 | + "environment-blending": ["environmentBlending"], |
| 53 | + "forced-colors": ["forcedColors"], |
| 54 | + grid: ["grid"], |
| 55 | + "horizontal-viewport-segments": ["horizontalViewportSegments"], |
| 56 | + hover: ["hover"], |
| 57 | + "inverted-colors": ["invertedColors"], |
| 58 | + "media-type": ["mediaType"], |
| 59 | + "nav-controls": ["navControls"], |
| 60 | + "overflow-block": ["overflowBlock"], |
| 61 | + "overflow-inline": ["overflowInline"], |
| 62 | + pointer: ["pointer"], |
| 63 | + "prefers-color-scheme": ["prefersColorScheme"], |
| 64 | + "prefers-contrast": ["prefersContrast"], |
| 65 | + "prefers-reduced-data": ["prefersReducedData"], |
| 66 | + "prefers-reduced-motion": ["prefersReducedMotion"], |
| 67 | + "prefers-reduced-transparency": ["prefersReducedTransparency"], |
| 68 | + scan: ["scan"], |
| 69 | + scripting: ["scripting"], |
| 70 | + update: ["update"], |
| 71 | + "vertical-viewport-segments": ["verticalViewportSegments"], |
| 72 | + "video-color-gamut": ["videoColorGamut"], |
| 73 | + "video-dynamic-range": ["videoDynamicRange"], |
| 74 | + |
| 75 | + // Numbers |
| 76 | + monochrome: ["monochromeBits"], |
| 77 | + color: ["colorBits"], |
| 78 | + resolution: ["dppx"], |
| 79 | + |
| 80 | + // Pixels |
| 81 | + width: ["width"], |
| 82 | + height: ["height"], |
| 83 | + "device-height": ["deviceHeight"], |
| 84 | + "device-width": ["deviceWidth"], |
| 85 | + |
| 86 | + // Combinations |
| 87 | + "aspect-ratio": ["width", "height"], |
| 88 | + "device-aspect-ratio": ["deviceHeight", "deviceWidth"], |
| 89 | +} as const satisfies Record<string, Feature[]>; |
| 90 | + |
38 | 91 | // Event was added in node 15, so until we drop the support for versions before it, we need to use this |
39 | 92 | class EventLegacy { |
40 | 93 | type: "change"; |
@@ -73,7 +126,17 @@ const EventCompat: typeof Event = typeof Event === "undefined" ? EventLegacy : E |
73 | 126 | const getFeaturesFromQuery = (query: EvaluateResult): Set<Feature> => { |
74 | 127 | const features = new Set<Feature>(); |
75 | 128 | query.simplePerms.forEach((perm) => { |
76 | | - Object.keys(perm).forEach((feature) => features.add(feature as Feature)); |
| 129 | + Object.keys(perm).forEach((mediaFeature) => { |
| 130 | + if (mediaFeature in MEDIA_FEATURE_TO_FEATURES) { |
| 131 | + MEDIA_FEATURE_TO_FEATURES[mediaFeature as keyof typeof MEDIA_FEATURE_TO_FEATURES].forEach((feature) => |
| 132 | + features.add(feature), |
| 133 | + ); |
| 134 | + } |
| 135 | + // // For debut, we can comment out those: |
| 136 | + // else { |
| 137 | + // console.error("Unrecognized " + mediaFeature); |
| 138 | + // } |
| 139 | + }); |
77 | 140 | }); |
78 | 141 | return features; |
79 | 142 | }; |
|
0 commit comments