From 885c4d2b3a6e98972278c0c0e807301a4435e84e Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 2 Nov 2026 22:22:50 -0500 Subject: [PATCH] Dark "Are you sure?" dialog on dark pages rails-ujs confirmations (data-confirm) show a dark dialog in Gitea's colours while the page is dark, instead of the browser's box drawn in the device's colours; OK carries on with the link, button or form. Light pages keep the browser's box. RedmineDarkMode.confirm(message, onOk) does the same for other scripts. The default mode is one setting (DEFAULT_MODE, Automatic). Co-Authored-By: Claude Opus 5.5 --- assets/javascripts/dark_mode.js | 73 +++++++++++++++++++ assets/stylesheets/dark_mode.css | 37 ++++++++++ config/locales/en.yml | 1 + lib/redmine_dark_mode.rb | 9 ++- lib/redmine_dark_mode/hooks.rb | 2 +- .../user_preference_patch.rb | 4 +- 6 files changed, 120 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/dark_mode.js b/assets/javascripts/dark_mode.js index 8130367..565afb6 100644 --- a/assets/javascripts/dark_mode.js +++ b/assets/javascripts/dark_mode.js @@ -53,3 +53,76 @@ if (media.addEventListener) media.addEventListener('change', function () { render(button); }); }); })(); + +// "Are you sure?" confirmations (data-confirm, through rails-ujs). The browser draws its own box in the device's +// colours, so on a dark page it can flash up bright; while the page is dark, a dark dialog asks instead, and OK +// carries on with the link, button or form. Light pages keep the browser's box. +(function () { + if (!window.RedmineDarkMode || !window.HTMLDialogElement) return; + + var nativeConfirm = window.Rails && Rails.confirm; + var approved = null; + + function ask(message, onOk) { + var labels = window.RedmineDarkMode.labels; + var dialog = document.createElement('dialog'); + dialog.className = 'dark-mode-confirm'; + var text = document.createElement('p'); + text.textContent = message; + var buttons = document.createElement('div'); + buttons.className = 'dark-mode-confirm-buttons'; + var cancel = document.createElement('button'); + cancel.type = 'button'; + cancel.textContent = labels.cancel; + var ok = document.createElement('button'); + ok.type = 'button'; + ok.className = 'dark-mode-confirm-ok'; + ok.textContent = labels.ok; + buttons.append(cancel, ok); + dialog.append(text, buttons); + document.body.appendChild(dialog); + + var confirmed = false; + cancel.addEventListener('click', function () { dialog.close(); }); + ok.addEventListener('click', function () { confirmed = true; dialog.close(); }); + // Clicking the dimmed page outside the dialog cancels, like Escape + dialog.addEventListener('click', function (e) { if (e.target === dialog) dialog.close(); }); + dialog.addEventListener('close', function () { + dialog.remove(); + if (confirmed) onOk(); + }); + dialog.showModal(); + ok.focus(); + } + + // For other scripts: asks in the dark dialog on dark pages, or the browser's box on light ones, then runs onOk + window.RedmineDarkMode.confirm = function (message, onOk) { + if (document.documentElement.classList.contains('dark-mode')) { + ask(message, onOk); + } else if (window.confirm(message)) { + onOk(); + } + }; + + if (!window.Rails) return; + + Rails.confirm = function (message, element) { + if (!document.documentElement.classList.contains('dark-mode')) { + return nativeConfirm ? nativeConfirm(message, element) : window.confirm(message); + } + // The second pass, after OK: let it through + if (approved === element) { + approved = null; + return true; + } + ask(message, function () { + approved = element; + if (element.tagName === 'FORM') { + element.requestSubmit ? element.requestSubmit() : element.submit(); + } else { + element.click(); + } + }); + return false; + }; +})(); diff --git a/assets/stylesheets/dark_mode.css b/assets/stylesheets/dark_mode.css index 523212d..26891ea 100644 --- a/assets/stylesheets/dark_mode.css +++ b/assets/stylesheets/dark_mode.css @@ -430,3 +430,40 @@ html.dark-mode .label-sub { color: #969aa1; } html.dark-mode .box-white, html.dark-mode div.issue_template { background-color: #1e1f20; } html.dark-mode fieldset.issue { background-color: #191a1c; } + +/* The dark "Are you sure?" dialog (dark_mode.js), in place of the browser's bright one */ +dialog.dark-mode-confirm { + min-width: 320px; + max-width: min(90vw, 480px); + padding: 0; + border: 1px solid #3f4248; + border-radius: 8px; + background: #1e1f20; + color: #d2d4d8; + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5); + font-size: 1rem; +} +dialog.dark-mode-confirm::backdrop { background: rgba(0, 0, 0, 0.55); } +dialog.dark-mode-confirm p { margin: 0; padding: 20px 20px 16px; white-space: pre-line; } +.dark-mode-confirm-buttons { + display: flex; + justify-content: flex-end; + gap: 8px; + padding: 12px 16px; + border-top: 1px solid #3f4248; + background: #1b1c1e; + border-radius: 0 0 8px 8px; +} +.dark-mode-confirm-buttons button { + min-width: 80px; + padding: 6px 14px; + border: 1px solid #3f4248; + border-radius: 4px; + background: #2c2e32; + color: #d2d4d8; + font: inherit; + cursor: pointer; +} +.dark-mode-confirm-buttons button:hover { background: #35373c; } +.dark-mode-confirm-buttons button.dark-mode-confirm-ok { background: #4183c4; border-color: #4183c4; color: #fff; } +.dark-mode-confirm-buttons button.dark-mode-confirm-ok:hover { background: #548fca; border-color: #548fca; } diff --git a/config/locales/en.yml b/config/locales/en.yml index 4ffb045..2a748fa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,3 +15,4 @@ en: label_dark_mode_dark: "On (dark)" label_dark_mode_switch_dark: "Switch to dark mode" label_dark_mode_switch_light: "Switch to light mode" + button_dark_mode_ok: "OK" diff --git a/lib/redmine_dark_mode.rb b/lib/redmine_dark_mode.rb index cbc8f19..ea49917 100644 --- a/lib/redmine_dark_mode.rb +++ b/lib/redmine_dark_mode.rb @@ -9,9 +9,12 @@ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. module RedmineDarkMode - # A user's choice: follow the device (the default), or always light or dark + # A user's choice: follow the device, or always light or dark MODES = %w[auto light dark].freeze + # Until someone chooses, Redmine follows their device + DEFAULT_MODE = 'auto' + def self.setup UserPreference.prepend(RedmineDarkMode::UserPreferencePatch) UserPreference.safe_attributes 'dark_mode' @@ -20,9 +23,9 @@ module RedmineDarkMode RedmineDarkMode::Hooks end - # The mode for this user (anonymous users follow the device, or their browser's saved choice) + # The mode for this user: their choice, else the default (anonymous users: their browser's saved choice, else the default) def self.mode_for(user) mode = user&.logged? ? user.pref.dark_mode : nil - MODES.include?(mode) ? mode : 'auto' + MODES.include?(mode) ? mode : DEFAULT_MODE end end diff --git a/lib/redmine_dark_mode/hooks.rb b/lib/redmine_dark_mode/hooks.rb index a01dc17..e1d498a 100644 --- a/lib/redmine_dark_mode/hooks.rb +++ b/lib/redmine_dark_mode/hooks.rb @@ -30,7 +30,7 @@ module RedmineDarkMode get mode() { return mode; }, set: function (value) { mode = value; apply(); }, loggedIn: #{User.current.logged?.to_json}, - labels: #{{ dark: l(:label_dark_mode_switch_dark), light: l(:label_dark_mode_switch_light) }.to_json} + labels: #{{ dark: l(:label_dark_mode_switch_dark), light: l(:label_dark_mode_switch_light), ok: l(:button_dark_mode_ok), cancel: l(:button_cancel) }.to_json} }; apply(); if (media.addEventListener) media.addEventListener('change', apply); diff --git a/lib/redmine_dark_mode/user_preference_patch.rb b/lib/redmine_dark_mode/user_preference_patch.rb index 9427062..c3ffce8 100644 --- a/lib/redmine_dark_mode/user_preference_patch.rb +++ b/lib/redmine_dark_mode/user_preference_patch.rb @@ -12,11 +12,11 @@ module RedmineDarkMode # Keeps the user's dark mode choice with their other preferences (My account) module UserPreferencePatch def dark_mode - self[:dark_mode].presence || 'auto' + self[:dark_mode].presence || RedmineDarkMode::DEFAULT_MODE end def dark_mode=(value) - self[:dark_mode] = RedmineDarkMode::MODES.include?(value.to_s) ? value.to_s : 'auto' + self[:dark_mode] = RedmineDarkMode::MODES.include?(value.to_s) ? value.to_s : RedmineDarkMode::DEFAULT_MODE end end end