All files / tooltip createTooltipTrigger.ts

100% Statements 147/147
100% Branches 19/19
100% Functions 11/11
100% Lines 147/147

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 1481x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 3x 9x 9x 3x 3x 3x 3x 9x 9x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 7x 7x 7x 7x 7x 7x 9x 9x 9x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 9x 9x 2x 1x 1x 1x 1x 1x 1x 2x 9x 9x 1x 1x 1x 1x 1x 9x 9x 9x 9x 2x 2x 2x 2x 2x 9x 9x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 17x 17x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
/*
 * Copyright 2020 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import { createEffect, createUniqueId, mergeProps, onCleanup, Accessor } from "solid-js";
 
import { createFocusable } from "../../internals/solid-aria/createFocusable";
import { createHover } from "../../internals/solid-aria/createHover";
import { createFocusVisible, createInteractionModality } from "../../internals/solid-aria/createFocusVisible";
 
import type { AriaTooltipTrigger, TooltipTriggerArguments } from "./types";
import type { TooltipTriggerState } from "../state/tooltip-trigger-state";
 
/**
 * Creates a Tooltip trigger bindings according to the WAI-ARIA specification.
 *
 * @link https://www.w3.org/WAI/ARIA/apg/patterns/tooltip
 */
export function createTooltipTrigger<T extends HTMLElement>(
	props: TooltipTriggerArguments,
	state: TooltipTriggerState,
	ref: Accessor<T>
): AriaTooltipTrigger {
	const tooltipId = createUniqueId();
	const modality = createInteractionModality();
 
	let isHovered = false;
	let isFocused = false;
 
	const handleShow = () => {
		if (isHovered || isFocused) {
			state.open(isFocused);
		}
	};
 
	const handleHide = (immediate?: boolean) => {
		if (!isHovered && !isFocused) {
			state.close(immediate);
		}
	};
 
	createEffect(() => {
		const onKeyDown = (e: KeyboardEvent) => {
			if (ref()) {
				// Escape after clicking something can give it keyboard focus
				// dismiss tooltip on esc key press
				if (e.key === "Escape") {
					e.stopPropagation();
					state.close(true);
				}
			}
		};
 
		if (state.isOpen) {
			document.addEventListener("keydown", onKeyDown, true);
 
			onCleanup(() => {
				document.removeEventListener("keydown", onKeyDown, true);
			});
		}
	});
 
	const onHoverStart = () => {
		if (props.trigger === "focus") {
			return;
		}
		// In chrome, if you hover a trigger, then another element obscures it, due to keyboard
		// interactions for example, hover will end. When hover is restored after that element disappears,
		// focus moves on for example, then the tooltip will reopen. We check the modality to know if the hover
		// is the result of moving the mouse.
		isHovered = modality() === "pointer";
 
		handleShow();
	};
 
	const onHoverEnd = () => {
		if (props.trigger === "focus") {
			return;
		}
		// no matter how the trigger is left, we should close the tooltip
		isFocused = false;
		isHovered = false;
		handleHide();
	};
 
	const onPressStart = () => {
		// no matter how the trigger is pressed, we should close the tooltip
		isFocused = false;
		isHovered = false;
		handleHide(true);
	};
 
	const isVisible = createFocusVisible();
 
	const onFocus = () => {
		if (isVisible) {
			isFocused = true;
			handleShow();
		}
	};
 
	const onBlur = () => {
		isFocused = false;
		isHovered = false;
		handleHide(true);
	};
 
	const { hoverProps } = createHover({
		isDisabled: props.isDisabled,
		onHoverStart,
		onHoverEnd
	});
 
	const { focusableProps } = createFocusable(
		{
			isDisabled: props.isDisabled,
			onFocus,
			onBlur
		},
		ref
	);
 
	return {
		get triggerProps() {
			const merged = mergeProps(focusableProps, hoverProps, {
				onPointerDown: onPressStart,
				onKeyDown: onPressStart,
				get "aria-describedby"() {
					return state.isOpen ? tooltipId : undefined;
				}
			});
 
			return merged;
		},
		tooltipProps: {
			id: tooltipId
		}
	};
}