Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(search): open command palette result in new tab with Cmd/Ctrl+Enter (#76597)

Luke Beltoneee4e9f37fe4
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index be65642663a2..62f8dc8391a9 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -1729,9 +1729,9 @@ snapshots:
components-search--product-recents-and-starred--light:
hash: v1.k794b7964.5df9ac27e8f914736f6fc30fbe27aba42d686145e0f06982dfe1050b74931081.9snshG25bFXRzfMF4nFJgaB7uHRXkt-2Du2b_Qoi9Lw
components-search--searching--dark:
- hash: v1.k794b7964.85766b937eb3e2cb31d77d0fc3d3270ea92b5a4c51577cddc5de92649787d1ac.TQ8fgQkYMWRzITSkjwIDMOahzlPUZXGugRI2b-7Q7Rs
+ hash: v1.k794b7964.7d18a800bf68133261661357a9f852eee3deb4b8a6c3e1d2bf354165308e1783.NZUBoWvHtgycHH1yCecSCzqGkQz2zHIG3CugokLolEU
components-search--searching--light:
- hash: v1.k794b7964.f7ed712a72accf99484489415f30aa0d1931f75806b0522e6ac88bfef3eff899.HKpJ3OTjiZbPtkMvpTjami0t7I5d-odknNJ_0-antHE
+ hash: v1.k794b7964.bc621f20b1af7f571eb9ed999c0f9bdb981cee59d9f926093e838e6de0473e95.EUdhWrEgvYdy40KgoOi-1ioGaH0Bojf7zJGaFsjkZAs
components-sentencelist--full-sentence--dark:
hash: v1.k794b7964.7e8e5dd2e09d246cc3668f362f2391fe67618bd1143aa393832297f5dc30e558.yptEWKc5RAUY613Fwd5cQ0VIinMvpdoI6cuKgagAGxk
components-sentencelist--full-sentence--light:
diff --git a/frontend/src/lib/components/Command/Command.tsx b/frontend/src/lib/components/Command/Command.tsx
index e9a5e688fc4a..23faa479f730 100644
--- a/frontend/src/lib/components/Command/Command.tsx
+++ b/frontend/src/lib/components/Command/Command.tsx
@@ -3,6 +3,7 @@ import { router } from 'kea-router'
import { useCallback } from 'react'
import { DialogPrimitive, DialogPrimitiveTitle } from 'lib/ui/DialogPrimitive/DialogPrimitive'
+import { newInternalTab } from 'lib/utils/newInternalTab'
import { Search } from '../Search/Search'
import { SearchItem } from '../Search/searchLogic'
@@ -13,14 +14,18 @@ export function Command(): JSX.Element {
const { closeCommand } = useActions(commandLogic)
const handleItemSelect = useCallback(
- (item: SearchItem) => {
+ (item: SearchItem, openInNewTab?: boolean) => {
closeCommand()
if (item.onSelect) {
item.onSelect()
return
}
if (item.href) {
- router.actions.push(item.href)
+ if (openInNewTab) {
+ newInternalTab(item.href)
+ } else {
+ router.actions.push(item.href)
+ }
}
},
[closeCommand]
diff --git a/frontend/src/lib/components/Search/Search.tsx b/frontend/src/lib/components/Search/Search.tsx
index 1b99a70b1332..1fe12ba4675d 100644
--- a/frontend/src/lib/components/Search/Search.tsx
+++ b/frontend/src/lib/components/Search/Search.tsx
@@ -30,6 +30,7 @@ import { ContextMenu, ContextMenuContent, ContextMenuGroup, ContextMenuTrigger }
import { Label } from 'lib/ui/Label/Label'
import { WrappingLoadingSkeleton } from 'lib/ui/WrappingLoadingSkeleton/WrappingLoadingSkeleton'
import { cn } from 'lib/utils/css-classes'
+import { newInternalTab } from 'lib/utils/newInternalTab'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
@@ -41,7 +42,7 @@ import type { UserTheme } from '~/types'
import { ScrollableShadows } from '../ScrollableShadows/ScrollableShadows'
import { RECENTS_LIMIT, STARRED_LIMIT, SearchItem, SearchLogicProps, searchLogic } from './searchLogic'
-import { formatRelativeTimeShort, getCategoryDisplayName } from './utils'
+import { SETTINGS_THEME_ITEM_ID, canOpenInNewTab, formatRelativeTimeShort, getCategoryDisplayName } from './utils'
// ============================================================================
// Constants
@@ -68,8 +69,6 @@ const PLACEHOLDER_OPTIONS = [
const PLACEHOLDER_CYCLE_INTERVAL = 3000
-const SETTINGS_THEME_ITEM_ID = '__settings_theme__'
-
const SETTINGS_THEME_ITEM_QUERY = ['dark', 'light', 'theme', 'appearance']
const EMPTY_SUGGESTED_ITEMS: SearchItem[] = []
@@ -221,7 +220,7 @@ interface SearchContextValue {
isSearching: boolean
isActive: boolean
inputRef: RefObject<HTMLInputElement>
- handleItemClick: (item: SearchItem) => void
+ handleItemClick: (item: SearchItem, openInNewTab?: boolean) => void
showAskAiLink: boolean
onAskAiClick?: () => void
highlightedItemRef: MutableRefObject<SearchItem | null>
@@ -371,8 +370,8 @@ export interface SearchRootProps {
logicKey?: SearchLogicProps['logicKey']
/** Whether the search is active (for placeholder animation) */
isActive?: boolean
- /** Callback when an item is selected */
- onItemSelect?: (item: SearchItem) => void
+ /** Callback when an item is selected. `openInNewTab` is true when activated with Cmd/Ctrl. */
+ onItemSelect?: (item: SearchItem, openInNewTab?: boolean) => void
/** Whether to show the Ask AI link */
showAskAiLink?: boolean
/** Callback when Ask AI is clicked */
@@ -501,16 +500,20 @@ function SearchRoot({
}, [isActive, setSearch])
const handleItemClick = useCallback(
- (item: SearchItem) => {
+ (item: SearchItem, openInNewTab: boolean = false) => {
if (item.disabledReason) {
return
}
+ if (openInNewTab && !canOpenInNewTab(item)) {
+ return
+ }
if (logicKey === 'command') {
const position = orderedItemsRef.current.findIndex((i) => i.id === item.id)
posthog.capture('command menu item selected', {
category: item.category,
item_type: item.itemType ?? null,
result_position: position >= 0 ? position : null,
+ opened_in_new_tab: openInNewTab,
})
}
if (item.id === SETTINGS_THEME_ITEM_ID) {
@@ -529,9 +532,13 @@ function SearchRoot({
}
}
if (onItemSelect) {
- onItemSelect(item)
+ onItemSelect(item, openInNewTab)
} else if (item.href) {
- router.actions.push(item.href)
+ if (openInNewTab) {
+ newInternalTab(item.href)
+ } else {
+ router.actions.push(item.href)
+ }
}
},
[onItemSelect, onAskAiClick, updateUser, toggleTheme, logicKey]
@@ -663,7 +670,17 @@ export interface SearchInputProps {
}
function SearchInput({ autoFocus, className }: SearchInputProps): JSX.Element {
- const { searchValue, setSearchValue, isActive, inputRef, showAskAiLink, onAskAiClick } = useSearchContext()
+ const {
+ searchValue,
+ setSearchValue,
+ isActive,
+ inputRef,
+ showAskAiLink,
+ onAskAiClick,
+ handleItemClick,
+ highlightedItemRef,
+ filteredItems,
+ } = useSearchContext()
const { text: placeholderText, isVisible: placeholderVisible } = useRotatingPlaceholder(isActive && !searchValue)
@@ -680,9 +697,24 @@ function SearchInput({ autoFocus, className }: SearchInputProps): JSX.Element {
e.preventDefault()
onAskAiClick?.()
router.actions.push(urls.ai(undefined, searchValue.trim()))
+ return
+ }
+ // Cmd/Ctrl+Enter opens the highlighted result in a new tab. Base UI's combobox input
+ // ignores modified Enter (so plain Enter still navigates in place), leaving this to us.
+ if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
+ const highlighted = highlightedItemRef.current
+ if (
+ highlighted &&
+ !highlighted.disabledReason &&
+ canOpenInNewTab(highlighted) &&
+ filteredItems.some((item) => item.id === highlighted.id)
+ ) {
+ e.preventDefault()
+ handleItemClick(highlighted, true)
+ }
}
},
- [showAskAiLink, searchValue, onAskAiClick]
+ [showAskAiLink, searchValue, onAskAiClick, handleItemClick, highlightedItemRef, filteredItems]
)
useEffect(() => {
@@ -886,6 +918,9 @@ function SearchResults({
<Autocomplete.Item
value={item}
onClick={(e) => {
+ // Plain clicks only. LinkPrimitive returns early on
+ // metaKey/ctrlKey, so modifier clicks never get here and
+ // are handled in the capture phase below instead.
e.preventDefault()
handleItemClick(item)
}}
@@ -898,7 +933,24 @@ function SearchResults({
highlightedItemRef.current = item
}
return (
- <div className="px-2">
+ <div
+ className="px-2"
+ onClickCapture={(e) => {
+ // LinkPrimitive swallows modifier clicks
+ // (stopPropagation, then return) before its
+ // own onClick runs, so the capture phase is
+ // the only place we still see them. Route
+ // them through the same call as
+ // Cmd/Ctrl+Enter so a mouse and a keyboard
+ // new-tab behave identically: the palette
+ // closes, the selection is captured, and
+ // action items stay inert.
+ if (e.metaKey || e.ctrlKey) {
+ e.preventDefault()
+ handleItemClick(item, true)
+ }
+ }}
+ >
<Link
// No `to` when disabled: Link only applies its
// disabled state and reason tooltip without one
@@ -1005,7 +1057,7 @@ function SearchFooter({ children }: SearchFooterProps): JSX.Element {
const { searchValue } = useSearchContext()
return (
- <div className="border-t px-2 py-1 text-xxs text-tertiary font-medium select-none flex items-center gap-1">
+ <div className="border-t px-2 py-1 text-xxs text-tertiary font-medium select-none flex flex-wrap items-center gap-1">
{children ?? (
<>
{filteredItems.length > 1 && (
@@ -1016,6 +1068,11 @@ function SearchFooter({ children }: SearchFooterProps): JSX.Element {
<span>
<KeyboardShortcut enter /> to activate
</span>
+ {filteredItems.some(canOpenInNewTab) && (
+ <span>
+ <KeyboardShortcut command enter /> to open in new tab
+ </span>
+ )}
{searchValue.trim() && (
<span>
<KeyboardShortcut tab /> to ask AI
diff --git a/frontend/src/lib/components/Search/utils.test.ts b/frontend/src/lib/components/Search/utils.test.ts
index eb3c1f69f4fe..30a617f2f276 100644
--- a/frontend/src/lib/components/Search/utils.test.ts
+++ b/frontend/src/lib/components/Search/utils.test.ts
@@ -1,4 +1,4 @@
-import { filterSearchItems } from './utils'
+import { SETTINGS_THEME_ITEM_ID, canOpenInNewTab, filterSearchItems } from './utils'
interface TestItem {
name: string
@@ -104,3 +104,27 @@ describe('filterSearchItems', () => {
})
})
})
+
+// Cmd/Ctrl+Enter routes through this, so a false positive on an action item runs that action
+// instead of opening a background tab, which in the case of "Log out" ends the session.
+describe('canOpenInNewTab', () => {
+ const cases: [label: string, item: { id: string; href?: string; onSelect?: () => void }, expected: boolean][] = [
+ ['allows a plain navigable result', { id: 'insight-1', href: '/insights/1' }, true],
+ ['blocks an action-only result such as "Log out"', { id: 'misc-logout', onSelect: jest.fn() }, false],
+ [
+ 'blocks an action that also carries an href',
+ { id: 'new-sql-query-tab', href: '/sql', onSelect: jest.fn() },
+ false,
+ ],
+ [
+ 'blocks the theme row, which navigates but toggles instead',
+ { id: SETTINGS_THEME_ITEM_ID, href: '/settings/theme' },
+ false,
+ ],
+ ['blocks a result with nothing to open', { id: 'no-href' }, false],
+ ]
+
+ it.each(cases)('%s', (_label, item, expected) => {
+ expect(canOpenInNewTab(item)).toBe(expected)
+ })
+})
diff --git a/frontend/src/lib/components/Search/utils.ts b/frontend/src/lib/components/Search/utils.ts
index 10794335c277..86baa09e7a63 100644
--- a/frontend/src/lib/components/Search/utils.ts
+++ b/frontend/src/lib/components/Search/utils.ts
@@ -2,6 +2,9 @@ import { Dayjs, dayjs } from 'lib/dayjs'
import { createFuse } from 'lib/utils/fuseSearch'
import { pluralize } from 'lib/utils/strings'
+/** Synthetic result that jumps to the theme setting, or toggles the theme outright. */
+export const SETTINGS_THEME_ITEM_ID = '__settings_theme__'
+
interface FuseSearchable {
name: string
displayName?: string
@@ -33,6 +36,22 @@ export function filterSearchItems<T extends FuseSearchable>(items: T[], query: s
return fuse.search(trimmed).map((r) => r.item)
}
+/** Structural so this module avoids importing searchLogic, which imports this one. */
+interface NewTabCandidate {
+ id: string
+ href?: string
+ onSelect?: () => void
+}
+
+/**
+ * Whether Cmd/Ctrl activation should open an item in a new tab. Only items that purely
+ * navigate qualify. An item carrying `onSelect`, and the theme row, run an action instead,
+ * so treating the modifier as "open in a new tab" would fire that action rather than open
+ * anything, which is merely surprising for most items but destructive for "Log out".
+ */
+export const canOpenInNewTab = (item: NewTabCandidate): boolean =>
+ !!item.href && !item.onSelect && item.id !== SETTINGS_THEME_ITEM_ID
+
export const getCategoryDisplayName = (category: string): string => {
const displayNames: Record<string, string> = {
create: 'Create new',
diff --git a/frontend/src/lib/lemon-ui/Link/Link.test.tsx b/frontend/src/lib/lemon-ui/Link/Link.test.tsx
index b31dd96b8d3c..67e96c170370 100644
--- a/frontend/src/lib/lemon-ui/Link/Link.test.tsx
+++ b/frontend/src/lib/lemon-ui/Link/Link.test.tsx
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom'
-import { render, screen } from '@testing-library/react'
+import { fireEvent, render, screen } from '@testing-library/react'
import { Link } from './Link'
@@ -29,4 +29,26 @@ describe('Link', () => {
const href = anchor?.getAttribute('href') ?? ''
expect(href.replace(/\s/g, '').toLowerCase()).not.toMatch(/^javascript:/)
})
+
+ // The command palette depends on modifier clicks NOT reaching the passed onClick, so it
+ // handles them in the capture phase instead. If this ever starts forwarding them, that
+ // palette handler and this onClick would both run, opening a new tab and navigating in place.
+ const modifierCases: [label: string, init: MouseEventInit, expectedCalls: number][] = [
+ ['plain', {}, 1],
+ ['meta', { metaKey: true }, 0],
+ ['ctrl', { ctrlKey: true }, 0],
+ ]
+
+ it.each(modifierCases)('invokes the passed onClick for a %s click %s time(s)', (label, init, expectedCalls) => {
+ const onClick = jest.fn()
+ const text = `modifier target ${label}`
+ render(
+ <Link to="/insights/1" disableClientSideRouting onClick={onClick}>
+ {text}
+ </Link>
+ )
+
+ fireEvent.click(screen.getByText(text), init)
+ expect(onClick).toHaveBeenCalledTimes(expectedCalls)
+ })
})
diff --git a/frontend/src/scenes/new-tab/NewTabScene.tsx b/frontend/src/scenes/new-tab/NewTabScene.tsx
index 0df7ea3c3c93..6a2af7c8ff32 100644
--- a/frontend/src/scenes/new-tab/NewTabScene.tsx
+++ b/frontend/src/scenes/new-tab/NewTabScene.tsx
@@ -4,6 +4,7 @@ import { useCallback } from 'react'
import { Search } from 'lib/components/Search/Search'
import { SearchItem } from 'lib/components/Search/searchLogic'
+import { newInternalTab } from 'lib/utils/newInternalTab'
import { SceneExport } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'
@@ -13,8 +14,13 @@ export const scene: SceneExport = {
export function NewTabScene(): JSX.Element {
const { searchParams } = useValues(router)
- const handleItemSelect = useCallback((item: SearchItem) => {
- if (item.href) {
+ const handleItemSelect = useCallback((item: SearchItem, openInNewTab?: boolean) => {
+ if (!item.href) {
+ return
+ }
+ if (openInNewTab) {
+ newInternalTab(item.href)
+ } else {
router.actions.push(item.href)
}
}, [])