forked from mantinedev/mantine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotlight.store.ts
146 lines (123 loc) · 4.28 KB
/
spotlight.store.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { clamp } from '@mantine/hooks';
import { createStore, MantineStore, useStore } from '@mantine/store';
export interface SpotlightState {
opened: boolean;
selected: number;
listId: string;
query: string;
empty: boolean;
registeredActions: Set<string>;
}
export type SpotlightStore = MantineStore<SpotlightState>;
export const createSpotlightStore = () =>
createStore<SpotlightState>({
opened: false,
empty: false,
selected: -1,
listId: '',
query: '',
registeredActions: new Set(),
});
export const useSpotlight = (store: SpotlightStore) => useStore(store);
export function updateSpotlightStateAction(
update: (state: SpotlightState) => Partial<SpotlightState>,
store: SpotlightStore
) {
const state = store.getState();
store.setState({ ...state, ...update(store.getState()) });
}
export function openSpotlightAction(store: SpotlightStore) {
updateSpotlightStateAction(() => ({ opened: true, selected: -1 }), store);
}
export function closeSpotlightAction(store: SpotlightStore) {
updateSpotlightStateAction(() => ({ opened: false }), store);
}
export function toggleSpotlightAction(store: SpotlightStore) {
updateSpotlightStateAction((state) => ({ opened: !state.opened }), store);
}
export function setSelectedAction(index: number, store: SpotlightStore) {
store.updateState((state) => ({ ...state, selected: index }));
}
export function setListId(id: string, store: SpotlightStore) {
store.updateState((state) => ({ ...state, listId: id }));
}
export function selectAction(index: number, store: SpotlightStore): number {
const state = store.getState();
const actionsList = document.getElementById(state.listId);
const selected = actionsList?.querySelector<HTMLButtonElement>('[data-selected]');
const actions = actionsList?.querySelectorAll<HTMLButtonElement>('[data-action]') ?? [];
const nextIndex = index === -1 ? actions.length - 1 : index === actions.length ? 0 : index;
const selectedIndex = clamp(nextIndex, 0, actions.length - 1);
selected?.removeAttribute('data-selected');
actions[selectedIndex]?.scrollIntoView({ block: 'nearest' });
actions[selectedIndex]?.setAttribute('data-selected', 'true');
setSelectedAction(selectedIndex, store);
return selectedIndex;
}
export function selectNextAction(store: SpotlightStore) {
return selectAction(store.getState().selected + 1, store);
}
export function selectPreviousAction(store: SpotlightStore) {
return selectAction(store.getState().selected - 1, store);
}
export function triggerSelectedAction(store: SpotlightStore) {
const state = store.getState();
const selected = document.querySelector<HTMLButtonElement>(`#${state.listId} [data-selected]`);
selected?.click();
}
export function registerAction(id: string, store: SpotlightStore) {
const state = store.getState();
state.registeredActions.add(id);
return () => {
state.registeredActions.delete(id);
};
}
export function setQuery(query: string, store: SpotlightStore) {
updateSpotlightStateAction(() => ({ query }), store);
Promise.resolve().then(() => {
selectAction(0, store);
updateSpotlightStateAction(
(state) => ({
empty: (state.query.trim().length > 0 && state.registeredActions.size === 0) || false,
}),
store
);
});
}
export function clearSpotlightState(
{ clearQuery }: { clearQuery: boolean | undefined },
store: SpotlightStore
) {
store.updateState((state) => ({
...state,
selected: -1,
query: clearQuery ? '' : state.query,
empty: clearQuery ? false : state.empty,
}));
}
export const spotlightActions = {
open: openSpotlightAction,
close: closeSpotlightAction,
toggle: toggleSpotlightAction,
updateState: updateSpotlightStateAction,
setSelectedAction,
setListId,
selectAction,
selectNextAction,
selectPreviousAction,
triggerSelectedAction,
registerAction,
setQuery,
clearSpotlightState,
};
export function createSpotlight() {
const store = createSpotlightStore();
const actions = {
open: () => openSpotlightAction(store),
close: () => closeSpotlightAction(store),
toggle: () => toggleSpotlightAction(store),
};
return [store, actions] as const;
}
export const [spotlightStore, spotlight] = createSpotlight();
export const { open: openSpotlight, close: closeSpotlight, toggle: toggleSpotlight } = spotlight;