function positionMenu($trigger) {
		const getProp = ($el, prop) => $el.getBoundingClientRect()[prop];
		const containerRight = getProp($el, "right");
		const containerLeft = getProp($el, "left");
		const containerBottom = getProp($el, "bottom");
		const { lineHeight } = editor.renderer;
		const margin = 10;

		// if menu is positioned off screen horizontally from the right
		const menuRight = getProp($menu, "right");
		if (menuRight + margin > containerRight) {
			const menuLeft = getProp($menu, "left");
			const [x] = relativePosition(
				menuLeft - Math.abs(menuRight - containerRight),
			);
			$menu.style.left = `${x - margin}px`;
		}

		// if menu is positioned off screen horizontally from the left
		const menuLeft = getProp($menu, "left");
		if (menuLeft - margin < containerLeft) {
			const [x] = relativePosition(
				menuLeft + Math.abs(menuLeft - containerLeft),
			);
			$menu.style.left = `${x + margin}px`;
		}

		if (shrink()) return;

		// if menu is positioned off screen vertically from the bottom
		const menuBottom = getProp($menu, "bottom");
		if (menuBottom > containerBottom) {
			const range = editor.getSelectionRange();
			let pos;

			if ($trigger === $start) {
				pos = range.start;
			} else {
				pos = range.end;
			}

			const { pageY } = renderer.textToScreenCoordinates(pos);
			const [, y] = relativePosition(null, pageY - lineHeight * 1.8);
			$menu.style.top = `${y}px`;
		}

		function shrink() {
			const [left, right] = [getProp($menu, "left"), getProp($menu, "right")];
			const tooLeft = left < containerLeft;
			const tooRight = right > containerRight;
			if (tooLeft || tooRight) {
				const { scale = 1 } = $menu.dataset;
				$menu.dataset.scale = Number.parseFloat(scale - 0.1);
				$menu.style.transform = `scale(${$menu.dataset.scale})`;
				positionMenu($trigger);
				return true;
			}
			return false;
		}
	}
Populates the menu items
@param {HTMLElement} $trigger
@returns