How to use named space
values with breakpoints?
#2344
-
I've been using named values in the
That's been working fine. Then I wanted to add different values for different breakpoints, and tried to do this:
I thought it was working initially, as I think I was editing the wrong item in the arrays. But it looks like it's not doing the right thing, which would be to pick the value based on the current breakpoint, just like when supplying an array to an sx value, like What it seems to be doing instead is repeating the property with each of the values from the array, which seems pretty unhelpful. Why would you want to add a property and then immediately override it again with a different value? Is this a bug? Is there any way to get the desired behavior of selecting the named space value based on the current breakpoint? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hey @fwextensions! Thanks for the issue. Responsive tuples can be passed in {
colors: {
gray: ['#111', '#333', '#aaa', '#eee'] // here a scale with gray.0, gray.1, gray.2, and gray.3
},
space: {
sm: ['1rem', '2rem'] // here, a responsive tuple... or a scale with sm.0, and sm.1 design tokens?
}
} If you're sure you'll always want to use a value like this you can export it use it when defining styles. export const Space = {
sm: ["1rem", "2rem"],
md: ["2rem", "3rem"],
};
<div sx={{ p: Space.sm }} /> |
Beta Was this translation helpful? Give feedback.
-
FWIW, this is the code I'm currently using, with a simple class added to track and encapsulate the variable creation, so it's easier to do in multiple places. class Variables {
constructor() {
this.vars = new Map();
}
add(name, value) {
const varName = `--${name}`;
const varCall = `var(${varName})`;
this.vars.set(varName, value);
return varCall;
}
object() {
return Object.fromEntries(this.vars);
}
}
const remUnits = 1.5;
const vars = new Variables();
const space = [
["none", [0, 0]], // 0 0
["xs", [.25, .5]], // 3.75px 9px
["sm", [.5, 1]], // 7.5px 18px
["md", [1, 2]], // 15px 36px
["lg", [2, 3]], // 30px 54px
["xl", [3, 5]], // 45px 90px
["xxl", [4, 8]], // 60px 144px
]
.reduce((result, [key, values]) => {
const negKey = `neg-${key}`;
const cssValues = values.map((value) => `${value * remUnits}rem`);
const negativeCSSValues = cssValues.map((value) => `-${value}`);
result[key] = vars.add(key, cssValues);
result[negKey] = vars.add(negKey, negativeCSSValues);
return result;
}, {});
const fontSizes = [ ... ];
fontSizes.body = vars.add("body", [fontSizes[4], fontSizes[6]]);
const styles = makeStyles({
root: {
...vars.object(),
},
... |
Beta Was this translation helpful? Give feedback.
Hey @fwextensions! Thanks for the issue.
Responsive tuples can be passed in
sx
prop when defining styles, but not when defining thetheme
, because the theme allows arrays as nested scales, and it's not possible to discern between them.If you're sure you'll always want to use a value like this you can export it use it when defining styles.