Switching between dark/light style overrides - material-ui

I want to override the color of some icons based on theme.type to a native PaletteColor. I can check the type and conditionally set the color like this:
const useStyles = makeStyles((theme: Theme) =>
createStyles({
someIcon: {
color:
theme.palette.type === "dark"
? theme.palette.warning.dark
: theme.palette.warning.light,
},
})
);
This is pretty verbose every time I want to override something though, so it can be refactored to themeColor below:
const themeColor = (theme: Theme, color: PaletteColor) =>
theme.palette.type === "dark"
? color.dark
: color.light;
const useStyles = makeStyles((theme: Theme) =>
createStyles({
someIcon: {
color: themeColor(theme, theme.palette.warning)
},
})
);
Which now makes me think, does Material-UI have some native helper that already does this?

I digged in the Material-UI source code for a while and here is what I found:
You can see any potential helper methods related to Material-UI style here.
There is this commit that changed palette.type to palette.mode, if you Ctrl+F to search for 'light' ? or 'dark' ?, you'll see that they also use ternary operator to assign the colors based on the dark | light mode so I doubt they have any helper method like what you described in the second snippet.

Related

How to change the look of overdue dates with flutter CupertinoDatePicker?

I don't know how to change the grey color of overdue dates. For me there is not enough contrast between past date, future date and today's date.
Looking at the source code of of CupertinoDatePicker, it seems that the colors are not easily changeable.
To quote from flutter's source code:
TextStyle _themeTextStyle(BuildContext context, { bool isValid = true }) {
final TextStyle style = CupertinoTheme.of(context).textTheme.dateTimePickerTextStyle;
return isValid
? style.copyWith(color: CupertinoDynamicColor.maybeResolve(style.color, context))
: style.copyWith(color: CupertinoDynamicColor.resolve(CupertinoColors.inactiveGray, context));
}
Therefore, the only direct impact you have on the colors is by wrapping your CupertinoDatePicker inside a CupertinoTheme and providing a value for textTheme.dateTimePickerTextStyle . Further customisation is not supported as of yet
Since there is no option to change the overlay color from the CupertinoDatePicker widget constructor, you will need to change the overlay color from its source code.
First, we will take an example of CupertinoDatePicker like this:
// ...
child: CupertinoDatePicker(
onDateTimeChanged: (date) {},
),
// ...
And it shows this on the screen:
Going inside its source code, you need to search for those lines:
const Widget _startSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capEndEdge: false);
const Widget _centerSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capStartEdge: false, capEndEdge: false);
const Widget _endSelectionOverlay = CupertinoPickerDefaultSelectionOverlay(capStartEdge: false);
This represents the implementation of that grey overlay that you see on the screen.
Then, you need to go through the CupertinoPickerDefaultSelectionOverlay source code also, you should find this:
/// default margin and use rounded corners on the left and right side of the
/// rectangular overlay.
/// Default to true and must not be null.
const CupertinoPickerDefaultSelectionOverlay({
super.key,
this.background = CupertinoColors.tertiarySystemFill,
this.capStartEdge = true,
this.capEndEdge = true,
}) : assert(background != null),
assert(capStartEdge != null),
assert(capEndEdge != null);
We want to change the color of that overlay background, so in this line:
this.background = CupertinoColors.tertiarySystemFill,
We will change its value with the Color we want, so as an example we will change it like this:
this.background = const Color.fromARGB(49, 0, 253, 30),
and after a hot restart, the result will be:
Notes:
If you want to use material colors like Colors.red,
Colors.green..., you will need to import the material library on
the top of that file.
You should mark the color with a const as I did in the sample of
code, otherwise, you will get an error.

Material-ui 5, unable to customize theme through ThemeProvider

