-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[system] Add useThemeProps & useTheme hooks (#26649)
- Loading branch information
Showing
19 changed files
with
118 additions
and
30 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
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,3 @@ | ||
import { Theme } from './createTheme'; | ||
|
||
export default function useTheme<T = Theme>(defaultTheme?: T): T; |
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,19 @@ | ||
import * as React from 'react'; | ||
import { useTheme as muiUseTheme } from '@material-ui/private-theming'; | ||
import { ThemeContext } from '@material-ui/styled-engine'; | ||
import createTheme from './createTheme'; | ||
|
||
function isObjectEmpty(obj) { | ||
return Object.keys(obj).length === 0; | ||
} | ||
|
||
export const systemDefaultTheme = createTheme(); | ||
|
||
function useTheme(defaultTheme = systemDefaultTheme) { | ||
const muiContextTheme = muiUseTheme(); | ||
const styledEngineContextTheme = React.useContext(ThemeContext); | ||
const contextTheme = muiContextTheme || styledEngineContextTheme; | ||
return !contextTheme || isObjectEmpty(contextTheme) ? defaultTheme : contextTheme; | ||
} | ||
|
||
export default useTheme; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { default } from './useThemeProps'; | ||
export * from './useThemeProps'; | ||
|
||
export { default as getThemeProps } from './getThemeProps'; |
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 './useThemeProps'; | ||
export { default as getThemeProps } from './getThemeProps'; |
24 changes: 24 additions & 0 deletions
24
packages/material-ui-system/src/useThemeProps/useThemeProps.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,24 @@ | ||
export interface ThemeWithProps { | ||
components?: any; | ||
} | ||
|
||
export type ThemedProps<Theme, Name extends keyof any> = Theme extends { | ||
components: Record<Name, { defaultProps: infer Props }>; | ||
} | ||
? Props | ||
: {}; | ||
|
||
export interface AdditionalThemeProps<Theme> { | ||
isRtl: boolean; | ||
theme: Theme; | ||
} | ||
|
||
export default function useThemeProps< | ||
Theme extends ThemeWithProps, | ||
Props, | ||
Name extends keyof any, | ||
>(params: { | ||
props: Props; | ||
name: Name; | ||
defaultTheme?: Theme; | ||
}): Props & ThemedProps<Theme, Name> & AdditionalThemeProps<Theme>; |
14 changes: 14 additions & 0 deletions
14
packages/material-ui-system/src/useThemeProps/useThemeProps.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,14 @@ | ||
import getThemeProps from './getThemeProps'; | ||
import useTheme from '../useTheme'; | ||
|
||
export default function useThemeProps({ props, name, defaultTheme }) { | ||
const contextTheme = useTheme(defaultTheme); | ||
const more = getThemeProps({ theme: contextTheme, name, props }); | ||
const theme = more.theme || contextTheme; | ||
|
||
return { | ||
theme, | ||
isRtl: theme.direction === 'rtl', | ||
...more, | ||
}; | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/material-ui-system/src/useThemeProps/useThemeProps.spec.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,32 @@ | ||
import { unstable_useThemeProps as useThemeProps } from '@material-ui/system'; | ||
import { expectType } from '@material-ui/types'; | ||
|
||
interface SliderProps { | ||
track?: boolean; | ||
valueLabelDisplay?: boolean; | ||
color: 'primary' | 'secondary'; | ||
} | ||
|
||
interface Theme { | ||
components?: { | ||
MuiSlider: { | ||
defaultProps: SliderProps; | ||
}; | ||
}; | ||
} | ||
|
||
function ThemedComponent() { | ||
const props = useThemeProps<Theme, SliderProps, 'MuiSlider'>({ | ||
props: { color: 'primary' }, | ||
name: 'MuiSlider', | ||
}); | ||
|
||
// additional props are valid | ||
expectType<boolean, typeof props.isRtl>(props.isRtl); | ||
expectType<Theme, typeof props.theme>(props.theme); | ||
|
||
// component's props are valid | ||
// Only existence of props is relevant here not type. | ||
props.track; | ||
props.valueLabelDisplay; | ||
} |
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 |
---|---|---|
@@ -1,15 +1,6 @@ | ||
import getThemeProps from './getThemeProps'; | ||
import useTheme from './useTheme'; | ||
import { unstable_useThemeProps as systemUseThemeProps } from '@material-ui/system'; | ||
import defaultTheme from './defaultTheme'; | ||
|
||
export default function useThemeProps({ props, name }) { | ||
const contextTheme = useTheme() || defaultTheme; | ||
const more = getThemeProps({ theme: contextTheme, name, props }); | ||
const theme = more.theme || contextTheme; | ||
|
||
return { | ||
theme, | ||
isRtl: theme.direction === 'rtl', | ||
...more, | ||
}; | ||
return systemUseThemeProps({ props, name, defaultTheme }); | ||
} |
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