Skip to content

Commit ffac6aa

Browse files
Merge pull request #40 from commitd/je-theme
Simplify the overriding of particular aspects of the Committed theme
2 parents 30e094a + 254037c commit ffac6aa

File tree

6 files changed

+1612
-450
lines changed

6 files changed

+1612
-450
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@committed/components",
3-
"version": "1.0.3",
3+
"version": "2.0.0",
44
"description": "Committed Component Library",
55
"author": "Committed",
66
"license": "MIT",

src/stories/colour.stories.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ Full palletes back the main colours and are keyed with `50, 100, 200, 300, 400,
2929
Click to copy color to clipboard.
3030

3131
<ThemeProvider>
32-
<Colors name="brand" colors={theme.palettes.brand} />
33-
<Colors name="primary" colors={theme.palettes.primary} />
34-
<Colors name="secondary" colors={theme.palettes.secondary} />
35-
<Colors name="error" colors={theme.palettes.error} />
36-
<Colors name="warning" colors={theme.palettes.warning} />
37-
<Colors name="success" colors={theme.palettes.success} />
38-
<Colors name="info" colors={theme.palettes.info} />
39-
<Colors name="neutrals" colors={theme.palettes.neutral} />
32+
<Colors name="brand" colors={theme.defaultPaletteColors.brand} />
33+
<Colors name="primary" colors={theme.defaultPaletteColors.primary} />
34+
<Colors name="secondary" colors={theme.defaultPaletteColors.secondary} />
35+
<Colors name="error" colors={theme.defaultPaletteColors.error} />
36+
<Colors name="warning" colors={theme.defaultPaletteColors.warning} />
37+
<Colors name="success" colors={theme.defaultPaletteColors.success} />
38+
<Colors name="info" colors={theme.defaultPaletteColors.info} />
39+
<Colors name="neutrals" colors={theme.defaultPaletteColors.neutral} />
4040
</ThemeProvider>
4141

4242
## Colours

src/theme/ThemeProvider.tsx

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import React, { FC } from 'react'
2-
import deepmerge from 'deepmerge'
32
import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles'
43
import { ThemeProvider as MuiThemeProvider } from '@material-ui/styles'
5-
import { BaseCSSProperties } from '@material-ui/styles/withStyles'
64
import CssBaseline from '@material-ui/core/CssBaseline'
7-
import * as themes from './theme'
8-
import { defaultFonts } from './fonts'
95
import { ThemeOptions } from '@material-ui/core/styles/createMuiTheme'
10-
import { createLightOptions, PaletteColors } from './theme'
6+
import {
7+
createCommittedFonts,
8+
createCommittedOverrides,
9+
createCommittedShape,
10+
createCommittedSpacing,
11+
createCommittedTypography,
12+
createCommittedPaletteOptions,
13+
defaultPaletteColors,
14+
FontOptions
15+
} from './theme'
16+
import deepmerge from 'deepmerge'
17+
import { PaletteOptions, Palette } from '@material-ui/core/styles/createPalette'
18+
import createMuiPalette from '@material-ui/core/styles/createPalette'
19+
import { ShapeOptions } from '@material-ui/core/styles/shape'
20+
import { SpacingOptions } from '@material-ui/core/styles/createSpacing'
21+
import { TypographyOptions } from '@material-ui/core/styles/createTypography'
22+
import { Overrides } from '@material-ui/core/styles/overrides'
23+
import { augmentColor } from './themeMaterialUtil'
1124

1225
export interface ThemeProviderProps {
1326
/**
14-
* To override the font families used. default is used for Typography,
27+
* Should either return a `theme.FontOptions` object to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults.
28+
*
29+
* default is used for Typography,
1530
* text for Text, display for Display and mono for Monospace.
1631
*
1732
* typography: { fontFamily: '-apple-system, BlinkMacSystemFont, "San Francisco", Roboto, "Segoe UI", "Helvetica Neue"'},
@@ -25,45 +40,76 @@ export interface ThemeProviderProps {
2540
* monospace: { fontFamily: 'Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace' }
2641
*
2742
*/
28-
fonts?: {
29-
typography?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
30-
heading?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
31-
subheading?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
32-
text?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
33-
display?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
34-
monospace?: { [P in keyof BaseCSSProperties]: BaseCSSProperties[P] }
35-
}
43+
createFonts?: () => FontOptions | undefined
3644
/**
37-
* To override the value of particular colors in the palette. For example, the primary, secondary or brand colors.
45+
* Should either return a [set of material-ui color intentions](https://material-ui.com/customization/palette/#customization) to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults.
46+
*
47+
* The material-ui colors can be specified: palette.primary, palette.secondary, palette.error, palette.warning, palette.info or palette.success
3848
*
39-
* Variants of each shade should be specified. Or a material-ui color preset should be used https://material-ui.com/customization/color/#color
49+
* Additionally the committed-theme colors can be specified: palette.brand, palette.neutral
4050
*/
41-
paletteColors?: Partial<PaletteColors>
51+
createPaletteOptions?: () => PaletteOptions
52+
/**
53+
* Should either return material-ui shape options i.e. `{ borderRadius: xx }` to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults.
54+
*/
55+
createShape?: () => ShapeOptions | undefined
56+
/**
57+
* Should either return [material-ui spacing options](https://material-ui.com/customization/spacing/) to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults.
58+
*/
59+
createSpacing?: () => SpacingOptions | undefined
60+
/**
61+
* Should either return [material-ui typography options](https://material-ui.com/customization/typography/) to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults.
62+
*/
63+
createTypography?: () =>
64+
| TypographyOptions
65+
| ((palette: Palette) => TypographyOptions)
66+
| undefined
67+
/**
68+
* Should either return [material-ui overrides options](https://material-ui.com/customization/globals/) to replace the Committed theme defaults, or it should return undefined to use the Material-UI defaults. It is passed the palette created using the `createPaletteOptions()` prop
69+
*/
70+
createOverrides?: (palette: Palette) => Overrides | undefined
4271
children?: React.ReactNode
4372
}
4473

4574
export const ThemeProvider: FC<ThemeProviderProps> = ({
46-
fonts = {},
47-
paletteColors = {},
75+
createPaletteOptions = createCommittedPaletteOptions,
76+
createFonts = createCommittedFonts,
77+
createOverrides = createCommittedOverrides,
78+
createShape = createCommittedShape,
79+
createSpacing = createCommittedSpacing,
80+
createTypography = createCommittedTypography,
4881
...rest
4982
}: ThemeProviderProps) => {
50-
const defaultFont = fonts.typography || defaultFonts.typography
51-
const allFonts = {
52-
typography: defaultFont,
53-
heading: fonts.heading || defaultFont,
54-
subheading: fonts.subheading || fonts.heading || defaultFont,
55-
text: fonts.text || defaultFont,
56-
display: fonts.display || defaultFont,
57-
monospace: fonts.monospace || defaultFonts.monospace
58-
}
59-
const lightOptions = createLightOptions(
60-
deepmerge(themes.defaultPaletteColors, paletteColors)
83+
const paletteOptions = createPaletteOptions()
84+
const palette = createMuiPalette(paletteOptions)
85+
// createMuiPalette() "augments" inputted colors (that may be in several forms) to make them conform to {main: #xxxx, light:#xxxx ,...etc}
86+
// manually augment committed custom theme colors that createMuiPalette is not aware of
87+
palette.success = augmentColor(
88+
paletteOptions.success,
89+
defaultPaletteColors.success
90+
)
91+
palette.warning = augmentColor(
92+
paletteOptions.warning,
93+
defaultPaletteColors.warning
6194
)
62-
const theme: ThemeOptions = Object.assign({}, lightOptions, {
63-
fonts: allFonts,
64-
typography: Object.assign(lightOptions.typography, allFonts.typography)
65-
})
66-
const muiTheme = responsiveFontSizes(createMuiTheme(theme))
95+
palette.brand = augmentColor(paletteOptions.brand, defaultPaletteColors.brand)
96+
palette.info = augmentColor(paletteOptions.info, defaultPaletteColors.info)
97+
palette.neutral = augmentColor(
98+
paletteOptions.neutral,
99+
defaultPaletteColors.neutral
100+
)
101+
102+
const themeOptions: ThemeOptions = {
103+
palette,
104+
fonts: createFonts(),
105+
shape: createShape(),
106+
spacing: createSpacing(),
107+
typography: createTypography(),
108+
overrides: createOverrides(palette)
109+
}
110+
111+
const muiTheme = responsiveFontSizes(createMuiTheme(themeOptions))
112+
const fonts = createFonts()
67113
return (
68114
<MuiThemeProvider theme={deepmerge(muiTheme, { fonts })}>
69115
<CssBaseline />

0 commit comments

Comments
 (0)