Skip to content

Commit

Permalink
fix(html): Improove typing for style element
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldedik committed Jan 27, 2025
1 parent 4cdac7d commit 2804024
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
18 changes: 9 additions & 9 deletions ludic/html.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Iterator
from typing import Self, Unpack
from typing import Generic, Self, Unpack

from .attrs import (
AreaAttrs,
Expand Down Expand Up @@ -58,11 +58,11 @@
from .base import BaseElement
from .elements import Element, ElementStrict
from .styles import (
Theme,
format_styles,
from_components,
from_loaded,
)
from .styles.types import TTheme
from .types import (
AnyChildren,
ComplexChildren,
Expand Down Expand Up @@ -533,16 +533,16 @@ def __init__(self, *children: NoChildren, **attrs: Unpack[HeadLinkAttrs]) -> Non
super().__init__(*children, **attrs)


class style(BaseElement, GlobalStyles):
class style(Generic[TTheme], BaseElement, GlobalStyles):
html_name = "style"

children: tuple[GlobalStyles | Callable[[Theme], GlobalStyles] | str]
children: tuple[GlobalStyles | Callable[[TTheme], GlobalStyles] | str]
attrs: StyleAttrs

def __init__(
self,
styles: GlobalStyles | Callable[[Theme], GlobalStyles] | str,
theme: Theme | None = None,
styles: GlobalStyles | Callable[[TTheme], GlobalStyles] | str,
theme: TTheme | None = None,
**attrs: Unpack[StyleAttrs],
) -> None:
super().__init__(styles, **attrs)
Expand All @@ -551,17 +551,17 @@ def __init__(
self.context["theme"] = theme

@classmethod
def use(cls, styles: GlobalStyles | Callable[[Theme], GlobalStyles]) -> Self:
def use(cls, styles: GlobalStyles | Callable[[TTheme], GlobalStyles]) -> Self:
return cls(styles)

@classmethod
def from_components(
cls, *components: type[BaseElement], theme: Theme | None = None
cls, *components: type[BaseElement], theme: TTheme | None = None
) -> Self:
return cls(from_components(*components, theme=theme), type="text/css")

@classmethod
def load(cls, cache: bool = False, theme: Theme | None = None) -> Self:
def load(cls, cache: bool = False, theme: TTheme | None = None) -> Self:
return cls(from_loaded(cache=cache, theme=theme), type="text/css")

def __getitem__(self, key: str | tuple[str, ...]) -> CSSProperties | GlobalStyles:
Expand Down
4 changes: 2 additions & 2 deletions ludic/styles/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ class Grid:
class Frame:
"""Frame layout config for a theme."""

# The width of the frame
# The width of the frame as fraction
numerator: int = 16

# The height of the frame
# The height of the frame as fraction
denominator: int = 9


Expand Down
16 changes: 15 additions & 1 deletion ludic/styles/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from abc import ABCMeta, abstractmethod
from collections.abc import Mapping
from typing import Literal, LiteralString, Self, SupportsIndex, TypedDict
from typing import (
TYPE_CHECKING,
Literal,
LiteralString,
Self,
SupportsIndex,
TypedDict,
)

from typing_extensions import TypeVar

from .utils import (
clamp,
Expand All @@ -10,6 +19,10 @@
pick_readable_color_for,
)

if TYPE_CHECKING:
from .themes import Theme

TTheme = TypeVar("TTheme", bound="Theme", default="Theme")
SizeUnit = Literal["px", "ex", "em", "ch", "rem", "vw", "vh", "vmin", "vmax", "%"]


Expand Down Expand Up @@ -589,6 +602,7 @@ def __sub__(self, value: float | int | LiteralString | SupportsIndex) -> Self:
"height": str,
"hyphens": Literal["none", "manual", "auto"],
# I
"inset": str | int,
"inline-size": str,
# J
"justify-content": Literal[
Expand Down

0 comments on commit 2804024

Please sign in to comment.