Created
February 16, 2019 22:02
-
-
Save aberba/f38fd71f377c3faae6054952577d4e2c to your computer and use it in GitHub Desktop.
React Component for Tippy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import ReactDOM from 'react-dom' | |
import Tippy from './ReactTippy' | |
class App extends React.Component { | |
state = { color: 'pink' } | |
render() { | |
return ( | |
<div> | |
<Tippy duration={200} delay={50} arrow={true} animation="scale"> | |
<button title="My title tooltip">Button text</button> | |
</Tippy> | |
<Tippy | |
html={ | |
<span style={{ color: this.state.color }}> | |
My HTML stateful tooltip | |
</span> | |
} | |
hideOnClick={false} | |
> | |
<button onClick={() => this.setState({ color: 'cyan' })}> | |
Button text | |
</button> | |
</Tippy> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render(<App />, document.getElementById('root')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import ReactDOM from 'react-dom' | |
import tippy from 'tippy.js' | |
class Tippy extends React.Component { | |
html = React.createRef() | |
componentDidMount() { | |
const { children, ...props } = this.props | |
this.tippyInstance = tippy.one(ReactDOM.findDOMNode(this), { | |
...props, | |
html: this.html.current | |
}) | |
} | |
componentDidUpdate() { | |
if (!this.props.html) { | |
this.tippyInstance.popper.querySelector('.tippy-content')[ | |
this.props.allowTitleHTML ? 'innerHTML' : 'textContent' | |
] = this.props.children.props.title | |
} | |
} | |
componentWillUnmount() { | |
if (this.tippyInstance) { | |
this.tippyInstance.destroy() | |
this.tippyInstance = null | |
} | |
} | |
render() { | |
return ( | |
<React.Fragment> | |
{this.props.children} | |
{this.props.html && <div ref={this.html}>{this.props.html}</div>} | |
</React.Fragment> | |
) | |
} | |
} | |
export default Tippy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
componentDidMount
.Where do you use
children
?about
ReactDOM.findDOMNode(this)
Do not use findDOMNodeeslint(react/no-find-dom-node)