-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Issue with using Responsive Grid with SSR #206
Comments
Hi @dukesx, thanks for creating such detailed issue, I was planning to implement something like this, but did not have enough time, if you would like to work on this ticket that is great, feel free to reach out here or on Discord if you need any help. |
I started with implementing something similar to the Ant Design approach, but there are a couple issues I stumbled upon: If I use a function to pass props to the media queries, only the first media query styles will be applied. This seems to be a bug in JSS: cssinjs/jss#1343 My current approach: export default createMemoStyles({
root: ({ columns, spacing, grow, span }: ColStyles) => ({
...mediaQueryStyles(columns, spacing, grow, span),
}),
[`@media (min-width: ${xs}px)`]: {
root: ({ breakpoints, columns, spacing, grow }: ColStyles) => ({
...mediaQueryStyles(columns, spacing, grow, breakpoints.xs),
}),
},
[`@media (min-width: ${sm}px)`]: {
root: ({ breakpoints, columns, spacing, grow }: ColStyles) => ({
...mediaQueryStyles(columns, spacing, grow, breakpoints.sm),
}),
},
[`@media (min-width: ${md}px)`]: {
root: ({ breakpoints, columns, spacing, grow }: ColStyles) => ({
...mediaQueryStyles(columns, spacing, grow, breakpoints.md),
}),
},
}); A lot of hardcoding, but sadly the only option I see right now. The solution seems to work though, but you get a lot of stylings / empty classes with this approach, which looks ugly. @rtivital: I extended the default theme with a breakpoints object. The problem is, that I can't use the theme prop inside the media query to get the breakpoints, since as stated above, media queries are bugged inside functions. Something like this does not work: export default createMemoStyles({
root: ({ theme, breakpoints, columns, spacing, grow, span }: ColStyles) => ({
...mediaQueryStyles(columns, spacing, grow, span),
[`@media (min-width: ${theme.breakpoints.xs}px)`]: {
...mediaQueryStyles(columns, spacing, grow, breakpoints.xs),
#},
}),
}); |
Hi @Everek, thanks for trying things out, I would not count on jss team resolving this issue soon, so I would propose working on a workaround. Since the media queries are bugged in jss we can use export function Grid({
themeOverride,
gutter = 'md',
children,
grow = false,
justify = 'flex-start',
align = 'stretch',
style,
columns = 12,
className,
breakpoints,
...others
}: GridProps) {
const theme = useMantineTheme(themeOverride);
const spacing = getSizeValue({ size: gutter, sizes: theme.spacing });
const cols = (Children.toArray(children) as React.ReactElement[]).map((col, index) =>
React.cloneElement(col, { gutter, grow, columns, key: index })
);
return (
<>
<style>
{`
@media (max-width: ${breakpoints.sm}px) {
.mantine-grid {
// grid styles here
}
.mantine-col {
// col styles here
}
}
`}
</style>
<div
style={{
margin: -spacing / 2,
display: 'flex',
flexWrap: 'wrap',
justifyContent: justify,
alignItems: align,
...style,
}}
className={cx('mantine-grid', className)}
{...others}
>
{cols}
</div>
</>
);
} It's nasty but should do the trick |
Good idea! I will try to work with that. You can assign the ticket to me if you want. |
Okay, thanks! |
Pull request has been opened: #230 |
Released in 2.4.0 |
Issue:
Using UseMediaQuery to support responsive layouts is a bad idea. Its just not worth the pain and effort. Media Query api doesn't works on server , which means if you are a Next JS guy, you are stuck with manually adding responsive classes in the CSS or inline or wait for someone to manually change viewport width.
Now thats alot of work, fortunately, someone has thought of this already and Ant design, has a good way of handling this. They Provide Breakpoints such as xs, sm, md,lg,xl,xxl on the grid's property to handle this on server and the client side. They do it on css side too but then its also handled on js side shall the viewport width change. Check this out:
https://ant.design/components/grid/
Thanks for reaching out!
Please provide following information:
Core
Latest
All Major
No need, issue is clear
One work around is that you can check ant design grid system, supports SSR, allows you to specify gutter and grid size for each breakpoint
-Of course
The text was updated successfully, but these errors were encountered: