@atlaskit/tooltip
Version:
A tooltip is a floating, non-actionable label used to explain a user interface element or feature.
121 lines • 3.24 kB
JavaScript
import { clearScheduled, scheduleTimeout } from './shared-schedule';
let active = null;
export function show(entry) {
let phase = 'waiting-to-show';
function isActive() {
return Boolean(active && active.entry === entry);
}
function cleanup() {
if (isActive()) {
clearScheduled();
active = null;
}
}
function done() {
if (isActive()) {
entry.done();
}
phase = 'done';
cleanup();
}
function immediatelyHideAndDone() {
if (isActive()) {
entry.hide({ isImmediate: true });
}
done();
}
function keep() {
if (!isActive()) {
return;
}
// aborting a wait to hide
if (phase === 'waiting-to-hide') {
phase = 'shown';
clearScheduled();
return;
}
// aborting hide animation
if (phase === 'hide-animating') {
phase = 'shown';
clearScheduled();
entry.show({ isImmediate: false });
return;
}
}
function requestHide({ isImmediate }) {
if (!isActive()) {
return;
}
// If we were not showing yet anyway; finish straight away
if (phase === 'waiting-to-show') {
immediatelyHideAndDone();
return;
}
// already waiting to hide
if (phase === 'waiting-to-hide') {
return;
}
if (isImmediate) {
immediatelyHideAndDone();
return;
}
phase = 'waiting-to-hide';
scheduleTimeout(() => {
phase = 'hide-animating';
entry.hide({ isImmediate: false });
}, entry.delay);
}
function finishHideAnimation() {
if (isActive() && phase === 'hide-animating') {
done();
}
}
function isVisible() {
return (phase === 'shown' ||
phase === 'waiting-to-hide' ||
phase === 'hide-animating');
}
function getInitialMouse() {
if (entry.source.type === 'mouse') {
return entry.source.mouse;
}
return null;
}
function start() {
const showImmediately = Boolean(active && active.isVisible());
// If there was an active tooltip; we tell it to remove itself at once!
if (active) {
clearScheduled();
active.entry.hide({ isImmediate: true });
active.entry.done();
active = null;
}
// this tooltip is now the active tooltip
active = {
entry,
isVisible,
};
function show() {
phase = 'shown';
entry.show({ isImmediate: showImmediately });
}
if (showImmediately) {
show();
return;
}
phase = 'waiting-to-show';
scheduleTimeout(show, entry.delay);
}
// let's get started!
start();
const result = {
keep,
abort: cleanup,
isActive,
requestHide,
finishHideAnimation,
getInitialMouse,
};
return result;
}
//# sourceMappingURL=tooltip-manager.js.map