Editor and linting
Syntax highlighting
- The
csstemplate is plain CSS, so any editor plugin that highlightscsstagged templates works. - In VS Code, the vscode-styled-components extension highlights
css`...`templates. It also offers CSS completions inside them. - The template inside
<style jsx>is an untagged template literal. Editor plugins that look for acsstag do not treat it as CSS. Usecssif you want highlighting. - Scope markers such as
s:c-1a2b3c-0only exist in the compiled output, so you never write them by hand.
CSS comments in VS Code
- Inside
<style jsx>, the VS Code comment command writes a JSX comment ({/* ... */}), which is not valid CSS. - The keybindings below toggle CSS comments (
/* ... */) instead. ctrl+shift+/toggles a comment on the current line. No selection is needed.ctrl+shift+oem_7toggles a comment around the selected lines.oem_7is the'key on a US keyboard.- Change the keys as needed. Put the entries in your
keybindings.json(command palette,Preferences: Open Keyboard Shortcuts (JSON)).
[
{
// Toggle a comment on the current line. No selection needed.
"key": "ctrl+shift+/",
"command": "runCommands",
"args": {
"commands": [
"cursorEnd", // so the cursor stays on the same line
"cursorHomeSelect",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(\\/\\* )(.+)( \\*\\/)|(.*)/$2${4:+/* }$4${4:+ */}/}",
},
},
],
},
"when": "resourceExtname =~ /\\.(js|ts|r)(?!on)/",
},
{
// Toggle a comment around a selected block.
"key": "ctrl+shift+oem_7",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(\\/\\*)([\\s\\S]*?)(\\*\\/)|([\\s\\S]+)/$2${4:+/* }$4${4:+ */}/}",
},
"when": "resourceExtname =~ /\\.(js|ts|r)(?!on)/",
},
]- The
whenclause matches.js,.jsx,.tsand.tsxfiles, and skips.json. Edit it to match your file types. - These keybindings were shared by @AverTry in #40.
Linting
- A bare
css`...`;statement is an unused expression to linters. Theno-unused-expressionsrule reports it asExpected an assignment or function call and instead saw an expression. - Set
allowTaggedTemplates: trueon the rule to allow tagged templates used as statements.
ESLint:
// eslint.config.js
export default [
{
rules: {
'no-unused-expressions': ['error', { allowTaggedTemplates: true }],
},
},
];typescript-eslint replaces the core rule, so turn the core rule off and set the option on its own rule:
// eslint.config.js
export default [
{
rules: {
'no-unused-expressions': 'off',
'@typescript-eslint/no-unused-expressions': ['error', { allowTaggedTemplates: true }],
},
},
];oxlint takes the same option:
// .oxlintrc.json
{
"rules": {
"no-unused-expressions": ["error", { "allowTaggedTemplates": true }],
},
}