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

[REF-1417] Convert underscore-prefixed style props to pseudo selector #2266

Merged
merged 11 commits into from Dec 11, 2023

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Dec 6, 2023

Use emotion CSS-in-JS for default components.

Convert underscore-prefixed style props, like _hover and _focus to emotion compatible self-referential pseudo selectors like &:hover and &:focus.

Test cases

Keys

  • Simple property
  • webkit/moz property
  • self-referential (underscore prefix, colon prefix)

Values

  • Each of the above with state vars / f-string
  • Each of the above with responsive list

Extension precedence

  • Each of the above combined with style={}
  • Each of the above combined with component styles from app

Types of Test

  • Unit: does the resulting style dict for a component (rx.el.div) look correct?
  • Integration: for different types of components, get computed css property

Sample Code

import reflex as rx
import reflex.components.radix.themes as rdxt


class FooState(rx.State):
    """The app state."""
    counter: int = 0
    text: str = ""
    rcolor: str = "lime"

    def foo(self):
        print("foo")


def index() -> rx.Component:
    return rx.fragment(
        rx.color_mode_button(rx.color_mode_icon(), float="right"),
        rx.button("Foo", on_click=FooState.foo),
        rx.button("Increment", on_click=FooState.set_counter(FooState.counter + 1)),
        rx.text(FooState.counter, _hover={"color": "hotpink"}),
        rx.text(f"({FooState.counter})"),
        rx.button("Decrement", on_click=FooState.set_counter(FooState.counter - 1)),
        rx.input(placeholder="text", value=FooState.text, on_change=FooState.set_text, _placeholder={"text_decoration": "underline"}),
        rx.el.input(
            placeholder="text",
            value=FooState.text,
            _placeholder={"text_decoration": "underline", "padding-left": "10px"},
            _focus_visible={"outline": "2px solid crimson"},
        ),
        rx.text(FooState.text),
        rx.el.button(
            "Plain Button",
            background_color=f"var(--{FooState.rcolor}-3)",
            padding="4px",
            _hover=[
                {"background_color": f"var(--{FooState.rcolor}-4)"},
                {"background_color": "var(--plum-4)"},
                {"background_color": "var(--tomato-4)"},
                {"background_color": "var(--grass-4)"},
                {"background_color": "var(--accent-4)"},
            ],
            _active={"background_color": f"var(--{FooState.rcolor}-5)"},
        ),
        rdxt.box(
            rx.box("Foo", height="500px"),
            overflow_y="scroll",
            border="1px solid black",
            height="200px",
            width=["100px", "200px", "300px", "400px", "500px"],
            style={
                "::first-letter": {
                    "font_size": "1.5rem",
                    "font_weight": "bold",
                    "color": "brown",
                },
                "::-webkit-scrollbar": {"display": "none"},
                "::after": {"content": f"'{FooState.text}'"},
            }
        ),
    )


# Add state and page to the app.
app = rx.App(theme=rdxt.Theme(accent_color="plum"))
app.add_page(index)
app.add_page(index, route="/foo")
app.compile()

@Alek99
Copy link
Contributor

Alek99 commented Dec 9, 2023

Is this ready for review? I'll try it out

@Alek99 Alek99 requested a review from picklelo December 11, 2023 04:35
@masenf masenf changed the base branch from nikhil/radix-primitives to main December 11, 2023 18:15
@masenf masenf closed this Dec 11, 2023
@masenf masenf reopened this Dec 11, 2023
Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

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

This is awesome, solves 3 issues in one PR

# The wrapped backtick is used to identify the Var for interpolation.
return f"`{str(new_var)}`", new_var._var_data

return style_item, None
Copy link
Contributor

Choose a reason for hiding this comment

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

If style_item is not a str would the type signature still be correct here? Should it be str(style_item)?

@picklelo picklelo merged commit e522674 into main Dec 11, 2023
45 checks passed
@picklelo picklelo deleted the masenf/pseudo-css branch January 30, 2024 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants