|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | 'use client'; |
4 | | -import React, { useEffect } from 'react'; |
5 | | -import clsx from 'clsx'; |
| 4 | +import React from 'react'; |
6 | 5 |
|
7 | | -import { Portal } from '@cloudscape-design/component-toolkit/internal'; |
8 | | - |
9 | | -import { Transition } from '../internal/components/transition'; |
10 | | -import { fireNonCancelableEvent } from '../internal/events'; |
11 | | -import PopoverArrow from '../popover/arrow'; |
12 | | -import PopoverBody from '../popover/body'; |
13 | | -import PopoverContainer from '../popover/container'; |
14 | | -import { InternalTooltipProps, TooltipProps } from './interfaces'; |
15 | | - |
16 | | -import styles from './styles.css.js'; |
| 6 | +import useBaseComponent from '../internal/hooks/use-base-component'; |
| 7 | +import { applyDisplayName } from '../internal/utils/apply-display-name'; |
| 8 | +import { getExternalProps } from '../internal/utils/external-props'; |
| 9 | +import { TooltipProps } from './interfaces'; |
| 10 | +import InternalTooltip from './internal'; |
17 | 11 |
|
18 | 12 | export { TooltipProps }; |
19 | 13 |
|
20 | | -export default function Tooltip({ |
21 | | - content, |
22 | | - getTrack, |
23 | | - trackKey, |
24 | | - position = 'top', |
25 | | - __dismissOnScroll, |
26 | | - className, |
27 | | - onEscape, |
28 | | -}: InternalTooltipProps) { |
29 | | - const trackRef = React.useRef<HTMLElement | SVGElement | null>(null); |
30 | | - |
31 | | - // Update the ref with the current tracked element |
32 | | - React.useEffect(() => { |
33 | | - trackRef.current = getTrack(); |
| 14 | +const Tooltip = ({ position = 'top', ...rest }: TooltipProps) => { |
| 15 | + const baseComponentProps = useBaseComponent('Tooltip', { |
| 16 | + props: { position }, |
34 | 17 | }); |
| 18 | + const externalProps = getExternalProps(rest); |
35 | 19 |
|
36 | | - if (!trackKey && (typeof content === 'string' || typeof content === 'number')) { |
37 | | - trackKey = content; |
38 | | - } |
39 | | - |
40 | | - useEffect(() => { |
41 | | - const controller = new AbortController(); |
42 | | - window.addEventListener( |
43 | | - 'keydown', |
44 | | - (event: KeyboardEvent) => { |
45 | | - if (event.key === 'Escape') { |
46 | | - // Prevent any surrounding modals or dialogs from acting on this Esc. |
47 | | - event.stopPropagation(); |
48 | | - fireNonCancelableEvent(onEscape); |
49 | | - } |
50 | | - }, |
51 | | - { |
52 | | - // The tooltip is often activated on mouseover, which means the focus can |
53 | | - // be anywhere else on the page. Capture also means that this gets called |
54 | | - // before any wrapper modals or dialogs can detect it and act on it. |
55 | | - capture: true, |
56 | | - signal: controller.signal, |
57 | | - } |
58 | | - ); |
59 | | - return () => { |
60 | | - controller.abort(); |
61 | | - }; |
62 | | - }, [onEscape]); |
| 20 | + return <InternalTooltip position={position} {...externalProps} {...baseComponentProps} />; |
| 21 | +}; |
63 | 22 |
|
64 | | - return ( |
65 | | - <Portal> |
66 | | - <div className={clsx(styles.root, className)} data-testid={trackKey}> |
67 | | - <Transition in={true}> |
68 | | - {() => ( |
69 | | - <PopoverContainer |
70 | | - trackRef={trackRef} |
71 | | - trackKey={trackKey} |
72 | | - size="medium" |
73 | | - fixedWidth={false} |
74 | | - position={position} |
75 | | - zIndex={7000} |
76 | | - arrow={position => <PopoverArrow position={position} />} |
77 | | - hideOnOverscroll={__dismissOnScroll} |
78 | | - className={className} |
79 | | - > |
80 | | - <PopoverBody dismissButton={false} dismissAriaLabel={undefined} onDismiss={undefined} header={undefined}> |
81 | | - {content} |
82 | | - </PopoverBody> |
83 | | - </PopoverContainer> |
84 | | - )} |
85 | | - </Transition> |
86 | | - </div> |
87 | | - </Portal> |
88 | | - ); |
89 | | -} |
| 23 | +applyDisplayName(Tooltip, 'Tooltip'); |
| 24 | +export default Tooltip; |
0 commit comments