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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add background color based on the OS mode #7117

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 .changeset/lucky-tips-occur.md
@@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---

fix:add background color based on the OS mode
12 changes: 12 additions & 0 deletions gradio/blocks.py
Expand Up @@ -1612,6 +1612,18 @@ def get_config_file(self):
"stylesheets": self.stylesheets,
"theme": self.theme.name,
"protocol": "sse_v1",
"body_css": {
"body_background_fill": self.theme._get_computed_value(
"body_background_fill"
),
"body_text_color": self.theme._get_computed_value("body_text_color"),
"body_background_fill_dark": self.theme._get_computed_value(
"body_background_fill_dark"
),
"body_text_color_dark": self.theme._get_computed_value(
"body_text_color_dark"
),
},
}

def get_layout(block):
Expand Down
25 changes: 25 additions & 0 deletions gradio/themes/base.py
Expand Up @@ -6,6 +6,7 @@
import textwrap
from pathlib import Path
from typing import Iterable
import warnings

import huggingface_hub
import semantic_version as semver
Expand Down Expand Up @@ -93,6 +94,30 @@ def repl_func(match):

return f"{css_code}\n{dark_css_code}"

def _get_computed_value(self, property: str, depth=0) -> str:
MAX_DEPTH = 100
if depth > MAX_DEPTH:
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't want to raise an exception in case there were "broken" themes with circular references before. They didn't error out before and don't want a breaking change.

warnings.warn(f"Cannot resolve '{property}' - circular reference detected.")
return ""
is_dark = property.endswith("_dark")
if is_dark:
set_value = getattr(
self, property, getattr(self, property[:-5], "")
) # if dark mode value is unavailable, use light mode value
else:
set_value = getattr(self, property, "")
pattern = r"(\*)([\w_]+)(\b)"

def repl_func(match, depth):
word = match.group(2)
dark_suffix = "_dark" if property.endswith("_dark") else ""
return self._get_computed_value(word + dark_suffix, depth + 1)

computed_value = re.sub(
pattern, lambda match: repl_func(match, depth), set_value
)
return computed_value

def to_dict(self):
"""Convert the theme into a python dictionary."""
schema = {"theme": {}}
Expand Down
24 changes: 24 additions & 0 deletions js/app/index.html
Expand Up @@ -16,6 +16,29 @@
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>

<style>
:root {
--bg: {{ config.get('body_css', {}).get('body_background_fill', 'white') }};
--col: {{ config.get('body_css', {}).get('body_text_color', '#1f2937') }};
--bg-dark: {{ config.get('body_css', {}).get('body_background_fill_dark', '#0b0f19') }};
--col-dark: {{ config.get('body_css', {}).get('body_text_color_dark', '#f3f4f6') }};
}


body {
background: var(--bg);
color: var(--col);
font-family: Arial, Helvetica, sans-serif;
Copy link
Member

Choose a reason for hiding this comment

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

Prevents the janky switch from a default serif to our sans-serif.

}

@media (prefers-color-scheme: dark) {
body {
background: var(--bg-dark);
color: var(--col-dark);
}
}
</style>

<script type="module" src="./src/main.ts"></script>
<meta property="og:url" content="https://gradio.app/" />
<meta property="og:type" content="website" />
Expand Down Expand Up @@ -74,6 +97,7 @@
</gradio-app>
<script>
const ce = document.getElementsByTagName("gradio-app");

if (ce[0]) {
ce[0].addEventListener("domchange", () => {
document.body.style.padding = "0";
Expand Down
1 change: 1 addition & 0 deletions js/app/vite.config.ts
Expand Up @@ -115,6 +115,7 @@ export default defineConfig(({ mode }) => {
} else if (
selector.indexOf(":root") > -1 ||
selector.indexOf("dark") > -1 ||
selector.indexOf("body") > -1 ||
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to add a prefix to body selectors, that breaks them.

fileName.indexOf(".svelte") > -1
) {
return selector;
Expand Down