Re-use nav selector function when possible

This commit is contained in:
Dustin Brett 2024-10-20 21:34:21 -07:00
parent 948692251e
commit 2ae0cdfd16
2 changed files with 7 additions and 8 deletions

View File

@ -19,6 +19,7 @@ import {
THIN_SCROLLBAR_WIDTH,
THIN_SCROLLBAR_WIDTH_NON_WEBKIT,
} from "utils/constants";
import { getNavButtonByTitle } from "hooks/useGlobalKeyboardShortcuts";
type StartMenuProps = {
toggleStartMenu: (showMenu?: boolean) => void;
@ -77,9 +78,7 @@ const StartMenu: FC<StartMenuProps> = ({ toggleStartMenu }) => {
else if (key.length === 1) {
toggleStartMenu(false);
const searchButton = document.querySelector<HTMLDivElement>(
`main > nav > div[title='${SEARCH_BUTTON_TITLE}']`
);
const searchButton = getNavButtonByTitle(SEARCH_BUTTON_TITLE);
if (searchButton) {
searchButton.click();

View File

@ -22,7 +22,7 @@ declare global {
}
}
const getByTitle = (title: string): HTMLButtonElement | null =>
export const getNavButtonByTitle = (title: string): HTMLButtonElement | null =>
document.querySelector(
`main > nav > div[title='${title}']`
) as HTMLButtonElement;
@ -61,14 +61,14 @@ const useGlobalKeyboardShortcuts = (): void => {
const altBindingsRef = useRef<Record<string, () => void>>({});
const shiftBindingsRef = useRef<Record<string, () => void>>({
E: () => open("FileExplorer"),
ESCAPE: () => getByTitle(START_BUTTON_TITLE)?.click(),
ESCAPE: () => getNavButtonByTitle(START_BUTTON_TITLE)?.click(),
F10: () => open("Terminal"),
F12: () => open("DevTools"),
F5: () => window.location.reload(),
R: () => open("Run"),
S: () => getByTitle(SEARCH_BUTTON_TITLE)?.click(),
S: () => getNavButtonByTitle(SEARCH_BUTTON_TITLE)?.click(),
X: () =>
getByTitle(START_BUTTON_TITLE)?.dispatchEvent(
getNavButtonByTitle(START_BUTTON_TITLE)?.dispatchEvent(
new MouseEvent("contextmenu", {
clientX: 1,
clientY: viewHeight() - 1,
@ -136,7 +136,7 @@ const useGlobalKeyboardShortcuts = (): void => {
) {
metaDown = false;
if (metaComboUsed) metaComboUsed = false;
else getByTitle(START_BUTTON_TITLE)?.click();
else getNavButtonByTitle(START_BUTTON_TITLE)?.click();
}
};