[Tooltip] Expose Composable Tooltip component
Where i can find example of usage tooltip component without icon-button?
- [ ] Closes #3665, #4671, #5057, #5227, #5340, #5567, #5752, #6778
Tooltip isn't documented. That means it's Implementation Detail. In other words, it can change api as frequently as the other components might need features from it. Use it at you own risk. But if you're ok with that the code from icon-button might help.
That's true. I think that exposing a composable Tooltip component would be great. But that will require some work.
+1 for exposing a composable tooltip component.
In the mean time here is some rough code I made from pulling apart icon-button. Entirely untested but seems to work in basic cases in my app.
Tooltip = React.createClass({
propTypes: {
tooltip: React.PropTypes.string,
tooltipPosition: React.PropTypes.string,
tooltipStyles: React.PropTypes.object,
touch: React.PropTypes.bool
},
getDefaultProps(){
return {
tooltipPosition: 'bottom-center'
};
},
getInitialState(){
return{
tooltipShown: false
};
},
getStyles() {
let styles = {
root: {
position: 'relative',
boxSizing: 'border-box'
},
tooltip: {
boxSizing: 'border-box'
}
};
return styles;
},
_showTooltip() {
this.setState({tooltipShown: true});
},
_hideTooltip() {
this.setState({tooltipShown: false});
},
_handleBlur(e) {
this._hideTooltip();
if (this.props.onBlur) this.props.onBlur(e);
},
_handleFocus(e) {
this._showTooltip();
if (this.props.onFocus) this.props.onFocus(e);
},
_handleMouseLeave(e) {
this._hideTooltip();
if (this.props.onMouseLeave) this.props.onMouseLeave(e);
},
_handleMouseEnter(e) {
this._showTooltip();
if (this.props.onMouseEnter) this.props.onMouseEnter(e);
},
mergeStyles(obj1, obj2){
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }
return obj1;
},
render(){
let {
tooltip,
touch,
...other,
} = this.props;
let tooltipPosition = this.props.tooltipPosition.split('-');
let styles = this.getStyles();
return(
<div
style={this.mergeStyles(styles.root, this.props.style)}
onBlur={this._handleBlur}
onFocus={this._handleFocus}
onMouseLeave={this._handleMouseLeave}
onMouseEnter={this._handleMouseEnter}>
<MUI.Tooltip
ref="tooltip"
label={tooltip}
show={this.state.tooltipShown}
touch={touch}
style={this.mergeStyles(styles.tooltip, this.props.tooltipStyles)}
verticalPosition={tooltipPosition[0]}
horizontalPosition={tooltipPosition[1]}/>
{this.props.children}
</div>
);
}
});
Wouldn't be better we use the Popover component to display the tooltip?
I had cases where the tooltips were being hidden per other absolute components (Header)..
It would be better indeed. Care to submit a PR? :grin:
I'm gonna try to work on it over the weekend
I'm not sure using Popover is a good idea. That seems overkill. RenderToLayer would be better.
They seems to have an interesting solution https://github.com/react-toolbox/react-toolbox/blob/dev/components/tooltip/Tooltip.jsx.
I'm not sure using Popover is a good idea. That seems overkill.
Yeah I agree. Plus we really ought to look into finding a way to make Popover not dependent on scroll state.
oh good point, RenderToLayer is ok for me as well, since it won't be a children from the element 'responsible' for the tooltip...
Th only thing I didn't like about the react-toolbox solution is that the tooltip is placed inside the 'responsible' element (the thing I'm suggesting to change :smile: )
I'm not sure using Popover is a good idea.
Right, I totally forgot about render to layer O.o
+1
Just started using MUI, and I'm thinking this is a much needed feature. How do we go about doing this? Can't we just grab the IconButton component and simply pull out the icon part? (look at @fignuts example above)
Material UI team has implemented a Tooltip component, but it's not documented yet. I tried it in my project and it works great so far.
Here is how I used it
<Tooltip show={this.state.showTooltip} label="tooltip label" horizontalPosition="left" verticalPosition="top" touch={true} />
Here is the link to the Tooltip src file
Can't wait for this. I've been using Google's MDL tooltip component so far, but would prefer a React implementation. Would love to contribute! @ChenLi0830, your link leads to a 404. :(
Is this what you meant? https://github.com/callemall/material-ui/blob/master/src/internal/Tooltip.js
Aha, they changed the file path. @ConAntonakos yeah, that's exactly what I meant. Change has been made. Thanks for pointing it out. :+1:
@ChenLi0830 how can I make it work with the fab button, it seems the snippet would work with other components but not the fab button any ideas?
@puranjayjain Can you be more specific on what the problem is? I haven't been using it recently, but 2 months ago I was using it with a FAB with no problem.
Here is pretty much what I had,
<FloatingActionButton
onTouchTap={this.handleFABClick}
onMouseEnter={()=>{this.setState({hoveredTooltip: true})}}
onMouseLeave={()=>{this.setState({hoveredTooltip: false})}}
/>
<Tooltip show={this.state.hoveredTooltip}
label={"this is a tip"}
style={{right: 62, top:16}}
horizontalPosition="left"
verticalPosition="top"
touch={true}
/>
@ChenLi0830 thanks a lot it worked! I was using the component inside the Fab component and overriding the styles (stupid mistake :smile: ) and that is the reason it wasn't working.
@puranjayjain Yeah use components which haven't been documented is not always straightforward. I'm glad it worked for you!
I know this is just an experimental/proposed sort of thing, but is there a way to get it to appear above Dialogs? zIndex doesn't seem to help with this sense (either as an MUI param or through the style prop)
edit: nevermind, works! :) (Putting it in Button content by default didn't work because of the overflow CSS; just had to make it visible Maybe for v0.16 something there can be a workaround for?)
@ChenLi0830 Do you know how to make tooltips appear under another component ? An svg icon for example.