使用高级 Chakra UI 组件更快地构建 💎
了解更多2024年10月19日
浮动标签可以节省表单空间,尤其是在屏幕空间有限的移动设备上。
要在 Chakra UI 中为输入组件添加浮动标签,请使用Field
、Input
和 Box
组件创建样式化的输入字段。
import { Box, Field, Input, defineStyle } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Box pos="relative" w="full">
<Input className="peer" placeholder="" />
<Field.Label css={floatingStyles}>Email</Field.Label>
</Box>
</Field.Root>
)
}
const floatingStyles = defineStyle({
pos: "absolute",
bg: "bg",
px: "0.5",
top: "-3",
insetStart: "2",
fontWeight: "normal",
pointerEvents: "none",
transition: "position",
_peerPlaceholderShown: {
color: "fg.muted",
top: "2.5",
insetStart: "3",
},
_peerFocusVisible: {
color: "fg",
top: "-3",
insetStart: "2",
},
})
有关 Chakra UI 输入组件的更多详细信息,请参阅文档.