Skip to content

Commit

Permalink
feat(power): add usePower composable (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Dec 9, 2023
1 parent 17b62df commit b77819a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function sidebar(): DefaultTheme.SidebarItem[] {
items: [
{ text: 'Api', link: '/composables/api' },
{ text: 'Image', link: '/composables/image' },
{ text: 'Power', link: '/composables/power' },
{ text: 'Url', link: '/composables/url' },
{ text: 'Utils', link: '/composables/utils' }
]
Expand Down
2 changes: 1 addition & 1 deletion docs/composables/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function useQuery<Data = any>(
```

```ts
import { useQuery } from '@globalbrain/sefirot/lib/composables/Api.vue'
import { useQuery } from '@globalbrain/sefirot/lib/composables/Api'
const { data, loading } = useQuery((http) => {
return http.get('/api/users')
Expand Down
65 changes: 65 additions & 0 deletions docs/composables/power.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Power <Badge text="3.10.0" />

`Power` module provides a composable to work with modal state.

## `usePower`

```ts
interface Power {
/**
* the modal state.
*/
state: Ref<boolean>
/**
* shows the modal.
*/
on(fn?: () => void): boolean
onAfter(fn: () => Promise<any>): Promise<boolean>
/**
* hides the modal.
*/
off(fn?: () => void): boolean
offAfter(fn: () => Promise<any>): Promise<boolean>
/**
* toggles the modal state.
*/
toggle(fn?: () => void): boolean
toggleAfter(fn: () => Promise<any>): Promise<boolean>
}
```

```vue
<script setup lang="ts">
import SButton from '@globalbrain/sefirot/lib/components/Button.vue'
import SModal from '@globalbrain/sefirot/lib/components/Modal.vue'
import { usePower } from '@globalbrain/sefirot/lib/composables/Power'
const { state, on, off } = usePower()
</script>
<template>
<SButton mode="info" label="Open dialog" @click="on" />
<SModal :open="state" @close="off">
<p>Modal content</p>
</SModal>
</template>
```

You can also pass a function to `on`, `off` and `toggle` methods.

```vue
<script setup lang="ts">
import SButton from '@globalbrain/sefirot/lib/components/Button.vue'
import SModal from '@globalbrain/sefirot/lib/components/Modal.vue'
import { usePower } from '@globalbrain/sefirot/lib/composables/Power'
const { state, on, off } = usePower()
</script>
<template>
<SButton mode="info" label="Open dialog" @click="on(() => console.log('opened'))" />
<SModal :open="state" @close="off(() => console.log('closed'))">
<p>Modal content</p>
</SModal>
</template>
```
58 changes: 58 additions & 0 deletions lib/composables/Power.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { type Ref, ref } from 'vue'

export interface Power {
state: Ref<boolean>
on(fn?: () => void): boolean
onAfter(fn: () => Promise<any>): Promise<boolean>
off(fn?: () => void): boolean
offAfter(fn: () => Promise<any>): Promise<boolean>
toggle(fn?: () => void): boolean
toggleAfter(fn: () => Promise<any>): Promise<boolean>
}

export function usePower(initialValue = false): Power {
const state = ref(initialValue)

function on(fn: () => void = () => {}): boolean {
typeof fn === 'function' && fn()
state.value = true
return state.value
}

async function onAfter(fn: () => Promise<any>): Promise<boolean> {
await fn()
return on()
}

function off(fn: () => void = () => {}): boolean {
typeof fn === 'function' && fn()
state.value = false
return state.value
}

async function offAfter(fn: () => Promise<any>): Promise<boolean> {
await fn()
return off()
}

function toggle(fn: () => void = () => {}): boolean {
typeof fn === 'function' && fn()
state.value = !state.value
return state.value
}

async function toggleAfter(fn: () => Promise<any>): Promise<boolean> {
await fn()
return toggle()
}

return {
state,
on,
onAfter,
off,
offAfter,
toggle,
toggleAfter
}
}

0 comments on commit b77819a

Please sign in to comment.