|
| 1 | +<!-- Copyright 2020 Adobe. All rights reserved. |
| 2 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 6 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 7 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 8 | +governing permissions and limitations under the License. --> |
| 9 | + |
| 10 | +import {Layout} from '@react-spectrum/docs'; |
| 11 | +export default Layout; |
| 12 | + |
| 13 | +import docs from 'docs:@react-spectrum/tooltip'; |
| 14 | +import {HeaderInfo, PropTable} from '@react-spectrum/docs'; |
| 15 | +import packageData from '@react-spectrum/tooltip/package.json'; |
| 16 | + |
| 17 | +```jsx import |
| 18 | +import {ActionButton} from '@react-spectrum/button'; |
| 19 | +import Delete from '@spectrum-icons/workflow/Delete'; |
| 20 | +import Edit from '@spectrum-icons/workflow/Edit'; |
| 21 | +import {Flex} from '@react-spectrum/layout'; |
| 22 | +import Question from '@spectrum-icons/workflow/Question'; |
| 23 | +import Resize from '@spectrum-icons/workflow/Resize'; |
| 24 | +import Save from '@spectrum-icons/workflow/SaveTo'; |
| 25 | +import {Text} from '@react-spectrum/text'; |
| 26 | +import ThumbUp from '@spectrum-icons/workflow/ThumbUp'; |
| 27 | +import { Tooltip, TooltipTrigger } from '@react-spectrum/tooltip'; |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | +category: Overlays |
| 32 | +keywords: [tooltip] |
| 33 | +--- |
| 34 | + |
| 35 | +# Tooltip |
| 36 | + |
| 37 | +<p>{docs.exports.Tooltip.description}</p> |
| 38 | + |
| 39 | +<HeaderInfo |
| 40 | + packageData={packageData} |
| 41 | + componentNames={['Tooltip']} |
| 42 | + sourceData={[ |
| 43 | + {type: 'Spectrum', url: 'https://spectrum.adobe.com/page/tooltip/'} |
| 44 | + ]} /> |
| 45 | + |
| 46 | +## Examples |
| 47 | +```tsx example |
| 48 | +<TooltipTrigger> |
| 49 | + <ActionButton aria-label="Edit Name"><Edit /></ActionButton> |
| 50 | + <Tooltip>Change Name</Tooltip> |
| 51 | +</TooltipTrigger> |
| 52 | +``` |
| 53 | + |
| 54 | +## Content |
| 55 | + |
| 56 | +Tooltips accept content as children. All content should be internationalized. |
| 57 | + |
| 58 | +### Accessibility |
| 59 | + |
| 60 | +Tooltip triggers must be focusable and hoverable in order to ensure that all users can activate them. When displayed, TooltipTrigger automatically associates the tooltip with the trigger element so that it is described by the tooltip content. |
| 61 | + |
| 62 | +## Tooltip Delay |
| 63 | + |
| 64 | +Tooltips should appear after a short delay when hovering the trigger, or instantly when using keyboard focus. This delay can be adjusted for hover. |
| 65 | +[View guidelines](https://spectrum.corp.adobe.com/page/tooltip/#Immediate-or-delayed-appearance) |
| 66 | + |
| 67 | +```tsx example |
| 68 | +<TooltipTrigger delay={0}> |
| 69 | + <ActionButton aria-label="Save"><Save /></ActionButton> |
| 70 | + <Tooltip>Saving applies your new settings right away.</Tooltip> |
| 71 | +</TooltipTrigger> |
| 72 | +``` |
| 73 | + |
| 74 | +### Warmup / Cooldown |
| 75 | + |
| 76 | +Tooltips have a warm up and cool down period, [see guidelines](https://spectrum.corp.adobe.com/page/tooltip/#Warmup-and-cooldown). |
| 77 | +Only one tooltip can be open at a time. |
| 78 | +```tsx example |
| 79 | +<Flex gap="size-200"> |
| 80 | + <TooltipTrigger> |
| 81 | + <ActionButton>Hover me</ActionButton> |
| 82 | + <Tooltip>I come up after a delay.</Tooltip> |
| 83 | + </TooltipTrigger> |
| 84 | + <TooltipTrigger> |
| 85 | + <ActionButton>Then hover me</ActionButton> |
| 86 | + <Tooltip>If you did it quickly, I appear immediately.</Tooltip> |
| 87 | + </TooltipTrigger> |
| 88 | +</Flex> |
| 89 | +``` |
| 90 | + |
| 91 | +## Tooltip placement |
| 92 | + |
| 93 | +Tooltips support a variety of placement options. |
| 94 | + |
| 95 | +### Placement |
| 96 | + |
| 97 | +The Tooltip's placement with respect to its trigger element can be adjusted using the `placement` |
| 98 | +prop. See the props table [below](#tooltiptrigger-props) for a full list of available placement combinations. |
| 99 | + |
| 100 | +```tsx example |
| 101 | +<TooltipTrigger placement="end"> |
| 102 | + <ActionButton aria-label="Foo">Placement</ActionButton> |
| 103 | + <Tooltip>In left-to-right, this is on the right. In right-to-left, this is on the left.</Tooltip> |
| 104 | +</TooltipTrigger> |
| 105 | +``` |
| 106 | + |
| 107 | +### Offset and cross offset |
| 108 | + |
| 109 | +The Tooltip's offset with respect to its trigger can be adjusted using the `offset` and |
| 110 | +`crossOffset` props. The `offset` prop controls the spacing applied along the main axis between the element and its |
| 111 | +trigger whereas the `crossOffset` prop handles the spacing applied along the cross axis. |
| 112 | + |
| 113 | +Below is a tooltip offset by an additional 50px above the trigger. |
| 114 | +```tsx example |
| 115 | +<TooltipTrigger offset={50}> |
| 116 | + <ActionButton aria-label="Offset from trigger">Offset</ActionButton> |
| 117 | + <Tooltip>This will shift up.</Tooltip> |
| 118 | +</TooltipTrigger> |
| 119 | +``` |
| 120 | +Below is a tooltip cross offset by an additional 100px to the right of the trigger. |
| 121 | +```tsx example |
| 122 | +<TooltipTrigger crossOffset={100} placement="bottom"> |
| 123 | + <ActionButton aria-label="Cross Offset from trigger">Cross Offset</ActionButton> |
| 124 | + <Tooltip>This will shift over to the right.</Tooltip> |
| 125 | +</TooltipTrigger> |
| 126 | +``` |
| 127 | + |
| 128 | +## Events |
| 129 | + |
| 130 | +TooltipTrigger accepts an `onOpenChange` handler which is triggered whenever the Tooltip is shown or hidden. |
| 131 | + |
| 132 | +The example below uses `onOpenChange` to update a separate element with the current open state of the |
| 133 | +Dialog. |
| 134 | + |
| 135 | +```tsx example |
| 136 | +function Example() { |
| 137 | + let [state, setState] = React.useState(false); |
| 138 | + |
| 139 | + return ( |
| 140 | + <Flex alignItems="center" gap="size-100"> |
| 141 | + <TooltipTrigger onOpenChange={(isOpen) => setState(isOpen)}> |
| 142 | + <ActionButton aria-label="Resize"><Resize /></ActionButton> |
| 143 | + <Tooltip>Resize text.</Tooltip> |
| 144 | + </TooltipTrigger> |
| 145 | + <Text>Current open state: {state.toString()}</Text> |
| 146 | + </Flex> |
| 147 | + ); |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +## Props |
| 152 | + |
| 153 | +### TooltipTrigger Props |
| 154 | +<PropTable component={docs.exports.TooltipTrigger} links={docs.links} /> |
| 155 | + |
| 156 | +### Tooltip Props |
| 157 | +<PropTable component={docs.exports.Tooltip} links={docs.links} /> |
| 158 | + |
| 159 | +## Visual options |
| 160 | +[View guidelines](https://spectrum.adobe.com/page/tooltip/#Table-of-options) |
| 161 | + |
| 162 | +**Positive** |
| 163 | +```tsx example |
| 164 | +<TooltipTrigger> |
| 165 | + <ActionButton aria-label="Approve"><ThumbUp /></ActionButton> |
| 166 | + <Tooltip variant="positive" showIcon>Approve workflow.</Tooltip> |
| 167 | +</TooltipTrigger> |
| 168 | +``` |
| 169 | + |
| 170 | +**Information** |
| 171 | +```tsx example |
| 172 | +<TooltipTrigger> |
| 173 | + <ActionButton aria-label="Information"><Question /></ActionButton> |
| 174 | + <Tooltip variant="info" showIcon>More information menu.</Tooltip> |
| 175 | +</TooltipTrigger> |
| 176 | +``` |
| 177 | + |
| 178 | +**Negative** |
| 179 | +```tsx example |
| 180 | +<TooltipTrigger> |
| 181 | + <ActionButton aria-label="Danger Will Robinson"><Delete /></ActionButton> |
| 182 | + <Tooltip variant="negative" showIcon>Dangerous action.</Tooltip> |
| 183 | +</TooltipTrigger> |
| 184 | +``` |
| 185 | + |
| 186 | +## Options |
| 187 | +A TooltipTrigger can be disabled without disabling the trigger it displays on. |
| 188 | + |
| 189 | +**isDisabled** |
| 190 | +```tsx example |
| 191 | +<TooltipTrigger isDisabled> |
| 192 | + <ActionButton aria-label="Danger Will Robinson" onPress={() => alert('pressed trigger')}><Delete /></ActionButton> |
| 193 | + <Tooltip variant="negative" showIcon>Dangerous action.</Tooltip> |
| 194 | +</TooltipTrigger> |
| 195 | +``` |
0 commit comments