Skip to content

Commit

Permalink
feat(useToggle): new function (vueuse#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Dec 4, 2020
1 parent 958638c commit f5d2915
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/useToggle/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# useToggle

> A boolean switcher with utility functions.
## Usage

```js
import { useToggle } from '@vueuse/core'

const [value, toggle] = useToggle()
```
30 changes: 30 additions & 0 deletions packages/core/useToggle/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defineDemo, html } from '../../_docs'
import { defineComponent } from 'vue-demi'
import { useToggle } from '.'

defineDemo(
{
name: 'useToggle',
category: 'State',
docs: require('./index.md'),
module,
},
defineComponent({
setup() {
const [value, toggle] = useToggle()
return {
value,
toggle,
}
},

template: html`
<div>
<p>Value: {{ value ? 'ON' : 'OFF' }}</p>
<button @click="toggle()">Toggle</button>
<button @click="value = true">Set ON</button>
<button @click="value = false">Set OFF</button>
</div>
`,
}),
)
14 changes: 14 additions & 0 deletions packages/core/useToggle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ref } from 'vue-demi'

/**
* A boolean switcher with utility functions.
*
* @see {@link https://vueuse.js.org/useToggle}
* @param [initialValue=false]
*/
export function useToggle(initialValue = false) {
const value = ref(initialValue)
const toggle = () => (value.value = !value.value)

return [value, toggle]
}

0 comments on commit f5d2915

Please sign in to comment.