material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[Tooltip] Expose Composable Tooltip component

Open SPAHI4 opened this issue 11 years ago • 32 comments

Where i can find example of usage tooltip component without icon-button?

  • [ ] Closes #3665, #4671, #5057, #5227, #5340, #5567, #5752, #6778

SPAHI4 avatar Nov 20 '15 15:11 SPAHI4

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.

alitaheri avatar Nov 20 '15 17:11 alitaheri

That's true. I think that exposing a composable Tooltip component would be great. But that will require some work.

oliviertassinari avatar Nov 20 '15 18:11 oliviertassinari

+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>
        );

    }
});

evelant avatar Jan 06 '16 14:01 evelant

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)..

andrejunges avatar Feb 05 '16 12:02 andrejunges

It would be better indeed. Care to submit a PR? :grin:

alitaheri avatar Feb 05 '16 17:02 alitaheri

I'm gonna try to work on it over the weekend

andrejunges avatar Feb 05 '16 17:02 andrejunges

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.

oliviertassinari avatar Feb 05 '16 17:02 oliviertassinari

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.

newoga avatar Feb 05 '16 17:02 newoga

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: )

andrejunges avatar Feb 05 '16 18:02 andrejunges

I'm not sure using Popover is a good idea.

Right, I totally forgot about render to layer O.o

alitaheri avatar Feb 05 '16 19:02 alitaheri

+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)

adrianmcli avatar Feb 18 '16 17:02 adrianmcli

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

ChenLi0830 avatar Feb 21 '16 13:02 ChenLi0830

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

ConAntonakos avatar Mar 24 '16 16:03 ConAntonakos

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 avatar Mar 24 '16 18:03 ChenLi0830

@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 avatar May 18 '16 07:05 puranjayjain

@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 avatar May 18 '16 13:05 ChenLi0830

@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 avatar May 20 '16 03:05 puranjayjain

@puranjayjain Yeah use components which haven't been documented is not always straightforward. I'm glad it worked for you!

ChenLi0830 avatar May 20 '16 11:05 ChenLi0830

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?)

rob2d avatar May 27 '16 00:05 rob2d

@ChenLi0830 Do you know how to make tooltips appear under another component ? An svg icon for example.

jadus avatar Jun 22 '16 22:06 jadus