Giter Club home page Giter Club logo

a11y_tooltips's Introduction

ARIA Tooltips or "Tooltips"

The term "tooltip" can mean different things depending on the designer or developer's intent.

A tooltip traditionally is meant to be used to provide short, non-essential, description to an element. However, "tooltips" are often used to reveal an element's accessible name. Often visually indistinguishable from each other, this script at least aims to allow developers to set whether the "tooltip" is meant to reveal the accessible name for the element, or whether it's meant to provide a description to the element it is associated with.

Rules for a tooltip

Mouse hover or keyboard focus should reveal the tooltip for the element it is associated with.

An element should only have a single "tooltip" associated with it. This means that a developer can only set a tooltip to either contain the element's accessible name, or a description.

An element with a custom tooltip cannot also have a title attribute to produce a native tooltip. This script will always remove a title attribute from the trigger of the tooltip.

A user should be able to hit the escape key to close a tooltip, regardless of if that tooltip was opened by focus or hover of an element.

Tooltips should be brief in their content. If an element requires a description that necessitates more than a brief explanation, the use of imagery, or more complex markup patterns than a text string (e.g. a list), then a "tooltip" is not the appropriate solution for your UI need.

Tooltips in general are a bit quirky on touch devices, since there's no "hovering". If you have a keyboard setup to work with your touch device, then the tooltips should be revealed when their trigger receives keyboard focus. If you do get a tooltip to display on tap, a touch listener becomes active to allow for a tap anywhere in the current viewport to dismiss the tooltip.

Minimum Required Mark-up

The script requires that the element that should have an associated tooltip be a child of a containing element that the script will search for.

<wrapping-element data-tooltip>
  <trigger-element data-tooltip-trigger>...</trigger-element>
</wrapping-element>

Depending on the context in which the "trigger element" (often an interactive control) lives in the DOM, the "wrapping element" should meet the expectations for its parent element's content model.

For instance:

<ul>
  <li data-tooltip>
    <trigger-element data-tooltip-trigger>...</trigger-element>
  </li>
</ul>

<!-- or -->

<div data-tooltip>
  <trigger-element data-tooltip-trigger>...</trigger-element>
</div>

<!-- or -->

<p data-tooltip>
  <trigger-element data-tooltip-trigger>...</trigger-element>
</p>

If the trigger element is meant to exist as a child element in an instance where a block level element were not appropriate (e.g. a paragraph), then the wrapping element should be a span.

<p>
  ...
  <span data-tooltip>
    <trigger-element data-tooltip-trigger>...</trigger-element>
  </span>
...

Customize the script

You may rather use your own data attributes, or classes with this script. As one of the function's parameters, you can pass in your values to overwrite the following:

tipConfig = {
  baseID: 'tt_',
  ariaHiddenTip: true,

  tipWrapperClass: 'tooltip',
  tipContentClass: 'tooltip__content',

  tipTypeAttr: 'data-tooltip',
  tipContentAttr: 'data-tooltip-content',

  tipSelector: '[data-tooltip-source]',
  triggerSelector: '[data-tooltip-trigger]'
}

Attributes

data-tooltip
This attribute is the default hook for the script to search for and find tooltip widgets in the DOM. The attribute an either be set to the empty string, or may accept the token "label", in which it will run the setup for the tooltip to display the accessible name of the trigger element.

data-tooltip-trigger This attribute is required to be placed on the element that will have the tooltip associated with it. This element should be an element that can natively receive keyboard focus, e.g. a button, <a href="...">, or a form control.

data-tooltip-content
This attribute may be used on the widget wrapping element (along with data-tooltip) to define the text string for the generated tooltip.

For example:

<div data-tooltip data-tooltip-content="Deleting an asset is permanent">
  <button data-tooltip-trigger>
    Delete
  </button>
</div>

data-tooltip-tip
This selector should be placed on the child element of data-tooltip that is the hard coded fall back for the tooltip, if this script were to not function.

For example:

<div data-tooltip>
  <button data-tooltip-trigger aria-describedby="foo">
    Delete
  </button>
  <p data-tooltip-tip id="foo">
    Deleting an asset is permanent.
  </p>
</div>

Default generated classes and tooltip markup

.tooltip
The outter wrapper of the tooltip content. This class allows for initial positioning of the tooltip.

.tooltip__content
The element that will receive an id to be referenced by the trigger element. If the tooltip acts as a description to the associated element, it will receive the role="tooltip" attribute (not that this role presently does all that much... the aria-describedby from the owning element (trigger) that points to the id of the tooltip is the attribute that is the most important for associating the tooltip with its associated element).

Tooltips, whether a description or accessible name will receive an aria-hidden="true" attribute, this can be modified by setting the ariaHiddenTip config option to false. The aria-hidden="true" will ensure that the tooltip cannot be navigated to by a screen reader's virtual cursor, resulting in a duplicate announcement of the content that was already announced when focusing the element that the tooltip is associated with. This default setting may change depending on future clarification in the ARIA specification about the use of role="tooltip".

License & Such

This script was written by Scott O'Hara.

It has an MIT license.

Do with it what you will :)

a11y_tooltips's People

Contributors

jfhector avatar scottaohara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

a11y_tooltips's Issues

Should the tooltip hoverable area be increased?

The following may not be a real scenario as I am not an expert screen magnifier user nor have I dexterity problems.

The problem:

  1. When having zoomed into the page with a screen magnifier, longer information in tooltips can become cut off from the viewport
  2. When panning to see the full information, if the mouse is moved outside of the tooltip the tooltip can disappear

I appreciate that isn't a WCAG requirement, but I was wondering whether it would be a decent thing to do to help some people out.

I see this problem similar to a standard menu navigation that shows a submenu and the submenu disappears if panning outside of it.

data-tooltip-source functionality described in readme, but not implemented

Thanks for creating this, it seems like a nice component :)

I tried out the data-tooltip-source thing and wondered why it wasn't working. I examined the source and noticed the code does not know about any tipSourceAttr in reality and has no respective functionality to deal with it.

Maybe you have a local version with this, but did not push it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.