Skip to content

Commit bd87a55

Browse files
Forward ref and box props to Pulsar to fix Tooltip rendering (#1565)
1 parent 0c36c45 commit bd87a55

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

index.d.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,22 +2058,19 @@ export type PreOwnProps = TextOwnProps
20582058
export type PreProps = PolymorphicBoxProps<'pre', PreOwnProps>
20592059
export declare const Pre: BoxComponent<PreOwnProps, 'pre'>
20602060

2061-
export interface PulsarProps {
2061+
export interface PulsarOwnProps {
20622062
/**
2063-
* The position the Tooltip is on.
2063+
* The position the pulsar is displayed
20642064
*/
20652065
position?: Exclude<PositionTypes, 'top' | 'bottom' | 'left' | 'right'>
20662066
/**
20672067
* The size of the pulsar
20682068
*/
20692069
size?: number
2070-
/**
2071-
* Called when the Pulsar is clicked
2072-
*/
2073-
onClick?: PaneProps['onClick']
20742070
}
20752071

2076-
export declare const Pulsar: React.FC<PulsarProps>
2072+
export type PulsarProps = PolymorphicBoxProps<'div', PulsarOwnProps>
2073+
export declare const Pulsar: BoxComponent<PulsarOwnProps, 'div'>
20772074

20782075
export interface RadioOwnProps {
20792076
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"size": "size-limit",
5151
"storybook": "start-storybook -p 6006",
5252
"test": "yarn lint && yarn tsd && yarn jest",
53-
"test:watch": "yarn run test -- --watch",
53+
"test:watch": "yarn jest --watch",
5454
"update-docs": "cd docs && yarn add evergreen-ui@latest --exact && git add package.json yarn.lock && git show-branch --no-name HEAD | grep -E 'v[0-9]+.[0-9]+.[0-9]+' && git commit --amend --no-edit || git commit -m \"Updated doc site evergreen version\""
5555
},
5656
"engines": {

src/pulsar/__tests__/Pulsar.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
import { render, screen } from '@testing-library/react'
3+
import userEvent from '@testing-library/user-event'
4+
import { mockRef } from '../../test/utils'
5+
import { Tooltip } from '../../tooltip'
6+
import { Pulsar } from '../src/Pulsar'
7+
8+
const makePulsarFixture = (props = {}) => <Pulsar data-testid="Pulsar" {...props} />
9+
10+
describe('Pulsar', () => {
11+
it('should forward ref', () => {
12+
const ref = mockRef()
13+
14+
render(makePulsarFixture({ ref }))
15+
16+
expect(ref.current).toBeInstanceOf(HTMLDivElement)
17+
})
18+
19+
it('should forward box props', () => {
20+
const backgroundColor = 'red'
21+
22+
render(makePulsarFixture({ backgroundColor }))
23+
24+
expect(screen.getByTestId('Pulsar')).toHaveStyle({ backgroundColor })
25+
})
26+
27+
it('should render Tooltip when wrapped and hovered', () => {
28+
render(<Tooltip content="Hello world">{makePulsarFixture()}</Tooltip>)
29+
30+
userEvent.hover(screen.getByTestId('Pulsar'))
31+
32+
expect(screen.getByText('Hello world')).toBeInTheDocument()
33+
})
34+
})

src/pulsar/src/Pulsar.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { memo } from 'react'
1+
import React, { memo, forwardRef } from 'react'
22
import PropTypes from 'prop-types'
3-
import { keyframes } from 'ui-box'
3+
import Box, { keyframes } from 'ui-box'
44
import Positions from '../../constants/src/Position'
55
import { Pane } from '../../layers'
66
import { majorScale } from '../../scales'
@@ -21,7 +21,7 @@ const pulseAnimation = keyframes('pulseAnimation', {
2121
const animationTiming = 'cubic-bezier(0, 0, 0.58, 1)'
2222
const animationDuration = '1.8s'
2323

24-
const pularAnimationStyles = {
24+
const pulsarAnimationStyles = {
2525
animation: `${pulseAnimation} ${animationDuration} ${animationTiming} both infinite`
2626
}
2727

@@ -49,45 +49,49 @@ const getPositionProps = ({ position, size }) => {
4949
return props
5050
}
5151

52-
export const Pulsar = memo(({ position = Positions.TOP_RIGHT, size = majorScale(1), onClick }) => {
53-
const { colors } = useTheme()
54-
const positionProps = getPositionProps({ position, size })
55-
const outerPadding = size * 0.25
52+
export const Pulsar = memo(
53+
forwardRef(({ position = Positions.TOP_RIGHT, size = majorScale(1), onClick, ...rest }, ref) => {
54+
const { colors } = useTheme()
55+
const positionProps = getPositionProps({ position, size })
56+
const outerPadding = size * 0.25
5657

57-
return (
58-
<Pane
59-
position="absolute"
60-
borderRadius="50%"
61-
backgroundColor={colors.blue100}
62-
boxSizing="content-box"
63-
opacity={0.7}
64-
display="flex"
65-
alignItems="center"
66-
justifyContent="center"
67-
padding={outerPadding}
68-
{...pularAnimationStyles}
69-
onClick={onClick}
70-
cursor={onClick ? 'pointer' : undefined}
71-
{...positionProps}
72-
>
73-
<Pane width={size} height={size} backgroundColor={colors.blue200} borderRadius="50%" opacity={0.7} />
74-
</Pane>
75-
)
76-
})
58+
return (
59+
<Pane
60+
ref={ref}
61+
position="absolute"
62+
borderRadius="50%"
63+
backgroundColor={colors.blue100}
64+
boxSizing="content-box"
65+
opacity={0.7}
66+
display="flex"
67+
alignItems="center"
68+
justifyContent="center"
69+
padding={outerPadding}
70+
{...pulsarAnimationStyles}
71+
onClick={onClick}
72+
cursor={onClick ? 'pointer' : undefined}
73+
{...positionProps}
74+
{...rest}
75+
>
76+
<Pane width={size} height={size} backgroundColor={colors.blue200} borderRadius="50%" opacity={0.7} />
77+
</Pane>
78+
)
79+
})
80+
)
7781

7882
Pulsar.propTypes = {
7983
/**
80-
* The position of the pulsar
84+
* Composes the Box component as the base.
8185
*/
82-
position: PropTypes.oneOf([Positions.TOP_LEFT, Positions.TOP_RIGHT, Positions.BOTTOM_LEFT, Positions.BOTTOM_RIGHT]),
86+
...Box.propTypes,
8387

8488
/**
85-
* The width/height of the dot
89+
* The position of the pulsar
8690
*/
87-
size: PropTypes.number,
91+
position: PropTypes.oneOf([Positions.TOP_LEFT, Positions.TOP_RIGHT, Positions.BOTTOM_LEFT, Positions.BOTTOM_RIGHT]),
8892

8993
/**
90-
* Called when the Pulsar is clicked
94+
* The width/height of the dot
9195
*/
92-
onClick: PropTypes.func
96+
size: PropTypes.number
9397
}

src/pulsar/stories/index.stories.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Button } from '../../buttons'
55
import Position from '../../constants/src/Position'
66
import { Popover } from '../../popover'
77
import { minorScale } from '../../scales'
8+
import { Tooltip } from '../../tooltip'
89
import { Link, Text } from '../../typography'
910
import { Nudge } from '../src/Nudge'
1011
import { Pulsar } from '../src/Pulsar'
@@ -82,5 +83,11 @@ storiesOf('nudge', module)
8283
<Box>Pulsar Custom Size</Box>
8384
<Pulsar size={4} />
8485
</Box>
86+
<Box position="relative">
87+
<Box>Pulsar with Tooltip</Box>
88+
<Tooltip content="Hello world">
89+
<Pulsar />
90+
</Tooltip>
91+
</Box>
8592
</Box>
8693
))

0 commit comments

Comments
 (0)