Movatterモバイル変換


[0]ホーム

URL:


Tooltip

Tailwind CSS Tooltip

Use responsive tooltip component with helper examples for tooltip ui, tooltip on hover, disabled tooltip, direction & more. Free download, open-source license.

Required ES init: Tooltip*
* UMD autoinits are enabled by default. This means that you don't need to initialize the component manually. However if you are using TW ElementsES format then you should pass the required components to theinitTWE method.

Basic example

To get started with using tooltips all you need to do is add the data-twe-target="tooltip" data attribute to an element where elementId is the id of the tooltip component. In the following example you can see the button that will trigger the tooltip element to be shown when hovered or focused.

Hover the link to see thetooltip

                  <p>        Hover the link to see the        <a          href="#"          class="text-primary dark:text-primary-400"          data-twe-toggle="tooltip"          title="Hi! I'm tooltip"          >tooltip</a        >      </p>
                  // Initialization for ES Users      import {        Tooltip,        initTWE,      } from "tw-elements";      initTWE({ Tooltip });

Hey there 👋 we're excited about TW elements and want to see it grow! If you enjoy it, help the project grow by sharing it with your peers.Every share counts, thank you!

Four directions

                  <button        type="button"        class="inline-block rounded bg-primary-100 px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-primary-700 transition duration-150 ease-in-out hover:bg-primary-accent-200 focus:bg-primary-accent-200 focus:outline-none focus:ring-0 active:bg-primary-accent-200 dark:bg-primary-300 dark:hover:bg-primary-400 dark:focus:bg-primary-400 dark:active:bg-primary-400"        data-twe-toggle="tooltip"        data-twe-placement="top"        data-twe-ripple-init        data-twe-ripple-color="light"        title="Tooltip on top">        Tooltip on top      </button>      <button        type="button"        class="inline-block rounded bg-primary-100 px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-primary-700 transition duration-150 ease-in-out hover:bg-primary-accent-200 focus:bg-primary-accent-200 focus:outline-none focus:ring-0 active:bg-primary-accent-200 dark:bg-primary-300 dark:hover:bg-primary-400 dark:focus:bg-primary-400 dark:active:bg-primary-400"        data-twe-toggle="tooltip"        data-twe-placement="right"        data-twe-ripple-init        data-twe-ripple-color="light"        title="Tooltip on right">        Tooltip on right      </button>      <button        type="button"        class="inline-block rounded bg-primary-100 px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-primary-700 transition duration-150 ease-in-out hover:bg-primary-accent-200 focus:bg-primary-accent-200 focus:outline-none focus:ring-0 active:bg-primary-accent-200 dark:bg-primary-300 dark:hover:bg-primary-400 dark:focus:bg-primary-400 dark:active:bg-primary-400"        data-twe-toggle="tooltip"        data-twe-placement="bottom"        data-twe-ripple-init        data-twe-ripple-color="light"        title="Tooltip on bottom">        Tooltip on bottom      </button>      <button        type="button"        class="inline-block rounded bg-primary-100 px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-primary-700 transition duration-150 ease-in-out hover:bg-primary-accent-200 focus:bg-primary-accent-200 focus:outline-none focus:ring-0 active:bg-primary-accent-200 dark:bg-primary-300 dark:hover:bg-primary-400 dark:focus:bg-primary-400 dark:active:bg-primary-400"        data-twe-toggle="tooltip"        data-twe-placement="left"        data-twe-ripple-init        data-twe-ripple-color="light"        title="Tooltip on left">        Tooltip on left      </button>
                  // Initialization for ES Users      import {        Ripple,        Tooltip,        initTWE,      } from "tw-elements";            initTWE({ Ripple, Tooltip });

With custom HTML

                  <button        type="button"        class="inline-block rounded bg-primary-100 px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-primary-700 transition duration-150 ease-in-out hover:bg-primary-accent-200 focus:bg-primary-accent-200 focus:outline-none focus:ring-0 active:bg-primary-accent-200 dark:bg-primary-300 dark:hover:bg-primary-400 dark:focus:bg-primary-400 dark:active:bg-primary-400"        data-twe-toggle="tooltip"        data-twe-html="true"        data-twe-ripple-init        data-twe-ripple-color="light"        title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">        Tooltip with html      </button>
                  // Initialization for ES Users      import {        Ripple,        Tooltip,        initTWE,      } from "tw-elements";            initTWE({ Ripple, Tooltip });

Disabled

Initialize the tooltip with the disabled option specified.

                  <span        class="inline-block"        tabindex="0"        data-twe-toggle="tooltip"        title="Disabled tooltip">        <button          class="pointer-events-none inline-block rounded bg-primary px-6 pb-2 pt-2.5 text-xs font-medium uppercase leading-normal text-white shadow-[0_4px_9px_-4px_#3b71ca] transition duration-150 ease-in-out hover:bg-primary-600 hover:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.3),0_4px_18px_0_rgba(59,113,202,0.2)] focus:bg-primary-600 focus:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.3),0_4px_18px_0_rgba(59,113,202,0.2)] focus:outline-none focus:ring-0 active:bg-primary-700 active:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.3),0_4px_18px_0_rgba(59,113,202,0.2)] disabled:opacity-70 dark:shadow-[0_4px_9px_-4px_rgba(59,113,202,0.5)] dark:hover:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.2),0_4px_18px_0_rgba(59,113,202,0.1)] dark:focus:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.2),0_4px_18px_0_rgba(59,113,202,0.1)] dark:active:shadow-[0_8px_9px_-4px_rgba(59,113,202,0.2),0_4px_18px_0_rgba(59,113,202,0.1)]"          type="button"          disabled>          Disabled button        </button>      </span>
                  // Initialization for ES Users      import {        Tooltip,        initTWE,      } from "tw-elements";            initTWE({ Tooltip })

Related resources

Tutorials:

Extended Docs:

Generators and builders:

Design System (Figma):

Tooltip - API


Import

Importing components depends on how your application works. If you intend to use the TW elementsES format, you must first import the component and then initialize it with theinitTWE method. If you are going to use theUMD format, just import thetw-elements package.

                    import { Tooltip, initTWE } from "tw-elements";        initTWE({ Tooltip });
                    import "tw-elements";

Usage

Via data attributes

                    <p>          Hover the link to see the          <a href="#" data-twe-toggle="tooltip" title="Hi! I'm tooltip"            >tooltip</a          >        </p>

Via JavaScript

                      const exampleEl = document.getElementById('example');          const tooltip = new Tooltip(exampleEl);
                      const exampleEl = document.getElementById('example');          const tooltip = new twe.Tooltip(exampleEl);

Overflowauto andscroll

Tooltip position attempts to automatically change when a parent container hasoverflow: auto oroverflow: scroll like our.table-responsive, but still keeps the original placement’s positioning. To resolve, set theboundary option to anything other than default value,'scrollParent', such as'window':

                      const exampleEl = document.getElementById('example');          const tooltip = new Tooltip(exampleEl, {            boundary: 'window'          });
                      const exampleEl = document.getElementById('example');          const tooltip = new twe.Tooltip(exampleEl, {            boundary: 'window'          });

Markup

The required markup for a tooltip is only adata attribute andtitle on the HTML element you wish to have a tooltip. The generated markup of a tooltip is rather simple, though it does require a position (by default, set totop by the plugin).

Making tooltips work for keyboard and assistive technology users

You should only add tooltips to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). Although arbitrary HTML elements (such as<span>s) can be made focusable by adding thetabindex="0" attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce the tooltip in this situation. Additionally, do not rely solely onhover as the trigger for your tooltip, as this will make your tooltips impossible to trigger for keyboard users.

                    <!-- HTML to write -->        <a href="#" data-twe-toggle="tooltip" title="Some tooltip text!"          >Hover over me</a        >        <!-- Generated markup by the plugin -->        <div role="tooltip">          <div>Some tooltip text!</div>        </div>

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name todata-twe-, as indata-twe-toggle="tooltip".

Note: For security reasons thesanitize,sanitizeFn andwhiteList options cannot be supplied using data attributes.

Name Type DefaultDescription
container string/element/booleanfalse Appends the tooltip to a specific element. Example:container: 'body'. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize.
html booleanfalse Insert HTML into the tooltip. If false,innerText property will be used to insert content into the DOM. Use text if you're worried about XSS attacks.
placement string/function'right' How to position the tooltip - auto | top | bottom | left | right. Whenauto is specified, it will dynamically reorient the tooltip. When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second. Thethis context is set to the tooltip instance.
selector string/booleanfalse If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have tooltips added. See this and an informative example.
template string ` <div role="tooltip"> <div data-twe-tooltip-inner-ref ></div> </div> ` Base HTML to use when creating the tooltip. The tooltip'stitle will be injected into the .tooltip-header. The tooltip'scontent will be injected into the .tooltip-body.
title string/element/function- Default title value iftitle attribute isn't present. If a function is given, it will be called with its this reference set to the element that the tooltip is attached to.
trigger string'click'

How tooltip is triggered -click |hover |focus |manual. You may pass multiple triggers; separate them with a space.


'manual' indicates that the tooltip will be triggered programmatically via the.tooltip('show'),.tooltip('hide') and.tooltip('toggle') methods; this value cannot be combined with any other trigger.

offset number/string0 Offset of the tooltip relative to its target. For more information refer to Popper'soffset docs.
fallbackPlacement string/array'flip' Allow to specify which position Popper will use on fallback. For more information refer to Popper'sbehavior docs.
boundary element/string'clippingParents' Overflow constraint boundary of the tooltip (applies only to Popper's preventOverflow modifier). Accepts the values of'viewport','window','scrollParent', or an HTMLElement reference (via JavaScript only). For more information refer to Popper'sdetectOverflow docs.
sanitize booleantrue Enable or disable the sanitization. If activated'template','content' and'title' options will be sanitized.
whiteList objectDefault value Object which contains allowed attributes and tags.
sanitizeFn null/functionnull Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.
popperConfig null/object{ hide: true } To change Bootstrap's default Popper config, seePopper's configuration.

Data attributes for individual tooltips:
Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.


Methods

Asynchronous methods and transitions:
All API methods areasynchronous and start atransition. They return to the caller as soon as the transition is started butbefore it ends. In addition, a method call on atransitioning component will be ignored.

Method DescriptionExample
show Reveals an element’s tooltip.Returns to the caller before the tooltip has actually been shown (i.e. before theshown.twe.tooltip event occurs). This is considered a “manual” triggering of the tooltip. tooltips whose title and content are both zero-length are never displayed.instance.show()
hide Hides an element’s tooltip.Returns to the caller before the tooltip has actually been hidden (i.e. before thehidden.twe.tooltip event occurs). This is considered a “manual” triggering of the tooltip.instance.hide()
toggle Toggles an element’s tooltip.Returns to the caller before the tooltip has actually been shown or hidden (i.e. before theshown.twe.tooltip orhidden.twe.tooltip event occurs). This is considered a “manual” triggering of the tooltip.instance.toggle()
dispose Hides and destroys an element’s tooltip. tooltips that use delegation (which are created using theselector option) cannot be individually destroyed on descendant trigger elements.instance.dispose()
enable Gives an element’s tooltip the ability to be shown.tooltips are enabled by default.instance.enable()
disable Removes the ability for an element’s tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled.instance.disable()
toggleEnabled Toggles the ability for an element’s tooltip to be shown or hidden.instance.toggleEnabled()
update Updates the position of an element’s tooltip.instance.update()
getInstance Static method which allows you to get the tooltip instance associated with a DOM element.Tooltip.getInstance()
getOrCreateInstance Static method which allows you to get the tooltip instance associated with a DOM element or create a new one in case it wasn't initialized.Tooltip.getOrCreateInstance()
                        const tooltipEl = document.getElementById('tooltip');            const tooltip = new Tooltip(tooltipEl);            tooltip.show();
                        const tooltipEl = document.getElementById('tooltip');            const tooltip = new twe.Tooltip(tooltipEl);            tooltip.show();

Events

Event typeDescription
show.twe.tooltip This event fires immediately when theshow instance method is called.
shown.twe.tooltip This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).
hide.twe.tooltip This event is fired immediately when thehide instance method has been called.
hidden.twe.tooltip This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.twe.tooltip This event is fired after theshow.twe.tooltip event when the tooltip template has been added to the DOM.
                          const tooltipEl = document.getElementById('tooltip');              const tooltip = new twe.Tooltip(tooltipEl);              tooltipEl.addEventListener('hidden.twe.tooltip', () => {                // do something...              });              tooltip.hide();

[8]ページ先頭

©2009-2025 Movatter.jp