Skip to content

Commit e8e5f79

Browse files
author
Cédric Delpoux
committed
Add standalone version
1 parent 7724b5c commit e8e5f79

File tree

7 files changed

+230
-116
lines changed

7 files changed

+230
-116
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 2.0.0 - 2017-18-10
1+
# 2.1.0 - 2017-12-08
2+
3+
* Added: Standalone version
4+
5+
# 2.0.0 - 2017-10-18
26

37
* Added: Nwb build
48
* Update: Nwb build

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,32 @@ https://unpkg.com/react-simple-tooltip/dist/react-simple-tooltip.min.js.
2626

2727
## Usage
2828

29+
### Attached to a Component
30+
2931
```javascript
3032
import React from "react"
3133
import Tooltip from "react-simple-tooltip"
3234

3335
const App = () =>
34-
<Tooltip>
36+
<Tooltip content="😎">
3537
<button>Hover me !</button>
3638
</Tooltip>
3739
```
3840

41+
### Standalone
42+
```javascript
43+
import React from "react"
44+
import Tooltip from "react-simple-tooltip"
45+
46+
const App = () =>
47+
<div style={{position: "relative"}}>
48+
<Tooltip
49+
style={{position: "absolute", top: "50%", right: "0"}}
50+
content="😎"
51+
/>
52+
</div>
53+
```
54+
3955
## Demo
4056

4157
See [Demo page][github-page]

demo/src/routes.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import React from "react"
22
import ReactSimpleTooltip from "../../src"
3+
import styled from "styled-components"
34

45
import demoHtml from "./demo.md"
56
import readmeHtml from "../../README.md"
67

8+
const Zone = styled.div`
9+
background: #44b39d;
10+
color: #fff;
11+
padding: 10px;
12+
`
13+
714
const data = []
815

