Skip to content

Commit 17f0f83

Browse files
committed
support old browsers, api and doc changes
1 parent 45f8160 commit 17f0f83

File tree

6 files changed

+93
-46
lines changed

6 files changed

+93
-46
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2018 Mohsin Ul Haq
3+
Copyright (c) 2018-present Mohsin Ul Haq <[email protected]>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ React tooltip component based on [react-popper](https://github.com/FezVrasta/rea
88
React wrapper around [popper.js](https://popper.js.org/) library.
99

1010
## Example
11-
https://codesandbox.io/s/v04l1ky2rl
11+
https://codesandbox.io/s/pykkz77z5j
1212

1313
### Usage
1414

@@ -23,24 +23,51 @@ import TooltipTrigger from 'react-popper-tooltip';
2323

2424
render(
2525
<TooltipTrigger
26-
tooltip={({ getTooltipProps, arrowProps, arrowPlacement }) => (
27-
<div className="tooltip" {...getTooltipProps()}>
26+
placement="right"
27+
trigger="click"
28+
tooltip={({
29+
getTooltipProps,
30+
getArrowProps,
31+
tooltipRef,
32+
arrowRef,
33+
placement
34+
}) => (
35+
<div
36+
{...getTooltipProps({
37+
ref: tooltipRef,
38+
className: 'tooltip-container'
39+
/* your props here */
40+
})}
41+
>
2842
<div
29-
data-placement={arrowPlacement}
30-
className="arrow"
31-
{...arrowProps}
43+
{...getArrowProps({
44+
ref: arrowRef,
45+
'data-placement': placement,
46+
className: 'tooltip-arrow'
47+
/* your props here */
48+
})}
3249
/>
33-
{tooltip}
50+
<div className="tooltip-body">Hello, World!</div>
3451
</div>
3552
)}
3653
>
37-
{({ getTriggerProps }) => <span {...getTriggerProps()}>{children}</span>}
54+
{({ getTriggerProps, triggerRef }) => (
55+
<div
56+
{...getTriggerProps({
57+
ref: triggerRef,
58+
className: 'trigger'
59+
/* your props here */
60+
})}
61+
>
62+
Click Me!
63+
</div>
64+
)}
3865
</TooltipTrigger>,
3966
document.getElementById('root')
4067
);
4168
```
4269

43-
`TooltipTrigger` is the only component exposed by the package. It's just a positioning engine. What to render is left completely to the user, which can be provided using render props.
70+
`TooltipTrigger` is the only component exposed by the package. It's just a positioning engine. What to render is left completely to the user, which can be provided using render props. Your props should be passed through `getTriggerProps`, `getTooltipProps` and `getArrowProps`.
4471

4572
Read more about [render prop](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce) pattern if you're not familiar with this approach.
4673

@@ -52,25 +79,46 @@ If you would like our opinionated container and arrow styles for your tooltip fo
5279

5380
```jsx
5481
import React from 'react';
55-
import TooltipTrigger from "react-popper-tooltip";
82+
import TooltipTrigger from 'react-popper-tooltip';
5683
import 'react-popper-tooltip/dist/styles.css';
5784

5885
const Tooltip = ({ tooltip, children, ...props }) => (
5986
<TooltipTrigger
6087
{...props}
61-
tooltip={({ getTooltipProps, tooltipRef, arrowStyle, arrowRef, arrowPlacement }) => (
62-
<div className="tooltip-container" ref={tooltipRef} {...getTooltipProps()}>
88+
tooltip={({
89+
getTooltipProps,
90+
getArrowProps,
91+
tooltipRef,
92+
arrowRef,
93+
placement
94+
}) => (
95+
<div
96+
{...getTooltipProps({
97+
ref: tooltipRef,
98+
className: 'tooltip-container'
99+
})}
100+
>
63101
<div
64-
className="tooltip-arrow"
65-
ref={arrowRef}
66-
style={arrowStyle}
67-
data-placement={arrowPlacement}
102+
{...getArrowProps({
103+
ref: arrowRef,
104+
'data-placement': placement,
105+
className: 'tooltip-arrow'
106+
})}
68107
/>
69108
{tooltip}
70109
</div>
71110
)}
72111
>
73-
{({ getTriggerProps, triggerRef }) => <span ref={triggerRef} {...getTriggerProps()}>{children}</span>}
112+
{({ getTriggerProps, triggerRef }) => (
113+
<span
114+
{...getTriggerProps({
115+
ref: triggerRef,
116+
className: 'trigger'
117+
})}
118+
>
119+
{children}
120+
</span>
121+
)}
74122
</TooltipTrigger>
75123
);
76124

@@ -80,7 +128,7 @@ export default Tooltip;
80128
Then you can use it as shown in the example below.
81129

82130
```jsx
83-
<Tooltip tooltip="Hi there!" placement="top" trigger="click">Click me</Tooltip>
131+
<Tooltip placement="top" trigger="click" tooltip="Hi there!">Click me</Tooltip>
84132
```
85133

86134
## Props
@@ -211,20 +259,20 @@ yourself to avoid your props being overridden (or overriding the props returned)
211259
### children function
212260

213261
| property | type | description |
214-
|-----------------|----------------|-----------------------------------------------------------------------|
262+
| --------------- | -------------- | --------------------------------------------------------------------- |
215263
| getTriggerProps | `function({})` | returns the props you should apply to the trigger element you render. |
216264
| triggerRef | `node` | returns the react ref you should apply to the trigger element. |
217265

218266

219267
### tooltip function
220268

221-
| property | type | description |
222-
|-----------------|----------------|-----------------------------------------------------------------------|
223-
| getTooltipProps | `function({})` | returns the props you should apply to the tooltip element you render. |
224-
| tooltipRef | `node` | return the react ref you should apply to the tooltip element. |
225-
| arrowStyle | `object` | return the styles you should apply to the tooltip arrow style attr. |
226-
| arrowRef | `node` | return the react ref you should apply to the tooltip arrow you render.|
227-
| placement | `string` | return the placement of the tooltip arrow element. |
269+
| property | type | description |
270+
| --------------- | -------------- | ---------------------------------------------------------------------- |
271+
| getTooltipProps | `function({})` | returns the props you should apply to the tooltip element you render. |
272+
| tooltipRef | `node` | return the react ref you should apply to the tooltip element. |
273+
| arrowStyle | `object` | return the styles you should apply to the tooltip arrow style attr. |
274+
| arrowRef | `node` | return the react ref you should apply to the tooltip arrow you render. |
275+
| placement | `string` | return the placement of the tooltip arrow element. |
228276

229277
## Inspiration and Thanks!
230278

babel.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ const presets = [
22
[
33
'@babel/env',
44
{
5-
targets: {
6-
edge: '17',
7-
firefox: '60',
8-
chrome: '67',
9-
safari: '11.1'
10-
},
115
useBuiltIns: 'usage'
126
}
137
],

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"url": "https://github.com/mohsinulhaq/react-popper-tooltip"
99
},
1010
"main": "dist/index.js",
11+
"files": [
12+
"dist"
13+
],
1114
"scripts": {
12-
"clean": "rimraf dist/",
13-
"prepare": "npm run clean && babel src -d dist -s && cp src/styles.css dist"
15+
"clean": "rimraf dist",
16+
"prepare": "npm run clean && babel src -d dist && cp src/styles.css dist"
1417
},
1518
"keywords": [
1619
"react",
@@ -23,14 +26,11 @@
2326
"author": "Mohsin Ul Haq <[email protected]>",
2427
"license": "MIT",
2528
"browserslist": [
26-
"last 2 Chrome versions",
27-
"last 2 Firefox versions",
28-
"last 1 Edge version",
29-
"last 1 Safari version"
29+
"defaults"
3030
],
3131
"peerDependencies": {
32-
"react": "^16.5.0",
33-
"react-dom": "^16.5.0"
32+
"react": "^16.0.0",
33+
"react-dom": "^16.0.0"
3434
},
3535
"dependencies": {
3636
"react-popper": "^1.0.2"

src/Tooltip.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export default class Tooltip extends PureComponent {
8383
}
8484
}
8585

86+
getArrowProps = (props = {}) => ({
87+
...props,
88+
style: { ...props.style, ...this.props.arrowProps.style }
89+
});
90+
8691
getTooltipProps = (props = {}) => {
8792
const isHoverTriggered = this.props.trigger === 'hover';
8893

@@ -113,10 +118,10 @@ export default class Tooltip extends PureComponent {
113118
>
114119
{tooltip({
115120
getTooltipProps: this.getTooltipProps,
121+
getArrowProps: this.getArrowProps,
116122
tooltipRef: innerRef,
117-
arrowStyle: arrowProps.style,
118123
arrowRef: arrowProps.ref,
119-
arrowPlacement: placement
124+
placement
120125
})}
121126
</TooltipContext.Provider>
122127
);

src/TooltipTrigger.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import React, { PureComponent } from 'react';
55
import { createPortal } from 'react-dom';
66
import T from 'prop-types';
77
import { Manager, Reference, Popper } from 'react-popper';
8-
import { callAll } from './utils';
98
import Tooltip from './Tooltip';
9+
import { callAll } from './utils';
1010

1111
const DEFAULT_MODIFIERS = {
1212
preventOverflow: {
@@ -185,9 +185,9 @@ export default class TooltipTrigger extends PureComponent {
185185
ref,
186186
style,
187187
placement,
188+
arrowProps,
188189
outOfBoundaries,
189-
scheduleUpdate,
190-
arrowProps
190+
scheduleUpdate
191191
}) => (
192192
<TooltipContext.Consumer>
193193
{({

0 commit comments

Comments
 (0)