Skip to content

Commit

Permalink
Enable Bootstrap dark theme if user prefers dark color scheme
Browse files Browse the repository at this point in the history
Javascript has to run as soon as possible to avoid flashes. Document ready event is too late.
  • Loading branch information
AntonKhorev committed Apr 10, 2024
1 parent 564c63f commit 8ec639e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -68,6 +68,9 @@ window.updateLinks = function (loc, zoom, layers, object) {
.toggleClass("disabled", editDisabled);
};

updateBootstrapTheme();
$(matchMedia("(prefers-color-scheme: dark)")).on("change", updateBootstrapTheme);

$(document).ready(function () {
// NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
// See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
Expand Down Expand Up @@ -148,3 +151,8 @@ $(document).ready(function () {
$("#edit_tab")
.attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
});

function updateBootstrapTheme() {
var prefersDark = matchMedia("(prefers-color-scheme: dark)").matches;
$("html").attr("data-bs-theme", prefersDark ? "dark" : "light");
}

0 comments on commit 8ec639e

Please sign in to comment.