我们也许会使用 Suit 命名规范去进行样式命名,这样我们的 HTML 和 CSS 代码看起来就像如下所示:
/* components/submit-button.css */.Button { /* all styles for Normal */ }.Button--disabled { /* overrides for Disabled */ }.Button--error { /* overrides for Error */ }.Button--in-progress { /* overrides for In Progress */
这样写看起来还挺棒的。使用 BEM 命令方式使我们有了 4 个样式变量这样我们不必使用嵌套选择器。使用Button这种首字母大写的方法可以很好的避免与之前的代码或者是其他的依赖代码进行冲突。另外我们使用了--语法这样能很清楚的显示出我们的依赖 Class。
/* components/submit-button.css */.normal { /* all styles for Normal */ }.disabled { /* all styles for Disabled */ }.error { /* all styles for Error */ }.inProgress { /* all styles for In Progress */
/* components/submit-button.css */.normal { /* all styles for Normal */ }.disabled { /* all styles for Disabled */ }.error { /* all styles for Error */ }.inProgress { /* all styles for In Progress */
/* Don't do this */`class=${[styles.normal, styles['in-progress']].join(" ")}`/* Using a single name makes a big difference */`class=${styles['in-progress']}`/* camelCase makes it even better */`class=${styles.inProgress}`
.common { /* all the common styles you want */}.normal { composes: common; /* anything that only applies to Normal */}.disabled { composes: common; /* anything that only applies to Disabled */}.error { composes: common; /* anything that only applies to Error */}.inProgress { composes: common; /* anything that only applies to In Progress */}
.element { composes: large from "./typography.css"; composes: dark-text from "./colors.css"; composes: padding-all-medium from "./layout.css"; composes: subtle-shadow from "./effect.css";}
/* this short hand: */.element { composes: padding-large margin-small from "./layout.css";}/* is equivalent to: */.element { composes: padding-large from "./layout.css"; composes: margin-small from "./layout.css";}
这开辟了一种可能,使用**极细粒**的类样式定义一些样式别名去给每一个网站使用:
.article { composes: flex vertical centered from "./layout.css";}.masthead { composes: serif bold 48pt centered from "./typography.css"; composes: paragraph-margin-below from "./layout.css";}.body { composes: max720 paragraph-margin-below from "layout.css"; composes: sans light paragraph-line-height from "./typography.css";}