Skip to content

Commit f52d4a9

Browse files
committed
fix: positioning also for cmd+k
1 parent 4871ed1 commit f52d4a9

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/command-pallette/CommandPalletteUI/CommandPaletteSearchBar.tsx

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,10 @@ import './CommandPaletteSearchBar.scss';
1010
export function CommandPaletteSearchBar({ slot }: { slot?: string }) {
1111
const { t } = useTranslation();
1212
const [open, setOpen] = useState(false);
13-
const inputRef = useRef<InputDomRef | null>(null);
14-
const commandPaletteRef = useRef<HTMLDivElement | null>(null);
1513
const [resourceCache, updateResourceCache] = useObjectState<
1614
Record<string, K8sResource[]>
1715
>();
1816

19-
useEffect(() => {
20-
if (!open || !inputRef.current || !commandPaletteRef.current) return;
21-
22-
const shellbarRect = inputRef.current.getBoundingClientRect();
23-
const paletteContent = commandPaletteRef.current.querySelector(
24-
'.command-palette-ui__content',
25-
) as HTMLElement;
26-
27-
if (!paletteContent) return;
28-
29-
const paletteWrapper = commandPaletteRef.current.querySelector(
30-
'.command-palette-ui__wrapper',
31-
) as HTMLElement;
32-
33-
if (paletteWrapper && window.screen.width > 1040) {
34-
paletteWrapper.style.left = `${shellbarRect.left +
35-
shellbarRect.width / 2 -
36-
paletteContent.offsetWidth / 2}px`;
37-
}
38-
}, [open]);
39-
4017
const setShowDialog = (value: boolean) => {
4118
const modalPresent = document.querySelector('ui5-dialog[open]');
4219
// disable opening palette if other modal is present
@@ -48,6 +25,7 @@ export function CommandPaletteSearchBar({ slot }: { slot?: string }) {
4825
return (
4926
<>
5027
<Input
28+
id="command-palette-search-bar"
5129
accessibleName="command-palette-search-bar"
5230
onClick={() => setOpen(true)}
5331
onInput={e => e.preventDefault()}
@@ -56,18 +34,15 @@ export function CommandPaletteSearchBar({ slot }: { slot?: string }) {
5634
icon={<Icon name="slim-arrow-right" />}
5735
slot={slot}
5836
placeholder={t('command-palette.search.quick-navigation')}
59-
ref={inputRef}
6037
/>
6138
{open &&
6239
createPortal(
63-
<div ref={commandPaletteRef}>
64-
<CommandPaletteUI
65-
showCommandPalette={open}
66-
hide={() => setShowDialog(false)}
67-
resourceCache={resourceCache}
68-
updateResourceCache={updateResourceCache}
69-
/>
70-
</div>,
40+
<CommandPaletteUI
41+
showCommandPalette={open}
42+
hide={() => setShowDialog(false)}
43+
resourceCache={resourceCache}
44+
updateResourceCache={updateResourceCache}
45+
/>,
7146
document.body,
7247
)}
7348
</>

src/command-pallette/CommandPalletteUI/CommandPaletteUI.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactNode, useEffect, useState } from 'react';
1+
import { ReactNode, useEffect, useRef, useState } from 'react';
22
import { useRecoilValue } from 'recoil';
33
import { useEventListener } from 'hooks/useEventListener';
44
import { addHistoryEntry, getHistoryEntries } from './search-history';
@@ -62,6 +62,8 @@ export function CommandPaletteUI({
6262
const [isHistoryMode, setHistoryMode] = useState(false);
6363
const [historyIndex, setHistoryIndex] = useState(0);
6464

65+
const commandPaletteRef = useRef<HTMLDivElement | null>(null);
66+
6567
const {
6668
results,
6769
suggestedQuery,
@@ -80,6 +82,21 @@ export function CommandPaletteUI({
8082
document.getElementById('command-palette-search')?.focus();
8183
}, []);
8284

85+
useEffect(() => {
86+
const headerInput = document.getElementById('command-palette-search-bar');
87+
const paletteCurrent = commandPaletteRef.current;
88+
89+
if (!showCommandPalette || !headerInput || !paletteCurrent) return;
90+
91+
const shellbarRect = headerInput.getBoundingClientRect();
92+
93+
if (paletteCurrent && window.screen.width > 1040) {
94+
paletteCurrent.style.left = `${shellbarRect.left +
95+
shellbarRect.width / 2 -
96+
paletteCurrent.offsetWidth / 2}px`;
97+
}
98+
}, [showCommandPalette]);
99+
83100
const commandPaletteInput = document.getElementById('command-palette-search');
84101

85102
const handleQuerySelection = () => {
@@ -179,7 +196,11 @@ export function CommandPaletteUI({
179196

180197
return (
181198
<Background hide={hide}>
182-
<div className="command-palette-ui__wrapper" role="dialog">
199+
<div
200+
className="command-palette-ui__wrapper"
201+
role="dialog"
202+
ref={commandPaletteRef}
203+
>
183204
<div className="command-palette-ui__content">
184205
<NamespaceContextDisplay
185206
namespaceContext={namespaceContext}

0 commit comments

Comments
 (0)