All files / toolbar toolbar.ts

95.51% Statements 149/156
88% Branches 44/50
100% Functions 13/13
95.51% Lines 149/156

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 1571x 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 1x 1x 1x 1x 9x 9x 9x 9x 9x 46x 45x 45x 1x 1x 46x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 9x 9x 9x 1x 1x 9x 9x 18x 18x     18x 18x 18x 12x 18x 8x 8x 10x 7x 10x 5x 5x 1x 5x 1x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 3x 1x 1x 1x 15x 15x 15x 15x 18x 9x 9x 16x     16x 9x 9x 9x 9x 9x 21x 21x 21x       21x 9x 9x 9x 8x 8x 9x 8x 8x 9x 9x 9x 9x 9x 9x 9x 8x 8x 9x 9x 9x 8x 8x 9x 8x 8x 9x 9x 9x 2x 2x 2x 2x 2x 7x 7x 7x 7x 7x  
/*
 * 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 { Accessor, JSX, createEffect, createSignal, mergeProps } from "solid-js";
 
import { createFocusManager } from "../../internals/react-aria/focus";
 
import type { AriaToolbar, AriaToolbarWithRef, ToolbarArguments, ToolbarDefaultArguments } from "./types";
 
const defaultProps: ToolbarDefaultArguments = {
	orientation: "horizontal"
};
 
export function createToolbar<T extends HTMLElement>(props: ToolbarArguments, ref: Accessor<T | null>): AriaToolbar<T>;
export function createToolbar<T extends HTMLElement>(props?: ToolbarArguments): AriaToolbarWithRef<T>;
 
/**
 * Creates a Toolbar bindings according to the WAI-ARIA specification.
 *
 * @link https://www.w3.org/WAI/ARIA/apg/patterns/toolbar
 */
export function createToolbar<T extends HTMLElement>(props?: ToolbarArguments, ref?: Accessor<T | null>): unknown {
	// Record the last focused child when focus moves out of the toolbar.
	let lastFocused: HTMLElement | null = null;
	let directiveRef: T | null = null;
 
	const getRef = () => {
		if (ref) {
			return ref();
		}
 
		return directiveRef;
	};
 
	const local = mergeProps(defaultProps, props);
	const [isInToolbar, setInToolbar] = createSignal(false);
 
	const focusManager = createFocusManager(getRef);
 
	createEffect(() => {
		const element = getRef();
 
		setInToolbar(!!(element && element.parentElement?.closest('[role="toolbar"]')));
	});
 
	const toolbarRef = (node: T) => {
		directiveRef = node;
	};
 
	const onKeyDown: JSX.EventHandlerUnion<HTMLElement, KeyboardEvent> = (e) => {
		// don't handle portalled events
		if (!e.currentTarget.contains(e.target as HTMLElement)) {
			return;
		}
 
		if (
			(local.orientation === "horizontal" && e.key === "ArrowRight") ||
			(local.orientation === "vertical" && e.key === "ArrowDown")
		) {
			focusManager.focusNext();
		} else if (
			(local.orientation === "horizontal" && e.key === "ArrowLeft") ||
			(local.orientation === "vertical" && e.key === "ArrowUp")
		) {
			focusManager.focusPrevious();
		} else if (e.key === "Home") {
			focusManager.focusFirst();
		} else if (e.key === "End") {
			focusManager.focusLast();
		} else if (e.key === "Tab") {
			// When the tab key is pressed, we want to move focus
			// out of the entire toolbar. To do this, move focus
			// to the first or last focusable child, and let the
			// browser handle the Tab key as usual from there.
			e.stopPropagation();
			lastFocused = document.activeElement as HTMLElement;
			if (e.shiftKey) {
				focusManager.focusFirst();
			} else {
				focusManager.focusLast();
			}
			return;
		} else {
			// if we didn't handle anything, return early so we don't preventDefault
			return;
		}
 
		// Prevent arrow keys from being handled by nested action groups.
		e.stopPropagation();
		e.preventDefault();
	};
 
	const onBlur: JSX.FocusEventHandler<T, FocusEvent> = (e) => {
		if (!e.currentTarget.contains(e.relatedTarget as HTMLElement) && !lastFocused) {
			lastFocused = e.target as HTMLElement;
		}
	};
 
	// Restore focus to the last focused child when focus returns into the toolbar.
	// If the element was removed, do nothing, either the first item in the first group,
	// or the last item in the last group will be focused, depending on direction.
	const onFocus: JSX.FocusEventHandler<T, FocusEvent> = (e) => {
		const element = getRef();
 
		if (lastFocused && !e.currentTarget.contains(e.relatedTarget as HTMLElement) && element?.contains(e.target)) {
			lastFocused?.focus();
			lastFocused = null;
		}
	};
 
	const toolbarProps: JSX.HTMLAttributes<T> = {
		get "aria-label"() {
			return local["aria-label"];
		},
		get "aria-labelledby"() {
			return local["aria-label"] == null ? local["aria-labelledby"] : undefined;
		},
		get role() {
			return !isInToolbar() ? "toolbar" : "group";
		},
		get "aria-orientation"() {
			return local.orientation;
		},
		get onKeyDown() {
			return !isInToolbar() ? onKeyDown : undefined;
		},
		// eslint-disable-next-line @typescript-eslint/ban-ts-comment
		// @ts-ignore
		get "oncapture:focus"() {
			return !isInToolbar() ? onFocus : undefined;
		},
		get "oncapture:blur"() {
			return !isInToolbar() ? onBlur : undefined;
		}
	};
 
	if (!ref) {
		return {
			toolbarProps,
			toolbarRef
		};
	}
 
	return {
		toolbarProps
	};
}