1
1
import React , { Children , useEffect , useRef , useState , useMemo } from 'react'
2
2
import type { SxProp } from '../sx'
3
- import sx from '../sx'
4
3
import { useId , useProvidedRefOrCreate , useOnEscapePress , useIsMacOS } from '../hooks'
5
4
import { invariant } from '../utils/invariant'
6
5
import { warning } from '../utils/warning'
7
- import styled from 'styled-components'
8
- import { get } from '../constants'
9
6
import { getAnchoredPosition } from '@primer/behaviors'
10
7
import type { AnchorSide , AnchorAlignment } from '@primer/behaviors'
11
8
import { isSupported , apply } from '@oddbird/popover-polyfill/fn'
12
- import { toggleStyledComponent } from '../internal/utils/toggleStyledComponent'
13
9
import { clsx } from 'clsx'
14
10
import classes from './Tooltip.module.css'
15
- import { useFeatureFlag } from '../FeatureFlags'
16
11
import { getAccessibleKeybindingHintString , KeybindingHint , type KeybindingHintProps } from '../KeybindingHint'
17
12
import VisuallyHidden from '../_VisuallyHidden'
18
13
import 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'
137
15
138
16
export type TooltipDirection = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
139
17
export type TooltipProps = React . PropsWithChildren <
@@ -203,6 +81,10 @@ const isInteractive = (element: HTMLElement) => {
203
81
}
204
82
export const TooltipContext = React . createContext < { tooltipId ?: string } > ( { } )
205
83
84
+ const BaseComponent = toggleSxComponent ( 'span' ) as React . ComponentType <
85
+ SxProp & React . HTMLAttributes < HTMLElement > & React . RefAttributes < HTMLSpanElement >
86
+ >
87
+
206
88
export const Tooltip = React . forwardRef (
207
89
(
208
90
{ direction = 's' , text, type = 'description' , children, id, className, keybindingHint, ...rest } : TooltipProps ,
@@ -212,7 +94,6 @@ export const Tooltip = React.forwardRef(
212
94
const child = Children . only ( children )
213
95
const triggerRef = useProvidedRefOrCreate ( forwardedRef as React . RefObject < HTMLElement > )
214
96
const tooltipElRef = useRef < HTMLDivElement > ( null )
215
- const enabled = useFeatureFlag ( CSS_MODULE_FEATURE_FLAG )
216
97
217
98
const [ calculatedDirection , setCalculatedDirection ] = useState < TooltipDirection > ( direction )
218
99
@@ -407,8 +288,8 @@ export const Tooltip = React.forwardRef(
407
288
child . props . onMouseLeave ?.( event )
408
289
} ,
409
290
} ) }
410
- < StyledTooltip
411
- className = { clsx ( className , { [ classes . Tooltip ] : enabled } ) }
291
+ < BaseComponent
292
+ className = { clsx ( className , classes . Tooltip ) }
412
293
ref = { tooltipElRef }
413
294
data-direction = { calculatedDirection }
414
295
{ ...rest }
@@ -440,7 +321,7 @@ export const Tooltip = React.forwardRef(
440
321
) : (
441
322
text
442
323
) }
443
- </ StyledTooltip >
324
+ </ BaseComponent >
444
325
</ >
445
326
</ TooltipContext . Provider >
446
327
)
0 commit comments