I have recently started using Material ui (v5) and I am facing some issues with theme customization. I want to change the font-family for all the components through the ThemeProvider and creating a new theme. I am able to see the theme in the react inspector with the correct values but those values don't get applied to the components (specifically fontFamily but nothing works really).
This is what my ThemeProvider looks like:
import React from "react";
import {
createTheme,
ThemeProvider as MUIThemeProvider,
} from "#mui/material/styles";
const theme = createTheme({
typography: {
fontFamily: "serif",
},
});
type ThemeProviderProps = {
children: React.ReactNode;
};
const ThemeProvider = ({ children }: ThemeProviderProps) => (
<MUIThemeProvider theme={theme}>{children}</MUIThemeProvider>
);
export default ThemeProvider;
The ThemeProvider then wraps every story (using storybook):
<div
style={{
height: "100%",
width: "100%",
}}
>
<ThemeProvider>
<Story {...context} />
</ThemeProvider>
</div>
);
export const decorators = [withThemeProvider];```
I am able to see the theme I created if I use the useTheme hook or in the ThemeProvider in the components inspector (please ignore the different fontFamily in the screenshot below):
When I look at the actual component I see that it is still getting the default styles:
I am sure I am doing something stupid somewhere?

Is there any quick way to implement a Material-UI theme?

I am aware of custom theming process within react-admin.
I was wondering if in react-admin it is possible to implement a custom theme without needing to go component by component and theming them ourselves. Something like for example Material Ui premium themes (see here)
Yes, definitely. You can pass a custom theme to the <Admin> component:
const App = () => (
<Admin theme={myTheme} dataProvider={simpleRestProvider('http://path.to.my.api')}>
// ...
</Admin>
);
Here is an example custom theme:
import { defaultTheme } from 'react-admin';
import merge from 'lodash/merge';
import indigo from '#material-ui/core/colors/indigo';
import pink from '#material-ui/core/colors/pink';
import red from '#material-ui/core/colors/red';
const myTheme = merge({}, defaultTheme, {
palette: {
primary: indigo,
secondary: pink,
error: red,
contrastThreshold: 3,
tonalOffset: 0.2,
},
typography: {
// Use the system font instead of the default Roboto font.
fontFamily: ['-apple-system', 'BlinkMacSystemFont', '"Segoe UI"', 'Arial', 'sans-serif'].join(','),
},
overrides: {
MuiButton: { // override the styles of all instances of this component
root: { // Name of the rule
color: 'white', // Some CSS
},
},
},
});
You can find more information at https://marmelab.com/react-admin/Theming.html#writing-a-custom-theme, and a complete example of custom admin theme in the e-commerce demo (https://github.com/marmelab/react-admin/blob/master/examples/demo/src/layout/themes.ts).

Why are colours lighter when using my own theme in Flutter?

I have created my own theme in Flutter but when I try to use these colours they are much lighter on the emulator. I'm new to Flutter so I'm not sure of the best way to create a theme but the colours do appear. What is the best way to set your own theme or why would the colours be lighter?
import 'package:flutter/material.dart';
extension CustomPrimaryColorScheme on ColorScheme {
Color get primaryColor => const Color(0xfffa4659);
Color get accentColor => const Color(0xfff0fff3);
Color get watermelonLight => const Color(0xffFFF0F1);
}
extension CustomColorScheme on ColorScheme {
Color get aqua =>const Color(0xff11cbd7);
Color get paleAqua => const Color(0xffc6f1e7);
Color get ice => const Color(0xfff0fff3);
Color get watermelon => const Color(0xfffa4659);
Color get brownGrey => const Color(0xff979797);
Color get charcoal => const Color(0xff313939);
}
backgroundColor: Theme.of(context).colorScheme.watermelon;

material-ui theme not applied automatically on AppBar?

I'm trying to integrate material-ui to my project and I have some issues with custom theme settings
I created a custom theme this way
App.js
const theme = createMuiTheme({
palette: {
primary: green,
secondary: red,
},
});
class App extends Component {
render() {
return (
<MuiThemeProvider theme={theme}>
<BrowserRouter>
<Switch>
...}}
Then in a component in the substructure I create some specific css.
Now my issue is that I'm obliged to define style appBar with a backgroud color and apply it explicitely on the AppBar component. If I don't do one of these two operations, the bg of the appBar remains light gray
What is weired is that I get the correct green from theme.palette.primary["500"], which means the theme is correctly configured
Header.js
const styles = theme => ({
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
appBar: {
backgroundColor: theme.palette.primary["500"]
},//...)}
class Header extends Component {
constructor(props) {
super(props)
this.classes = props.classes
}
render() {
return (<I18n>
{(tsl, {i18n, t, ready}) => {
return (
<div className={this.classes.root}>
<AppBar position="static" color="default" className={this.classes.appBar}>
<Toolbar>...(irrelevant code)
I followed the examples in https://material-ui.com/demos/app-bar/ where the first example has the light gray color, then all the other examples have a blue bg, but there's nothing in the source code that was added to apply the blue color (in my opinion)
Any help please? thanks folks
Use <AppBar position="static" color="primary">.
By default AppBar is using the colors from the grey palette.
We are considering defaulting to primary for the color prop since it's not following the spec and having grey as a default for something as prominent as the app bar is a bad idea anyway.