Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | 5x 117x 117x 6x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 15x 15x 111x 111x 102x 111x 111x 111x 4x 4x 14x 14x 14x 14x 14x 1x 13x 13x 13x 4x 4x 9x 10x 10x 10x 10x 10x 10x 10x 10x 16x 16x 16x 16x 14x 16x 2x 14x 5x 5x 5x 4x 1x 3x 3x 53x 53x 53x 53x 53x 15x 15x 2x 54x 54x 54x 54x 54x 54x 54x 53x 53x 53x 53x 53x 53x 53x 42x 38x 38x 4x 4x 3x 42x 42x 1x 42x 42x 39x 102x 33x 16x 16x 22x 55x 18x 37x 37x 37x 37x 37x 425x 425x 111x 214x 214x 212x 212x 107x 107x 102x 102x 2x 1x 1x 1x 1x 1x 1x 214x 15x 58x 2x 2x 24x 24x 6x 6x 6x 6x 6x 6x 6x 6x 1x 44x 1x 43x 43x 1x 42x 42x 42x 42x 42x 42x 38x 16x 22x 21x 22x 1x 1x 1x 22x 22x 3x 22x 22x 22x 2x 2x 2x 2x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 16x 16x 16x 16x 16x 16x 16x 16x 1x 15x 4x 15x 15x 15x 15x 1x 1x 15x 5x 15x 5x 15x | import Popper from 'popper.js' import { Defaults } from './defaults' import { Selectors } from './selectors' import { isUsingTouch, isIE, supportsTouch } from './bindGlobalEventListeners' import { createPopperElement, elementCanReceiveFocus, getChildren, computeArrowTransform, updatePopperPosition, getPopperPlacement, getOffsetDistanceInPx, getValue, closest, closestCallback, isCursorOutsideInteractiveBorder, applyTransitionDuration, setVisibilityState, updatePopperElement, evaluateProps, defer, toArray, focus, toggleTransitionEndListener, debounce } from './utils' let idCounter = 1 export default function createTippy(reference, collectionProps) { const props = evaluateProps(reference, collectionProps) // If the reference shouldn't have multiple tippys, return null early if (!props.multiple && reference._tippy) { return null } /* ======================= 🔒 Private members 🔒 ======================= */ let popperMutationObserver = null let lastTriggerEvent = {} let lastMouseMoveEvent = null let showTimeoutId = 0 let hideTimeoutId = 0 let isPreparingToShow = false let transitionEndListener = () => {} let listeners = [] let referenceJustProgrammaticallyFocused = false let debouncedOnMouseMove = props.interactiveDebounce > 0 ? debounce(onMouseMove, props.interactiveDebounce) : onMouseMove /* ======================= 🔑 Public members 🔑 ======================= */ const id = idCounter++ const popper = createPopperElement(id, props) const popperChildren = getChildren(popper) const state = { isEnabled: true, isVisible: false, isDestroyed: false } const popperInstance = null // 🌟 tippy instance const tip = { // properties id, reference, popper, popperChildren, popperInstance, props, state, // methods clearDelayTimeouts, set, setContent, show, hide, enable, disable, destroy } addTriggersToReference() reference.addEventListener('click', onReferenceClick) if (!props.lazy) { tip.popperInstance = createPopperInstance() tip.popperInstance.disableEventListeners() } Iif (props.showOnInit) { /** * Firefox has a bug where the tooltip will be placed incorrectly due to * strange layout on load, `setTimeout` gives the layout time to adjust * properly */ setTimeout(prepareShow, 20) } // Ensure the reference element can receive focus (and is not a delegate) if (props.a11y && !props.target && !elementCanReceiveFocus(reference)) { reference.setAttribute('tabindex', '0') } // Install shortcuts reference._tippy = tip popper._tippy = tip return tip /* ======================= 🔒 Private methods 🔒 ======================= */ /** * If the reference was clicked, it also receives focus */ function onReferenceClick() { defer(() => { referenceJustProgrammaticallyFocused = false }) } /** * Listener for the `followCursor` prop */ function followCursorListener(event) { const { clientX, clientY } = (lastMouseMoveEvent = event) if (!tip.popperInstance) { return } tip.popperInstance.reference = { getBoundingClientRect: () => ({ width: 0, height: 0, top: clientY, left: clientX, right: clientX, bottom: clientY }), clientWidth: 0, clientHeight: 0 } tip.popperInstance.scheduleUpdate() } /** * Creates the tippy instance for a delegate when it's been triggered */ function createDelegateChildTippy(event) { const targetEl = closest(event.target, tip.props.target) if (targetEl && !targetEl._tippy) { const content = tip.props.content if (content) { createTippy(targetEl, { ...tip.props, content, target: '', showOnInit: true }) prepareShow(event) } } } /** * Setup before show() is invoked (delays, etc.) */ function prepareShow(event) { clearDelayTimeouts() Iif (tip.state.isVisible) { return } // Is a delegate, create an instance for the child target Iif (tip.props.target) { return createDelegateChildTippy(event) } isPreparingToShow = true if (tip.props.wait) { return tip.props.wait(tip, event) } /** * If the tooltip has a delay, we need to be listening to the mousemove as * soon as the trigger event is fired so that it's in the correct position * upon mount */ Iif (hasFollowCursorBehavior()) { if (popperChildren.arrow) { popperChildren.arrow.style.margin = '0' } document.addEventListener('mousemove', followCursorListener) } const delay = getValue(tip.props.delay, 0, Defaults.delay) if (delay) { showTimeoutId = setTimeout(() => { show() }, delay) } else { show() } } /** * Setup before hide() is invoked (delays, etc.) */ function prepareHide() { clearDelayTimeouts() Iif (!tip.state.isVisible) { return } isPreparingToShow = false const delay = getValue(tip.props.delay, 1, Defaults.delay) Eif (delay) { hideTimeoutId = setTimeout(() => { Eif (tip.state.isVisible) { hide() } }, delay) } else { hide() } } /** * Cleans up old listeners */ function cleanupOldMouseMoveListeners() { document.body.removeEventListener('mouseleave', prepareHide) document.removeEventListener('mousemove', debouncedOnMouseMove) } /** * Event listener invoked upon trigger */ function onTrigger(event) { Iif (!tip.state.isEnabled) { return } const shouldStopEvent = supportsTouch && isUsingTouch && ['mouseenter', 'mouseover', 'focus'].indexOf(event.type) > -1 Iif (shouldStopEvent && tip.props.touchHold) { return } if (!tip.state.isVisible) { lastTriggerEvent = event } // Toggle show/hide when clicking click-triggered tooltips if ( event.type === 'click' && tip.props.hideOnClick !== false && tip.state.isVisible ) { prepareHide() } else { prepareShow(event) } } /** * Event listener used for interactive tooltips to detect when they should hide */ function onMouseMove(event) { const referenceTheCursorIsOver = closestCallback( event.target, el => el._tippy ) const isCursorOverPopper = closest(event.target, Selectors.POPPER) === tip.popper const isCursorOverReference = referenceTheCursorIsOver === tip.reference if (isCursorOverPopper || isCursorOverReference) { return } if ( isCursorOutsideInteractiveBorder( getPopperPlacement(tip.popper), tip.popper.getBoundingClientRect(), event, tip.props ) ) { cleanupOldMouseMoveListeners() prepareHide() } } /** * Event listener invoked upon mouseleave */ function onMouseLeave(event) { Iif ( ['mouseleave', 'mouseout'].indexOf(event.type) > -1 && supportsTouch && isUsingTouch && tip.props.touchHold ) { return } Iif (tip.props.interactive) { document.body.addEventListener('mouseleave', prepareHide) document.addEventListener('mousemove', debouncedOnMouseMove) return } prepareHide() } /** * Event listener invoked upon blur */ function onBlur(event) { if (event.target !== tip.reference || isUsingTouch) { return } Iif (tip.props.interactive) { if (!event.relatedTarget) { return } if (closest(event.relatedTarget, Selectors.POPPER)) { return } } prepareHide() } /** * Event listener invoked when a child target is triggered */ function onDelegateShow(event) { if (closest(event.target, tip.props.target)) { prepareShow(event) } } /** * Event listener invoked when a child target should hide */ function onDelegateHide(event) { if (closest(event.target, tip.props.target)) { prepareHide() } } /** * Creates the popper instance for the tip */ function createPopperInstance() { const { tooltip } = tip.popperChildren const { popperOptions } = tip.props const arrowSelector = Selectors[tip.props.arrowType === 'round' ? 'ROUND_ARROW' : 'ARROW'] const arrow = tooltip.querySelector(arrowSelector) const config = { placement: tip.props.placement, ...(popperOptions || {}), modifiers: { ...(popperOptions ? popperOptions.modifiers : {}), arrow: { element: arrowSelector, ...(popperOptions && popperOptions.modifiers ? popperOptions.modifiers.arrow : {}) }, flip: { enabled: tip.props.flip, padding: tip.props.distance + 5 /* 5px from viewport boundary */, behavior: tip.props.flipBehavior, ...(popperOptions && popperOptions.modifiers ? popperOptions.modifiers.flip : {}) }, offset: { offset: tip.props.offset, ...(popperOptions && popperOptions.modifiers ? popperOptions.modifiers.offset : {}) } }, onCreate() { tooltip.style[getPopperPlacement(tip.popper)] = getOffsetDistanceInPx( tip.props.distance, Defaults.distance ) if (arrow && tip.props.arrowTransform) { computeArrowTransform(arrow, tip.props.arrowTransform) } }, onUpdate() { const styles = tooltip.style styles.top = '' styles.bottom = '' styles.left = '' styles.right = '' styles[getPopperPlacement(tip.popper)] = getOffsetDistanceInPx( tip.props.distance, Defaults.distance ) Iif (arrow && tip.props.arrowTransform) { computeArrowTransform(arrow, tip.props.arrowTransform) } } } /** * Ensure the popper's position stays correct if its dimensions change. * Use .update() over .scheduleUpdate() so there is no 1 frame flash * due to async update. */ const observer = new MutationObserver(() => { tip.popperInstance.update() }) observer.observe(tip.popper, { childList: true, subtree: true }) Iif (popperMutationObserver) { popperMutationObserver.disconnect() } popperMutationObserver = observer // fixes https://github.com/atomiks/tippyjs/issues/193 tip.popper.addEventListener('mouseenter', event => { if (tip.state.isVisible && lastTriggerEvent.type === 'mouseenter') { prepareShow(event) } }) tip.popper.addEventListener('mouseleave', event => { if ( lastTriggerEvent.type === 'mouseenter' && tip.props.interactiveDebounce === 0 && isCursorOutsideInteractiveBorder( getPopperPlacement(tip.popper), tip.popper.getBoundingClientRect(), event, tip.props ) ) { prepareHide() } }) return new Popper(tip.reference, tip.popper, config) } /** * Mounts the tooltip to the DOM */ function mount(onceUpdated) { if (!tip.popperInstance) { tip.popperInstance = createPopperInstance() Iif (!tip.props.livePlacement) { tip.popperInstance.disableEventListeners() } } else { tip.popperInstance.scheduleUpdate() if (tip.props.livePlacement && !hasFollowCursorBehavior()) { tip.popperInstance.enableEventListeners() } } /** * If the instance previously had followCursor behavior, it will be * positioned incorrectly if triggered by `focus` afterwards. * Update the reference back to the real DOM element */ tip.popperInstance.reference = tip.reference if (hasFollowCursorBehavior()) { Iif (tip.popperChildren.arrow) { tip.popperChildren.arrow.style.margin = '' } } updatePopperPosition(tip.popperInstance, onceUpdated, true) if (!tip.props.appendTo.contains(tip.popper)) { tip.props.appendTo.appendChild(tip.popper) } } /** * Determines if the instance is in `followCursor` mode */ function hasFollowCursorBehavior() { return ( tip.props.followCursor && !isUsingTouch && lastTriggerEvent.type !== 'focus' ) } /** * Updates the tooltip's position on each animation frame + timeout */ function makeSticky() { const updatePosition = () => { applyTransitionDuration([tip.popper], tip.props.updateDuration) if (tip.popperInstance) { tip.popperInstance.scheduleUpdate() } if (tip.state.isVisible) { requestAnimationFrame(() => { defer(updatePosition) }) } else { applyTransitionDuration([tip.popper], 0) } } updatePosition() } /** * Invokes a callback once the tooltip has fully transitioned out */ function onTransitionedOut(duration, callback) { onTransitionEnd(duration, () => { Eif (!tip.state.isVisible && tip.props.appendTo.contains(tip.popper)) { callback() } }) } /** * Invokes a callback once the tooltip has fully transitioned in */ function onTransitionedIn(duration, callback) { onTransitionEnd(duration, callback) } /** * Invokes a callback once the tooltip's CSS transition ends */ function onTransitionEnd(duration, callback) { // Make callback synchronous if duration is 0 if (duration === 0) { return callback() } const { tooltip } = tip.popperChildren const listener = e => { if (e.target === tooltip) { toggleTransitionEndListener(tooltip, 'remove', listener) callback() } } toggleTransitionEndListener(tooltip, 'remove', transitionEndListener) toggleTransitionEndListener(tooltip, 'add', listener) transitionEndListener = listener } /** * Adds an event listener to the reference */ function on(eventType, handler, acc) { tip.reference.addEventListener(eventType, handler) acc.push({ eventType, handler }) } /** * Adds event listeners to the reference based on the `trigger` prop */ function addTriggersToReference() { listeners = tip.props.trigger .trim() .split(' ') .reduce((acc, eventType) => { Iif (eventType === 'manual') { return acc } if (!props.target) { on(eventType, onTrigger, acc) switch (eventType) { case 'mouseenter': on('mouseleave', onMouseLeave, acc) break case 'focus': on(isIE ? 'focusout' : 'blur', onBlur, acc) break } } else { switch (eventType) { case 'mouseenter': on('mouseover', onDelegateShow, acc) on('mouseout', onDelegateHide, acc) break case 'focus': on('focusin', onDelegateShow, acc) on('focusout', onDelegateHide, acc) break case 'click': on(eventType, onDelegateShow, acc) break } } return acc }, []) } /** * Removes event listeners from the reference */ function removeTriggersFromReference() { listeners.forEach(({ eventType, handler }) => { tip.reference.removeEventListener(eventType, handler) }) } /* ======================= 🔑 Public methods 🔑 ======================= */ /** * Enables the instance to allow it to show or hide */ function enable() { tip.state.isEnabled = true } /** * Disables the instance to disallow it to show or hide */ function disable() { tip.state.isEnabled = false } /** * Clears pending timeouts related to the `delay` prop if any */ function clearDelayTimeouts() { clearTimeout(showTimeoutId) clearTimeout(hideTimeoutId) } /** * Sets new props for the instance and redraws the tooltip */ function set(options) { const prevProps = tip.props const nextProps = evaluateProps(tip.reference, { ...tip.props, ...options, performance: true }) nextProps.performance = options.performance || prevProps.performance tip.props = nextProps Iif ('trigger' in options) { removeTriggersFromReference() addTriggersToReference() } Iif ('interactiveDebounce' in options) { cleanupOldMouseMoveListeners() debouncedOnMouseMove = debounce(onMouseMove, options.interactiveDebounce) } updatePopperElement(tip.popper, prevProps, nextProps) tip.popperChildren = getChildren(tip.popper) } /** * Shortcut for .set({ content: newContent }) */ function setContent(content) { set({ content }) } /** * Shows the tooltip */ function show( duration = getValue(tip.props.duration, 0, Defaults.duration[0]) ) { if ( tip.state.isDestroyed || !tip.state.isEnabled || (isUsingTouch && !tip.props.touch) ) { return } // Destroy tooltip if the reference element is no longer on the DOM Iif ( !tip.reference.isVirtual && !document.documentElement.contains(tip.reference) ) { return destroy() } // Do not show tooltip if the reference element has a `disabled` attribute if (tip.reference.hasAttribute('disabled')) { return } // If the reference was just programmatically focused for accessibility reasons Iif (referenceJustProgrammaticallyFocused) { referenceJustProgrammaticallyFocused = false return } tip.props.onShow(tip) tip.popper.style.visibility = 'visible' tip.state.isVisible = true // Prevent a transition if the popper is at the opposite placement applyTransitionDuration( [tip.popper, tip.popperChildren.tooltip, tip.popperChildren.backdrop], 0 ) mount(() => { if (!tip.state.isVisible) { return } if (!hasFollowCursorBehavior()) { // Arrow will sometimes not be positioned correctly. Force another update. tip.popperInstance.scheduleUpdate() } // Set initial position near the cursor if (hasFollowCursorBehavior()) { tip.popperInstance.disableEventListeners() const delay = getValue(tip.props.delay, 0, Defaults.delay) Iif (lastTriggerEvent.type) { followCursorListener( delay && lastMouseMoveEvent ? lastMouseMoveEvent : lastTriggerEvent ) } } applyTransitionDuration( [ tip.popperChildren.tooltip, tip.popperChildren.backdrop, tip.popperChildren.backdrop ? tip.popperChildren.content : null ], duration ) if (tip.props.interactive) { tip.reference.classList.add('tippy-active') } Iif (tip.props.sticky) { makeSticky() } setVisibilityState( [tip.popperChildren.tooltip, tip.popperChildren.backdrop], 'visible' ) onTransitionedIn(duration, () => { Iif (tip.props.updateDuration === 0) { tip.popperChildren.tooltip.classList.add('tippy-notransition') } Iif (tip.props.interactive && lastTriggerEvent.type === 'focus') { focus(tip.popper) } tip.reference.setAttribute('aria-describedby', tip.popper.id) tip.props.onShown(tip) }) }) } /** * Hides the tooltip */ function hide( duration = getValue(tip.props.duration, 1, Defaults.duration[1]) ) { Iif (tip.state.isDestroyed || !tip.state.isEnabled) { return } tip.props.onHide(tip) Iif (tip.props.updateDuration === 0) { tip.popperChildren.tooltip.classList.remove('tippy-notransition') } Iif (tip.props.interactive) { tip.reference.classList.remove('tippy-active') } tip.popper.style.visibility = 'hidden' tip.state.isVisible = false applyTransitionDuration( [ tip.popperChildren.tooltip, tip.popperChildren.backdrop, tip.popperChildren.backdrop ? tip.popperChildren.content : null ], duration ) setVisibilityState( [tip.popperChildren.tooltip, tip.popperChildren.backdrop], 'hidden' ) Iif ( tip.props.interactive && !referenceJustProgrammaticallyFocused && lastTriggerEvent.type === 'focus' ) { referenceJustProgrammaticallyFocused = true focus(tip.reference) } onTransitionedOut(duration, () => { Eif (!isPreparingToShow) { document.removeEventListener('mousemove', followCursorListener) lastMouseMoveEvent = null } tip.reference.removeAttribute('aria-describedby') tip.popperInstance.disableEventListeners() tip.props.appendTo.removeChild(tip.popper) tip.props.onHidden(tip) }) } /** * Destroys the tooltip */ function destroy(destroyTargetInstances) { if (tip.state.isDestroyed) { return } // Ensure the popper is hidden if (tip.state.isVisible) { hide(0) } removeTriggersFromReference() tip.reference.removeEventListener('click', onReferenceClick) delete tip.reference._tippy if (tip.props.target && destroyTargetInstances) { toArray(tip.reference.querySelectorAll(tip.props.target)).forEach( child => child._tippy && child._tippy.destroy() ) } if (tip.popperInstance) { tip.popperInstance.destroy() } if (popperMutationObserver) { popperMutationObserver.disconnect() } tip.state.isDestroyed = true } } |