How to configure MUI v5 custom font-family? - material-ui

I have the following:
cont theme = createTheme({
typography: {
allVariants: MetricFontFaces,
fontFamily: 'MetricHPEXS'
} as TypographyOptions,
components: {
MuiFormControl: {
styleOverrides: {
root: {
fontFamily: 'MetricHPEXS'
}
}
},
MuiInput: {
styleOverrides: {
root: {
fontFamily: 'MetricHPEXS'
},
formControl: {
fontFamily: 'MetricHPEXS'
},
input: {
fontFamily: 'MetricHPEXS'
}
}
},
MuiOutlinedInput: {
styleOverrides: {
input: {
fontFamily: 'MetricHPEXS'
}
}
}
}
});
Then I try to use it like this in my storybook:
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>
{story()}
<TextField value="sdasdasd" />
</ThemeProvider>
</StyledEngineProvider>
The custom font-family is not applying.
How do I properly create the MUI v5 theme such that all components will accept the same custom font?

In the end, we applied the font settings directly onto the MuiCssBaseline in order to have everything else pick up the font-family. Example:
import { TypographyOptions } from '#mui/material/styles/createTypography';
export const muiThemeOptions = {
typography: {
fontFamily: 'MetricHPEXS'
} as TypographyOptions,
components: {
MuiCssBaseline: {
styleOverrides: MetricFontFaces // <== here
},
...
}
}
Where MetricFontFaces is a giant string with all of the #font-face defined. While this worked on applying the font globally, but because MetricFontFaces is a string, we couldn't apply other CSS styling easily. There is no simple way (at least I could not find them) to supply #font-face via the MuiCssBaseline.

Related

Tailwind Custom Colors Not recompiled with Twin.macro and styled component

I'm trying to use a custom color, but I got a problem when I change the color on the tailwind config, Twin.macro does not recompile the style,
p.s: it's worked fine when I use it with className
// tailwind.config.js
module.exports = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx}',
'./src/components/**/*.{js,ts,jsx,tsx}',
],
important: true,
theme: {
extend: {
colors: {
// primary: '#00bcd4',
primary: '#fde047',
},
},
}
}
// babel-plugin-macros.config.js
module.exports = {
twin: {
preset: 'styled-components',
autoCssProp: false,
},
}
// .babelrc.js
module.exports = {
presets: [['next/babel', { 'preset-react': { runtime: 'automatic' } }]],
plugins: ['babel-plugin-macros', 'babel-plugin-styled-components'],
}
Example of usage
<div className="text-primary">Hello </div> // Color change
<div tw="text-primary">#Dégage_Akhannouch</div> // Color does not change

how do i remove the outline on focus in DataGrid

