11import React , { Children , useEffect , useRef , useState , useMemo } from 'react'
22import type { SxProp } from '../sx'
3- import sx from '../sx'
43import { useId , useProvidedRefOrCreate , useOnEscapePress , useIsMacOS } from '../hooks'
54import { invariant } from '../utils/invariant'
65import { warning } from '../utils/warning'
7- import styled from 'styled-components'
8- import { get } from '../constants'
96import { getAnchoredPosition } from '@primer/behaviors'
107import type { AnchorSide , AnchorAlignment } from '@primer/behaviors'
118import { isSupported , apply } from '@oddbird/popover-polyfill/fn'
12- import { toggleStyledComponent } from '../internal/utils/toggleStyledComponent'
139import { clsx } from 'clsx'
1410import classes from './Tooltip.module.css'
15- import { useFeatureFlag } from '../FeatureFlags'
1611import { getAccessibleKeybindingHintString , KeybindingHint , type KeybindingHintProps } from '../KeybindingHint'
1712import VisuallyHidden from '../_VisuallyHidden'
1813import useSafeTimeout from '../hooks/useSafeTimeout'
19-
20- const CSS_MODULE_FEATURE_FLAG = 'primer_react_css_modules_ga'
21-
22- const animationStyles = `
23- animation-name: tooltip-appear;
24- animation-duration: 0.1s;
25- animation-fill-mode: forwards;
26- animation-timing-function: ease-in;
27- animation-delay: 0s;
28- `
29-
30- const StyledTooltip = toggleStyledComponent (
31- CSS_MODULE_FEATURE_FLAG ,
32- 'span' ,
33- styled . span `
34- /* Overriding the default popover styles */
35- display: none;
36- &[popover] {
37- position: absolute;
38- padding: 0.5em 0.75em;
39- width: max-content;
40- margin: auto;
41- clip: auto;
42- white-space: normal;
43- font: normal normal 11px/1.5 ${ get ( 'fonts.normal' ) } ;
44- -webkit-font-smoothing: subpixel-antialiased;
45- color: var(--tooltip-fgColor, ${ get ( 'colors.fg.onEmphasis' ) } );
46- text-align: center;
47- word-wrap: break-word;
48- background: var(--tooltip-bgColor, ${ get ( 'colors.neutral.emphasisPlus' ) } );
49- border-radius: ${ get ( 'radii.2' ) } ;
50- border: 0;
51- opacity: 0;
52- max-width: 250px;
53- inset: auto;
54- /* for scrollbar */
55- overflow: visible;
56- }
57- /* class name in chrome is :popover-open */
58- &[popover]:popover-open {
59- display: block;
60- }
61- /* class name in firefox and safari is \:popover-open */
62- &[popover].\\:popover-open {
63- display: block;
64- }
65-
66- @media (forced-colors: active) {
67- outline: 1px solid transparent;
68- }
69-
70- // This is needed to keep the tooltip open when the user leaves the trigger element to hover tooltip
71- &::after {
72- position: absolute;
73- display: block;
74- right: 0;
75- left: 0;
76- height: var(--overlay-offset, 0.25rem);
77- content: '';
78- }
79-
80- /* South, East, Southeast, Southwest after */
81- &[data-direction='n']::after,
82- &[data-direction='ne']::after,
83- &[data-direction='nw']::after {
84- top: 100%;
85- }
86- &[data-direction='s']::after,
87- &[data-direction='se']::after,
88- &[data-direction='sw']::after {
89- bottom: 100%;
90- }
91-
92- &[data-direction='w']::after {
93- position: absolute;
94- display: block;
95- height: 100%;
96- width: 8px;
97- content: '';
98- bottom: 0;
99- left: 100%;
100- }
101- /* East before and after */
102- &[data-direction='e']::after {
103- position: absolute;
104- display: block;
105- height: 100%;
106- width: 8px;
107- content: '';
108- bottom: 0;
109- right: 100%;
110- margin-left: -8px;
111- }
112-
113- /* Animation definition */
114- @keyframes tooltip-appear {
115- from {
116- opacity: 0;
117- }
118- to {
119- opacity: 1;
120- }
121- }
122- /* Animation styles */
123- &:popover-open,
124- &:popover-open::before {
125- ${ animationStyles }
126- }
127-
128- /* Animation styles */
129- &.\\:popover-open,
130- &.\\:popover-open::before {
131- ${ animationStyles }
132- }
133-
134- ${ sx } ;
135- ` ,
136- )
14+ import { toggleSxComponent } from '../internal/utils/toggleSxComponent'
13715
13816export type TooltipDirection = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
13917export type TooltipProps = React . PropsWithChildren <
@@ -203,6 +81,10 @@ const isInteractive = (element: HTMLElement) => {
20381}
20482export const TooltipContext = React . createContext < { tooltipId ?: string } > ( { } )
20583
84+ const BaseComponent = toggleSxComponent ( 'span' ) as React . ComponentType <
85+ SxProp & React . HTMLAttributes < HTMLElement > & React . RefAttributes < HTMLSpanElement >
86+ >
87+
20688export const Tooltip = React . forwardRef (
20789 (
20890 { direction = 's' , text, type = 'description' , children, id, className, keybindingHint, ...rest } : TooltipProps ,
@@ -212,7 +94,6 @@ export const Tooltip = React.forwardRef(
21294 const child = Children . only ( children )
21395 const triggerRef = useProvidedRefOrCreate ( forwardedRef as React . RefObject < HTMLElement > )
21496 const tooltipElRef = useRef < HTMLDivElement > ( null )
215- const enabled = useFeatureFlag ( CSS_MODULE_FEATURE_FLAG )
21697
21798 const [ calculatedDirection , setCalculatedDirection ] = useState < TooltipDirection > ( direction )
21899
@@ -407,8 +288,8 @@ export const Tooltip = React.forwardRef(
407288 child . props . onMouseLeave ?.( event )
408289 } ,
409290 } ) }
410- < StyledTooltip
411- className = { clsx ( className , { [ classes . Tooltip ] : enabled } ) }
291+ < BaseComponent
292+ className = { clsx ( className , classes . Tooltip ) }
412293 ref = { tooltipElRef }
413294 data-direction = { calculatedDirection }
414295 { ...rest }
@@ -440,7 +321,7 @@ export const Tooltip = React.forwardRef(
440321 ) : (
441322 text
442323 ) }
443- </ StyledTooltip >
324+ </ BaseComponent >
444325 </ >
445326 </ TooltipContext . Provider >
446327 )
0 commit comments