Skip to content

Commit

Permalink
[theme] Allow changing the font-size
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Mar 17, 2018
1 parent 6c672df commit 003db87
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
17 changes: 17 additions & 0 deletions docs/src/pages/customization/themes/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ so `rem` units allow us to accommodate the users settings, resulting in a much b
Users change font size settings for all kinds of reasons, from poor eyesight to choosing optimum settings
for devices that can be vastly different in size and viewing distance.

To change the font-size of Material-UI you can provide a `fontSize` property.
The default value is `14px`.

```js
const theme = createMuiTheme({
typography: {
// In Japanese the characters are usually larger.
fontSize: 12,
},
});
```

The computed font size by the browser follows this mathematical equation:

![font-size](/static/images/font-size.gif)
<!-- https://latex.codecogs.com/gif.latex?computed&space;=&space;specification&space;\frac{typography.fontSize}{14}&space;\frac{html&space;font&space;size}{typography.htmlFontSize} -->

### Typography - HTML font size

You might want to change the `<html>` element default font size. For instance, when using the [10px simplification](https://www.sitepoint.com/understanding-and-using-rem-units-in-css/).
Expand Down
4 changes: 2 additions & 2 deletions src/BottomNavigation/BottomNavigationAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export const styles = theme => ({
},
label: {
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.pxToRem(theme.typography.fontSize - 2),
fontSize: theme.typography.pxToRem(12),
opacity: 1,
transition: 'font-size 0.2s, opacity 0.2s',
transitionDelay: '0.1s',
},
labelSelected: {
fontSize: theme.typography.pxToRem(theme.typography.fontSize),
fontSize: theme.typography.pxToRem(14),
},
labelHidden: {
opacity: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ export const styles = theme => ({
padding: `${theme.spacing.unit - 1}px ${theme.spacing.unit}px`,
minWidth: theme.spacing.unit * 8,
minHeight: 32,
fontSize: theme.typography.pxToRem(theme.typography.fontSize - 1),
fontSize: theme.typography.pxToRem(13),
},
sizeLarge: {
padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
minWidth: theme.spacing.unit * 14,
minHeight: 40,
fontSize: theme.typography.pxToRem(theme.typography.fontSize + 1),
fontSize: theme.typography.pxToRem(15),
},
fullWidth: {
width: '100%',
Expand Down
2 changes: 1 addition & 1 deletion src/List/ListSubheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const styles = theme => ({
color: theme.palette.text.secondary,
fontFamily: theme.typography.fontFamily,
fontWeight: theme.typography.fontWeightMedium,
fontSize: theme.typography.pxToRem(theme.typography.fontSize),
fontSize: theme.typography.pxToRem(14),
}),
colorPrimary: {
color: theme.palette.primary.main,
Expand Down
6 changes: 3 additions & 3 deletions src/Tabs/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ export const styles = theme => ({
},
},
label: {
fontSize: theme.typography.pxToRem(theme.typography.fontSize),
fontSize: theme.typography.pxToRem(14),
whiteSpace: 'normal',
[theme.breakpoints.up('md')]: {
fontSize: theme.typography.pxToRem(theme.typography.fontSize - 1),
fontSize: theme.typography.pxToRem(13),
},
},
labelWrapped: {
[theme.breakpoints.down('sm')]: {
fontSize: theme.typography.pxToRem(theme.typography.fontSize - 2),
fontSize: theme.typography.pxToRem(12),
},
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/styles/createTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export default function createTypography(palette: Object, typography: Object | F
} =
typeof typography === 'function' ? typography(palette) : typography;

const coef = fontSize / 14;
function pxToRem(value) {
return `${value / htmlFontSize}rem`;
return `${value / htmlFontSize * coef}rem`;
}

return deepmerge(
Expand Down Expand Up @@ -109,7 +110,7 @@ export default function createTypography(palette: Object, typography: Object | F
color: palette.text.secondary,
},
button: {
fontSize: pxToRem(fontSize),
fontSize: pxToRem(14),
textTransform: 'uppercase',
fontWeight: fontWeightMedium,
fontFamily,
Expand Down
5 changes: 5 additions & 0 deletions src/styles/createTypography.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ describe('createTypography', () => {
assert.strictEqual(typography.fontSize, 15);
});

it('should accept a custom font size', () => {
const typography = createTypography(palette, { fontSize: 16 });
assert.strictEqual(typography.body1.fontSize, '1rem', 'should be 16px');
});

it('should create a typography with a custom baseFontSize', () => {
const typography = createTypography(palette, { htmlFontSize: 10 });
assert.strictEqual(typography.display4.fontSize, '11.2rem');
Expand Down
Binary file added static/images/font-size.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 003db87

Please sign in to comment.