跳到内容
+

主题阴影

了解默认主题的阴影比例以及如何自定义它。

默认令牌

Joy UI 使用 T 恤尺寸比例(sm、md、lg 等)来定义组件(如 CardMenu 等)使用的阴影。这些令牌分组在 theme.shadow 节点内

已复制

令牌

浅色

深色

xs

sm

md

lg

xl

自定义默认阴影

将键值对提供给 shadow 节点以覆盖默认阴影

import { extendTheme } from '@mui/joy/styles';

const theme = extendTheme({
  shadow: {
    xs: '{CSS box-shadow}',
    sm: '{CSS box-shadow}',
    md: '{CSS box-shadow}',
    lg: '{CSS box-shadow}',
    xl: '{CSS box-shadow}',
  },
});

// Then, pass it to `<CssVarsProvider theme={theme}>`.

添加新的阴影

您可以将任何自定义键添加到 shadow 节点

import { extendTheme } from '@mui/joy/styles';

const theme = extendTheme({
  shadow: {
    subtle: '{CSS box-shadow}',
    strong: '{CSS box-shadow}',
  },
});

// Then, pass it to `<CssVarsProvider theme={theme}>`.

TypeScript

在 TypeScript 中工作时,您需要使用新键来扩充主题的 Shadow 接口

// You can put this to any file that's included in your tsconfig
declare module '@mui/joy/styles' {
  interface Shadow {
    subtle: string;
    strong: string;
  }
}

阴影环

阴影环可以为浅色和深色方案配置。要创建阴影环,请为 shadowRing 节点提供有效的 CSS box-shadow 值

import { extendTheme } from '@mui/joy/styles';

const theme = extendTheme({
  colorSchemes: {
    light: {
      // This creates a 1px box-shadow.
      shadowRing: '0 0 0 1px rgba(0 0 0 / 0.1)',
    },
    dark: {
      shadowChannel: '0 0 0 1px rgba(255 255 255 / 0.1)',
    },
  },
});

// Then, pass it to `<CssVarsProvider theme={theme}>`.

阴影颜色

阴影的颜色来自名为 var(--joy-shadowChannel) 的主题令牌。您可以为浅色和深色方案自定义值

import { extendTheme } from '@mui/joy/styles';

const theme = extendTheme({
  colorSchemes: {
    light: {
      shadowChannel: '12 12 12',
    },
    dark: {
      shadowChannel: '0 0 0',
    },
  },
});

// Then, pass it to `<CssVarsProvider theme={theme}>`.

自定义元素上的阴影

要自定义特定实例上的阴影颜色或阴影环,请使用 theme.shadow.* 中的原始值。

// ✅
<Button
  sx={(theme) => ({
    boxShadow: theme.shadow.md,
    '--joy-shadowChannel': theme.vars.palette.primary.mainChannel,
    '--joy-shadowRing': 'inset 0 -3px 0 rgba(0 0 0 / 0.24)',
  })}
>

// ❌ Both of these do not work
<Button
  sx={(theme) => ({
    boxShadow: 'md',
    '--joy-shadowChannel': theme.vars.palette.primary.mainChannel,
    '--joy-shadowRing': 'inset 0 -3px 0 rgba(0 0 0 / 0.24)',
  })}
>
<Button
  sx={(theme) => ({
    boxShadow: theme.vars.shadow.md,
    '--joy-shadowChannel': theme.vars.palette.primary.mainChannel,
    '--joy-shadowRing': 'inset 0 -3px 0 rgba(0 0 0 / 0.24)',
  })}
>