Skip to content

Commit e6a5db6

Browse files
authored
fix(Tooltip): update TypeScript types for TooltipProps (#3159)
* fix(Tooltip): update TypeScript types for TooltipProps * chore: add changeset --------- Co-authored-by: Josh Black <[email protected]>
1 parent c3c8ad5 commit e6a5db6

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

.changeset/dirty-files-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Update TooltipProps to extend from StyledTooltip props

src/Tooltip/Tooltip.tsx

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@ import {invariant} from '../utils/invariant'
77
import styled from 'styled-components'
88
import {get} from '../constants'
99
import {useOnEscapePress} from '../hooks/useOnEscapePress'
10+
import {ComponentProps} from '../utils/types'
1011

11-
export type TooltipProps = {
12-
direction?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
13-
text?: string
14-
noDelay?: boolean
15-
align?: 'left' | 'right'
16-
wrap?: boolean
17-
type?: 'label' | 'description'
18-
'aria-label'?: React.AriaAttributes['aria-label']
19-
} & SxProp
20-
21-
export type TriggerPropsType = {
22-
'aria-describedby'?: string
23-
'aria-labelledby'?: string
24-
'aria-label'?: string
25-
onBlur?: React.FocusEventHandler
26-
onFocus?: React.FocusEventHandler
27-
onMouseEnter?: React.MouseEventHandler
28-
ref?: React.RefObject<HTMLElement>
29-
}
30-
31-
const TooltipEL = styled.div<TooltipProps>`
12+
const StyledTooltip = styled.div`
3213
// tooltip element itself
3314
position: absolute;
3415
z-index: 1000000;
@@ -294,7 +275,30 @@ const TooltipEL = styled.div<TooltipProps>`
294275
${sx};
295276
`
296277

297-
export const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
278+
export type TooltipProps = React.PropsWithChildren<
279+
{
280+
direction?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
281+
text?: string
282+
noDelay?: boolean
283+
align?: 'left' | 'right'
284+
wrap?: boolean
285+
type?: 'label' | 'description'
286+
'aria-label'?: React.AriaAttributes['aria-label']
287+
} & SxProp &
288+
ComponentProps<typeof StyledTooltip>
289+
>
290+
291+
export type TriggerPropsType = {
292+
'aria-describedby'?: string
293+
'aria-labelledby'?: string
294+
'aria-label'?: string
295+
onBlur?: React.FocusEventHandler
296+
onFocus?: React.FocusEventHandler
297+
onMouseEnter?: React.MouseEventHandler
298+
ref?: React.RefObject<HTMLElement>
299+
}
300+
301+
export const Tooltip = ({
298302
direction = 'n',
299303
// used for description type
300304
text,
@@ -306,7 +310,7 @@ export const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
306310
type = 'label',
307311
children,
308312
...rest
309-
}) => {
313+
}: TooltipProps) => {
310314
const id = useId()
311315
const triggerRef = useRef<HTMLElement>(null)
312316
const child = Children.only(children)
@@ -376,7 +380,7 @@ export const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
376380
child.props.onMouseEnter?.(event)
377381
},
378382
})}
379-
<TooltipEL
383+
<StyledTooltip
380384
data-direction={direction}
381385
data-state={open ? 'open' : undefined}
382386
data-align={align}
@@ -390,7 +394,7 @@ export const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
390394
id={id}
391395
>
392396
{text ?? label}
393-
</TooltipEL>
397+
</StyledTooltip>
394398
</Box>
395399
)
396400
}

src/Tooltip/__tests__/Tooltip.types.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export function shouldAcceptCallWithNoProps() {
55
return <Tooltip />
66
}
77

8+
export function shouldAcceptAdditionalProps() {
9+
return <Tooltip id="test" style={{}} className="test" />
10+
}
11+
812
export function shouldNotAcceptSystemProps() {
913
// @ts-expect-error system props should not be accepted
1014
return <Tooltip backgroundColor="thistle" />

0 commit comments

Comments
 (0)