Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export { default as ZodJSONCoder } from "./ZodJSONCoder"
export { default as listFromCommaSeparatedString } from "./listFromCommaSeparatedString"
export { default as env } from "./env"
export { default as getBaseFilename } from "./getBaseFilename"
export { default as isMac } from "./isMac"
export { default as useKeyboardShortcut } from "./useKeyboardShortcut"
5 changes: 5 additions & 0 deletions src/common/utils/isMac.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isMac = () => {
return window.navigator.userAgent.toLowerCase().includes("mac")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it work on an iPad with keyboard attached? :trollface:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know 😢

}

export default isMac
19 changes: 19 additions & 0 deletions src/common/utils/useKeyboardShortcut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client"

import { useEffect } from "react"

const useKeyboardShortcut = (
handleKeyDown: (event: KeyboardEvent) => void,
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
dependencies: any[]
) => {
useEffect(() => {
window.addEventListener("keydown", handleKeyDown)
return () => {
window.removeEventListener("keydown", handleKeyDown)
}
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [handleKeyDown, ...dependencies])
}

export default useKeyboardShortcut
47 changes: 32 additions & 15 deletions src/features/sidebar/view/base/SecondaryHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ReactNode } from "react"
import { SxProps } from "@mui/system"
import { Box, Divider, IconButton } from "@mui/material"
import { Box, Divider, IconButton, Tooltip } from "@mui/material"
import { useTheme } from "@mui/material/styles"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faBars, faChevronLeft } from "@fortawesome/free-solid-svg-icons"
import { isMac, useKeyboardShortcut } from "@/common"

export default function SecondaryHeader({
showOpenSidebar,
Expand All @@ -20,6 +21,18 @@ export default function SecondaryHeader({
children?: ReactNode
sx?: SxProps
}) {
useKeyboardShortcut(event => {
const isActionKey = isMac() ? event.metaKey : event.ctrlKey
if (isActionKey && event.key === ".") {
event.preventDefault()
if (showOpenSidebar) {
onToggleSidebarOpen(true)
} else if (showCloseSidebar) {
onToggleSidebarOpen(false)
}
}
}, [showOpenSidebar, showCloseSidebar, onToggleSidebarOpen])
const openCloseShortcutString = isMac() ? " (⌘ + .)" : "(^ + .)"
const theme = useTheme()
return (
<Box
Expand All @@ -31,22 +44,26 @@ export default function SecondaryHeader({
>
<Box sx={{ display: "flex", alignItems: "center", padding: 2 }}>
{showOpenSidebar &&
<IconButton
color="primary"
onClick={() => onToggleSidebarOpen(true)}
edge="start"
>
<FontAwesomeIcon icon={faBars} size="sm" style={{ aspectRatio: 1 }} />
</IconButton>
<Tooltip title={`Show Projects${openCloseShortcutString}`}>
<IconButton
color="primary"
onClick={() => onToggleSidebarOpen(true)}
edge="start"
>
<FontAwesomeIcon icon={faBars} size="sm" style={{ aspectRatio: 1 }} />
</IconButton>
</Tooltip>
}
{showCloseSidebar &&
<IconButton
color="primary"
onClick={() => onToggleSidebarOpen(false)}
edge="start"
>
<FontAwesomeIcon icon={faChevronLeft} size="sm" style={{ aspectRatio: 1 }} />
</IconButton>
<Tooltip title={`Hide Projects${openCloseShortcutString}`}>
<IconButton
color="primary"
onClick={() => onToggleSidebarOpen(false)}
edge="start"
>
<FontAwesomeIcon icon={faChevronLeft} size="sm" style={{ aspectRatio: 1 }} />
</IconButton>
</Tooltip>
}
<Box sx={{ display: "flex", flexGrow: 1, justifyContent: "end" }}>
{trailingItem}
Expand Down