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

Tailwind dark: prefix does not work #311

Closed
smojef opened this issue Jan 28, 2023 · 6 comments
Closed

Tailwind dark: prefix does not work #311

smojef opened this issue Jan 28, 2023 · 6 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@smojef
Copy link

smojef commented Jan 28, 2023

According to Tailwind documentation, we should be able to use classes such as dark:text-yellow, but it does not work.

import nicegui.ui as ui    
ui.label("TEST").classes('text-red dark:text-yellow')    
ui.run(dark=True)

This shows red text on dark background.
Did I missed something?

@falkoschindler
Copy link
Contributor

Good question! Currently I have no idea why it doesn't work. 😕

@falkoschindler falkoschindler added the help wanted Extra attention is needed label Jan 30, 2023
@falkoschindler
Copy link
Contributor

I just realized that we currently pass the dark argument of ui.run to Quasar only:

const dark = {{ dark }};
Quasar.Dark.set(dark === None ? "auto" : dark);

Somehow we probably need to inform Tailwind about the mode we are in.

@falkoschindler
Copy link
Contributor

And I noticed that text-yellow is not a Tailwind color. Something like text-yellow-400 should work better. But there seems to be more about it.

@falkoschindler
Copy link
Contributor

falkoschindler commented Feb 2, 2023

Another step forward: Your default color text-red is also not a Tailwind class but from Quasar. It's overriding Tailwind classes (probably due to the order of importing the libraries in index.html). So to make dark: work, use Tailwind colors only:

ui.label('TEST').classes('text-red-400 dark:text-yellow-400')

But: This only considers the current system settings, not the dark argument in ui.run. For dark=None it works as expected, but dark=True is ignored by the dark: color.

@falkoschindler
Copy link
Contributor

I think I found a solution in 0093bfd. This adds the preferred mode (given via ui.run or ui.page) to the Tailwind configuration.

@smojef Would you like to try it out?

@falkoschindler falkoschindler self-assigned this Feb 2, 2023
falkoschindler added a commit that referenced this issue Feb 3, 2023
@falkoschindler falkoschindler added this to the v1.1.6 milestone Feb 3, 2023
@falkoschindler falkoschindler added bug Something isn't working and removed help wanted Extra attention is needed labels Feb 3, 2023
@smojef
Copy link
Author

smojef commented Feb 3, 2023

I added ui.label('TEST').classes('text-red-400 dark:text-yellow-400') under ui.label('World') of my Hello world example #339.

  • With version 1.1.5 the label was always red
  • With the latest github version:
    • The label is yellow if started with dark=True. (OK)
    • The label is red if started with dark=False. (OK)

But the label did not change when I flip my Dark switch. So looking at your changes, I edited my on_dark as follows:

async def on_dark(event):
    # HACK: Is that the proper way of switching dark mode?
    js = '''
        const dark = {value};
        Quasar.Dark.set(dark === None ? "auto" : dark);
        if (dark !== None) tailwind.config.darkMode = "class";
        document.body.classList.remove("dark");    
        if (dark === True) document.body.classList.add("dark");
        Quasar.Dark.isActive;
        '''.replace('{value}', str(bool(event.value)))  # .lower() is not required?
    ret = await ui.run_javascript(js, respond=True) 
    info.log.push(f"Dark mode set to {ret}")

And then everything seems to work.

Perhaps the dark mode switching could be encapsulated in a JS function for reuse instead? And then, even better, exposed in nicegui API ;-)

@smojef smojef closed this as completed Feb 3, 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