Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map tiles dark mode with leaflet-osm plugin #5396

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/assets/javascripts/osm.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,13 @@ OSM = {
Math.pow(Math.sin(latdiff / 2), 2) +
Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)
));
},

isDarkMap: function() {
var siteTheme = $('html').attr('data-bs-theme');
var mapTheme = $('html').attr('data-map-theme');
if (mapTheme) return mapTheme === 'dark';
if (siteTheme) return siteTheme === 'dark';
return window.matchMedia('(prefers-color-scheme: dark)').matches
}
};
2 changes: 1 addition & 1 deletion app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

body {
font-size: $typeheight;
--dark-mode-map-filter: brightness(.8);
--dark-mode-map-filter: none;
}

time[title] {
Expand Down
15 changes: 14 additions & 1 deletion vendor/assets/leaflet/leaflet.osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ L.OSM.TileLayer = L.TileLayer.extend({
},

initialize: function (options) {
isDarkMap = OSM.isDarkMap();
options.filter = (isDarkMap ? options.darkFilter : options.lightFilter) || options.filter || 'none';
options = L.Util.setOptions(this, options);
L.TileLayer.prototype.initialize.call(this, options.url);
url = isDarkMap ? options.darkUrl : options.lightUrl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no lightUrl anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if the provided layers can be expected always to be a light-mode-first choice, the logic can be simplified to this more unreadable version:

    options.filter = isDarkMap && options.darkFilter || 'none';
    options = L.Util.setOptions(this, options);
    url = isDarkMap && options.darkUrl;
    this.schemeClass = url && 'dark';

But I thought an implementation agnostic to that would be easier to think through with more options for cartographers' choices.

if (url) this.schemeClass = isDarkMap ? 'dark' : 'light';
L.TileLayer.prototype.initialize.call(this, url || options.url);
this.on('add', function () {
container = this.getContainer();
if (container) {
if (this.schemeClass) container.classList.add(this.schemeClass);
container.style.setProperty('--dark-mode-map-filter', this.options.filter);
if (document.getElementById('map').contains(container)) document.querySelector('.key-ui').style.setProperty('--dark-mode-map-filter', this.options.filter);
}
});
}
});

Expand Down Expand Up @@ -39,6 +51,7 @@ L.OSM.CycleMap = L.OSM.TileLayer.extend({
L.OSM.TransportMap = L.OSM.TileLayer.extend({
options: {
url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
darkUrl: 'https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}{r}.png?apikey={apikey}',
maxZoom: 21,
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
}
Expand Down
Loading