-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
SpotlightProvider.tsx
114 lines (97 loc) · 3.16 KB
/
SpotlightProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useDidUpdate, useDisclosure } from '@mantine/hooks';
import React, { useRef, useState } from 'react';
import { useSpotlightEvents } from './events';
import { SpotlightContext } from './Spotlight.context';
import { InnerSpotlightProps, Spotlight } from './Spotlight/Spotlight';
import type { SpotlightAction } from './types';
import { useActionsState } from './use-actions-state/use-actions-state';
import { useSpotlightShortcuts } from './use-spotlight-shortcuts/use-spotlight-shortcuts';
export interface SpotlightProviderProps extends InnerSpotlightProps {
/** Actions list */
actions: SpotlightAction[] | ((query: string) => SpotlightAction[]);
/** Your application */
children?: React.ReactNode;
/** Called when spotlight opens */
onSpotlightOpen?(): void;
/** Called when spotlight closes */
onSpotlightClose?(): void;
/** Called when user enters text in search input */
onQueryChange?(query: string): void;
/** Keyboard shortcut or list of shortcuts to trigger spotlight */
shortcut?: string | string[] | null;
/** Should search be cleared when spotlight closes */
cleanQueryOnClose?: boolean;
/** Spotlight will not render if disabled is set to true */
disabled?: boolean;
/** Tags to ignore shortcut hotkeys on. */
tagsToIgnore?: string[];
}
export function SpotlightProvider({
actions: initialActions,
children,
shortcut = 'mod + K',
onSpotlightClose,
onSpotlightOpen,
onQueryChange,
cleanQueryOnClose = true,
transitionDuration = 150,
disabled = false,
tagsToIgnore = ['INPUT', 'TEXTAREA', 'SELECT'],
...others
}: SpotlightProviderProps) {
const timeoutRef = useRef<number>(-1);
const [query, setQuery] = useState('');
const [actions, { registerActions, updateActions, removeActions, triggerAction }] =
useActionsState(initialActions, query);
useDidUpdate(() => {
updateActions(initialActions);
}, [initialActions]);
const handleQueryChange = (value: string) => {
setQuery(value);
onQueryChange?.(value);
};
const [opened, { open, close, toggle }] = useDisclosure(false, {
onClose: () => {
onSpotlightClose?.();
if (cleanQueryOnClose) {
timeoutRef.current = window.setTimeout(() => {
handleQueryChange('');
}, transitionDuration);
}
},
onOpen: () => {
onSpotlightOpen?.();
window.clearTimeout(timeoutRef.current);
},
});
const ctx = {
openSpotlight: open,
closeSpotlight: close,
toggleSpotlight: toggle,
registerActions,
removeActions,
triggerAction,
opened,
actions,
query,
};
useSpotlightShortcuts(shortcut, open, tagsToIgnore);
useSpotlightEvents({ open, close, toggle, registerActions, removeActions, triggerAction });
return (
<SpotlightContext.Provider value={ctx}>
{!disabled && (
<Spotlight
actions={actions}
onClose={close}
opened={opened}
query={query}
onQueryChange={handleQueryChange}
transitionDuration={transitionDuration}
{...others}
/>
)}
{children}
</SpotlightContext.Provider>
);
}
SpotlightProvider.displayName = '@mantine/spotlight/SpotlightProvider';