916
for (let x = 1; x <= 30; x++) {
@@ -17,7 +24,7 @@ const routes = [
1724
demo: {
1825
component: (
1926
<ReactSimpleTooltip placement="top" content="😎" arrow={15}>
20-
<button>Hover me !</button>
27+
<Zone>Hover me !</Zone>
2128
</ReactSimpleTooltip>
2229
),
2330
displayName: "ReactSimpleTooltip",
@@ -26,6 +33,31 @@ const routes = [
2633
},
2734
label: "Demo",
2835
},
36+
{
37+
path: "/standalone",
38+
demo: {
39+
component: (
40+
<div
41+
style={{
42+
position: "relative",
43+
height: "100px",
44+
background: "#fff",
45+
border: "1px solid #44B39D",
46+
}}
47+
>
48+
<ReactSimpleTooltip
49+
style={{position: "absolute", bottom: "0", left: "50%"}}
50+
placement="bottom"
51+
content="😎"
52+
/>
53+
</div>
54+
),
55+
displayName: "ReactSimpleTooltip",
56+
hiddenProps: ["children"],
57+
html: demoHtml,
58+
},
59+
label: "Standalone",
60+
},
2961
{
3062
path: "/readme",
3163
html: readmeHtml,

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-simple-tooltip",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"author": {
55
"name": "Cédric Delpoux",
66
"email": "[email protected]"
@@ -29,7 +29,6 @@
2929
"test:watch": "yarn test -- --watch"
3030
},
3131
"dependencies": {
32-
"prop-types": "^15.6.0",
3332
"styled-components": "^2.1.2"
3433
},
3534
"devDependencies": {
@@ -54,13 +53,14 @@
5453
"markdown-loader": "^2.0.1",
5554
"nwb": "^0.18.0",
5655
"prettier": "^1.5.3",
56+
"prop-types": "^15.6.0",
5757
"react": "^15.6.1",
5858
"react-demo-page": "^0.2.2",
5959
"react-dom": "^15.6.1",
6060
"react-test-renderer": "^15.6.1"
6161
},
6262
"peerDependencies": {
63-
"react": "^16.0.0",
64-
"react-dom": "^16.0.0"
63+
"react": "^15.0.0 || ^16.0.0",
64+
"react-dom": "^15.0.0 || ^16.0.0"
6565
}
6666
}

src/components/Tooltip/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ const Top = Base.extend`
1111
bottom: 100%;
1212
left: 50%;
1313
transform: translateX(-50%);
14-
${props => props.offset && `margin-bottom: ${props.offset}px;`};
14+
margin-bottom: ${props => props.offset}px;
1515
`
1616

1717
const Bottom = Base.extend`
1818
top: 100%;
1919
left: 50%;
2020
transform: translateX(-50%);
21-
${props => props.offset && `margin-top: ${props.offset}px;`};
21+
margin-top: ${props => props.offset}px;
2222
`
2323

2424
const Left = Base.extend`
2525
right: 100%;
2626
top: 50%;
2727
transform: translateY(-50%);
28-
${props => props.offset && `margin-right: ${props.offset}px;`};
28+
margin-right: ${props => props.offset}px;
2929
`
3030

3131
const Right = Base.extend`
3232
left: 100%;
3333
top: 50%;
3434
transform: translateY(-50%);
35-
${props => props.offset && `margin-left: ${props.offset}px;`};
35+
margin-left: ${props => props.offset}px;
3636
`
3737

3838
const tooltips = {
@@ -42,11 +42,11 @@ const tooltips = {
4242
bottom: Bottom,
4343
}
4444

45-
const Tooltip = ({children, open, zIndex, placement, offset}) => {
45+
const Tooltip = ({children, offset, open, placement, zIndex}) => {
4646
const Component = tooltips[placement] || tooltips.top
4747
return (
4848
open && (
49-
<Component open={open} zIndex={zIndex} offset={offset}>
49+
<Component offset={offset} open={open} zIndex={zIndex}>
5050
{children}
5151
</Component>
5252
)
@@ -55,10 +55,10 @@ const Tooltip = ({children, open, zIndex, placement, offset}) => {
5555

5656
Tooltip.propTypes = {
5757
children: PropTypes.any.isRequired,
58+
offset: PropTypes.number,
5859
open: PropTypes.bool,
59-
zIndex: PropTypes.number,
6060
placement: PropTypes.string,
61-
offset: PropTypes.number,
61+
zIndex: PropTypes.number,
6262
}
6363

6464
export default Tooltip

src/index.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,57 @@ class Wrapper extends React.Component {
3737
arrow,
3838
background,
3939
border,
40-
color,
4140
children,
41+
color,
42+
content,
4243
fixed,
4344
fontFamily,
4445
fontSize,
45-
content,
4646
padding,
4747
placement,
4848
radius,
4949
zIndex,
5050
...props
5151
} = this.props
52-
return (
52+
const hasTrigger = children !== undefined && children !== null
53+
const tooltipElement = (
54+
<Tooltip
55+
open={!hasTrigger || fixed ? true : open}
56+
placement={placement}
57+
offset={arrow}
58+
zIndex={zIndex}
59+
>
60+
<Bubble
61+
background={background}
62+
border={border}
63+
color={color}
64+
radius={radius}
65+
fontFamily={fontFamily}
66+
fontSize={fontSize}
67+
padding={padding}
68+
>
69+
<Arrow
70+
width={arrow}
71+
background={background}
72+
border={border}
73+
color={color}
74+
placement={placement}
75+
/>
76+
{content}
77+
</Bubble>
78+
</Tooltip>
79+
)
80+
return hasTrigger ? (
5381
<Container
5482
onMouseEnter={!fixed && this.handleMouseEnter}
5583
onMouseLeave={!fixed && this.handleMouseLeave}
5684
{...props}
5785
>
5886
{children}
59-
<Tooltip
60-
open={fixed ? true : open}
61-
placement={placement}
62-
offset={arrow}
63-
zIndex={zIndex}
64-
>
65-
<Bubble
66-
background={background}
67-
border={border}
68-
color={color}
69-
radius={radius}
70-
fontFamily={fontFamily}
71-
fontSize={fontSize}
72-
padding={padding}
73-
>
74-
<Arrow
75-
width={arrow}
76-
background={background}
77-
border={border}
78-
color={color}
79-
placement={placement}
80-
/>
81-
{content}
82-
</Bubble>
83-
</Tooltip>
87+
{tooltipElement}
8488
</Container>
89+
) : (
90+
<Container {...props}>{tooltipElement}</Container>
8591
)
8692
}
8793
}
@@ -90,7 +96,7 @@ Wrapper.propTypes = {
9096
arrow: PropTypes.number,
9197
background: PropTypes.string,
9298
border: PropTypes.string,
93-
children: PropTypes.any.isRequired,
99+
children: PropTypes.any,
94100
color: PropTypes.string,
95101
content: PropTypes.any.isRequired,
96102
fixed: PropTypes.bool,
@@ -106,6 +112,7 @@ Wrapper.defaultProps = {
106112
arrow: 8,
107113
background: "#000",
108114
border: "#000",
115+
children: null,
109116
color: "#fff",
110117
fixed: false,
111118
fontFamily: "inherit",

0 commit comments

Comments
 (0)