Skip to content

Commit a6412d0

Browse files
committed
Make matchMedia features object return values via a property getter,
allowing dynamic use of the object in event handlers. Add window.matchMedia query + event listener for color-scheme changes, allows map to adapt without having to shake it.
1 parent e2cadfd commit a6412d0

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/mapml-viewer.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -995,38 +995,49 @@ export class HTMLMapmlViewerElement extends HTMLElement {
995995

996996
// less obviously useful: aspect-ratio, orientation, (device) resolution, overflow-block, overflow-inline
997997

998+
const map = this;
998999
const features = {
9991000
'prefers-lang': {
10001001
type: 'discrete',
1001-
values: [
1002-
...new Set(navigator.languages.map((code) => code.slice(0, 2)))
1003-
]
1002+
get values() {
1003+
return [
1004+
...new Set(navigator.languages.map((code) => code.slice(0, 2)))
1005+
];
1006+
}
10041007
},
10051008
'map-projection': {
10061009
type: 'discrete',
1007-
values: [this.projection.toLowerCase()] // change this in map-projectionchange event handler
1010+
get values() {
1011+
return [map.projection.toLowerCase()]; // change this in map-projectionchange event handler
1012+
}
10081013
},
10091014
'map-zoom': {
10101015
type: 'range',
10111016
valueType: 'integer',
10121017
canBeNegative: false,
10131018
canBeZero: true,
1014-
extraValues: {
1015-
min: 0,
1016-
max: this.zoom // change this on map-moveend
1019+
get extraValues() {
1020+
return {
1021+
min: 0,
1022+
max: map.zoom // change this on map-moveend
1023+
};
10171024
}
10181025
},
10191026
'prefers-color-scheme': {
10201027
type: 'discrete',
1021-
values: [
1022-
window.matchMedia('(prefers-color-scheme: dark)').matches
1023-
? 'dark'
1024-
: 'light'
1025-
]
1028+
get values() {
1029+
return [
1030+
window.matchMedia('(prefers-color-scheme: dark)').matches
1031+
? 'dark'
1032+
: 'light'
1033+
];
1034+
}
10261035
},
10271036
'prefers-map-content': {
10281037
type: 'discrete',
1029-
values: M.options.contentPreference
1038+
get values() {
1039+
return M.options.contentPreference;
1040+
}
10301041
}
10311042
};
10321043

@@ -1124,12 +1135,7 @@ export class HTMLMapmlViewerElement extends HTMLElement {
11241135
});
11251136

11261137
const observeProperties = () => {
1127-
const notifyIfChanged = (mapEvent) => {
1128-
if (mapEvent.name === 'map-projectionchange') {
1129-
features['map-projection'].values = [this.projection.toLowerCase()];
1130-
} else if (mapEvent.name === 'map-moveend') {
1131-
features['map-zoom'].extraValues.max = this.zoom;
1132-
}
1138+
const notifyIfChanged = () => {
11331139
const newMatches = solveMediaQueryList(parsedQuery, {
11341140
features,
11351141
solveUnknownFeature
@@ -1145,11 +1151,16 @@ export class HTMLMapmlViewerElement extends HTMLElement {
11451151
// Subscribe to internal events for changes in projection, zoom, and extent
11461152
this.addEventListener('map-projectionchange', notifyIfChanged);
11471153
this.addEventListener('map-moveend', notifyIfChanged);
1154+
const colorSchemeQuery = window.matchMedia(
1155+
'(prefers-color-scheme: dark)'
1156+
);
1157+
colorSchemeQuery.addEventListener('change', notifyIfChanged);
11481158

11491159
// Stop observing function
11501160
stopObserving = () => {
11511161
this.removeEventListener('map-projectionchange', notifyIfChanged);
11521162
this.removeEventListener('map-moveend', notifyIfChanged);
1163+
colorSchemeQuery.removeEventListener('change', notifyIfChanged);
11531164
};
11541165
};
11551166

0 commit comments

Comments
 (0)