要使任何新颜色通过 color 属性可用,请将它们插入扩展主题的 colorSchemes 键中。 您还可以使用 styled 和 sx API 访问它们。
extendTheme({colorSchemes:{light:{palette:{// `gradient` is a new color tokengradient:{primary:'linear-gradient(to top, var(--joy-palette-primary-main), #000)',},},},},});// `sx` prop usage example:<Buttonsx={{background:(theme)=> theme.vars.palette.gradient.primary }}/>;
TypeScript
当在 TypeScript 中工作时,扩充主题的 Palette 接口,以包含新的令牌。
// You can put this to any file that's included in your tsconfig
declare module '@mui/joy/styles'{interfacePalette{
gradient:{
primary: string;};}}
要添加全新的调色板,使用任何类型的比例,并使它们通过 color 属性可用,请将它们插入扩展主题的 colorSchemes 键中。
下面的代码片段将自定义的 secondary 调色板添加到主题中。
import{ extendTheme }from'@mui/joy/styles';const theme =extendTheme({colorSchemes:{light:{palette:{secondary:{// Credit:// https://github.com/tailwindlabs/tailwindcss/blob/master/src/public/colors.js50:'#fdf2f8',100:'#fce7f3',200:'#fbcfe8',300:'#f9a8d4',400:'#f472b6',500:'#ec4899',600:'#db2777',700:'#be185d',800:'#9d174d',900:'#831843',// Adjust the global variant tokens as you'd like.// The tokens should be the same for all color schemes.solidBg:'var(--joy-palette-secondary-400)',solidActiveBg:'var(--joy-palette-secondary-500)',outlinedBorder:'var(--joy-palette-secondary-500)',outlinedColor:'var(--joy-palette-secondary-700)',outlinedActiveBg:'var(--joy-palette-secondary-100)',softColor:'var(--joy-palette-secondary-800)',softBg:'var(--joy-palette-secondary-200)',softActiveBg:'var(--joy-palette-secondary-300)',plainColor:'var(--joy-palette-secondary-700)',plainActiveBg:'var(--joy-palette-secondary-100)',},},},dark:{palette:{secondary:{// Credit:// https://github.com/tailwindlabs/tailwindcss/blob/master/src/public/colors.js50:'#fdf2f8',100:'#fce7f3',200:'#fbcfe8',300:'#f9a8d4',400:'#f472b6',500:'#ec4899',600:'#db2777',700:'#be185d',800:'#9d174d',900:'#831843',// Adjust the global variant tokens as you'd like.// The tokens should be the same for all color schemes.solidBg:'var(--joy-palette-secondary-400)',solidActiveBg:'var(--joy-palette-secondary-500)',outlinedBorder:'var(--joy-palette-secondary-700)',outlinedColor:'var(--joy-palette-secondary-600)',outlinedActiveBg:'var(--joy-palette-secondary-900)',softColor:'var(--joy-palette-secondary-500)',softBg:'var(--joy-palette-secondary-900)',softActiveBg:'var(--joy-palette-secondary-800)',plainColor:'var(--joy-palette-secondary-500)',plainActiveBg:'var(--joy-palette-secondary-900)',},},},},});// Then, pass it to `<CssVarsProvider theme={theme}>`.
// You can put this to any file that's included in your tsconfigimport type { PaletteRange }from'@mui/joy/styles';
declare module '@mui/joy/styles'{interfaceColorPalettePropOverrides{// apply to all Joy UI components that support `color` prop
secondary:true;}interfacePalette{// this will make the node `secondary` configurable in `extendTheme`// and add `secondary` to the theme's palette.
secondary: PaletteRange;}}
import*as React from'react';import{ CssVarsProvider, extendTheme }from'@mui/joy/styles';import Box from'@mui/joy/Box';import Button from'@mui/joy/Button';// ⚠️ If the value is `undefined`, it should be `undefined` for other color schemes as well.const theme =extendTheme({colorSchemes:{light:{palette:{primary:{solidActiveBg:undefined,},},},dark:{palette:{primary:{solidActiveBg:undefined,},},},},});const useEnhancedEffect =typeof window !=='undefined'? React.useLayoutEffect : React.useEffect;exportdefaultfunctionRemoveActiveTokens(){// the `node` is used for attaching CSS variables to this demo, you might not need it in your application.const[node, setNode]= React.useState(null);useEnhancedEffect(()=>{setNode(document.getElementById('remove-active-tokens-demo'));},[]);return(<CssVarsProvidertheme={theme}colorSchemeNode={node ||null}><Boxid="remove-active-tokens-demo"sx={{display:'flex',gap:1,flexWrap:'wrap'}}><Button>Button</Button></Box></CssVarsProvider>);}