/* * Copyright 2024 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import { Tooltip as AriaTooltip, TooltipProps as AriaTooltipProps, TooltipTrigger as AriaTooltipTrigger, TooltipTriggerComponentProps as AriaTooltipTriggerComponentProps, OverlayArrow, TooltipRenderProps } from 'react-aria-components/Tooltip'; import {centerPadding, setColorScheme, style} from '../style' with {type: 'macro'}; import {ColorScheme, ColorSchemeContext} from './Provider'; import { createContext, forwardRef, MutableRefObject, ReactNode, useCallback, useContext, useState } from 'react'; import {DOMProps, DOMRef, GlobalDOMAttributes} from '@react-types/shared'; import {UnsafeStyles} from './style-utils' with {type: 'macro'}; import {useDOMRef} from './useDOMRef'; import {useLocale} from 'react-aria/I18nProvider'; export interface TooltipTriggerProps extends Omit, Pick { /** The content of the tooltip. */ children: ReactNode; /** * The placement of the element with respect to its anchor element. * * @default 'top' */ placement?: 'start' | 'end' | 'right' | 'left' | 'top' | 'bottom'; } export interface TooltipProps extends Omit< AriaTooltipProps, | 'children' | 'className' | 'style' | 'render' | 'triggerRef' | 'UNSTABLE_portalContainer' | 'isEntering' | 'isExiting' | 'placement' | 'containerPadding' | 'offset' | 'crossOffset' | 'shouldFlip' | 'arrowBoundaryOffset' | 'isOpen' | 'defaultOpen' | 'onOpenChange' | keyof GlobalDOMAttributes >, DOMProps, UnsafeStyles { /** The content of the tooltip. */ children: ReactNode; } const tooltip = style({ ...setColorScheme(), justifyContent: 'center', alignItems: 'center', maxWidth: 160, minHeight: 24, boxSizing: 'border-box', font: 'ui-sm', color: { default: 'gray-25', forcedColors: 'ButtonText' }, borderWidth: { forcedColors: 1 }, borderStyle: { forcedColors: 'solid' }, borderColor: { forcedColors: 'transparent' }, backgroundColor: 'neutral', borderRadius: 'default', paddingX: 'edge-to-text', paddingY: centerPadding(), transition: 'default', transitionDuration: 200, transitionTimingFunction: { isExiting: 'in' }, translateX: { placement: { left: { isEntering: 4, isExiting: 4 }, right: { isEntering: -4, isExiting: -4 } } }, translateY: { placement: { top: { isEntering: 4, isExiting: 4 }, bottom: { isEntering: -4, isExiting: -4 } } }, opacity: { isEntering: 0, isExiting: 0 }, overflowWrap: { default: 'break-word' } }); const arrowStyles = style({ display: 'block', fill: 'gray-800', width: 10, height: 5, rotate: { placement: { top: 0, bottom: '180deg', left: '-90deg', right: '90deg' } }, translateX: { placement: { left: '-25%', right: '25%' } } }); let InternalTooltipTriggerContext = createContext>({}); /** * Display container for Tooltip content. Has a directional arrow dependent on its placement. */ export const Tooltip = forwardRef(function Tooltip( props: TooltipProps, ref: DOMRef ) { let {children, UNSAFE_style, UNSAFE_className = ''} = props; let domRef = useDOMRef(ref); let { containerPadding, crossOffset, placement = 'top', shouldFlip } = useContext(InternalTooltipTriggerContext); let colorScheme = useContext(ColorSchemeContext); let {locale, direction} = useLocale(); let [borderRadius, setBorderRadius] = useState(0); // TODO: should we pass through lang and dir props in RAC? let tooltipRef = useCallback( (el: HTMLDivElement) => { // oxlint-disable-next-line react/react-compiler (domRef as MutableRefObject).current = el; if (el) { el.lang = locale; el.dir = direction; let spectrumBorderRadius = typeof window !== 'undefined' ? window.getComputedStyle(el).borderRadius : ''; if (spectrumBorderRadius !== '') { setBorderRadius(parseInt(spectrumBorderRadius, 10)); } } }, [locale, direction, domRef] ); return ( UNSAFE_className + tooltip({...renderProps, colorScheme})}> {renderProps => ( <> {children} )} ); }); /** * TooltipTrigger wraps around a trigger element and a Tooltip. It handles opening and closing * the Tooltip when the user hovers over or focuses the trigger, and positioning the Tooltip * relative to the trigger. */ export function TooltipTrigger(props: TooltipTriggerProps): ReactNode { let {containerPadding, crossOffset, placement, shouldFlip, ...triggerProps} = props; return ( {props.children} ); } // This is purely so that storybook generates the types for both Menu and MenuTrigger interface ICombined extends Omit, TooltipTriggerProps {} // eslint-disable-next-line @typescript-eslint/no-unused-vars export function CombinedTooltip(props: ICombined): ReactNode { return
; }