I'm trying to remove the outline that appears when you focus on a cell in Material UI's DataGrid component.
None of these methods work:
const useStyles = makeStyles((theme) => ({
// Method 1:
'#global': {
'.MuiDataGrid-cell:focus': {
outline: 0,
},
},
// Method 2:
cell: {
'& .MuiDataGrid-cell:focus': {
outline: 0,
},
},
// Method 3:
cell: {
':focus': { outline: 0 },
},
const classes = useStyles()
const dataGridColumns: GridColumns = [
{
...other,
cellClassName: classes.cell,
}]
Edit:
'#global': {
'.MuiDataGrid-root .MuiDataGrid-cell:focus': {
outline: 0,
},
},
worked for me, but I would prefer not to use a global css adjustment.
You can modify the MuiDataGrid-cell class by using Material UI's useStyles() function like the following (no need to declare globally):
import { makeStyles } from '#material-ui/core/styles';
import { DataGrid } from '#material-ui/data-grid';
const useStyles = makeStyles({
root: {
'&.MuiDataGrid-root .MuiDataGrid-cell:focus': {
outline: 'none',
},
}
});
const MyComponent = () => {
const classes = useStyles();
return (
<DataGrid
className={classes.root}
// ...other table setup props
/>
);
}
export default MyComponent;
Resources:
https://material-ui.com/styles/advanced/#with-material-ui-core
https://material-ui.com/api/data-grid/#css
https://github.com/mui-org/material-ui-x/issues/420
Edit: Updated for 4.0.0-alpha.29
const useStyles = makeStyles({
root: {
'&.MuiDataGrid-root .MuiDataGrid-columnHeader:focus, &.MuiDataGrid-root .MuiDataGrid-cell:focus': {
outline: 'none',
},
}
});
Just for completion sake this is how I styled the DataGrid with styled components and it worked.
const StyledDataGrid = styled(DataGrid)`
&.MuiDataGrid-root .MuiDataGrid-columnHeader:focus,
&.MuiDataGrid-root .MuiDataGrid-cell:focus {
outline: none;
}
`;
If you're using MUI >= 5, you can simply do the follow:
<DataGrid
sx={{
"&.MuiDataGrid-root .MuiDataGrid-cell:focus-within": {
outline: "none !important",
},
}}
...
/>
const useStyles = makeStyles(
() => createStyles({
root: {
'& .MuiDataGrid-columnHeader:focus-within, & .MuiDataGrid-cell:focus-within': {
outline: 'none',
},
'& .MuiDataGrid-columnHeader:focus, & .MuiDataGrid-cell:focus': {
outline: 'none',
},
},
}),
);
I just needed to do this as well for a project I'm working on.
You can override many of the classes used by the datagrid, but you'll need to make sure that the selector being used is more specific in order for it to take precedence.
Many of their classes are documented here in the CSS section.
The below snippet worked for me.
const useStyles = makeStyles((theme) => ({
root: {
"& .MuiDataGrid-root": {
"& .MuiDataGrid-colCell:focus": {
outline: "none"
},
},
},
}));
The perfect way for me was add this code to your css file:
.MuiDataGrid-root .MuiDataGrid-columnHeader,
.MuiDataGrid-root .MuiDataGrid-cell {
outline: none !important;
}
I found that if we set it just for :focus, then if we click to a button of cell, outline still visible

How do we extend material-ui's CssBaseline?

I'm thinking to do it like the following, but I'm not sure if this is the recommended way to do it:
import * as React from "react"
import { withStyles, createStyles, Theme } from "#material-ui/core"
import CssBaseline from "#material-ui/core/CssBaseline"
// global styles for a Mapper app
class MyCssBaseline extends React.Component {
render() {
return <CssBaseline />
}
}
export default withStyles(styles)(MapperCssBaseline)
function styles(_theme: Theme) {
return createStyles({
"#global": {
fontSize: 12,
// ... custom styles here ...
}
})
}
Is this how? Or is there a more recommended way?
I managed to do it with overrides when creating theme
export const theme: Theme = createMuiTheme({
overrides: {
MuiCssBaseline: {
'#global': {
svg: {
maxWidth: '100%',
maxHeight: '100%',
},
},
},
},
})

Specifing a hover border color on a textfield using a custom theme with Material UI

Having trouble finding documentation on how to change the default border color of an outlined Textfield on hover using a custom theme on Material ui.
MuiOutlinedInput: {
root: {
"&:hover:not($disabled):not($focused):not($error) $notchedOutline": {
border: "2px solid",
borderColor: "yellow"
}
}
}
Reference
Example
MUI v5
MuiTextField: {
styleOverrides: {
root: {
'& .MuiOutlinedInput-root': {
'& fieldset': {
borderColor: 'black',
},
'&:hover fieldset': {
borderColor: 'red',
},
'&.Mui-focused fieldset': {
borderColor: 'yellow',
},
},
},
},
}

chartjs datalabels change font and color of text displaying inside pie chart

I am using chartjs
and datalabels
I have achieved everything I needed from chartjs and its plugin. Here is my final out
Here is my code
( function ( $ ) {
"use strict";
/////////////Pie chart START here//////////////////////////////
var ctx = document.getElementById( "pieChart" );
ctx.height = 130;
var myChart = new Chart( ctx, {
type: 'pie',
data: {
datasets: [ {
data: [ 40, 20, 10, 3, 7, 15, 4, 52 ],
backgroundColor: [
"rgba(0,128,128)",
"rgba(255,20,147)",
"rgba(0,0,128)",
"rgba(0,128,0)",
"rgba(128,0,0)",
"rgba(255,0,0)",
"rgba(218,112,214)",
"rgba(70,130,180)"
],
hoverBackgroundColor: [
"rgba(0,128,128)",
"rgba(255,20,147)",
"rgba(0,0,128)",
"rgba(0,128,0)",
"rgba(128,0,0)",
"rgba(255,0,0)",
"rgba(218,112,214)",
"rgba(70,130,180)"
]
} ],
labels: [
"Open",
"On-Hold (Need Spares)",
"In-Process",
"Closed",
"Re-Open",
"On-Hold (Condemnation)",
"On-Hold (For Decision)",
"On-Hold (For Revision)"
]
},
options: {
responsive: true,
legend: {
position: 'left',
labels: {
fontSize:17,
}
}
}
} );
/////////////Pie chart END here//////////////////////////////
} )( jQuery );
Now I need to change the font size and the color of text(data) displaying inside each slice of pie chart. Any help ?
P.s: I am using chart.js v2.7.2
I use Chart js and datalebels to, and can do this like this:
plugins: {
datalabels: {
color: #ffffff,
formatter: function (value) {
return Math.round(value) + '%';
},
font: {
weight: 'bold',
size: 16,
}
}
}
Of course in my example i add the '%', thats why i use that function in formatter.
Remember that 'plugins' is part of 'options' in the chart.
Here is the page of the plugin datalabels with more things you can do
If you want to change font family then you can do like this:
add font-family e.g adding 'Josefin Sans' font family
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Josefin+Sans">
and then mention family: 'Josefin Sans' in the font JSON object. like this:-
plugins: {
datalabels: {
color: #ffffff,
formatter: function (value) {
return Math.round(value) + '%';
},
font: {
weight: 'bold',
size: 16,
family: 'Josefin Sans',
}
}
}
To change the color for each data set you can use
{
data: {
datasets: [
{
datalabels: {
labels: {
value: {
color: 'green'
}
}
}
}]
}
Found it helpful https://chartjs-plugin-datalabels.netlify.app/guide/labels.html#dataset-overrides
In my case to make it work I had to add quotes to the color value:
color: "#ffffff",
plugins: {
datalabels: {
color: "#ffffff",
formatter: function (value) {
return Math.round(value) + '%';
},
font: {
weight: 'bold',
size: 16,
}
}
}