Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/app/news/+layout-demos-dark-mode.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix unreadable light-on-light text in the stack, box, spacer and aspect ratio layout demos when the docs are viewed in dark mode.
59 changes: 59 additions & 0 deletions docs/app/tests/test_layout_demo_contrast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Regression coverage for fixed-color layout demos staying readable in dark mode."""

import re
from pathlib import Path

import pytest

DOCS = Path(__file__).resolve().parents[2]

# Named CSS backgrounds do not adapt to the theme, so a box using one must pin
# its own text color instead of inheriting the theme's foreground.
FIXED_BACKGROUND = re.compile(r'(?:bg|background_color)="[a-z]+"')


def enclosing_call(source: str, index: int) -> str:
"""Return the argument text of the component call that contains ``index``.

Args:
source: The document source.
index: An offset inside the call's argument list.

Returns:
The text between the call's opening and closing parentheses.
"""
depth = 0
start = index
while start > 0:
start -= 1
if source[start] == ")":
depth += 1
elif source[start] == "(":
if depth == 0:
break
depth -= 1
depth = 0
end = start
while end < len(source):
end += 1
if source[end] == "(":
depth += 1
elif source[end] == ")":
if depth == 0:
break
depth -= 1
return source[start + 1 : end]


@pytest.mark.parametrize(
"doc",
["layout/stack.md", "layout/box.md", "layout/spacer.md", "layout/aspect_ratio.md"],
)
def test_fixed_color_boxes_pin_a_text_color(doc):
"""Every component with a fixed CSS background also sets an explicit text color."""
source = (DOCS / "library" / doc).read_text()
for match in FIXED_BACKGROUND.finditer(source):
call = enclosing_call(source, match.start())
assert re.search(r'\bcolor="', call), (
f"{doc}: {match.group(0)} has no explicit text color in {call.strip()!r}"
)
4 changes: 4 additions & 0 deletions docs/library/layout/aspect_ratio.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rx.grid(
rx.box(
"Widescreen 16:9",
background_color="papayawhip",
color="black",
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
width="100%",
height="100%",
),
Expand All @@ -32,6 +33,7 @@ rx.grid(
rx.box(
"Letterbox 4:3",
background_color="orange",
color="black",
width="100%",
height="100%",
),
Expand All @@ -41,6 +43,7 @@ rx.grid(
rx.box(
"Square 1:1",
background_color="green",
color="white",
width="100%",
height="100%",
),
Expand All @@ -50,6 +53,7 @@ rx.grid(
rx.box(
"Portrait 5:7",
background_color="lime",
color="black",
width="100%",
height="100%",
),
Expand Down
2 changes: 2 additions & 0 deletions docs/library/layout/box.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rx.box(
rx.box(
"CSS color",
background_color="yellow",
color="black",
border_radius="2px",
width="20%",
margin="4px",
Expand All @@ -28,6 +29,7 @@ rx.box(
rx.box(
"CSS color",
background_color="orange",
color="black",
border_radius="5px",
width="40%",
margin="8px",
Expand Down
6 changes: 3 additions & 3 deletions docs/library/layout/spacer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Creates an adjustable, empty space that can be used to tune the spacing between

```python demo
rx.flex(
rx.center(rx.text("Example"), bg="lightblue"),
rx.center(rx.text("Example"), bg="lightblue", color="black"),
rx.spacer(),
rx.center(rx.text("Example"), bg="lightgreen"),
rx.center(rx.text("Example"), bg="lightgreen", color="black"),
rx.spacer(),
rx.center(rx.text("Example"), bg="salmon"),
rx.center(rx.text("Example"), bg="salmon", color="black"),
Comment thread
FarhanAliRaza marked this conversation as resolved.
width="100%",
)
```
Expand Down
18 changes: 16 additions & 2 deletions docs/library/layout/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ rx.flex(
rx.box(
"Example",
bg="orange",
color="black",
border_radius="3px",
width="20%",
),
rx.box(
"Example",
bg="lightblue",
color="black",
border_radius="3px",
width="30%",
),
rx.box(
"Example",
bg="lightgreen",
color="black",
border_radius="3px",
width="50%",
),
Expand All @@ -78,18 +81,21 @@ rx.flex(
rx.box(
"Example",
bg="orange",
color="black",
border_radius="3px",
width="20%",
),
rx.box(
"Example",
bg="lightblue",
color="black",
border_radius="3px",
width="30%",
),
rx.box(
"Example",
bg="lightgreen",
color="black",
border_radius="3px",
width="50%",
),
Expand All @@ -104,28 +110,32 @@ rx.flex(

```python demo
rx.hstack(
rx.box("Example", bg="red", border_radius="3px", width="10%"),
rx.box("Example", bg="red", color="black", border_radius="3px", width="10%"),
rx.box(
"Example",
bg="orange",
color="black",
border_radius="3px",
width="10%",
),
rx.box(
"Example",
bg="yellow",
color="black",
border_radius="3px",
width="10%",
),
rx.box(
"Example",
bg="lightblue",
color="black",
border_radius="3px",
width="10%",
),
rx.box(
"Example",
bg="lightgreen",
color="black",
border_radius="3px",
width="60%",
),
Expand All @@ -137,28 +147,32 @@ rx.hstack(

```python demo
rx.vstack(
rx.box("Example", bg="red", border_radius="3px", width="20%"),
rx.box("Example", bg="red", color="black", border_radius="3px", width="20%"),
rx.box(
"Example",
bg="orange",
color="black",
border_radius="3px",
width="40%",
),
rx.box(
"Example",
bg="yellow",
color="black",
border_radius="3px",
width="60%",
),
rx.box(
"Example",
bg="lightblue",
color="black",
border_radius="3px",
width="80%",
),
rx.box(
"Example",
bg="lightgreen",
color="black",
border_radius="3px",
width="100%",
),
Expand Down
Loading