@it-corp/vpbank-spotlight
Version:
Command center components for react and VPbank components
142 lines (138 loc) • 4.65 kB
JavaScript
'use client';
;
var vpbankHooks = require('@it-corp/vpbank-hooks');
var vpbankStore = require('@it-corp/vpbank-store');
const createSpotlightStore = () => vpbankStore.createStore({
opened: false,
empty: false,
selected: -1,
listId: "",
query: "",
registeredActions: /* @__PURE__ */ new Set()
});
const useSpotlight = (store) => vpbankStore.useStore(store);
function updateSpotlightStateAction(update, store) {
const state = store.getState();
store.setState({ ...state, ...update(store.getState()) });
}
function openSpotlightAction(store) {
updateSpotlightStateAction(() => ({ opened: true, selected: -1 }), store);
}
function closeSpotlightAction(store) {
updateSpotlightStateAction(() => ({ opened: false }), store);
}
function toggleSpotlightAction(store) {
updateSpotlightStateAction((state) => ({ opened: !state.opened }), store);
}
function setSelectedAction(index, store) {
store.updateState((state) => ({ ...state, selected: index }));
}
function setListId(id, store) {
store.updateState((state) => ({ ...state, listId: id }));
}
function selectAction(index, store) {
const state = store.getState();
const actionsList = document.getElementById(state.listId);
const selected = actionsList?.querySelector("[data-selected]");
const actions = actionsList?.querySelectorAll("[data-action]") ?? [];
const nextIndex = index === -1 ? actions.length - 1 : index === actions.length ? 0 : index;
const selectedIndex = vpbankHooks.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;
}
function selectNextAction(store) {
return selectAction(store.getState().selected + 1, store);
}
function selectPreviousAction(store) {
return selectAction(store.getState().selected - 1, store);
}
function triggerSelectedAction(store) {
const state = store.getState();
const selected = document.querySelector(
`#${state.listId} [data-selected]`
);
selected?.click();
}
function registerAction(id, store) {
const state = store.getState();
state.registeredActions.add(id);
return () => {
state.registeredActions.delete(id);
};
}
function setQuery(query, store) {
updateSpotlightStateAction(() => ({ query }), store);
Promise.resolve().then(() => {
selectAction(0, store);
updateSpotlightStateAction(
(state) => ({
empty: state.query.trim().length > 0 && state.registeredActions.size === 0 || false
}),
store
);
});
}
function clearSpotlightState({ clearQuery }, store) {
store.updateState((state) => ({
...state,
selected: -1,
query: clearQuery ? "" : state.query,
empty: clearQuery ? false : state.empty
}));
}
const spotlightActions = {
open: openSpotlightAction,
close: closeSpotlightAction,
toggle: toggleSpotlightAction,
updateState: updateSpotlightStateAction,
setSelectedAction,
setListId,
selectAction,
selectNextAction,
selectPreviousAction,
triggerSelectedAction,
registerAction,
setQuery,
clearSpotlightState
};
function createSpotlight() {
const store = createSpotlightStore();
const actions = {
open: () => openSpotlightAction(store),
close: () => closeSpotlightAction(store),
toggle: () => toggleSpotlightAction(store)
};
return [store, actions];
}
const [spotlightStore, spotlight] = createSpotlight();
const {
open: openSpotlight,
close: closeSpotlight,
toggle: toggleSpotlight
} = spotlight;
exports.clearSpotlightState = clearSpotlightState;
exports.closeSpotlight = closeSpotlight;
exports.closeSpotlightAction = closeSpotlightAction;
exports.createSpotlight = createSpotlight;
exports.createSpotlightStore = createSpotlightStore;
exports.openSpotlight = openSpotlight;
exports.openSpotlightAction = openSpotlightAction;
exports.registerAction = registerAction;
exports.selectAction = selectAction;
exports.selectNextAction = selectNextAction;
exports.selectPreviousAction = selectPreviousAction;
exports.setListId = setListId;
exports.setQuery = setQuery;
exports.setSelectedAction = setSelectedAction;
exports.spotlight = spotlight;
exports.spotlightActions = spotlightActions;
exports.spotlightStore = spotlightStore;
exports.toggleSpotlight = toggleSpotlight;
exports.toggleSpotlightAction = toggleSpotlightAction;
exports.triggerSelectedAction = triggerSelectedAction;
exports.updateSpotlightStateAction = updateSpotlightStateAction;
exports.useSpotlight = useSpotlight;
//# sourceMappingURL=spotlight.store.cjs.map