-
Notifications
You must be signed in to change notification settings - Fork 265
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
Allow styling of components #142
Comments
I was able to accomplish something similar via global selectors. For example
|
Thanks, that works. Would be nice to have something simpler like my examples though. |
@lorensr I'm trying to stick as much as possible to "just CSS". Since we're inverting the CSS model (everything is scoped by default, no globals), we needed syntax for opting into globals, and that's the only reason I broke that rule with So, arguably our current solution is simpler, just not necessarily easier. I'd be open to exploring that model though in other modules, or maybe a fork of |
Thanks @zachdixon for helping out. I purposefully also put this example in the README, referencing how I'd style |
This still really needs improvement (or you should switch away from When you're using something like |
Is there a chance to reopen this? Possible use case is individually styling one off components without impacting the styles globally. I managed to do this by using |
We recently had this request in mui/material-ui#20787: The best solution I could come up with is to do: import React from 'react';
import Typography from '@material-ui/core/Typography';
export default function Index() {
return (
<div className="root">
<Typography variant="h4" component="h1">
Next.js example
</Typography>
<style jsx>{`
.root :global(h1) {
background: red;
}
`}</style>
</div>
);
} which is basically what was proposed 3 years ago by @zachdixon in #142 (comment). Note that I have used this starting point: https://github.com/mui-org/material-ui/tree/master/examples/nextjs. |
Hi, I also struggle to find the best pattern to handle global styling. I try to favour styled-jsx against Material UI as much as possible, so I can mix non material-ui components with mui components and have consistent styling. This is how I try to define the global style, and override MUI defaults. const AppLayout = ({ children }: AppLayoutProps) => (
<div className="global-style">
{children}
<style jsx global>{`
.global-style h1, .global-style .MuiTypographi-h1 {
font-size: 3.815em;
/* 61px */
}
h2, .MuiTypography-h2 {
font-size: 2.441em;
/* 29px */
}
... The first one will work, but feels clumsy. A palliative would be using PostCSS, which allows a cleaner nesting. Ok, but not really simple. The second one won't have enough specificity, Material UI style is added later in the tree so I can't override it. Third alternative is using theme overrides. Works perfect, but it makes me duplicate my style, both for Any thoughts on this? |
PostCSS with nesting is better, as you just wrap the whole style with a class to get +1 specificity, but breaks VS code. There does not seem to be a "styled-jsx + postcss" plugin yet. Basically the downside is the need to use another 3rd party lib.
Same pattern applies to "scoped" style that you may want to apply to children. Edit: just for the record, it works fine with Storybook too, by just setting up Babel config and post css config correctly for the whole project. Haven't really tested with Jest yet but it did not seem to break my tests. Edit 2: I guess you also have to keep the ".global" className unique each time you use this pattern. In my scenario it could be Edit 3: using <style jsx>
{`
& :global(.MuiButton-root) {
color: ${someColorFromCustomProps};
}
`}
</style> The & seems sufficient to isolate the component. If I remove it, there are clashes between instances of the same component, if I add it everything is fine. |
Actually when I don't use a export const Button = (props: ButtonProps) => {
const { preferredTechnology, ...muiButtonProps } = props;
return (
<MuiButton
{...muiButtonProps}
className={clsx("vns-button", props.className)}
>
{props.children}
<style jsx>
{`
& :global(.MuiButton-root) {
color: ${technologyToColor[preferredTechnology] || colors.blueNext};
}
`}
</style>
</MuiButton>
);
}; Also tried a simpler style, eg It works if I use a basic HTML button however, so the style or my components do not seem to be faulty. @oliviertassinari do you have any insight on this? Your example is using a (I also tried using a fragment but then styled-jsx has nothing to attach the class to) Edit: I think having a wrapping |
@eric-burel I came to the same conclusion when experimenting with different options. |
Both of these, when rendered, become an unstyled div:
It would be nice if there were a way to style the div that
<Paper>
becomes.The text was updated successfully, but these errors were encountered: