3

I am trying to set up a new self convention to make my React code better organized like the following:

export default function HeaderExample() {
  const compStyle = getStyle();
  return (
    <>
      <h1>Hello World</h1>
      <style jsx>{compStyle}</style>
    </>
  )
}

function getStyle() {
  return `
        h1 {
          color: blue;
        }
      `
}

It works fine, but writing the return in getStyle() is pretty hard because I don't get formatting or syntax highlight help from the IDE.

How can I get those? Plugin / Library / Setting / Giving it a type? Any of those will help.

And at the same chance, do you think this can cause any issues down the road?

2
  • return css` .... -- it gets colored for me (and offers CSS code completion) i.imgur.com/9eKQCMS.png For that I had to clone bundled Language Injection rule for html and adjust it to be CSS (so no need for manual injections) i.imgur.com/tKVXM7O.png . Please note: I'm not JavaScript person at all and do not know if just prefixing a template string with css is enough -- maybe some actual code is also needed (that handles that css prefix in some way). Commented Feb 23, 2022 at 20:12
  • webstorm --> // language=CSS Commented Feb 24, 2022 at 9:35

2 Answers 2

1

WebStorm has a built-in feature Language Injection. for that.

Here you will find in detail documentation of that feature.

Short version.

  1. Place your cursor in the relevant code block and click alt + enter.

enter image description here

  1. In the DropDown, select Inject language or reference. Without clicking to the right, click enter again.

enter image description here

  1. A window should open, now select the language you like to inject. If you select CSS the result should be:

enter image description here

To answer your second question. It shouldn't cause any trouble down the road.

edit addition:

It is possible to trigger the same behaviour with a comment laguange=css

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! that solves it, do you know if there's maybe an easier way like @Arjit Tiwari offered for vscode? As it gets repetitive to do it for every component?
I don't know another way, but I think it should be possible to automate this. If you are using styles like that in many places, also take a look at Styled Components.
@eliezra236 just found out you can do the same as Arjit Tiwari wrote for VSCode in Webstorm with // language=CSS. See my edit.
For the automation, also see documentation on language injection rules definition
1

In vscode you can use ES6 String css vscode extension

Just add /*css*/ before ` like this

function getStyle() {
  return /*css*/`
        h1 {
          color: blue;
        }
      `
}

2 Comments

Is there anything similar for WebStorm?
I do not use Webstorm for web development, I think you can use Language Injection for Webstorm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.