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

ADD prototype version dark mode for Airflow UI #39355

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions airflow/www/static/css/bootstrap-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
html[data-color-scheme='dark'] {
filter:invert(100%)hue-rotate(180deg);
}
html[data-color-scheme='light'] {

}
body {
margin: 0;
Expand Down
54 changes: 54 additions & 0 deletions airflow/www/static/js/toggle_theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/* document, localStorage, window */

const STORAGE_THEME_KEY = 'darkTheme';
const HTML_THEME_DATASET_KEY = 'data-color-scheme';
const HTML = document.documentElement;
const TOGGLE_BUTTON_ID = 'themeToggleButton';

const getJsonFromStorage = (key)=>JSON.parse(localStorage.getItem(key));
const updateTheme = (isDark)=>{
localStorage.setItem(STORAGE_THEME_KEY, isDark);
HTML.setAttribute(HTML_THEME_DATASET_KEY, isDark ? 'dark' : 'light');
}
;
const initTheme = ()=>{
const isDark = getJsonFromStorage(STORAGE_THEME_KEY) ?? window.matchMedia('(prefers-color-scheme: dark)').matches;
updateTheme(isDark);
}
;
const toggleTheme = ()=>{
const isDark = getJsonFromStorage(STORAGE_THEME_KEY);
updateTheme(!isDark);
}
;
window.addEventListener('click', (e)=>{
if (e.target.id !== TOGGLE_BUTTON_ID)
return;
toggleTheme();
}
);
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e)=>{
const isDark = e.matches;
updateTheme(isDark);
}
);
initTheme();
7 changes: 7 additions & 0 deletions airflow/www/templates/appbuilder/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
{% include 'appbuilder/navbar_menu.html' %}
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active">
<div style="padding: 20px; cursor: pointer" id="themeToggleButton">
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a title and aria-label saying something like "Toggle dark mode (experimental)"

<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-moon" viewBox="0 0 16 16" id="themeToggleButton">
YounHS marked this conversation as resolved.
Show resolved Hide resolved
<path d="M6 .278a.77.77 0 0 1 .08.858 7.2 7.2 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277q.792-.001 1.533-.16a.79.79 0 0 1 .81.316.73.73 0 0 1-.031.893A8.35 8.35 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.75.75 0 0 1 6 .278M4.858 1.311A7.27 7.27 0 0 0 1.025 7.71c0 4.02 3.279 7.276 7.319 7.276a7.32 7.32 0 0 0 5.205-2.162q-.506.063-1.029.063c-4.61 0-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286"></path>
</svg>
</div>
</li>
{% include 'appbuilder/navbar_right.html' %}
</ul>
</div>
Expand Down
10 changes: 9 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,16 @@ def render_template(self, *args, **kwargs):
**kwargs,
)

class CustomAirflow(AirflowBaseView):
def render_template(self, *args, **kwargs):
rendered_template = super().render_template(*args, **kwargs)

script_tag = '<script src="/static/js/toggle_theme.js"></script>'

return rendered_template.replace("<head>", f"{script_tag}<head>")


class Airflow(AirflowBaseView):
class Airflow(CustomAirflow):
"""Main Airflow application."""

@expose("/health")
Expand Down