naive-ui/design-notes/how-to-name-a-style-var.md

51 lines
919 B
Markdown
Raw Normal View History

2020-11-12 19:32:08 +08:00
# How to Name a Style Var
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
For example, you have a button, which has `default` and `error` type. How will you name the background color of the button.
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
```
buttonColor
errorButtonColor
```
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
or
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
```
buttonColor
buttonColorError
```
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
The second is the better (The second is easy to extend and read. Also it follow the css pattern for example `padding-right` and `padding-left`).
When it comes to hover style?
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
```
buttonColor
buttonColorHover
buttonColorError
buttonColorErrorHover <-
```
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
or
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
```
buttonColor
buttonColorHover
buttonColorError
buttonColorHoverError <-
```
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
The second is the better since if you want write a map entity, you always loop on types. For example:
2021-02-05 11:57:41 +08:00
2020-11-12 19:32:08 +08:00
```js
// better
2021-02-05 11:57:41 +08:00
;['Default', 'Error'].map((type) => [
2020-11-12 19:32:08 +08:00
'buttonColor' + type,
'buttonColorHover' + type
])
// worse, hard to maintain
2021-02-05 11:57:41 +08:00
;['Default', 'Error'].map((type) => [
2020-11-12 19:32:08 +08:00
'button' + type + 'Color',
'buttonHover' + type + 'Color'
])
2021-02-05 11:57:41 +08:00
```