-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
The render sequence is wrong when passing CSS classes to child #27
Comments
Hi, With TSS, if we aren't using SSR*, we can avoid using media queries altogether. *It's in my roadmap to make this approach compatible with SRR though.
import { useMemo } from "react";
import { useWindowInnerSize } from "powerhooks/useWindowInnerSize";
import { useTheme as useMuiTheme } from "@mui/material/styles";
import { createMakeAndWithStyles } from "tss-react/compat";
function useTheme() {
// gives window.innerWidth, re-render when it changes.
const { windowInnerWidth } = useWindowInnerSize();
const muiTheme = useMuiTheme();
const theme = useMemo(
() => ({
...muiTheme,
windowInnerWidth
}),
[windowInnerWidth, muiTheme]
);
return theme;
}
export const breakpointsValues = {
"sm": 600,
"md": 960,
"lg": 1280,
"xl": 1920,
} as const;
export const { makeStyles, withStyles } = createMakeAndWithStyles({ useTheme });
import { makeStyles, breakpointsValues } from "./theme";
const useStyles = makeStyles()(({ windowInnerWidth }) => ({
root: {
color: (() => {
/*
if (windowInnerWidth >= breakpointsValues.xl) {
return "red";
}
if (windowInnerWidth >= breakpointsValues.lg) {
return "cyan";
}
*/
if (windowInnerWidth >= breakpointsValues.sm) {
return "yellow";
}
return "blue";
})()
}
}));
export const ComponentB = ({ extraClasses }: { extraClasses?: string }) => {
const { classes, cx } = useStyles();
return <div className={cx(classes.root, extraClasses)}>Hi</div>;
}; We found that it's much easier to reason about responsive styles with this approach. It's also easier to debug. If you want something that could be implemented by a codemod scrip you could write this:
import { makeStyles, breakpointsValues } from "./theme";
const useStyles = makeStyles()(({ windowInnerWidth }) => ({
root: {
color: "blue",
...(windowInnerWidth >= breakpointsValues.sm ? {
color: "yellow"
} : {})
}
}));
export const ComponentB = ({ extraClasses }: { extraClasses?: string }) => {
const { classes, cx } = useStyles();
return <div className={cx(classes.root, extraClasses)}>Hi</div>;
}; Best |
@chengjiaapraamcos if the website in question is apraamcos.com.au forget about my approach. It's SSR. |
Yes bro, it is SSR and hope there can be a proper fix... Would it be an easy thing to fix or it might affect too many code? |
I think I can pull it off but it's a big feature that will require at least a day of work. In the meantime, I know it's less than ideal but you could use |
Tons of thanks mate, we will use the windowInnerWidth workaround for now and wait for the proper fix. && is not quite ideal as it needs to be added in too many places and also it could not work with the classes been passed to more than two hierarchies. |
You are welcome 👍🏻 function useWindowSize() {
const [windowSize, setWindowSize] = useState({
//It's up to you to figure out what is the best default values here
width: 0,
height: 0,
});
useEffect(() => {
//Only called on frontend
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Add event listener
window.addEventListener("resize", handleResize);
// Call handler right away so state gets updated with initial window size
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
} |
Thanks for providing this proper workaround. It's very useful but we still need to change all the breakpoints to this way which is a bit impossible for us. It's good as a short term solution so far and we are still waiting for the proper fix. Tons of thanks for all your supports so far :) |
Well understood,
Sorry to insist on the status of this issue but this is a feature request. A good feature request but a feature request still. TSS currently works as advertised.
You are welcome 😊 |
Everything should work in v1.1.0 |
Just tried, working like a charm! Tons of thanks and it's perfect now! Amazing and tons of thanks for your help!!! |
Thank you for you appreciation 😊 |
Let's assume we have ComponentA and ComponentB in different files.
Ideally when using JSS, the extraClass in ComponentA will override the root class in ComponentB which can produce the right result on screen, which is that the text should be in "red" color.
Problems:
If using clsx to merge classes, then two classes will be generated but in wrong sequence. The root class in ComponentB will always override extraClass in componentA and display "blue" color which is wrong.
If using cx to merge classes, then one merged class will be generated in right sequence, but if there's any breakpoint related css in ComponentB, then it will not work well because the css priority is always breakpoint first, so it will display "yellow" color which is also wrong.
Thanks for building this amazing package and this is the only issue we have so far.
Hope you can provide a fix or let us know if there's any workaround. Our project is huge so finding them and changing them one by one is kind of impossible..
Cheers
CJ
The text was updated successfully, but these errors were encountered: