Skip to content

Commit 2721b61

Browse files
authored
fix: Fix command palette (#3777)
1 parent 7e8e0eb commit 2721b61

File tree

7 files changed

+101
-87
lines changed

7 files changed

+101
-87
lines changed

src/command-pallette/CommandPaletteProvider.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/command-pallette/CommandPalletteUI/CommandPaletteSearchBar.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
flex-grow: 1;
77
}
88

9-
@media (max-width: 1040px) {
9+
@container (max-width: 1040px) {
1010
.command-palette-search-bar {
1111
width: 100% !important;
1212
}

src/command-pallette/CommandPalletteUI/CommandPaletteSearchBar.tsx

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { useEffect, RefObject, useState } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { createPortal } from 'react-dom';
4+
import { useRecoilValue } from 'recoil';
45
import { Icon, Input } from '@ui5/webcomponents-react';
56
import { K8sResource } from 'types';
7+
import { useEventListener } from 'hooks/useEventListener';
68
import { useObjectState } from 'shared/useObjectState';
79
import { CommandPaletteUI } from './CommandPaletteUI';
8-
import { useRecoilValue } from 'recoil';
910
import { availableNodesSelector } from 'state/navigation/availableNodesSelector';
11+
import { showKymaCompanionState } from 'state/companion/showKymaCompanionAtom';
1012
import { SCREEN_SIZE_BREAKPOINT_M } from './types';
1113
import './CommandPaletteSearchBar.scss';
1214

@@ -26,11 +28,15 @@ export function CommandPaletteSearchBar({
2628
useRecoilValue(availableNodesSelector); // preload the values to prevent page rerenders
2729
const { t } = useTranslation();
2830
const [open, setOpen] = useState(shouldFocus || false);
31+
const [shellbarWidth, setShellbarWidth] = useState(window.innerWidth);
2932
const [resourceCache, updateResourceCache] = useObjectState<
3033
Record<string, K8sResource[]>
3134
>();
35+
const showCompanion = useRecoilValue(showKymaCompanionState);
3236
const shouldShowDialog = shouldFocus ? shouldFocus : open;
3337

38+
const htmlWrapEl = document.getElementById('html-wrap');
39+
3440
const setShowDialog = (value: boolean) => {
3541
const modalPresent =
3642
document.querySelector('ui5-dialog[open]') ||
@@ -41,6 +47,56 @@ export function CommandPaletteSearchBar({
4147
}
4248
};
4349

50+
const onKeyPress = (e: Event) => {
51+
const { key, metaKey, ctrlKey } = e as KeyboardEvent;
52+
// for (Edge, Chrome) || (Firefox, Safari)
53+
const isMac = (
54+
(navigator as any).userAgentData?.platform || navigator.platform
55+
)
56+
?.toLowerCase()
57+
?.startsWith('mac');
58+
const modifierKeyPressed = (isMac && metaKey) || (!isMac && ctrlKey);
59+
60+
if (
61+
(key === 'k' || key === 'K') &&
62+
modifierKeyPressed &&
63+
window.location.pathname !== '/clusters'
64+
) {
65+
setShowDialog(!shouldShowDialog);
66+
// [on Firefox] prevent opening the browser search bar via CMD/CTRL+K
67+
e.preventDefault();
68+
}
69+
};
70+
71+
useEventListener('keydown', onKeyPress, [shouldShowDialog]);
72+
73+
let timer: ReturnType<typeof setTimeout>;
74+
function handleChangedWidth() {
75+
clearTimeout(timer);
76+
77+
timer = setTimeout(() => {
78+
setShellbarWidth(
79+
showCompanion.show
80+
? shellbarRef?.current?.getBoundingClientRect().width || 0
81+
: window.innerWidth,
82+
);
83+
}, 0);
84+
}
85+
86+
useEffect(() => {
87+
const elementObserver = new ResizeObserver(() => {
88+
handleChangedWidth();
89+
});
90+
91+
if (htmlWrapEl) {
92+
elementObserver.observe(htmlWrapEl);
93+
}
94+
return () => {
95+
elementObserver.disconnect();
96+
};
97+
// eslint-disable-next-line react-hooks/exhaustive-deps
98+
}, [showCompanion]);
99+
44100
useEffect(() => {
45101
const shellbarCurr = shellbarRef?.current;
46102
const searchButton = shellbarCurr?.shadowRoot?.querySelector(
@@ -53,7 +109,7 @@ export function CommandPaletteSearchBar({
53109
if (
54110
searchButton &&
55111
searchField &&
56-
window.innerWidth > SCREEN_SIZE_BREAKPOINT_M
112+
shellbarWidth > SCREEN_SIZE_BREAKPOINT_M
57113
) {
58114
searchButton.style.display = 'none';
59115

@@ -65,7 +121,7 @@ export function CommandPaletteSearchBar({
65121
shellbarCurr?.removeAttribute('show-search-field');
66122
searchField.style.display = 'none';
67123
}
68-
}, [window.innerWidth, shellbarRef?.current]); // eslint-disable-line react-hooks/exhaustive-deps
124+
}, [shellbarRef?.current, shellbarWidth, shouldShowDialog]); // eslint-disable-line react-hooks/exhaustive-deps
69125

70126
return (
71127
<>
@@ -90,8 +146,9 @@ export function CommandPaletteSearchBar({
90146
}}
91147
resourceCache={resourceCache}
92148
updateResourceCache={updateResourceCache}
149+
shellbarWidth={shellbarWidth}
93150
/>,
94-
document.body,
151+
htmlWrapEl || document.body,
95152
)}
96153
</>
97154
);

src/command-pallette/CommandPalletteUI/CommandPaletteUI.scss

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.command-palette-ui {
2-
position: fixed;
2+
position: absolute;
33
top: calc(var(--_ui5-v2-7-0_shellbar_root_height) + 0.5rem);
44
width: 100%;
55
height: 100%;
@@ -14,8 +14,8 @@
1414
display: flex;
1515
width: 60vw;
1616
max-width: 700px;
17-
position: fixed;
18-
top: 0.5rem;
17+
position: absolute;
18+
top: calc(var(--_ui5-v2-7-0_shellbar_root_height) * -1);
1919
opacity: 0%; //prevent visually jumping
2020
}
2121

@@ -41,23 +41,37 @@
4141
display: none;
4242
}
4343
}
44+
45+
&__wrapper.full-size {
46+
width: 100%;
47+
max-width: unset;
48+
height: 100%;
49+
opacity: 100%;
50+
51+
.input-container {
52+
.search-with-display-more {
53+
margin-right: 2.5rem;
54+
}
55+
56+
.input-back-button {
57+
display: block;
58+
padding: 0 0.25rem;
59+
box-sizing: content-box;
60+
}
61+
}
62+
}
4463
}
4564

4665
@media (max-width: 1040px) {
4766
.command-palette-ui {
4867
&__wrapper {
49-
width: 100vw;
68+
width: 100%;
5069
max-width: unset;
51-
top: 0;
5270
height: 100%;
5371
opacity: 100%;
5472
}
5573

5674
.input-container {
57-
display: flex;
58-
flex-direction: row;
59-
align-items: center;
60-
6175
.search-with-display-more {
6276
margin-right: 2.5rem;
6377
}

src/command-pallette/CommandPalletteUI/CommandPaletteUI.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Button, Icon, Input } from '@ui5/webcomponents-react';
1616
import './CommandPaletteUI.scss';
1717
import { handleActionIfFormOpen } from 'shared/components/UnsavedMessageBox/helpers';
1818
import { isResourceEditedState } from 'state/resourceEditedAtom';
19+
import { showKymaCompanionState } from 'state/companion/showKymaCompanionAtom';
1920
import { isFormOpenState } from 'state/formOpenAtom';
2021
import { SCREEN_SIZE_BREAKPOINT_M } from './types';
2122

@@ -46,13 +47,15 @@ type CommandPaletteProps = {
4647
hide: () => void;
4748
resourceCache: Record<string, K8sResource[]>;
4849
updateResourceCache: (key: string, resources: K8sResource[]) => void;
50+
shellbarWidth: number;
4951
};
5052

5153
export function CommandPaletteUI({
5254
showCommandPalette,
5355
hide,
5456
resourceCache,
5557
updateResourceCache,
58+
shellbarWidth,
5659
}: CommandPaletteProps) {
5760
const namespace = useRecoilValue(activeNamespaceIdState);
5861
const [isResourceEdited, setIsResourceEdited] = useRecoilState(
@@ -69,6 +72,7 @@ export function CommandPaletteUI({
6972
const [activeResultIndex, setActiveResultIndex] = useState(0);
7073
const [isHistoryMode, setHistoryMode] = useState(false);
7174
const [historyIndex, setHistoryIndex] = useState(0);
75+
const showCompanion = useRecoilValue(showKymaCompanionState);
7276

7377
const commandPaletteRef = useRef<HTMLDivElement | null>(null);
7478

@@ -110,18 +114,19 @@ export function CommandPaletteUI({
110114

111115
//position Command Palette
112116
if (
113-
window.innerWidth > SCREEN_SIZE_BREAKPOINT_M &&
117+
shellbarWidth > SCREEN_SIZE_BREAKPOINT_M &&
114118
headerInput &&
115119
paletteCurrent
116120
) {
117121
const shellbarRect = headerInput.getBoundingClientRect();
118-
paletteCurrent.style.right = `${window.innerWidth -
119-
shellbarRect.right}px`;
122+
paletteCurrent.style.right = `${shellbarWidth - shellbarRect.right}px`;
123+
paletteCurrent.style.left = 'unset';
120124
paletteCurrent.style.opacity = '100%'; //prevent visually jumping
121125
} else if (paletteCurrent) {
122126
paletteCurrent.style.right = '0px';
127+
paletteCurrent.style.left = '0px';
123128
}
124-
}, [showCommandPalette, window.innerWidth]); // eslint-disable-line react-hooks/exhaustive-deps
129+
}, [showCommandPalette, shellbarWidth]); // eslint-disable-line react-hooks/exhaustive-deps
125130

126131
const commandPaletteInput = document.getElementById('command-palette-search');
127132

@@ -237,7 +242,11 @@ export function CommandPaletteUI({
237242
return (
238243
<Background hide={hide}>
239244
<div
240-
className="command-palette-ui__wrapper"
245+
className={`command-palette-ui__wrapper ${
246+
showCompanion.show && shellbarWidth < SCREEN_SIZE_BREAKPOINT_M
247+
? 'full-size'
248+
: ''
249+
}`}
241250
role="dialog"
242251
ref={commandPaletteRef}
243252
>

src/header/Header.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
ui5-shellbar.header {
77
padding: 0 !important;
88
border-radius: 0.5rem;
9+
container-type: inline-size;
910

1011
&::part(root) {
1112
padding-inline-start: 0.75rem;

src/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { savePreviousPath } from 'state/useAfterInitHook';
99

1010
import App from './components/App/App';
1111
import { Spinner } from 'shared/components/Spinner/Spinner';
12-
import { CommandPaletteProvider } from 'command-pallette/CommandPaletteProvider';
1312
import { NotificationProvider } from 'shared/contexts/NotificationContext';
1413

1514
import { ThemeProvider } from '@ui5/webcomponents-react';
@@ -65,9 +64,7 @@ root.render(
6564
<BrowserRouter>
6665
<Suspense fallback={<Spinner />}>
6766
<NotificationProvider>
68-
<CommandPaletteProvider>
69-
<App />
70-
</CommandPaletteProvider>
67+
<App />
7168
</NotificationProvider>
7269
</Suspense>
7370
</BrowserRouter>

0 commit comments

Comments
 (0)