-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[theme] Responsive font sizes #14573
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
Merged
oliviertassinari
merged 2 commits into
mui:next
from
n-batalha:feature/add_csskit_responsive_typography
May 2, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
docs/src/pages/customization/themes/ResponsiveFontSizes.js
This file contains hidden or 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,15 @@ | ||
| import React from 'react'; | ||
| import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles'; | ||
| import { ThemeProvider } from '@material-ui/styles'; | ||
| import Typography from '@material-ui/core/Typography'; | ||
|
|
||
| let theme = createMuiTheme(); | ||
| theme = responsiveFontSizes(theme); | ||
|
|
||
| export default function ResponsiveFontSizes() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Typography variant="h3">Responsive h3</Typography> | ||
| </ThemeProvider> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
| export function alignProperty({ size, grid }) { | ||
| const sizeBelow = size - (size % grid); | ||
| const sizeAbove = sizeBelow + grid; | ||
|
|
||
| return size - sizeBelow < sizeAbove - size ? sizeBelow : sizeAbove; | ||
| } | ||
|
|
||
| // fontGrid finds a minimal grid (in rem) for the fontSize values so that the | ||
| // lineHeight falls under a x pixels grid, 4px in the case of Material Design, | ||
| // without changing the relative line height | ||
| export function fontGrid({ lineHeight, pixels, htmlFontSize }) { | ||
| return pixels / (lineHeight * htmlFontSize); | ||
| } | ||
|
|
||
| /** | ||
| * generate a responsive version of a given CSS property | ||
| * @example | ||
| * responsiveProperty({ | ||
| * cssProperty: 'fontSize', | ||
| * min: 15, | ||
| * max: 20, | ||
| * unit: 'px', | ||
| * breakpoints: [300, 600], | ||
| * }) | ||
| * | ||
| * // this returns | ||
| * | ||
| * { | ||
| * fontSize: '15px', | ||
| * '@media (min-width:300px)': { | ||
| * fontSize: '17.5px', | ||
| * }, | ||
| * '@media (min-width:600px)': { | ||
| * fontSize: '20px', | ||
| * }, | ||
| * } | ||
| * | ||
| * @param {Object} params | ||
| * @param {string} params.cssProperty - The CSS property to be made responsive | ||
| * @param {number} params.min - The smallest value of the CSS property | ||
| * @param {number} params.max - The largest value of the CSS property | ||
| * @param {number} [params.unit] - The unit to be used for the CSS property | ||
| * @param {Array.number} [params.breakpoints] - An array of breakpoints | ||
| * @param {number} [params.alignStep] - Round scaled value to fall under this grid | ||
| * @returns {Object} responsive styles for {params.cssProperty} | ||
| */ | ||
| export function responsiveProperty({ | ||
| cssProperty, | ||
| min, | ||
| max, | ||
| unit = 'rem', | ||
| breakpoints = [600, 960, 1280], | ||
| transform = null, | ||
| }) { | ||
| const output = { | ||
| [cssProperty]: `${min}${unit}`, | ||
| }; | ||
|
|
||
| const factor = (max - min) / breakpoints[breakpoints.length - 1]; | ||
| breakpoints.forEach(breakpoint => { | ||
| let value = min + factor * breakpoint; | ||
|
|
||
| if (transform !== null) { | ||
| value = transform(value); | ||
| } | ||
|
|
||
| output[`@media (min-width:${breakpoint}px)`] = { | ||
| [cssProperty]: `${Math.round(value * 10000) / 10000}${unit}`, | ||
| }; | ||
| }); | ||
|
|
||
| return output; | ||
| } | ||
This file contains hidden or 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,99 @@ | ||
| import { assert } from 'chai'; | ||
| import { alignProperty, fontGrid, responsiveProperty } from './cssUtils'; | ||
|
|
||
| describe('cssUtils', () => { | ||
| describe('alignProperty', () => { | ||
| const tests = [ | ||
| { args: { size: 8, grid: 4 }, expected: 8 }, | ||
| { args: { size: 8, grid: 1 }, expected: 8 }, | ||
| { args: { size: 8, grid: 9 }, expected: 9 }, | ||
| { args: { size: 8, grid: 7 }, expected: 7 }, | ||
| { args: { size: 8, grid: 17 }, expected: 0 }, | ||
| ]; | ||
|
|
||
| tests.forEach(test => { | ||
| const { | ||
| args: { size, grid }, | ||
| expected, | ||
| } = test; | ||
|
|
||
| it(`aligns ${size} on grid ${grid} to ${expected}`, () => { | ||
| const sizeAligned = alignProperty({ size, grid }); | ||
| assert.strictEqual(sizeAligned, expected); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fontGrid', () => { | ||
| const tests = [ | ||
| { lineHeight: 1.3, pixels: 4, htmlFontSize: 16 }, | ||
| { lineHeight: 1.6, pixels: 9, htmlFontSize: 15 }, | ||
| { lineHeight: 1.0, pixels: 3, htmlFontSize: 14 }, | ||
| ]; | ||
|
|
||
| tests.forEach(test => { | ||
| const { lineHeight, pixels, htmlFontSize } = test; | ||
|
|
||
| describe(`when ${lineHeight} lineHeight, ${pixels} pixels, | ||
| ${htmlFontSize} htmlFontSize`, () => { | ||
| const grid = fontGrid({ lineHeight, pixels, htmlFontSize }); | ||
|
|
||
| it(`should return a font grid such that the relative lineHeight is aligned`, () => { | ||
| const absoluteLineHeight = grid * lineHeight * htmlFontSize; | ||
| assert.strictEqual(Math.round((absoluteLineHeight % pixels) * 100000) / 100000, 0); | ||
| }); | ||
| }); | ||
|
|
||
| it(`with ${lineHeight} lineHeight, ${pixels} pixels, | ||
| ${htmlFontSize} htmlFontSize, the font grid is such that | ||
| there is no smaller font aligning the lineHeight`, () => { | ||
| const grid = fontGrid({ lineHeight, pixels, htmlFontSize }); | ||
| const absoluteLineHeight = grid * lineHeight * htmlFontSize; | ||
| assert.strictEqual(Math.floor(absoluteLineHeight / pixels), 1); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('responsiveProperty', () => { | ||
| describe('when providing two breakpoints and pixel units', () => { | ||
| it('should respond with three styles in pixels', () => { | ||
| const result = responsiveProperty({ | ||
| cssProperty: 'fontSize', | ||
| min: 15, | ||
| max: 20, | ||
| unit: 'px', | ||
| breakpoints: [300, 600], | ||
| }); | ||
|
|
||
| assert.deepEqual(result, { | ||
| fontSize: '15px', | ||
| '@media (min-width:300px)': { | ||
| fontSize: '17.5px', | ||
| }, | ||
| '@media (min-width:600px)': { | ||
| fontSize: '20px', | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when providing one breakpoint and requesting rem units', () => { | ||
| it('should respond with two styles in rem', () => { | ||
| const result = responsiveProperty({ | ||
| cssProperty: 'fontSize', | ||
| min: 0.875, | ||
| max: 1, | ||
| unit: 'rem', | ||
| breakpoints: [500], | ||
| }); | ||
|
|
||
| assert.deepEqual(result, { | ||
| fontSize: '0.875rem', | ||
| '@media (min-width:500px)': { | ||
| fontSize: '1rem', | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,90 @@ | ||
| import convertLength from 'convert-css-length'; | ||
| import { responsiveProperty, alignProperty, fontGrid } from './cssUtils'; | ||
|
|
||
| function isUnitless(value) { | ||
| return String(parseFloat(value)).length === String(value).length; | ||
| } | ||
|
|
||
| export default function responsiveFontSizes(themeInput, options = {}) { | ||
| const { | ||
| breakpoints = ['sm', 'md', 'lg'], | ||
| disableAlign = false, | ||
| factor = 2, | ||
| variants = [ | ||
| 'h1', | ||
| 'h2', | ||
| 'h3', | ||
| 'h4', | ||
| 'h5', | ||
| 'h6', | ||
| 'subtitle1', | ||
| 'subtitle2', | ||
| 'body1', | ||
| 'body2', | ||
| 'caption', | ||
| 'button', | ||
| 'overline', | ||
| ], | ||
| } = options; | ||
|
|
||
| const theme = { ...themeInput }; | ||
| theme.typography = { ...theme.typography }; | ||
| const typography = theme.typography; | ||
|
|
||
| // Convert between css lengths e.g. em->px or px->rem | ||
| // Set the baseFontSize for your project. Defaults to 16px (also the browser default). | ||
| const convert = convertLength(typography.htmlFontSize); | ||
| const breakpointValues = breakpoints.map(x => theme.breakpoints.values[x]); | ||
|
|
||
| variants.forEach(variant => { | ||
| const style = typography[variant]; | ||
| const remFontSize = parseFloat(convert(style.fontSize, 'rem')); | ||
|
|
||
| if (remFontSize <= 1) { | ||
| return; | ||
| } | ||
|
|
||
| const maxFontSize = remFontSize; | ||
| const minFontSize = 1 + (maxFontSize - 1) / factor; | ||
|
|
||
| let { lineHeight } = style; | ||
|
|
||
| if (!isUnitless(lineHeight) && !disableAlign) { | ||
| throw new Error( | ||
| [ | ||
| `Material-UI: unsupported non-unitless line height with grid alignment.`, | ||
| 'Use unitless line heights instead.', | ||
| ].join('\n'), | ||
| ); | ||
| } | ||
|
|
||
| if (!isUnitless(lineHeight)) { | ||
| // make it unitless | ||
| lineHeight = parseFloat(convert(lineHeight, 'rem')) / parseFloat(remFontSize); | ||
| } | ||
|
|
||
| let transform = null; | ||
|
|
||
| if (!disableAlign) { | ||
| transform = value => | ||
| alignProperty({ | ||
| size: value, | ||
| grid: fontGrid({ pixels: 4, lineHeight, htmlFontSize: typography.htmlFontSize }), | ||
| }); | ||
| } | ||
|
|
||
| typography[variant] = { | ||
| ...style, | ||
| ...responsiveProperty({ | ||
| cssProperty: 'fontSize', | ||
| min: minFontSize, | ||
| max: maxFontSize, | ||
| unit: 'rem', | ||
| breakpoints: breakpointValues, | ||
| transform, | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| return theme; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.