现代化的React UI 框架 Chakra-UI
npm install @chakra-ui/react @emotion/react @emotion/styled framer-motion
npm install @chakra-ui/theme
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import theme from '@chakra-ui/theme';
import { ChakraProvider, CSSReset } from "@chakra-ui/react";
ReactDOM.render(
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>,
document.getElementById('root')
);
样式属性 | css属性 | 主题 |
---|---|---|
m,margin | margin | space |
mx | margin-left & margin-right | space |
my | margin-top& margin-bottom | space |
p,padding | padding | space |
px | padding-left & padding-right | space |
py | padding-top & padding-bottom | space |
bg | background | color |
bgColor | background-color | color |
w,h | width, height | size |
boxSize | width & height | size |
d | display | none |
pos | position | none |
import { Box } from '@chakra-ui/react'
function App() {
return <Box w={800} h={400} bgColor="hotpink"></Box>
}
export default App;
import React from 'react';
import { Box, Button, useColorMode, Text } from '@chakra-ui/react'
function App() {
const { colorMode, toggleColorMode } = useColorMode(); // 解构返回的对象
return <Box w={800} h={400} bgColor="hotpink">
<Text>{colorMode}</Text>
<Button onClick={toggleColorMode} >切换颜色</Button>
</Box>
}
export default App;
import React from 'react';
import { Box, Button, useColorMode, Text, useColorModeValue } from '@chakra-ui/react'
function App() {
const { colorMode, toggleColorMode } = useColorMode();
const bgColor =useColorModeValue('pink', 'skyblue'); // 接收两个参数,浅色模式、暗色模式的色值
return <Box w={800} h={400} bgColor={bgColor}>
<Text>{colorMode}</Text>
<Button onClick={toggleColorMode} >切换颜色</Button>
</Box>
}
export default App;
import {Box, Button, LightMode , DarkMode} from '@chakra-ui/react';
return <Box w={800} h={400}>
<Text>{colorMode}</Text>
<LightMode>
<Button onClick={toggleColorMode} >切换颜色</Button>
</LightMode>
</Box>
设置默认颜色模式:通过 theme.config.initialColorMode 可以设置应用使用的默认主题.
使用操作系统所使用的颜色模式:通过 theme.config.useSystemColorMode 可以设置将应用的颜色模式设置为操作系统所使用的颜色模式.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import theme from '@chakra-ui/theme'
import { ChakraProvider } from "@chakra-ui/react";
// 设置默认颜色模式
// theme.config.initialColorMode = 'dark'
// 使用操作系统使用的颜色模式
theme.config.useSystemColorMode = true
ReactDOM.render(
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>,
document.getElementById('root')
);
import React from 'react';
import { Box } from '@chakra-ui/react'
function App() {
return <Box w={800} h={400} bgColor="gray.300">
chakra ui
</Box>
}
export default App;
// 这里的预设值单位为rem,数值从0开始,以0.25rem递增,如(0:0 、 1:"0.25rem")
// mb="5" 表示 margin-bottom: 1.25rem;
return <Box w={800} h={400} bgColor="gray.300" mb="5"> chakra ui </Box>
return <Box w="2xs" h="10" bgColor="gray.300" mb="5">chakra ui</Box>
// theme.js
export default {
breakpoints: ["30em", "48em", "62em", "80em"] ,
};
return <Box w={["100px","200px","300px","800px"]} h="10" bgColor="gray.300" mb="5" mt="6">
chakra ui
</Box>
import { chakra } from '@chakra-ui/react';
const MyButton = chakra('button', {
baseStyle: { // 设置一些默认样式
bg: 'red.200'
}
})
function App() {
return (
<div>
//直接以chakra. 可以传递charka样式的纯html
<chakra.button bg="red.100" px="3" py="2">chakra.button</chakra.button>
<MyButton>按钮</MyButton>
</div>
)
}
export default App;
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { ChakraProvider } from '@chakra-ui/react'
import { extendTheme } from '@chakra-ui/react'
const theme = extendTheme({
components: {
Button: {
variants: {
primary: {
bgColor: 'blue.500',
color: 'white'
},
danger: {
bgColor: 'red.500',
color: 'white'
}
}
}
},
})
ReactDOM.render(
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>,
document.getElementById('root')
);
import { Button } from '@chakra-ui/react';
function App() {
return (
<div>
<Button variant="primary">主题扩展按钮</Button>
</div>
)
}
export default App;
//card.theme.js
import type { ComponentStyleConfig } from '@chakra-ui/theme'
const Card: ComponentStyleConfig = {
baseStyle: {
display: 'flex',
flexDirection: 'column',
background: 'white',
alignItems: 'center',
gap: 6,
},
variants: {
rounded: {
padding: 8,
borderRadius: 'xl',
boxShadow: 'xl',
},
smooth: {
padding: 6,
borderRadius: 'base',
boxShadow: 'md',
},
},
defaultProps: {
variant: 'smooth',
},
}
export default Card
// theme.js
import { extendTheme } from '@chakra-ui/react';
import Card from './card.theme';
const theme = extendTheme({
components: {
Card
},
})
export default theme
// themeKey 我们扩展到主题theme的 card,
const styles = useStyleConfig(themeKey, props)
// card.js
import { useStyleConfig, Box } from '@chakra-ui/react'
const MyCard = function (props) {
const { variant, ...rest } = props
const styles = useStyleConfig('Card', { variant })
return <Box __css={styles} {...rest} />
}
export default MyCard
import { MyCard } from './LG/index';
function App() {
return (
<MyCard>卡片</MyCard>
)
}
export default App;