-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Make theme.palette.augmentColor() pure #12390
Comments
@designjudo Here is how we handle the problem on our product. import { createMuiTheme } from '@material-ui/core/styles'
import deepmerge from 'deepmerge'; // < 1kb payload overhead when lodash/merge is > 3kb.
const rawTheme = createMuiTheme()
const warning = {
main: '#',
}
rawTheme.platte.augmentColor(warning)
const theme = deepmerge(rawTheme, {
palette: {
warning,
},
})
For this one, we have been wrapping most of the Material-UI components to add or remove some properties. |
Currently we've been rewriting the theme object with an extended palette, so:
So, I guess I could just call the shades when I do this?
|
@designjudo I'm not sure to understand your second point. I think that I have provided guidance on the best strategy for now in #12390 (comment). Using an extended palette is fine. The only sad thing about |
Regarding the typings of augmentColor, there is an error. The method, as shown in @oliviertassinari example may accept a single parameter, and this parameter can be a SimplePaletteColorOptions, as specified, but also a full Color. The definition should be: augmentColor: (
color: PaletteColorOptions,
mainShade?: number | string,
lightShade?: number | string,
darkShade?: number | string,
) => void; (note the color type and the question marks in the parameters) File: core/styles/createPalette.d.ts Please comment if you prefer a separate issue and/or a PR. |
@sveyret I confirm. Do you want to submit a pull request with the fix? :) |
OK, I'll do it in a few minutes… |
Here it is: #13376 |
It would probably be better if augmentColor was a stand-alone exported function, so you didn't have to create a dummy |
@jacobweber I'm not sure it will be possible, the const theme = {
palette: {
// Used by `getContrastText()` to maximize the contrast between the background and
// the text.
contrastThreshold = 3,
// Used by the functions below to shift a color's luminance by approximately
// two indexes within its tonal palette.
// E.g., shift from Red 500 to Red 300 or Red 700.
tonalOffset = 0.2,
},
}; |
@oliviertassinari Would the scope of this change to Currently if I pass in an object to createMuiTheme with a palette that only defines the |
@ryancogswell What side effects do you have in mind? Oh yes, it's because of augmentColor :/. |
@oliviertassinari I'll get a pull request for this sometime in the next week. |
<!-- Thanks so much for your PR, your contribution is appreciated! ❤️ --> - [X] I have followed (at least) the [PR section of the contributing guide](https://github.com/mui-org/material-ui/blob/master/CONTRIBUTING.md#submitting-a-pull-request). This change avoids mutating the `color` argument to augmentColor. Includes the corresponding typescript change to indicate return type for augmentColor and test coverage of the typescript and code changes. Closes #12390 ### Breaking change The `theme.palette.augmentColor()` method do no longer perform a side effect on its input color. In order to use it correctly, you have to use the output of this function. ```diff -const background = { main: color }; -theme.palette.augmentColor(background); +const background = theme.palette.augmentColor({ main: color }); console.log({ background }); ```
We love the theme object, but starting to see how it could be extended.
• In most cases, a primary, secondary, error and grey color palette will support most applications.
• I am having to add custom colors to the theme to cover a warning situation.
Rather than extend the theme to custom code new palettes, why not use the power of this theme to handle warning color just like error?
Also, I would like to see warning and error as color props for the Button component:
This will greatly reduce the amount of code developers are having to write for more use cases for buttons, and use the power of the theme to consistently style applications.
Great work!!
The text was updated successfully, but these errors were encountered: