@stackoverfloweth/vue-compositions
Version:
A collection of reusable vue compositions.
34 lines • 1.3 kB
JavaScript
import { ref } from 'vue';
/**
* `useBoolean` is a utility composition for managing a boolean state.
* It returns an object with `value`, `toggle`, `setTrue`, and `setFalse` properties.
*
* @param {MaybeRef<boolean>} [valueRef] - Optional parameter. A Vue ref object or a boolean that holds the initial state.
*
* @returns {UseBoolean} - An object with the following properties:
* `value`: a Vue ref object that holds the current boolean state.
* `toggle`: a method to toggle the state.
* `setTrue`: a method to set the state to true.
* `setFalse`: a method to set the state to false.
*
* @example
* const { toggle, setTrue, setFalse, value } = useBoolean()
* toggle() // toggle the state
* setTrue() // set the state to true
* setFalse() // set the state to false
* console.log(value.value) // check the current state
*/
export function useBoolean(valueRef) {
const value = ref(valueRef ?? false);
const toggle = () => {
value.value = !value.value;
};
const setTrue = () => {
value.value = true;
};
const setFalse = () => {
value.value = false;
};
return { value, toggle, setTrue, setFalse };
}
//# sourceMappingURL=useBoolean.js.map