-
-
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.
[system] Add createTheme util (#26490)
- Loading branch information
Showing
35 changed files
with
236 additions
and
169 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
43 changes: 43 additions & 0 deletions
43
packages/material-ui-system/src/createTheme/createTheme.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,43 @@ | ||
import { Breakpoints, BreakpointsOptions } from './createBreakpoints'; | ||
import { Shape, ShapeOptions } from './shape'; | ||
import { Spacing, SpacingOptions } from './createSpacing'; | ||
|
||
export { Breakpoint, BreakpointOverrides } from './createBreakpoints'; | ||
|
||
export type Direction = 'ltr' | 'rtl'; | ||
|
||
export interface ThemeOptions { | ||
shape?: ShapeOptions; | ||
breakpoints?: BreakpointsOptions; | ||
direction?: Direction; | ||
mixins?: unknown; | ||
palette?: Record<string, any>; | ||
shadows?: unknown; | ||
spacing?: SpacingOptions; | ||
transitions?: unknown; | ||
components?: Record<string, any>; | ||
typography?: unknown; | ||
zIndex?: Record<string, number>; | ||
} | ||
|
||
export interface Theme { | ||
shape: Shape; | ||
breakpoints: Breakpoints; | ||
direction: Direction; | ||
palette: Record<string, any> & { mode: 'light' | 'dark' }; | ||
shadows?: unknown; | ||
spacing: Spacing; | ||
transitions?: unknown; | ||
components?: Record<string, any>; | ||
mixins?: unknown; | ||
typography?: unknown; | ||
zIndex?: unknown; | ||
} | ||
|
||
/** | ||
* Generate a theme base on the options received. | ||
* @param options Takes an incomplete theme object and adds the missing parts. | ||
* @param args Deep merge the arguments with the about to be returned theme. | ||
* @returns A complete, ready to use theme object. | ||
*/ | ||
export default function createTheme(options?: ThemeOptions, ...args: object[]): Theme; |
35 changes: 35 additions & 0 deletions
35
packages/material-ui-system/src/createTheme/createTheme.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,35 @@ | ||
import { deepmerge } from '@material-ui/utils'; | ||
import createBreakpoints from './createBreakpoints'; | ||
import shape from './shape'; | ||
import createSpacing from './createSpacing'; | ||
|
||
function createTheme(options = {}, ...args) { | ||
const { | ||
breakpoints: breakpointsInput = {}, | ||
palette: paletteInput = {}, | ||
spacing: spacingInput, | ||
shape: shapeInput = {}, | ||
...other | ||
} = options; | ||
|
||
const breakpoints = createBreakpoints(breakpointsInput); | ||
const spacing = createSpacing(spacingInput); | ||
|
||
let muiTheme = deepmerge( | ||
{ | ||
breakpoints, | ||
direction: 'ltr', | ||
components: {}, // Inject component definitions. | ||
palette: { mode: 'light', ...paletteInput }, | ||
spacing, | ||
shape: { ...shape, shapeInput }, | ||
}, | ||
other, | ||
); | ||
|
||
muiTheme = args.reduce((acc, argument) => deepmerge(acc, argument), muiTheme); | ||
|
||
return muiTheme; | ||
} | ||
|
||
export default createTheme; |
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 './createTheme'; | ||
export * from './createTheme'; |
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 './createTheme'; |
File renamed without changes.
File renamed without changes.
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 @@ | ||
export default function merge(acc: object, item: object): object; |
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,49 @@ | ||
import { SimpleStyleFunction, spacing, PropsFor } from './Box'; | ||
|
||
export type SpacingProps = PropsFor<typeof spacing>; | ||
export function createUnarySpacing<Spacing>(theme: { spacing: Spacing }): Spacing extends number | ||
? (abs: number | string) => number | number | ||
: Spacing extends any[] | ||
? <Index extends number>(abs: Index | string) => Spacing[Index] | string | ||
: Spacing extends (...args: unknown[]) => unknown | ||
? Spacing | ||
: // warns in Dev | ||
() => undefined; | ||
|
||
export const margin: SimpleStyleFunction< | ||
| 'm' | ||
| 'mt' | ||
| 'mr' | ||
| 'mb' | ||
| 'ml' | ||
| 'mx' | ||
| 'my' | ||
| 'margin' | ||
| 'marginTop' | ||
| 'marginRight' | ||
| 'marginBottom' | ||
| 'marginLeft' | ||
| 'marginX' | ||
| 'marginY' | ||
>; | ||
|
||
export type MarginProps = PropsFor<typeof margin>; | ||
|
||
export const padding: SimpleStyleFunction< | ||
| 'p' | ||
| 'pt' | ||
| 'pr' | ||
| 'pb' | ||
| 'pl' | ||
| 'px' | ||
| 'py' | ||
| 'padding' | ||
| 'paddingTop' | ||
| 'paddingRight' | ||
| 'paddingBottom' | ||
| 'paddingLeft' | ||
| 'paddingX' | ||
| 'paddingY' | ||
>; | ||
|
||
export type PaddingProps = PropsFor<typeof padding>; |
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,15 @@ | ||
import { StyleFunction } from './Box'; | ||
import { CSSObject } from './createStyled'; | ||
|
||
export interface StyleOptions<PropKey> { | ||
cssProperty?: PropKey | keyof React.CSSProperties | false; | ||
prop: PropKey; | ||
/** | ||
* dot access in `Theme` | ||
*/ | ||
themeKey?: string; | ||
transform?: (cssValue: unknown) => number | string | React.CSSProperties | CSSObject; | ||
} | ||
export function style<PropKey extends string, Theme extends object>( | ||
options: StyleOptions<PropKey>, | ||
): StyleFunction<{ [K in PropKey]?: unknown } & { theme: Theme }>; |
Oops, something went wrong.