npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@closeio/smart-tooltip-delay

v1.0.0

Published

Smart tooltip delay experience in a breeze

Downloads

6,487

Readme

smart-tooltip-delay

NPM JavaScript Style Guide

Smart tooltip delay experience in a breeze.

This tiny library can help you create a tooltip experience where the first one is shown with a delay and any subsequent ones are shown immediately or with a different delay. After a defined amount of time of no tooltip activity, the original tooltip delay is restored.

The GIF above shows a smart tooltip delay where tooltips are shown after 600ms, but once the first one is shown, any subsequent ones are shown immediately. After 2500ms of no tooltip activity the original 600ms delay is restored.

Interested in working on projects like this? Close is looking for great engineers to join our team!

Install

yarn add @closeio/smart-tooltip-delay

Benefits

  • Extremely lightweight (less than 400B minzipped).
  • Library-agnostic approach — you can combine multiple tooltip libraries and have the smart delay working for each.
  • No other 3rd-party dependencies.

API

When instantiating SmartTooltipDelay, you can provide an object with 3 options:

  1. delay – default delay for each tooltip
  2. noDelay – delay set for each subsequently shown tooltip (going from delay to noDelay)
  3. resetAfterTime – time in milliseconds after which the default delay is restored (going from noDelay to delay)

Usage

To integrate with a tooltip library, follow these steps:

  1. const instance = new SmartTooltipDelay({ delay: 600, noDelay: 0, resetAfterTime: 2500 })
  2. Set your tooltip library's delay dynamically using instance.getCurrentDelay() whenever the user attempts to show a tooltip
  3. Whenever a tooltip is shown, call instance.show()
  4. Whenever a tooltip is hidden, call instance.hide()
  5. Enjoy smart tooltip experience

You can create a single SmartTooltipDelay instance for the entire app or you can create instances for each button toolbar.

Example using Tippy.js

This example uses a single shared smart delay and a tooltip library called Tippy.js.

sharedDelay.js

import SmartTooltipDelay from '@closeio/smart-tooltip-delay';

export default new SmartTooltipDelay({
  delay: 600,
  noDelay: 0,
  resetAfterTime: 2500,
});

MyTooltip.js

import sharedSmartDelay from './sharedDelay.js';

export default function MyTooltip({ content /** … more props … */ }) {
  // Show with a smart delay, hide immediately
  const delayArrayRef = useRef([sharedSmartDelay.getCurrentDelay(), 0]);

  // Update Tippy's delay when we try to interact with it.
  const handleOnTrigger = useCallback(() => {
    delayArrayRef.current[0] = sharedSmartDelay.getCurrentDelay();
  }, []);

  // Tell the smart delay that the tooltip has been shown.
  const handleOnShow = useCallback(() => {
    sharedSmartDelay.show();
  }, []);

  // Tell the smart delay that the tooltip has been hidden.
  const handleOnHide = useCallback(() => {
    sharedSmartDelay.hide();
  }, []);

  return (
    <Tippy
      delay={delayArrayRef.current}
      onTrigger={handleOnTrigger}
      onShow={handleOnShow}
      onHide={handleOnHide}
      // … more props
    >
      {content}
    </Tippy>
  );
}

Example using Bootstrap v2.3.2 Tooltips

This example uses a single shared smart delay and a tooltips from the Bootstrap framework.

sharedDelay.js

import SmartTooltipDelay from '@closeio/smart-tooltip-delay';

export default new SmartTooltipDelay({
  delay: 600,
  noDelay: 0,
  resetAfterTime: 2500,
});

applySmartDelay.js

import $ from 'jquery';
import sharedSmartDelay from './sharedDelay.js';

/**
 * Extends Bootstrap Tooltip v2.3.2 with smart delay.
 */
export default function applySmartDelay() {
  // Do "Smart" tooltip delay by default.
  $.fn.tooltip.defaults.delay = { show: sharedSmartDelay.getCurrentDelay() };

  // Override Tooltip's `enter` method to sneak in a different "show" delay.
  const Tooltip = $.fn.tooltip.Constructor;
  const _enter = Tooltip.prototype.enter;
  Tooltip.prototype.enter = function (e) {
    const self = $(e.currentTarget).data(this.type);

    if (self && self.options && self.options.hasSmartDelay) {
      self.options.delay = { show: sharedSmartDelay.getCurrentDelay() };
    }

    _enter.call(this, e);
  };

  // Set the "Smart" behaviour without overriding potential custom per-tooltip delay.
  const _getOptions = Tooltip.prototype.getOptions;
  Tooltip.prototype.getOptions = function (initialOptions) {
    const options = _getOptions.call(this, initialOptions);
    // If delay wasn't set, and it is not 0, use smart delay
    options.hasSmartDelay = !initialOptions.delay && initialOptions.delay !== 0;
    return options;
  };

  $(document).on({
    'show.bs.tooltip': sharedSmartDelay.show,
    'hide.bs.tooltip': sharedSmartDelay.hide,
  });
}

License

MIT © Close