Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new tooltip component to DS #2054

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Add new tooltip component to DS #2054

wants to merge 7 commits into from

Conversation

contolini
Copy link
Member

@contolini contolini commented Sep 18, 2024

We have two cf.gov apps that use tooltips but limited documentation on best practices or how to use them in new projects. This PR adds one implementation to the DS. It requires a third party library called Tippy.js.

It requires code in the following format:

Some sentence that requires additional clarity <span data-tooltip="relevant-tooltip">{% include 'icons/help-round.svg' %}</span>
<template id="relevant-tooltip">
    <div class="tippy-heading">Here's the tooltip heading</div>
    <div class="tippy-body">And the tooltip body</div>
</template>

Creating the CFPB theme requires augmenting the tippy-* classes. There's no CSS that's usable without the Tippy library so I didn't create an o-tooltip organism.

Testing

  1. Visit the tooltip page at the PR preview website.

Screenshots

tooltip

Notes

  • It doesn't feel great adding Tippy.js to the main package.json when its code will be used on few pages.

Todos

  • Add more documentation about usage and design guidelines.

Checklist

  • PR has an informative and human-readable title
  • Changes are limited to a single goal (no scope creep)
  • Code can be automatically merged (no conflicts)
  • Code follows the standards laid out in the CFPB development guidelines
  • Passes all existing automated tests
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future todos are captured in comments
  • Visually tested in supported browsers and devices (see checklist below 👇)
  • Project documentation has been updated
  • Reviewers requested with the Reviewers tool ➡️

Testing checklist

Browsers

  • Chrome on desktop
  • Firefox
  • Safari on macOS
  • Edge
  • Safari on iOS
  • Chrome on Android

Accessibility

  • Keyboard friendly
  • Screen reader friendly

Other

  • Is useable without CSS
  • Is useable without JS
  • Flexible from small to large screens
  • No linting errors or warnings
  • JavaScript tests are passing

Copy link

netlify bot commented Sep 18, 2024

Thanks for the improvements! Browse a preview of your changes using the link below.

Name Link
🔨 Latest commit e7a442b
🔍 Latest deploy log https://app.netlify.com/sites/cfpb-design-system/deploys/67290d4f1946f800083f0131
😎 Deploy Preview https://deploy-preview-2054--cfpb-design-system.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@jenn-franklin jenn-franklin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing.

Proposing some tweaks to the content:

Intro
A tooltip provides short, descriptive information when a user hovers or focuses on an element. It contains helpful but non-critical information and is useful in a space-constrained user interface.

Guidelines
Be judicious in using tooltips and explore other design options that keep content visible before choosing to use a tooltip. Only consider using a tooltip for short, non-critical information in a space-constrained user interface. Because a tooltip is hidden until activated, ensure content within a tooltip is not essential for completing a task on the page. For more guidance, see the USWDS tooltip page.

Behavior

  • Activate a tooltip by hovering or focusing on a help (question mark) icon situated next to text.
  • Press the escape key to dismiss open tooltips.
  • When a tooltip is at the edge of the user’s viewport, it should automatically reorient itself away from the edge of the screen to prevent content clipping.

Accessibility
As USWDS states, tooltips are progressive enhancements for the title attribute and will display as the title attribute if the component doesn’t initialize. When testing tooltips for accessibility, ensure they are compliant with USWDS tooltip accessibility tests.

@contolini contolini force-pushed the tooltips branch 5 times, most recently from 72fde13 to 97188f9 Compare October 24, 2024 15:34
@contolini
Copy link
Member Author

@anselmbradford after trying a handful of techniques to conditionally load the tooltip third-party dependency only when a tooltip is used, I ultimately decided to not bundle the tooltip code with the rest of the DS and instead added it as a separate entrypoint using the exports field. This allows developers to selectively add tooltips to their project by importing @cfpb/cfpb-design-system/tooltips. I added a note to the variation code snippet with usage instructions.

@anselmbradford
Copy link
Member

When resolving conflicts, you'll want to delete the npm-packages-offline-cache folder.

@anselmbradford
Copy link
Member

Should we have a cursor change on hover?

@anselmbradford
Copy link
Member

If JS is off, it seems like the (?) icons should be hidden.

@contolini
Copy link
Member Author

Should we have a cursor change on hover?

I say no because a pointer cursor indicates something will happen on click (and nothing does in our case).

If JS is off, it seems like the (?) icons should be hidden.

I originally didn't do this because I figured there are scenarios where a user might want some words in the middle of a sentence to trigger a tooltip and it'd be bad if those words disappeared when JS is disabled. But our guidance explicitly states that help icons should trigger them so I'll add this feature and include a note about it in the guidance section.

@anselmbradford
Copy link
Member

I say no because a pointer cursor indicates something will happen on click (and nothing does in our case).

If you're hovered on the (?), the tooltip shows. If you then click the (?), the tooltip is dismissed—so that's the action I was thinking the cursor change would indicate.

If not a cursor change, should there be a hover change on roll over? Or is that too button like? I noticed https://designsystem.digital.gov/components/tooltip/ has both a hover color change and a cursor change, but they also show the tooltip on buttons, so I'm unclear if that's completely covered by the buttons themselves, or if some of that is for the tooltip. Maybe @jenn-franklin can tell us how it should be and that's that.

@jenn-franklin
Copy link
Member

👍 to a cursor change + click to close. No hover change. Thanks for calling this out!

We have two cf.gov apps that use tooltips but limited documentation
on best practices or how to use them in new projects. This PR adds
one implementation to the DS. It requires a third party library called
Tippy.js.
The `exports` field allows multiple entrypoints to be defined instead of
just a single `main` entrypoint. This allows the bundled code to be selectively
imported by developers. For our purposes, we can package the majority of our DS
code into a single file but keep the tooltip code separate. Tooltips are used
on very few pages and they rely on a third party library that we don't want users
to unnecessarily download on every page of cf.gov.

See https://nodejs.org/api/packages.html#package-entry-points
@anselmbradford
Copy link
Member

Just in case you missed it—npm-packages-offline-cache still needs to be deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

4 participants