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

[Feature request] Make a style! macro to write style directly in Rust files #9

Closed
sambonbonne opened this issue Sep 20, 2023 · 8 comments

Comments

@sambonbonne
Copy link

Hello,

A nice feature provided by some other CSS tools for Rust (styled and stylers) have a style! macro to allow writing CSS directly in .rs files.

Instead of using `style_sheet!("path/to/file.scss"), this would allow us to write directly:

style! {
  .first {
    color: red;

    strong {
      color: blue;
    }
  }
}

Of course, this does not mean the style_sheet! macro should be remove, both can be complementary.

@myFavShrimp
Copy link
Owner

I like this idea. This change itself should be relatively easy to implement with the current state of turf. To keep everything consistent, it should definitely respect turf's settings, and with #8 planned, it's important to discuss how the proposed style! macro should behave in conjunction with file creation.

What seems particularly interesting to me is what the files should be called that are output when a user decides to set the separate_css_files_path.

@sambonbonne
Copy link
Author

I can be the rust file name but with .css instead of .rs?

If you call the macro in my_component.rs, the file can be named my_component.css, but it implies you don't have filenames conflicts.

To avoid these conflicts, the output can re-use the complete path so you if you have style! in src/path/to/my_component.rs, the generated file can be in <output_dir>/path/to/my_component.css. It can also be in the form of a replacement for /, for example <output_dir>/path_to_my_component.css.

@myFavShrimp
Copy link
Owner

This is only possible on the nightly toolchain of Rust (see https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_file). Using this part of the Span api makes it necessary to hide this feature from users of stable Rust, which I don't want.

An idea that came to my mind is using a hash (maybe of the resulting css style sheet?) as a file name. This makes every file name unique. However the names are quite ugly and can be harder to use.

To make it easier to handle the resulting files, two new variables similar to the current STYLE_SHEET can be generated which have the name of the files stored in them.

@sambonbonne
Copy link
Author

Wouldn't it work with the file! macro? (See the doc)

A hash would be working but if some user want to use an external tool to build some CSS together after turf compiled files (and group some files by components/purpose), it would become difficult, I'm not sure this is suitable (also, I just saw noticed you completed #8, thank you, I will give it a try!)

@myFavShrimp
Copy link
Owner

Macros are resolved in the file they are used in. This wouldn't work because if turf needs to read the macros output, file! has to be used inside turf and would therefore return the file name of turf's source file

@sambonbonne
Copy link
Author

Oh, right, I understand

Unfortunately I have no other idea so I guess this won't be possible to have a "proper" filename

@sambonbonne
Copy link
Author

@myFavShrimp it seems you also added this feature since 0.7.0, I tried it and it works as expected so I guess this issue can be closed?

@myFavShrimp
Copy link
Owner

This feature is implemented and available when building from the repository but it is not released yet. You are right, this issue can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants