Skip to content
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

Generate @media styles dynamically #322

Closed
acomanescu opened this issue Nov 13, 2024 · 3 comments
Closed

Generate @media styles dynamically #322

acomanescu opened this issue Nov 13, 2024 · 3 comments
Assignees
Labels
package: system Specific to @mui/system

Comments

@acomanescu
Copy link

acomanescu commented Nov 13, 2024

Summary

I have a case where I want to define my breakpoints in a JS file since CSS variables cannot be used in @media selectors and I am not able to generate the styles dynamically, even if that can be done at build time and not at runtime.

Examples

const breakpoints = {
  xs: 0,
  sm: 640,
  md: 768,
  lg: 1024,
  xl: 1280,
} as const;

const lessThan = (breakpoint: keyof typeof breakpoints) => {
  return `@media (max-width: ${breakpoints[breakpoint] - 1}px)`;
};

const greaterThan = (breakpoint: keyof typeof breakpoints) => {
  return `@media (min-width: ${breakpoints[breakpoint]}px)`;
};

export default function Layout({ children }: LayoutProps): React.JSX.Element {
  return (
    <div
      className={css({
        display: "grid",
        [greaterThan("md")]: {
          gridTemplateColumns: "var(--sidebar-width) 1fr",
        },
     } as React.CSSProperties)}
   >

Apparently this results in an error: Error: greaterThan is not defined .

I wasn't able to find any resource about this. Is it possible?

Motivation

This allows us to have a global way to define the media breakpoints.

Search keywords: styles, media

@acomanescu acomanescu added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 13, 2024
@zannager zannager added the package: system Specific to @mui/system label Nov 13, 2024
@brijeshb42
Copy link
Contributor

Have you considered adding these methods in the theme itself ? Like

const pigmentConfig = {
  theme: {
    // ... rest of the theme,
    lessThan: (breakpoint: keyof typeof breakpoints) => {
  	  return `@media (max-width: ${breakpoints[breakpoint] - 1}px)`;
	},
	greaterThan: (breakpoint: keyof typeof breakpoints) => {
	  return `@media (min-width: ${breakpoints[breakpoint]}px)`;
  },
}

Then in your source, you can do

css(({ theme }) => ({
        display: "grid",
        [theme.greaterThan("md")]: {
          gridTemplateColumns: "var(--sidebar-width) 1fr",
        },
     } as React.CSSProperties))

@brijeshb42 brijeshb42 added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 14, 2024
@acomanescu
Copy link
Author

Hi @brijeshb42 . Thank you, that's exactly what I was looking for! Works great.

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Nov 14, 2024
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@acomanescu How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system
Projects
None yet
Development

No branches or pull requests

3 participants