solid-awesome-hooks
Version:
A collection of awesome hooks for solid-js
23 lines (22 loc) • 677 B
JavaScript
import { createSignal } from "solid-js";
export const useVisibleState = (initialState) => {
const [isOpen, setOpen] = createSignal(Boolean(initialState));
const hide = () => setOpen(false);
const reveal = () => setOpen(true);
const withAction = (action, callback) => (...args) => {
const result = callback?.(...args);
if (action === "hide")
hide();
else
reveal();
return result;
};
return {
isOpen,
setOpen,
hide,
reveal,
/** A useful wrapper which adds `reveal` or `hide` action for wrapping function */
withAction,
};
};