智能工具提示延迟
智能工具提示在微风中延迟体验.
这个微小的库可以帮助您创建工具提示体验,其中第一个被显示为延迟,随后的任何一个被显示为立即或不同的延迟. 无工具提示活度的确定时间后,恢复原始工具提示延迟.
上面的GIF显示一个智能工具提示延迟,在600ms后显示工具提示,但一旦显示第一个提示,任何后继提示立即显示. 在2500ms没有工具提示活动后,恢复了原来的600ms延迟.
有兴趣做这样的项目吗? 关闭 正在寻找 伟大的工程师 加入我们的队伍!
安装
yarn add @closeio/smart-tooltip-delay
福利
- 极轻(小于400B minzipped).
- 库-不可知性方法——可以结合多个工具提示库,使智能延迟为每个库工作.
- 没有其他第三方依赖关系.
API 密码
当瞬间 SmartTooltipDelay,可以提供3个选项:
delay- 每个工具提示的默认延迟noDelay- 为随后显示的每个工具提示(从delay改为noDelay(中文(简体) )resetAfterTime- 以毫秒计,然后恢复默认延迟的时间(从noDelay改为delay(中文(简体) )
使用量
要与工具提示库相融合,请遵循这些步骤:
const instance = new SmartTooltipDelay({ delay: 600, noDelay: 0, resetAfterTime: 2500 })- 动态使用工具提示库设置延迟
instance.getCurrentDelay()当用户试图显示工具提示时 - 显示工具提示时,请拨
instance.show() - 每当工具提示被隐藏时,请打电话
instance.hide() - 享受智能工具提示体验
您可以创建单曲 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,
});
}
许可证
麻省理工学院 关闭
