Skip to content

Commit 900f21c

Browse files
committed
history: support clicks anywhere to deselect
1 parent ef32030 commit 900f21c

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

special-pages/pages/history/app/components/App.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useSearchCommit } from '../global/hooks/useSearchCommit.js';
1919
import { useRangesData } from '../global/Providers/HistoryServiceProvider.js';
2020
import { usePlatformName } from '../types.js';
2121
import { useLayoutMode } from '../global/hooks/useLayoutMode.js';
22+
import { useClickAnywhereElse } from '../global/hooks/useClickAnywhereElse.jsx';
2223

2324
export function App() {
2425
const platformName = usePlatformName();
@@ -39,6 +40,7 @@ export function App() {
3940
useURLReflection();
4041
useSearchCommit();
4142
useSearchCommitForRange();
43+
const clickAnywhere = useClickAnywhereElse();
4244

4345
/**
4446
* onClick can be passed directly to the main container,
@@ -62,7 +64,13 @@ export function App() {
6264
}, [onKeyDown, query]);
6365

6466
return (
65-
<div class={styles.layout} data-theme={isDarkMode ? 'dark' : 'light'} data-platform={platformName} data-layout-mode={mode}>
67+
<div
68+
class={styles.layout}
69+
data-theme={isDarkMode ? 'dark' : 'light'}
70+
data-platform={platformName}
71+
data-layout-mode={mode}
72+
onClick={clickAnywhere}
73+
>
6674
<aside class={styles.aside}>
6775
<Sidebar ranges={ranges} />
6876
</aside>

special-pages/pages/history/app/global/Providers/SelectionProvider.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ export function useRowInteractions(mainRef) {
9999
if (handled) {
100100
event.preventDefault();
101101
event.stopImmediatePropagation();
102-
} else {
103-
console.log('did not handle selection');
104102
}
105-
} else {
106-
dispatch({ kind: 'reset', reason: 'click occurred outside of rows' });
107103
}
108104
}
109105

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useSelectionDispatch } from '../Providers/SelectionProvider.js';
2+
import { useCallback } from 'preact/hooks';
3+
4+
/**
5+
* Custom hook that creates a callback function to handle click events occurring outside of specified elements.
6+
* The callback dispatches a reset action when the click event target is not a button or an anchor element.
7+
*/
8+
export function useClickAnywhereElse() {
9+
const dispatch = useSelectionDispatch();
10+
return useCallback(
11+
(e) => {
12+
if (e.target?.closest?.('button,a') === null) {
13+
dispatch({ kind: 'reset', reason: 'click occurred outside of rows' });
14+
}
15+
},
16+
[dispatch],
17+
);
18+
}

0 commit comments

Comments
 (0)