@closeio/smart-tooltip-delay

中文文档
1.0.0发布于 2020/09/24MITESM + CJS零依赖近 30 天下载 4.8万

Smart tooltip delay experience in a breeze

原始使用文档
文档目录7 节
  1. Install
  2. Benefits
  3. API
  4. Usage
  5. Example using Tippy.js
  6. Example using Bootstrap v2.3.2 Tooltips
  7. License

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

版本与发布记录

@closeio/smart-tooltip-delay 共发布 1 个版本,首次发布于 2020/09/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2020/09/246 年前

关于 @closeio/smart-tooltip-delay 的常见问题

@closeio/smart-tooltip-delay 是什么?

Smart tooltip delay experience in a breeze

如何安装 @closeio/smart-tooltip-delay?

在项目目录运行 npm install @closeio/smart-tooltip-delay,或使用 pnpm add / yarn add / bun add。

@closeio/smart-tooltip-delay 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/closeio__smart-tooltip-delay 是否存在。

@closeio/smart-tooltip-delay 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@closeio/smart-tooltip-delay 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@closeio/smart-tooltip-delay 可以商用吗?

@closeio/smart-tooltip-delay 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@closeio/smart-tooltip-delay 的下载量有多少?

根据 npm 官方下载统计,@closeio/smart-tooltip-delay 在 2026/10/10 – 2026/11/08 这 30 天内被下载 48,437 次,日均约 1,615 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@closeio/smart-tooltip-delay 还在维护吗?

最新版本 1.0.0 发布于 2020/09/24(6 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/09/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。