-
-
Notifications
You must be signed in to change notification settings - Fork 522
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
Support for css modules in jsx/tsx #589
Comments
In new implementation it’s supported as |
I was thinking this should be some kind of config, not really messing with the basic abbreviation itself (which I agree would be a disaster). I also think that creating a custom syntax that produces this behavior is undesirable, even if the syntax is extremely simple. A very simple idea I had would be a user-provided post-expansion function, where it takes the expanded abbreviation as input, and produces an arbitrary string as output, that then replaces the expanded abbreviation. We already have the concept of filters/actions and the This could be generalized to cover many use cases, not just this one. For instance it would be relatively trivial to convert |
As an aside, my workaround for this issue is:
This is obviously an ugly hack that has a bazillion problems (such as trashing your cursor position and needing to be run manually instead of after expansion) |
In new Emmet, abbreviation is parsed into AST so you can walk over abbreviation nodes and rewrite it as you like. The problem is that Emmet has multiple implementations: in JS and Python (also unofficial Java implementation in WebStorm) so there’s no single solution. A custom option might work here but for now it will be applied globally to Emmet, not per-project |
A custom option that is applied globally would work for me. I mean vscode already has workspace-specific customizability for emmet (AFAIK) so I would still reap the benefits. Also, even though I need emmet to expand A way to hook into the AST and modify the nodes myself would also be perfectly fine, I imagine I could write the transformer easily enough Is the transformation from |
This setting would affect JSX context only, e.g. your HTML and Django templates would behave as usual. Also, new Emmet will use web-based UI for config, although JSON config in editor might still be possible |
This feature will be super useful for me too. thanks |
Absolutely need this feature. Also |
How about |
@svobik7 sounds good. I guess the only possible value here is a literal? You won't type, like, string here? Something like |
@sergeche sure. If multiple classNames are needed it could work same as single dot statement:
But I think that the second use-case is out of the scope of this issue for now. |
I guess you could, since kebab class names have to be transformed to The multiple classes would be nice to have but I can live without. I'd also prefer it transformed to |
Heres an extension that I found that converts to css modules syntax. Found this to be the best solution until we get emmet support. https://marketplace.visualstudio.com/items?itemName=jingxiu.CSS-Modules-transform |
Excuse me, is this problem being handled? BTW, is there any way to uses
|
Any official progress? |
@oychao not yet, but thanks for reminding me, will take a look |
@sergeche Any chance I may remind you of this once again? Would really appreciate the feature! |
Just imagine how many people are writing react jsx/ tsx on daily basis. How many of them are using module css? Probably a lot. And the best way I could find thus far in VSCode is to write div.{styles.myClass which turns into the complete .Now I am also getting a warning when using NX monorepo scaffolding to generate libs in TypeScript that the above should be written as <div className={styles['myClass']}> which I cannot get with emmet at all.. it's a pain :) In order to at least get rid of the lint warning for the time being, we can remove "noPropertyAccessFromIndexSignature": true from the tsconfig of the library. Hopefully emmet will properly support module css in react at some point. |
Would be cool to be able to customize things like this with plugins. Then using styleName prop with react-css-modules would be easy, just replacing className with styleName. Or someone could use other name for css import, not just |
Hi man, have you some news ? 🙄 |
Any progress?? |
There’s already some basic JSX support. Let’s sum up what features should be implemented:
That’s right? |
and for vue 😄
|
I just pushed update (not released yet) which adds the following:
Summing up, by default Emmet will work as follows: equal(expand('.bar', { syntax: 'jsx' }), '<div className="bar"></div>');
equal(expand('..bar', { syntax: 'jsx' }), '<div styleName={styles.bar}></div>');
equal(expand('..foo-bar', { syntax: 'jsx' }), '<div styleName={styles[\'foo-bar\']}></div>');
equal(expand('.foo', { syntax: 'vue' }), '<div class="foo"></div>');
equal(expand('..foo', { syntax: 'vue' }), '<div :class="foo"></div>'); See https://github.com/emmetio/emmet/blob/master/test/expand.ts#L106-L114 And you can customize attribute mapping and value prefixes with jsx: {
options: {
'jsx.enabled': true,
'markup.attributes': {
'class': 'className',
'class*': 'styleName',
'for': 'htmlFor'
},
'markup.valuePrefix': {
'class*': 'styles'
}
}
},
vue: {
options: {
'markup.attributes': {
'class*': ':class',
}
}
} https://github.com/emmetio/emmet/blob/master/src/config.ts#L390-L409 If this is the expected behaviour, I’ll release it as next version |
@sergeche that looks great. Considering most projects exclusively use one strategy for component classes it might make sense to let folks re-map the default, single period behavior ( |
With this update, you’ll be able to re-map |
I've always used jsx: {
options: {
'jsx.enabled': true,
'markup.attributes': {
'class': 'className',
'class*': 'className',
'for': 'htmlFor'
},
'markup.valuePrefix': {
'class*': 'styles'
}
}
}, right? Also it would be cool if we could map |
@gabriel-peracio Yes, your example is correct. As for |
@sergeche |
@msobkyy it’s not released yet. Will report here as soon as new version comes out |
Just released v2.4.0 of Sublime Text plugin which features CSS modules support: https://github.com/emmetio/py-emmet/blob/master/CHANGELOG.md#120-2023-01-19 JS version (for VSCode etc.) coming soon. ST users, please check if this feature works as expected or can be improved |
Hi, thanks your great work. I found that, the current way to use CSS Module on jsx is a little complex. <div.{styles.foo} // <div className={styles.foo}></div> and it's seem that, there is no way to customize for that use case, can we? Just want to be able to config <div..foo // <div className={styles.foo}></div> Thanks. |
@iahu did you used CSS module feature in Sublime Text editor? It should work as expected if you configure it properly |
@sergeche Thanks your replay. Yes, I use it in ST. after your prompt and read this document , I add two configures on my editor. {
"config": {
"markup.attributes": {
"class*": "className"
},
"markup.valuePrefix": {
"class*": "styles"
},
}
} the |
finally find the way to config it, put the {
"config": {
"markup.valuePrefix": {
"class*": "styles"
},
"jsx": {
"options": {
"markup.attributes": {
"class*": "className"
},
"output.selfClosingStyle": "xhtml",
},
},
},
} |
@sergeche was just wondering, is this in vscode yet? |
@Jack-Castify just published v2.4.0 of Emmet npm package which includes this feature. It’s up to VSCode devs to update package and support this feature |
Please note, I have used At the moment Do you think we adjust I like the idea that I'm a very happy user of Emmet and I didn't even know |
@scottwillmoore you want Emmet to output |
Yep! For . <div className="*"></div>
.foo <div className="foo">*</div>
.foo.bar <div className="foo bar">*</div>
.. <div styleName={*}></div>
..foo <div styleName={styles.foo}>*</div>
..foo..bar <div styleName={classNames(styles.foo, styles.bar)}>*</div> The In addition, provide configuration to adjust .. <div className={*}></div>
..foo <div className={styles.foo}>*</div>
..foo..bar <div className={cx(styles.foo, styles.bar)}>*</div> What do you think? EDIT: These edge cases should probably be considered... I guess just expand to .foo..bar <div className="foo bar">*</div>
..foo.bar <div styleName={styles.foo, styles.bar}>*</div> |
I guess it will require a separate config in Also note that for JSX, a special abbreviation type is supported: you can type As for |
Okay awesome, will look into it. Is everyone happy with this suggestion if I move forward on it? Also, I am aware of I think .{} <div className={}>*</div>
.{foo} <div className={foo}>*</div>
.{styles.foo} <div className={styles.foo}>*</div> |
I think in |
Hello, sorry if this is the wrong place, but there is there any configuration to make this work with vanilla Current Behavior: Expected Behavior: |
@jcnevess no, such output is not supported |
.foo
<div className="foo"></div>
<div className={styles.foo}></div>
Sorry for reopening this issue, but #533 was closed by the author and I didn't want it to quietly disappear. I've wanted this for years and haven't found a clean solution yet. I took a look at the emmet source code and I can't think of a clean way to implement this myself.
@sergeche you mentioned you would try to implement this in an upcoming emmet version - did you manage to do it?
The text was updated successfully, but these errors were encountered: