简体   繁体   English

使用 CSS-in-JS 时,我应该为 CSS 类使用哪种外壳?

[英]Which casing should I use for CSS classes when using CSS-in-JS?

I've read that the CSS-spec does not specify case-sensitivity, which has led to some browsers treating, for example, .myheader as equal to .myHeader .我读过 CSS 规范没有指定区分大小写,这导致某些浏览器将.myheader视为等于.myHeader But since the standard in Javascript is to write variables in camelcase, how do I reconcile these two when using CSS-in-JS?但是由于Javascript中的标准是用驼峰写法编写变量,那么在使用CSS-in-JS时如何协调这两者呢?

My React code could look like this:我的 React 代码可能如下所示:

import style from './style.module.css'
//...
<Button className={style.myButton} />

And the CSS would then break casing-convention:然后 CSS 将打破套管惯例:

.myButton {
    background-color: blue;
}

OR my React code could look like this (which is kinda ugly):或者我的 React 代码可能看起来像这样(这有点难看):

import style from './style.module.css'
//...
<Button className={style['my-button']} />

And the CSS would then follow the kebab-case convention:然后 CSS 将遵循 kebab-case 约定:

.my-button {
    background-color: blue;
}

Ultimately it is up to you.最终取决于你。 If you are the only person who will be working on the project it doesn't matter.如果您是唯一参与该项目的人,那没关系。 If you are working with a team, discuss with them their preference.如果您与团队合作,请与他们讨论他们的偏好。

Personally, I've used all forms across my projects.就个人而言,我在我的项目中使用了所有形式。 It just depended on what looked right or felt right with the code I was working with, like in your case where you've mentioned kebab-case looks a bit ugly in amongst your React code.它只取决于我正在使用的代码看起来正确或感觉正确,就像你提到的烤肉串在你的 React 代码中看起来有点难看一样。

Best practice would suggest kebab-case;最佳实践建议使用烤肉串; but yeah, you don't have to, however, kebab-case may not work so well in the instance of modules/objects ( className={style.like-this-example} ).但是的,你不必然而,烤肉串情况可能不会在模块/对象的实例(工作这么好className={style.like-this-example} )。

While agree with @Dexterians, its a choice that as team we should make.虽然同意@Dexterians,但作为团队我们应该做出选择。 However, I would give preference to naming conventions over it's usage in a specific language.但是,我更喜欢命名约定而不是它在特定语言中的用法。 To me I would choose class name my-button over myButton and then usage style['my-button']对我来说,我会在 myButton 上选择类名 my-button,然后使用 style['my-button']

This way we stay consistent on names irrespective of language constraint.这样我们就可以在名称上保持一致而不受语言限制。

Wut?唔? CSS is not case sensitive? CSS 不区分大小写?

I think you need to read.我认为你需要阅读。

Are class names in CSS selectors case sensitive? CSS 选择器中的类名是否区分大小写?

Are CSS selectors case-sensitive? CSS 选择器是否区分大小写?

You've kind of contradicted yourself @Anders - while you're trying to follow conventions - you're doing as @Dexterians and @Bharat have said, doing your own thing that suits your needs :P你有点自相矛盾@Anders - 当你试图遵循惯例时 - 你正在按照@Dexterians 和@Bharat 所说的那样做,做适合你需要的自己的事情:P

What is "convention" to one developer may not neccessaily be "convention" to another.对一个开发人员来说是“约定”的东西不一定对另一个开发人员来说就是“约定”。 Alas why you need to work with your team.唉,为什么你需要与你的团队合作。

In React first one always works in case of modules, so you do not need to worry about it.在 React 中,第一个总是适用于模块,所以你不需要担心它。 So, going with the below convention works fine.因此,遵循以下约定可以正常工作。

React:反应:

import style from './style.module.css'
//...
<Button className={style.myButton} />

CSS CSS

.myButton {
    background-color: blue;
}

Refer to this Medium link for more insight: CSS in JS有关更多见解,请参阅此 Medium 链接: JS 中的 CSS

kebab-case is preffered and suggested by people for casing CSS classes.人们喜欢并建议使用 kebab-case 来封装 CSS 类。 You can read here why it should be used 你可以在这里阅读为什么应该使用它

You can use it in react like this:您可以像这样在反应中使用它:

style['my-button']

I know you said it is ugly.我知道你说它很丑。 But it is the only way.但这是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM