@closeio/smart-tooltip-delay

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

微风中智能工具提示延迟体验

中文使用文档
文档目录7 节
  1. 安装
  2. 福利
  3. API 密码
  4. 使用量
  5. 使用 Tipy.js 的示例
  6. 使用 Bootstrap v2.3.2 的示例 工具提示
  7. 许可证

智能工具提示延迟

NPM JavaScript Style Guide

智能工具提示在微风中延迟体验.

这个微小的库可以帮助您创建工具提示体验,其中第一个被显示为延迟,随后的任何一个被显示为立即或不同的延迟. 无工具提示活度的确定时间后,恢复原始工具提示延迟.

上面的GIF显示一个智能工具提示延迟,在600ms后显示工具提示,但一旦显示第一个提示,任何后继提示立即显示. 在2500ms没有工具提示活动后,恢复了原来的600ms延迟.

有兴趣做这样的项目吗? 关闭 正在寻找 伟大的工程师 加入我们的队伍!

安装

yarn add @closeio/smart-tooltip-delay

福利

  • 极轻(小于400B minzipped).
  • 库-不可知性方法——可以结合多个工具提示库,使智能延迟为每个库工作.
  • 没有其他第三方依赖关系.

API 密码

当瞬间 SmartTooltipDelay,可以提供3个选项:

  1. delay - 每个工具提示的默认延迟
  2. noDelay - 为随后显示的每个工具提示(从 delay 改为 noDelay(中文(简体) )
  3. resetAfterTime - 以毫秒计,然后恢复默认延迟的时间(从 noDelay 改为 delay(中文(简体) )

使用量

要与工具提示库相融合,请遵循这些步骤:

  1. const instance = new SmartTooltipDelay({ delay: 600, noDelay: 0, resetAfterTime: 2500 })
  2. 动态使用工具提示库设置延迟 instance.getCurrentDelay() 当用户试图显示工具提示时
  3. 显示工具提示时,请拨 instance.show()
  4. 每当工具提示被隐藏时,请打电话 instance.hide()
  5. 享受智能工具提示体验

您可以创建单曲 SmartTooltipDelay ,或者您可以为每个按钮工具栏创建实例.

使用 Tipy.js 的示例

此示例使用单个共享智能延迟和名为Tipy.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>
  );
}

使用 Bootstrap v2.3.2 的示例 工具提示

这个例子使用一个共享的智能延迟和从"靴子"框架中取出的工具提示.

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,
  });
}

许可证

麻省理工学院 关闭

版本与发布记录

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

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

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

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

微风中智能工具提示延迟体验

如何安装 @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 和提交记录判断。