-
Notifications
You must be signed in to change notification settings - Fork 26.6k
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
UPDATE: Readme file update with specificity,Responsiveness and common pitfalls #3053
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,6 +9,9 @@ | |||||
1. [Nesting](#nesting) | ||||||
1. [Inline](#inline) | ||||||
1. [Themes](#themes) | ||||||
1. [Specificity](#specificity) | ||||||
1. [Responsiveness](#responsiveness) | ||||||
1. [Common Pitfalls](#common-pitfalls) | ||||||
|
||||||
## Naming | ||||||
|
||||||
|
@@ -430,3 +433,185 @@ | |||||
--- | ||||||
|
||||||
CSS puns adapted from [Saijo George](https://saijogeorge.com/css-puns/). | ||||||
|
||||||
## Specificity | ||||||
|
||||||
- Handle CSS specificity carefully to avoid conflicts. | ||||||
|
||||||
> Why? Specificity can cause styles to be overridden unintentionally. In CSS-in-JavaScript, the order in which style objects are applied matters, and higher specificity selectors can lead to unexpected results. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = { | ||||||
button: { | ||||||
color: "red", | ||||||
}, | ||||||
".specialButton": { | ||||||
color: "blue", | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = { | ||||||
button: { | ||||||
color: "red", | ||||||
}, | ||||||
specialButton: { | ||||||
color: "blue", | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
- Avoid using !important. | ||||||
|
||||||
> Why? It’s generally better to manage specificity through more precise selectors rather than forcing the style with !important. Using !important reduces maintainability by making it harder to override the styles later on. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = { | ||||||
button: { | ||||||
color: "blue !important", | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = { | ||||||
button: { | ||||||
color: "blue", | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
--- | ||||||
|
||||||
## Responsiveness | ||||||
|
||||||
- Use media queries effectively by keeping them consistent across components. | ||||||
|
||||||
> Why? Inconsistent usage of media queries can lead to a disjointed user experience. Keeping media queries defined in a centralized location (like a theme) helps maintain uniformity. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = { | ||||||
container: { | ||||||
width: "100%", | ||||||
"@media (max-width: 600px)": { | ||||||
width: "50%", | ||||||
}, | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = (theme) => ({ | ||||||
container: { | ||||||
width: "100%", | ||||||
[theme.breakpoints.small]: { | ||||||
width: "50%", | ||||||
}, | ||||||
}, | ||||||
}); | ||||||
``` | ||||||
|
||||||
- Build responsive components using percentage-based widths and dynamic units. | ||||||
|
||||||
> Why? Responsive designs should scale smoothly across devices. Using flexible units like percentages or viewport-based units ensures components can adapt to various screen sizes. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = { | ||||||
container: { | ||||||
width: "600px", | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = { | ||||||
container: { | ||||||
width: "100%", | ||||||
maxWidth: "90vw", | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
--- | ||||||
|
||||||
## Common Pitfalls | ||||||
|
||||||
- Overlapping styles: Be careful with conflicting styles in different parts of the component tree. | ||||||
|
||||||
> Why? Overlapping styles can result in conflicts that are hard to track down, especially in large applications. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = { | ||||||
button: { | ||||||
color: "red", | ||||||
}, | ||||||
container: { | ||||||
button: { | ||||||
color: "blue", | ||||||
}, | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = { | ||||||
button: { | ||||||
color: "red", | ||||||
}, | ||||||
specialButton: { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
color: "blue", | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
- Style objects with conditional logic: Avoid placing too much logic into style objects. | ||||||
|
||||||
> Why? While it's possible to use JavaScript conditions to modify styles dynamically, overusing them can lead to cluttered, hard-to-read code. | ||||||
|
||||||
```js | ||||||
// bad | ||||||
const styles = (isActive) => ({ | ||||||
button: { | ||||||
color: isActive ? "green" : "red", | ||||||
}, | ||||||
}); | ||||||
|
||||||
// good | ||||||
const styles = { | ||||||
buttonActive: { | ||||||
color: "green", | ||||||
}, | ||||||
buttonInactive: { | ||||||
Comment on lines
+582
to
+585
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, should these be class names?
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||||||
color: "red", | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
- Forgetting to centralize breakpoints: Always define breakpoints and styles centrally. | ||||||
|
||||||
> Why? Managing breakpoints centrally helps in maintaining consistent responsiveness across your app and avoids duplicate logic. | ||||||
|
||||||
```js | ||||||
Copy code | ||||||
// bad | ||||||
const styles = { | ||||||
button: { | ||||||
width: '100%', | ||||||
'@media (max-width: 500px)': { | ||||||
width: '50%', | ||||||
}, | ||||||
}, | ||||||
}; | ||||||
|
||||||
// good | ||||||
const styles = (theme) => ({ | ||||||
button: { | ||||||
width: '100%', | ||||||
[theme.breakpoints.small]: { | ||||||
width: '50%', | ||||||
}, | ||||||
}, | ||||||
}); | ||||||
|
||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this an improvement?
.specialButton
targets a class, andspecialButton
targets a nonexistent tag name. did you mean'button.specialButton'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specialButton in the second is just a property to styles. It is not a class we pass it using styles.specialButton. It is a css-in-js syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you elaborate? I’m not familiar with any css-in-js solution that conflates tag names and class names in this way.