Shards React
Shards React

Shards React

Documentation

Tooltip

Tooltips are powerful components powered behind the scenes by Popper.js that can be attached to any element.

Basic Example

Tooltips can be created using the Tooltip component.

import React from "react";
import { Tooltip, Button } from "shards-react";

export default class TooltipExample extends React.Component {
  constructor(props) {
    super(props);
    this.toggle = this.toggle.bind(this);
    this.state = { open: false };
  }

  toggle() {
    this.setState({
      open: !this.state.open
    });
  }

  render() {
    return (
      <div>
        <Button id="TooltipExample">Hover Me!</Button>
        <Tooltip
          open={this.state.open}
          target="#TooltipExample"
          toggle={this.toggle}
        >
          😁 Woo! I am a tooltip!
        </Tooltip>
      </div>
    );
  }
}

Placement

By default, the tooltips are placed on top. However, you can change this via the placement prop.

import React from "react";
import { Tooltip, Button } from "shards-react";

export default class TooltipPlacementExample extends React.Component {
  constructor(props) {
    super(props);
    this.toggle = this.toggle.bind(this);
    this.state = {
      top: false,
      left: false,
      bottom: false,
      right: false
    };
  }

  toggle(pos) {
    const newState = {};
    newState[pos] = !this.state[pos];
    this.setState({ ...this.state, ...newState });
  }

  render() {
    return (
      <div>
        <Button id="TooltipTop" className="mr-2">
          Top
        </Button>
        <Tooltip
          open={this.state.top}
          target="#TooltipTop"
          toggle={() => this.toggle("top")}
        >
          I am on top! ☝️
        </Tooltip>

        <Button id="TooltipBottom" className="mr-2">
          Bottom
        </Button>
        <Tooltip
          placement="bottom"
          open={this.state.bottom}
          target="#TooltipBottom"
          toggle={() => this.toggle("bottom")}
        >
          I am at the bottom! 👇
        </Tooltip>

        <Button id="TooltipLeft" className="mr-2">
          Left
        </Button>
        <Tooltip
          placement="left"
          open={this.state.left}
          target="#TooltipLeft"
          toggle={() => this.toggle("left")}
        >
          I am on the left! 👈
        </Tooltip>

        <Button id="TooltipRight" className="mr-2">
          Right
        </Button>
        <Tooltip
          placement="right"
          open={this.state.right}
          target="#TooltipRight"
          toggle={() => this.toggle("right")}
        >
          I am on the right! 👉
        </Tooltip>
      </div>
    );
  }
}

Triggers

You can control what triggers your tooltip to show using the trigger prop.

import React from "react";
import { Tooltip, Button } from "shards-react";

export default class TooltipClickExample extends React.Component {
  constructor(props) {
    super(props);
    this.toggle = this.toggle.bind(this);
    this.state = { open: false };
  }

  toggle() {
    this.setState({
      open: !this.state.open
    });
  }

  render() {
    return (
      <div>
        <Button id="TooltipClickExample">Click Me!</Button>
        <Tooltip
          trigger="click"
          open={this.state.open}
          target="#TooltipClickExample"
          toggle={this.toggle}
        >
          😁 You clicked to see me!
        </Tooltip>
      </div>
    );
  }
}

Props

The following props are available for the Tooltip component.

PropDescriptionTypeDefaultRequired
targetThe target element.Custom-No
containerThe tooltip container.Custom-No
triggerThe trigger(s) (click, hover, focus).String"hover"No
openWhether the tooltip is open, or not.BoolfalseNo
disabledWhether the tooltip is disabled, or not.BoolfalseNo
classNameThe tooltip class name.String-No
arrowClassNameThe arrow class name.String-No
innerClassNameThe tooltip inner class name.String-No
offsetThe tooltip offset.StringNumber-No
delayThe show/hide delay in ms.NumberShape{ show: 0, hide: 0 }No
boundariesElementThe boundaries element for the tooltip instance.StringElement-No
placementThe tooltip placement.Enum"top"No
placementPrefixThe placement prefix.String"bs-tooltip"No
noArrowWhether to hide the arrow, or not.BoolfalseNo
toggleThe toggle function.Funcfunction() {}No
modifiersPopper modifiers object.Object-No
autohideWhether the tooltip should auto-hide, or not.BooltrueNo