All files / app/ui/tooltip TooltipTrigger.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72  74x 74x                     115x 74x       115x       124x 124x 124x   124x     238x                                         242x                         115x 4x     115x 2x   74x  
import { Placement } from 'popper.js';
import React, { Component, ReactEventHandler, ReactNode } from 'react';
import { Manager, Popper, Reference } from 'react-popper';
 
export interface TooltipTriggerProps {
    placement?: Placement;
    tooltip: ReactNode;
}
 
export interface TooltipTriggerState {
    shouldShow: boolean;
}
 
export default class TooltipTrigger extends Component<TooltipTriggerProps, TooltipTriggerState> {
    static defaultProps = {
        placement: 'bottom',
    };
 
    state: Readonly<TooltipTriggerState> = {
        shouldShow: false,
    };
 
    render() {
        const { children, placement, tooltip } = this.props;
        const { shouldShow } = this.state;
 
        return (
            <Manager>
                <Reference>
                    { ({ ref }) => (
                        <span
                            onBlur={ this.handleHide }
                            onFocus={ this.handleShow }
                            onMouseEnter={ this.handleShow }
                            onMouseLeave={ this.handleHide }
                            ref={ ref }
                        >
                            { children }
                        </span>
                    ) }
                </Reference>
 
                <Popper
                    modifiers={ {
                        hide: { enabled: false },
                        flip: { enabled: false },
                        preventOverflow: { enabled: false },
                    } }
                    placement={ placement }
                >
                    { ({ ref, style }) => (
                        shouldShow && <div
                            ref={ ref }
                            style={ style }
                        >
                            { tooltip }
                        </div>
                    ) }
                </Popper>
            </Manager>
        );
    }
 
    private handleShow: ReactEventHandler<HTMLElement> = () => {
        this.setState({ shouldShow: true });
    };
 
    private handleHide: ReactEventHandler<HTMLElement> = () => {
        this.setState({ shouldShow: false });
    };
}