Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(overlay): open/close overlay on keypress #196

Merged
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
8 changes: 8 additions & 0 deletions packages/astro/src/overlay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export default {
}),
);
}, 500);

eventTarget.dispatchEvent(
new CustomEvent('toggle-plugin', {
detail: {
state: true,
},
}),
);
});

Spotlight.onSevereEvent(() => {
Expand Down
9 changes: 9 additions & 0 deletions packages/overlay/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Trigger from './components/Trigger';
import type { Integration, IntegrationData } from './integrations/integration';
import { getSpotlightEventTarget } from './lib/eventTarget';
import { log } from './lib/logger';
import useKeyPress from './lib/useKeyPress';
import { connectToSidecar } from './sidecar';
import { NotificationCount, SpotlightOverlayOptions } from './types';

Expand All @@ -25,6 +26,14 @@ export default function App({
const [triggerButtonCount, setTriggerButtonCount] = useState<NotificationCount>({ count: 0, severe: false });
const [isOpen, setOpen] = useState(openOnInit);

useKeyPress(['ctrlKey', 'shiftKey', 's'], () => {
setOpen(prev => !prev);
});

useKeyPress(['metaKey', 'shiftKey', 's'], () => {
setOpen(prev => !prev);
});

useEffect(() => {
// Map that holds the information which kind of content type should be dispatched to which integration(s)
const contentTypeToIntegrations = new Map<string, Integration[]>();
Expand Down
2 changes: 1 addition & 1 deletion packages/overlay/src/components/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
const navigate = useNavigate();
const location = useLocation();

useKeyPress('Escape', () => {
useKeyPress(['Escape'], () => {

Check warning on line 37 in packages/overlay/src/components/Tabs.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/components/Tabs.tsx#L37

Added line #L37 was not covered by tests
if (setOpen && location.pathname.split('/').length === 2) {
setOpen(false);
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/overlay/src/components/Trigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function Trigger({
getAnchorClasses(anchor),
isOpen ? '!hidden' : '',
)}
title="Spotlight by Sentry"
onClick={() => setOpen(!isOpen)}
>
<ToolbarItem count={countSum} severe={Boolean(notificationCount.severe)}>
Expand Down
20 changes: 15 additions & 5 deletions packages/overlay/src/lib/useKeyPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

/**
* useKeyPress
* @param {string} key - the name of the key to respond to, compared against event.key
* @param {string[]} keys - an array of keys to respond to, compared against event.key
* @param {function} action - the action to perform on key press
* @param {boolean} propagate - whether to stop event propagation (default is false)
*/
// https://www.caktusgroup.com/blog/2020/07/01/usekeypress-hook-react/
export default function useKeyPress(key: string, action: () => void, propagate = false) {
export default function useKeyPress(keys: string[], action: () => void, propagate = false) {

Check warning on line 9 in packages/overlay/src/lib/useKeyPress.ts

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/lib/useKeyPress.ts#L9

Added line #L9 was not covered by tests
useEffect(() => {
function onKeyup(e: KeyboardEvent) {
if (!propagate) e.stopPropagation();
if (e.key === key) action();

if (
keys.every((key: string) => {
if (key in e) return e[key as keyof KeyboardEvent];
return e.key.toLowerCase() === key.toLowerCase();
})
) {
action();
}

Check warning on line 21 in packages/overlay/src/lib/useKeyPress.ts

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/lib/useKeyPress.ts#L13-L21

Added lines #L13 - L21 were not covered by tests
}

Check warning on line 23 in packages/overlay/src/lib/useKeyPress.ts

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/lib/useKeyPress.ts#L23

Added line #L23 was not covered by tests
window.addEventListener('keyup', onKeyup);

Check warning on line 25 in packages/overlay/src/lib/useKeyPress.ts

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/lib/useKeyPress.ts#L25

Added line #L25 was not covered by tests
return () => window.removeEventListener('keyup', onKeyup);
}, [key, action, propagate]);
}, [keys, action, propagate]);

Check warning on line 27 in packages/overlay/src/lib/useKeyPress.ts

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/lib/useKeyPress.ts#L27

Added line #L27 was not covered by tests
}
Loading