切换提示
看起来像工具提示,但功能类似于弹出窗口。
import { Button } from "@chakra-ui/react"
import { ToggleTip } from "@/components/ui/toggle-tip"
import { LuInfo } from "react-icons/lu"
const Demo = () => {
return (
<ToggleTip content="This is some additional information.">
<Button size="xs" variant="ghost">
<LuInfo />
</Button>
</ToggleTip>
)
}
设置
¥Setup
为了方便使用,请为 ToggleTip
组件创建一个封闭的组件组合。
¥For ease of use, create a closed component composition for the ToggleTip
component.
import {
Popover as ChakraPopover,
IconButton,
type IconButtonProps,
Portal,
} from "@chakra-ui/react"
import * as React from "react"
import { HiOutlineInformationCircle } from "react-icons/hi"
export interface ToggleTipProps extends ChakraPopover.RootProps {
showArrow?: boolean
portalled?: boolean
portalRef?: React.RefObject<HTMLElement>
content?: React.ReactNode
contentProps?: ChakraPopover.ContentProps
}
export const ToggleTip = React.forwardRef<HTMLDivElement, ToggleTipProps>(
function ToggleTip(props, ref) {
const {
showArrow,
children,
portalled = true,
content,
contentProps,
portalRef,
...rest
} = props
return (
<ChakraPopover.Root
{...rest}
positioning={{ ...rest.positioning, gutter: 4 }}
>
<ChakraPopover.Trigger asChild>{children}</ChakraPopover.Trigger>
<Portal disabled={!portalled} container={portalRef}>
<ChakraPopover.Positioner>
<ChakraPopover.Content
width="auto"
px="2"
py="1"
textStyle="xs"
rounded="sm"
ref={ref}
{...contentProps}
>
{showArrow && (
<ChakraPopover.Arrow>
<ChakraPopover.ArrowTip />
</ChakraPopover.Arrow>
)}
{content}
</ChakraPopover.Content>
</ChakraPopover.Positioner>
</Portal>
</ChakraPopover.Root>
)
},
)
export interface InfoTipProps extends Partial<ToggleTipProps> {
buttonProps?: IconButtonProps | undefined
}
export const InfoTip = React.forwardRef<HTMLDivElement, InfoTipProps>(
function InfoTip(props, ref) {
const { children, buttonProps, ...rest } = props
return (
<ToggleTip content={children} {...rest} ref={ref}>
<IconButton
variant="ghost"
aria-label="info"
size="2xs"
colorPalette="gray"
{...buttonProps}
>
<HiOutlineInformationCircle />
</IconButton>
</ToggleTip>
)
},
)
或者,你可以使用以下命令将其添加到你的项目中。
¥Alternatively, you can add it to your project using the following command.
npx @chakra-ui/cli snippet add toggle-tip
该代码片段包含 Popover
组件的封闭组件组合。
¥The snippet includes a closed component composition for the Popover
component.
用法
¥Usage
import { InfoTip, ToggleTip } from "@/components/ui/toggle-tip"
<ToggleTip content="...">
<button />
</ToggleTip>
示例
¥Examples
信息提示
¥Info Tip
使用 InfoTip
组件显示信息提示。此组件默认渲染带有信息图标的图标按钮。
¥Use the InfoTip
component to display an info tip. This component renders an
icon button with an info icon by default.
用于在落地页中显示有关功能的附加信息。
File size: 1.45 kB
import { FormatByte, HStack, Text } from "@chakra-ui/react"
import { InfoTip } from "@/components/ui/toggle-tip"
const Demo = () => {
return (
<HStack justify="center">
<Text textStyle="lg">
File size: <FormatByte value={1450.45} />
</Text>
<InfoTip content="The file size for content.tsx is 1.45kb" />
</HStack>
)
}
属性
¥Props
根元素
¥Root
Prop | Default | Type |
---|---|---|
autoFocus | true | boolean Whether to automatically set focus on the first focusable content within the popover when opened. |
closeOnEscape | true | boolean Whether to close the popover when the escape key is pressed. |
closeOnInteractOutside | true | boolean Whether to close the popover when the user clicks outside of the popover. |
lazyMount | false | boolean Whether to enable lazy mounting |
modal | false | boolean Whether the popover should be modal. When set to `true`: - interaction with outside elements will be disabled - only popover content will be visible to screen readers - scrolling is blocked - focus is trapped within the popover |
portalled | true | boolean Whether the popover is portalled. This will proxy the tabbing behavior regardless of the DOM position of the popover content. |
skipAnimationOnMount | false | boolean Whether to allow the initial presence animation. |
unmountOnExit | false | boolean Whether to unmount on exit. |
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' The color palette of the component |
size | 'md' | 'xs' | 'sm' | 'md' | 'lg' The size of the component |
as | React.ElementType The underlying element to render. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | boolean Whether to remove the component's style. | |
defaultOpen | boolean The initial open state of the popover when rendered. Use when you don't need to control the open state of the popover. | |
id | string The unique identifier of the machine. | |
ids | Partial<{
anchor: string
trigger: string
content: string
title: string
description: string
closeTrigger: string
positioner: string
arrow: string
}> The ids of the elements in the popover. Useful for composition. | |
immediate | boolean Whether to synchronize the present change immediately or defer it to the next frame | |
initialFocusEl | () => HTMLElement | null The element to focus on when the popover is opened. | |
onEscapeKeyDown | (event: KeyboardEvent) => void Function called when the escape key is pressed | |
onExitComplete | VoidFunction Function called when the animation ends in the closed state | |
onFocusOutside | (event: FocusOutsideEvent) => void Function called when the focus is moved outside the component | |
onInteractOutside | (event: InteractOutsideEvent) => void Function called when an interaction happens outside the component | |
onOpenChange | (details: OpenChangeDetails) => void Function invoked when the popover opens or closes | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => void Function called when the pointer is pressed down outside the component | |
open | boolean The controlled open state of the popover | |
persistentElements | (() => Element | null)[] Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event | |
positioning | PositioningOptions The user provided options used to position the popover content | |
present | boolean Whether the node is present (controlled by the user) |