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

The style method not work when using css variables #1293

Closed
CrystalWindSnake opened this issue Jul 30, 2023 · 3 comments
Closed

The style method not work when using css variables #1293

CrystalWindSnake opened this issue Jul 30, 2023 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@CrystalWindSnake
Copy link
Contributor

CrystalWindSnake commented Jul 30, 2023

Description

from nicegui import ui

ui.add_body_html(
    """
 <div id="my-ele"></div>
"""
)

pointer = ui.query("#my-ele")

pointer.classes("h-[4rem] w-[4rem]")
pointer.tailwind.background_color("blue-400").position("absolute").z_index("50")
pointer.style(f"""--top: 10px;--left: 50px;top: var(--top);left: var(--left)""")

The --left and --top variable set by the style method in the above code do not work on the corresponding label.

@CrystalWindSnake CrystalWindSnake changed the title The style method not work when getting the element object through ui.query method The style method not work when setting css var Jul 30, 2023
@CrystalWindSnake CrystalWindSnake changed the title The style method not work when setting css var The style method not work when using css variables Jul 30, 2023
@falkoschindler
Copy link
Contributor

Thanks, @CrystalWindSnake!

Minimal reproduction:

ui.add_body_html('<div id="element">Test</div>')
ui.query('#element').style('--color: red; color: var(--color);')

In contrast, this works:

ui.label('Test').style('--color: red; color: var(--color);')

It looks like this line in query.js

document.querySelectorAll(this.selector).forEach((e) => (e.style[key] = val))

should be replaced with

document.querySelectorAll(this.selector).forEach((e) => e.style.setProperty(key, val))

Needs some more investigation and testing.

@falkoschindler
Copy link
Contributor

ChatGPT's explanation why we need to use setProperty in this case:

The element.style[key] = value syntax in JavaScript works for directly setting CSS properties on an element, where key is the JavaScript-style camel case name of the CSS property you want to set (like backgroundColor for the CSS property background-color), and value is the value you want to set that property to.

However, CSS variables, which start with --, can't be set this way. This is due to the naming rules in JavaScript for property names. A JavaScript property name can't start with a hyphen -, and certainly can't start with --. This is why element.style['--my-variable'] = value would not work.

The setProperty method doesn't have this limitation, because it's a function and the variable name is passed as a string argument. This means it can accept anything as a property name, including strings that start with --, which is why it's used for setting CSS variables.

So, you can use element.style[key] = value for normal CSS properties like backgroundColor, width, height, etc. For CSS variables, which start with --, you should use element.style.setProperty(key, value).

@CrystalWindSnake
Copy link
Contributor Author

👍

@falkoschindler falkoschindler added the bug Something isn't working label Aug 1, 2023
@falkoschindler falkoschindler self-assigned this Aug 1, 2023
@falkoschindler falkoschindler added this to the 1.3.7 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants