Apply mutilple at-rules to the same element. #508
-
You can also try the following codes in this sandbox: I'm trying to apply several at-rules with different conditions to the same elements: @media (min-width: 60px) {
.my-container {
width: 60px;
}
}
@media (min-width: 400px) {
.my-container {
width: 400px;
}
} It is a quite common practice to make the element responsive. So I tried to make styles with griffel: (To make things easier to notice, I used background color instead of width in the sample.) const useStyles = makeStyles({
container: {
height: "24px",
"@media (min-width: 60px)": {
backgroundColor: "red",
},
"@media (min-width: 400px)": {
backgroundColor: "blue",
},
},
}); But the it doesn't work and the container is always in red! So I looked into the style buckets: <style media="(min-width: 400px)" data-make-styles-bucket="m"></style>
<style media="(min-width: 60px)" data-make-styles-bucket="m"></style> Aha! The bucket for rule Then I tried This is what would become if I used (input) => {
const classname1 = useResetStyles1();
const classname2 = useResetStyles2();
const classname3 = useResetStyles3();
switch (input) {
case 1:
return classname1;
case 2:
return classname2;
case 3:
return classname3;
}
} I wonder what's the best practice in this case? And are atomic at-rules safe to use? Do we need to add a warning in the document? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@wfwf1997 order of media queries should be deterministic to have deterministic results, the default comparison is intentionally not smartest as we don't want to enforce our opinions. However, you can configure how style elements will ordered, https://griffel.js.org/react/api/create-dom-renderer#comparemediaqueries |
Beta Was this translation helpful? Give feedback.
@wfwf1997 order of media queries should be deterministic to have deterministic results, the default comparison is intentionally not smartest as we don't want to enforce our opinions.
However, you can configure how style elements will ordered, https://griffel.js.org/react/api/create-dom-renderer#comparemediaqueries