【问题标题】:How to generate a Fluent UI theme with in the library?如何在库中生成 Fluent UI 主题?
【发布时间】:2021-11-09 09:05:02
【问题描述】:

我正在使用 Fluent UI 创建一个 React 应用程序 - React,我希望每个用户在注册时都可以选择主颜色、文本颜色和背景颜色,并且他们的应用程序将以相同的方式呈现在该主题中在Theming Designer

主题设计师给了我以下输出。

const appTheme: PartialTheme = createTheme({
  palette: {
    themePrimary: "#ff5460",
    themeLighterAlt: "#0a0304",
    themeLighter: "#290d0f",
    themeLight: "#4d191d",
    themeTertiary: "#993239",
    themeSecondary: "#e04a54",
    themeDarkAlt: "#ff656f",
    themeDark: "#ff7d86",
    themeDarker: "#ff9fa6",
    neutralLighterAlt: "#000000",
    neutralLighter: "#000000",
    neutralLight: "#000000",
    neutralQuaternaryAlt: "#000000",
    neutralQuaternary: "#000000",
    neutralTertiaryAlt: "#000000",
    neutralTertiary: "#c8c8c8",
    neutralSecondary: "#d0d0d0",
    neutralPrimaryAlt: "#dadada",
    neutralPrimary: "#ffffff",
    neutralDark: "#f4f4f4",
    black: "#f8f8f8",
    white: "#000000",
  },
});

是否有一个函数可以在 Fluent UI 中生成主题,我只想提供 Primary Color、Text Color 和 Background Color,它提供了整个主题,以便我可以将其添加到 Theme Provider 组件中。

【问题讨论】:

    标签: reactjs office-ui-fabric fluent-ui fluentui-react


    【解决方案1】:

    通过创建函数来解决,

    import {
      BaseSlots,
      createTheme,
      getColorFromString,
      isDark,
      Theme,
      ThemeGenerator,
      themeRulesStandardCreator,
    } from "@fluentui/react";
    
    const generateTheme = (
      primaryColor: string,
      textColor: string,
      backgroundColor: string
    ): Theme => {
      const themeRules = themeRulesStandardCreator();
      const colors = {
        pColor: getColorFromString(primaryColor)!,
        tColor: getColorFromString(textColor)!,
        bColor: getColorFromString(backgroundColor)!,
      };
    
      const currentIsDark = isDark(
        themeRules[BaseSlots[BaseSlots.backgroundColor]].color!
      );
    
      ThemeGenerator.insureSlots(themeRules, currentIsDark);
      ThemeGenerator.setSlot(
        themeRules[BaseSlots[BaseSlots.primaryColor]],
        colors.pColor,
        currentIsDark,
        true,
        true
      );
      ThemeGenerator.setSlot(
        themeRules[BaseSlots[BaseSlots.foregroundColor]],
        colors.tColor,
        currentIsDark,
        true,
        true
      );
      ThemeGenerator.setSlot(
        themeRules[BaseSlots[BaseSlots.backgroundColor]],
        colors.bColor,
        currentIsDark,
        true,
        true
      );
    
      const themeAsJson: {
        [key: string]: string;
      } = ThemeGenerator.getThemeAsJson(themeRules);
    
      const finalTheme = createTheme({
        ...{ palette: themeAsJson },
        isInverted: currentIsDark,
      });
    
      return finalTheme;
    };
    
    export default generateTheme;
    

    其实这应该是 Fluent UI 的一部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2022-10-13
      • 2020-08-30
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多