Skip to content

Commit 5589cbb

Browse files
Alek99Alek Petuskeypicklelo
authored
Refactor rx.color (#2522)
* Refactor * Lint * Change name space to top level * Update test * Lint * Fix pyright * Update pyi --------- Co-authored-by: Alek Petuskey <alekpetuskey@aleks-mbp.lan> Co-authored-by: Nikhil Rao <nikhil@reflex.dev>
1 parent ba3de9b commit 5589cbb

File tree

7 files changed

+54
-8
lines changed

7 files changed

+54
-8
lines changed

reflex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import importlib
1010
from typing import Type
1111

12-
from reflex.constants.colors import Color as color
1312
from reflex.page import page as page
1413
from reflex.utils import console
1514
from reflex.utils.format import to_snake_case
@@ -254,6 +253,7 @@
254253
"reflex.compiler.utils": ["get_asset_path"],
255254
"reflex.components": _ALL_COMPONENTS + ["chakra", "next"],
256255
"reflex.components.component": ["memo"],
256+
"reflex.components.core": ["color"],
257257
"reflex.components.el": ["el"],
258258
"reflex.components.lucide": ["lucide"],
259259
"reflex.components.radix": ["radix"],

reflex/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ from reflex.components import NoSSRComponent as NoSSRComponent
447447
from reflex.components import chakra as chakra
448448
from reflex.components import next as next
449449
from reflex.components.component import memo as memo
450+
from reflex.components.core import color as color
450451
from reflex.components import el as el
451452
from reflex.components import lucide as lucide
452453
from reflex.components import radix as radix

reflex/components/core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from . import layout as layout
44
from .banner import ConnectionBanner, ConnectionModal
5+
from .colors import color
56
from .cond import Cond, cond
67
from .debounce import DebounceInput
78
from .foreach import Foreach

reflex/components/core/colors.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""The colors used in Reflex are a wrapper around https://www.radix-ui.com/colors."""
2+
3+
from reflex.constants.colors import Color, ColorType, ShadeType
4+
5+
6+
def color(
7+
color: ColorType,
8+
shade: ShadeType = 7,
9+
alpha: bool = False,
10+
) -> Color:
11+
"""Create a color object.
12+
13+
Args:
14+
color: The color to use.
15+
shade: The shade of the color to use.
16+
alpha: Whether to use the alpha variant of the color.
17+
18+
Returns:
19+
The color object.
20+
"""
21+
return Color(color, shade, alpha)

reflex/constants/colors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""The colors used in Reflex are a wrapper around https://www.radix-ui.com/colors."""
2+
23
from dataclasses import dataclass
34
from typing import Literal
45

@@ -40,6 +41,20 @@
4041
ShadeType = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
4142

4243

44+
def format_color(color: ColorType, shade: ShadeType, alpha: bool) -> str:
45+
"""Format a color as a CSS color string.
46+
47+
Args:
48+
color: The color to use.
49+
shade: The shade of the color to use.
50+
alpha: Whether to use the alpha variant of the color.
51+
52+
Returns:
53+
The formatted color.
54+
"""
55+
return f"var(--{color}-{'a' if alpha else ''}{shade})"
56+
57+
4358
@dataclass
4459
class Color:
4560
"""A color in the Reflex color palette."""
@@ -52,3 +67,14 @@ class Color:
5267

5368
# Whether to use the alpha variant of the color
5469
alpha: bool = False
70+
71+
def __format__(self, format_spec: str) -> str:
72+
"""Format the color as a CSS color string.
73+
74+
Args:
75+
format_spec: The format specifier to use.
76+
77+
Returns:
78+
The formatted color.
79+
"""
80+
return format_color(self.color, self.shade, self.alpha)

reflex/utils/serializers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Any, Callable, Dict, List, Set, Tuple, Type, Union, get_type_hints
99

1010
from reflex.base import Base
11-
from reflex.constants.colors import Color
11+
from reflex.constants.colors import Color, format_color
1212
from reflex.utils import exceptions, format, types
1313

1414
# Mapping from type to a serializer.
@@ -232,7 +232,7 @@ def serialize_datetime(dt: Union[date, datetime, time, timedelta]) -> str:
232232

233233

234234
@serializer
235-
def serialize_color(color: Color) -> SerializedType:
235+
def serialize_color(color: Color) -> str:
236236
"""Serialize a color.
237237
238238
Args:
@@ -241,10 +241,7 @@ def serialize_color(color: Color) -> SerializedType:
241241
Returns:
242242
The serialized color.
243243
"""
244-
if color.alpha:
245-
return f"var(--{color.color}-a{color.shade})"
246-
else:
247-
return f"var(--{color.color}-{color.shade})"
244+
return format_color(color.color, color.shade, color.alpha)
248245

249246

250247
try:

tests/utils/test_serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from reflex.base import Base
8-
from reflex.constants.colors import Color as color
8+
from reflex.components.core.colors import color
99
from reflex.utils import serializers
1010
from reflex.vars import Var
1111

0 commit comments

Comments
 (0)