-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
676 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
docs/src/pages/customization/components/GlobalThemeVariants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react'; | ||
import { | ||
createMuiTheme, | ||
makeStyles, | ||
ThemeProvider, | ||
} from '@material-ui/core/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
root: { | ||
'& > *': { | ||
margin: theme.spacing(1), | ||
}, | ||
}, | ||
})); | ||
|
||
const defaultTheme = createMuiTheme(); | ||
|
||
const theme = createMuiTheme({ | ||
variants: { | ||
MuiButton: [ | ||
{ | ||
props: { variant: 'dashed' }, | ||
styles: { | ||
textTransform: 'none', | ||
border: `2px dashed ${defaultTheme.palette.primary.main}`, | ||
color: defaultTheme.palette.primary.main, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', color: 'secondary' }, | ||
styles: { | ||
border: `2px dashed ${defaultTheme.palette.secondary.main}`, | ||
color: defaultTheme.palette.secondary.main, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', size: 'large' }, | ||
styles: { | ||
borderWidth: 4, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', color: 'secondary', size: 'large' }, | ||
styles: { | ||
fontSize: 18, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export default function GlobalThemeVariants() { | ||
const classes = useStyles(); | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<ThemeProvider theme={theme}> | ||
<Button variant="dashed">Dashed</Button> | ||
<Button variant="dashed" color="secondary"> | ||
Secondary | ||
</Button> | ||
<Button variant="dashed" size="large"> | ||
Large | ||
</Button> | ||
<Button variant="dashed" color="secondary" size="large"> | ||
Secondary large | ||
</Button> | ||
</ThemeProvider> | ||
</div> | ||
); | ||
} |
79 changes: 79 additions & 0 deletions
79
docs/src/pages/customization/components/GlobalThemeVariants.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import { | ||
createMuiTheme, | ||
makeStyles, | ||
Theme, | ||
ThemeProvider, | ||
} from '@material-ui/core/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
declare module '@material-ui/core/Button/Button' { | ||
interface ButtonPropsVariantOverrides { | ||
dashed: true; | ||
} | ||
} | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
root: { | ||
'& > *': { | ||
margin: theme.spacing(1), | ||
}, | ||
}, | ||
})); | ||
|
||
const defaultTheme = createMuiTheme(); | ||
|
||
const theme = createMuiTheme({ | ||
variants: { | ||
MuiButton: [ | ||
{ | ||
props: { variant: 'dashed' }, | ||
styles: { | ||
textTransform: 'none', | ||
border: `2px dashed ${defaultTheme.palette.primary.main}`, | ||
color: defaultTheme.palette.primary.main, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', color: 'secondary' }, | ||
styles: { | ||
border: `2px dashed ${defaultTheme.palette.secondary.main}`, | ||
color: defaultTheme.palette.secondary.main, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', size: 'large' }, | ||
styles: { | ||
borderWidth: 4, | ||
}, | ||
}, | ||
{ | ||
props: { variant: 'dashed', color: 'secondary', size: 'large' }, | ||
styles: { | ||
fontSize: 18, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export default function GlobalThemeVariants() { | ||
const classes = useStyles(); | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<ThemeProvider theme={theme}> | ||
<Button variant="dashed">Dashed</Button> | ||
<Button variant="dashed" color="secondary"> | ||
Secondary | ||
</Button> | ||
<Button variant="dashed" size="large"> | ||
Large | ||
</Button> | ||
<Button variant="dashed" color="secondary" size="large"> | ||
Secondary large | ||
</Button> | ||
</ThemeProvider> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './propsToClassKey'; | ||
export * from './propsToClassKey'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './propsToClassKey'; |
1 change: 1 addition & 0 deletions
1
packages/material-ui-styles/src/propsToClassKey/propsToClassKey.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function propsToClassKey(props: object): string; |
38 changes: 38 additions & 0 deletions
38
packages/material-ui-styles/src/propsToClassKey/propsToClassKey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import MuiError from '@material-ui/utils/macros/MuiError.macro'; | ||
|
||
// TODO: remove this once the capitalize method is moved to the @material-ui/utils package | ||
export function capitalize(string) { | ||
if (typeof string !== 'string') { | ||
throw new MuiError('Material-UI: capitalize(string) expects a string argument.'); | ||
} | ||
|
||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
|
||
function isEmpty(string) { | ||
return string.length === 0; | ||
} | ||
|
||
/** | ||
* Generates string classKey based on the properties provided. It starts with the | ||
* variant if defined, and then it appends all other properties in alphabetical order. | ||
* | ||
* @param {object} props - the properties for which the classKey should be created | ||
*/ | ||
export default function propsToClassKey(props) { | ||
const { variant, ...rest } = props; | ||
|
||
let classKey = variant || ''; | ||
|
||
Object.keys(rest) | ||
.sort() | ||
.forEach((key) => { | ||
if (key === 'color') { | ||
classKey += isEmpty(classKey) ? props[key] : capitalize(props[key]); | ||
} else { | ||
classKey += `${isEmpty(classKey) ? key : capitalize(key)}${capitalize(props[key])}`; | ||
} | ||
}); | ||
|
||
return classKey; | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/material-ui-styles/src/propsToClassKey/propsToClassKey.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect } from 'chai'; | ||
import propsToClassKey from './propsToClassKey'; | ||
|
||
describe('propsToClassKey', () => { | ||
it('should return the variant value as string', () => { | ||
expect(propsToClassKey({ variant: 'custom' })).to.equal('custom'); | ||
}); | ||
|
||
it('should combine the variant with other props', () => { | ||
expect(propsToClassKey({ variant: 'custom', size: 'large' })).to.equal('customSizeLarge'); | ||
}); | ||
|
||
it('should append the props after the variant in alphabetical order', () => { | ||
expect(propsToClassKey({ variant: 'custom', size: 'large', mode: 'static' })).to.equal( | ||
'customModeStaticSizeLarge', | ||
); | ||
}); | ||
|
||
it('should not prefix the color prop', () => { | ||
expect(propsToClassKey({ variant: 'custom', color: 'primary' })).to.equal('customPrimary'); | ||
}); | ||
|
||
it('should work without variant in props', () => { | ||
expect(propsToClassKey({ color: 'primary', size: 'large', mode: 'static' })).to.equal( | ||
'primaryModeStaticSizeLarge', | ||
); | ||
}); | ||
|
||
it('should not capitalize the first prop ', () => { | ||
expect(propsToClassKey({ size: 'large', zIndex: 'toolbar' })).to.equal( | ||
'sizeLargeZIndexToolbar', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './useThemeVariants'; | ||
export * from './useThemeVariants'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './useThemeVariants'; |
1 change: 1 addition & 0 deletions
1
packages/material-ui-styles/src/useThemeVariants/useThemeVariants.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function useThemeVariants(props: object, name: string): string; |
Oops, something went wrong.