Themes
@cfxjs/react-ui
now support Dark Mode. You can switch theme at any time through a very simple API, no third-party styles or configs.
Switch themes
CfxProvider
to the root node.CssBaseline
provides basic CSS support, which is similar to normalize
.Now you can change the theme as follows:
Make sure
CfxProvider
andCssBaseline
are already on the root component.Get the current theme of the page through hook
useTheme
.Update the value of
theme.type
, and the theme of all components will follow automatically.
import { CssBaseline, CfxProvider } from '@cfxjs/react-ui'
const App = () => {
const [themeType, setThemeType] = useState('dark')
const switchThemes = () => {
setThemeType(lastThemeType => (lastThemeType === 'dark' ? 'light' : 'dark'))
}
return (
<CfxProvider theme={{ type: themeType }}>
<CssBaseline />
<YourComponent onClick={switchThemes} />
</CfxProvider>
)
}
Customizing theme
Customizing a theme is very simple in @cfxjs/react-ui
, you just need to provide a new theme Object
,
and all the components will change automatically.
Here is a complete sample project for reference.
Of course, if a component doesn't use your customized variables, it doesn't make any additional changes or rendering.
import { CssBaseline, CfxProvider } from '@cfxjs/react-ui'
const myTheme = {
palette: {
success: '#000',
},
}
const App = () => (
<CfxProvider theme={myTheme}>
<CssBaseline />
<YourAppComponent onClick={switchThemes} />
</CfxProvider>
)
View all types of Theme definitions
If you are using TypeScript, you can import various preset types:
import {
CfxUIThemes,
CfxUIThemesFont,
CfxUIThemesPalette,
CfxUIThemesExpressiveness,
CfxUIThemesLayout,
} from '@cfxjs/react-ui'
// usage:
const myStyles: CfxUIThemes = {
/* ... */
}
const myPalette: Partial<CfxUIThemesPalette> = {
/* ... */
}
If you don't use TypeScript, to learn more about preset types, see here.
Customizing components
Overriding styles with className
You can override the style by defining a className
on the component.
import { Button, Row } from '@cfxjs/react-ui'
const MyPage = () => (
<Row>
<Button className="page-button">Click me!</Button>
</Row>
)
// in css file:
.page-button {
padding: 0;
}
Overriding styles with inline-style
Defining an inline-style
will also correctly override the component.
const MyPage = () => (
<Row>
<Button style={{ margin: '20px' }}>Click me!</Button>
</Row>
)
Build your own Components with theme
Sometimes you want to create new components, but you want to be consistent with the current theme. Now you can try using theme variables in your components
import { useTheme } from '@cfxjs/react-ui'
const currentTheme = useTheme()
const MyComponent = () => (
<div style={{ color: currentTheme.palette.success }}>
<span>hello world!</span>
</div>
)