📅  最后修改于: 2023-12-03 15:34:39.607000             🧑  作者: Mango
Fluent UI 是一个由微软开发的 React 组件库。它提供了一套美观、现代化的 UI 组件,使得开发者可以快速搭建出高效、顺畅的用户界面。Fluent UI 支持多种主题,且组件 API 简明清晰,让开发者能够轻松地调整组件的行为和外观。
你可以通过 npm 来安装 Fluent UI:
npm install @fluentui/react
使用 Fluent UI,你需要先引入相应的组件,例如按钮组件:
import { DefaultButton } from '@fluentui/react/lib/Button';
接着,在你的渲染函数中使用该组件:
function MyComponent() {
return (
<DefaultButton text="Click me" />
);
}
Fluent UI 提供了多种主题,你可以使用 ThemeProvider
组件来设定组件的主题。下面是一份可以切换明暗两种模式的示例:
import { initializeIcons } from '@fluentui/font-icons-mdl2';
import { loadTheme, mergeStyles, DefaultButton, ThemeProvider } from '@fluentui/react';
import { useState } from 'react';
initializeIcons();
export default function App() {
const lightTheme = {
palette: {
themePrimary: '#0078d4',
themeLighterAlt: '#eff6fc',
themeLighter: '#deecf9',
themeLight: '#c7e0f4',
themeTertiary: '#71afe5',
themeSecondary: '#2b88d8',
themeDarkAlt: '#106ebe',
themeDark: '#005a9e',
themeDarker: '#004578',
neutralLighterAlt: '#faf9f8',
neutralLighter: '#f3f2f1',
neutralLight: '#edebe9',
neutralQuaternaryAlt: '#e1dfdd',
neutralQuaternary: '#d0d0d0',
neutralTertiaryAlt: '#c8c6c4',
neutralTertiary: '#a19f9d',
neutralSecondary: '#605e5c',
neutralPrimaryAlt: '#3b3a39',
neutralPrimary: '#323130',
neutralDark: '#201f1e',
black: '#000000',
white: '#ffffff',
},
};
const darkTheme = {
palette: {
themePrimary: '#ffffff',
themeLighterAlt: '#060606',
themeLighter: '#171717',
themeLight: '#2e2e2e',
themeTertiary: '#595959',
themeSecondary: '#c8c8c8',
themeDarkAlt: '#e1e1e1',
themeDark: '#e8e8e8',
themeDarker: '#f4f4f4',
neutralLighterAlt: '#0b0b0b',
neutralLighter: '#151515',
neutralLight: '#252525',
neutralQuaternaryAlt: '#2f2f2f',
neutralQuaternary: '#373737',
neutralTertiaryAlt: '#595959',
neutralTertiary: '#c8c8c8',
neutralSecondary: '#e1e1e1',
neutralPrimaryAlt: '#e8e8e8',
neutralPrimary: '#f4f4f4',
neutralDark: '#f8f8f8',
black: '#ffffff',
white: '#1f1f1f',
},
};
const [currentTheme, setCurrentTheme] = useState(lightTheme);
const toggleTheme = () => {
setCurrentTheme(currentTheme === lightTheme ? darkTheme : lightTheme);
};
return (
<ThemeProvider theme={currentTheme}>
<div className={mergeStyles({
backgroundColor: currentTheme.palette.white,
color: currentTheme.palette.black,
minHeight: '100vh',
padding: 20,
})}
>
<DefaultButton text="Switch theme" onClick={toggleTheme} />
</div>
</ThemeProvider>
);
}
Fluent UI 是一个非常强大的 React 组件库,它提供了丰富的组件和主题,让开发者能够轻松地搭建出高效流畅的用户界面。除此之外,Fluent UI 还有许多其他的特性和功能,可以大大提高开发效率和代码质量。