-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(checkpoints): add useCheckpoint composable
- Loading branch information
1 parent
3b2de42
commit ec0ce29
Showing
38 changed files
with
561 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 4 additions & 17 deletions
21
packages/private/docs/.vitepress/theme/components/VPSwitchStoreStyle.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/private/docs/.vitepress/theme/composables/useBodyClass.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { MaybeRefOrGetter, toRef, watch } from 'vue' | ||
|
||
interface UseBodyClassOptions { | ||
trueClass: string | ||
falseClass: string | ||
} | ||
|
||
export function useBodyClass(value: MaybeRefOrGetter<boolean>, options: UseBodyClassOptions): void { | ||
const { trueClass } = options | ||
const { falseClass } = options | ||
|
||
const updateBodyClass = (newValue: boolean) => { | ||
document.body.classList.remove(newValue ? falseClass : trueClass) | ||
document.body.classList.add(newValue ? trueClass : falseClass) | ||
} | ||
|
||
watch(toRef(value), updateBodyClass, { immediate: true }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Composables {#composables} | ||
|
||
Checkpoinnts composables. | ||
|
||
## useCheckpoint {#use-checkpoint} | ||
|
||
The `useCheckpoint` composable returns the label for a checkpoint, and registers a listener so that any changes to that result will cause a re-render. | ||
|
||
When first accessed, this composable will create a listener so that changes to the label will cause a re-render. When the component containing this composable is unmounted, the listener will be automatically removed. | ||
|
||
### Parameters | ||
|
||
<div class="hide-default-store"> | ||
|
||
- `checkpoints` ([`Checkpoints`](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/)): The [`Checkpoints`](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/) object to be accessed. | ||
|
||
</div> | ||
|
||
- `checkpointId` ([`MaybeRefOrGetter`](https://vuejs.org/api/utility-types.html#maybereforgetter)`<string>`): The [Id](https://tinybase.org/api/common/type-aliases/identity/id/) of the checkpoint. | ||
|
||
### Returns | ||
|
||
- `ComputedRef<string | undefined>`: A **readonly** reference to the string label for the requested checkpoint, an empty string if it was never set, or `undefined` if the checkpoint does not exist.. | ||
|
||
### Example | ||
|
||
<div class="hide-default-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { useCell, injectStore, useCheckpoint } from 'vue-tinybase/custom-store' | ||
import { Store1Key, Checkpoints1Key } from './store' | ||
const store = injectStore(Store1Key) | ||
const checkpoints = injectCheckpoints(Checkpoints1Key) | ||
const checkpointLabel = useCheckpoint(checkpoints, '1') | ||
// UI will be empty | ||
store.setCell('pets', 'fido', 'sold', true) | ||
checkpoints.addCheckpoint('sale') | ||
// UI will show: 'sale' | ||
</script> | ||
<template> | ||
<div>{{ checkpointLabel }}</div> | ||
</template> | ||
``` | ||
|
||
</div> | ||
|
||
<div class="hide-custom-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { useCell, injectStore, useCheckpoint, injectCheckpoints } from 'vue-tinybase' | ||
const store = injectStore() | ||
const checkpoints = injectCheckpoints() | ||
const checkpointLabel = useCheckpoint('1') | ||
// UI will be empty | ||
store.setCell('pets', 'fido', 'sold', true) | ||
checkpoints.addCheckpoint('sale') | ||
// UI will show: 'sale' | ||
</script> | ||
<template> | ||
<div>{{ checkpointLabel }}</div> | ||
</template> | ||
``` | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Context {#context} | ||
|
||
Context-related functions for providing and injecting checkpoints object into the app or part of the app. | ||
|
||
For all the examples assume the following `store.ts` file: | ||
|
||
<div class="hide-custom-store"> | ||
|
||
```ts | ||
import { createStore, createCheckpoints } from 'tinybase' | ||
|
||
export const store = createStore() | ||
export const checkpoints = createCheckpoints(store) | ||
``` | ||
|
||
</div> | ||
|
||
<div class="hide-default-store"> | ||
|
||
```ts | ||
import { createStore, createCheckpoints } from 'tinybase' | ||
import type { InjectionKey } from 'vue' | ||
|
||
export const store1 = createStore() | ||
export const store2 = createStore() | ||
|
||
export const checkpoints1 = createCheckpoints(store1) | ||
export const checkpoints2 = createCheckpoints(store2) | ||
|
||
export const Store1Key = Symbol('Store1') as InjectionKey<typeof store1> | ||
export const Store2Key = Symbol('Store2') as InjectionKey<typeof store2> | ||
|
||
export const Checkpoints1Key = Symbol('Checkpoints1') as InjectionKey<typeof checkpoints1> | ||
export const Checkpoints2Key = Symbol('Checkpoints2') as InjectionKey<typeof checkpoints2> | ||
``` | ||
|
||
</div> | ||
|
||
## provideCheckpoints {#provide-checkpoints} | ||
|
||
Provide a checkpoints object to all child components, enabling them to access the checkpoints object without having to pass it down as a prop. | ||
|
||
- **Parameters** | ||
|
||
<div class="hide-default-store"> | ||
|
||
- `checkpointsKey` (`string | symbol`): Unique injection key. | ||
|
||
</div> | ||
|
||
- `checkpoints` ([`Checkpoints`](https://tinybase.org/api/checkpoints/interfaces/checkpoints/checkpoints/)): The checkpoints object to provide. | ||
|
||
- **Example** | ||
|
||
<div class="hide-custom-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { provideCheckpoints } from 'vue-tinybase' | ||
import { checkpoints } from './store' | ||
provideCheckpoints(checkpoints) | ||
</script> | ||
``` | ||
|
||
</div> | ||
|
||
<div class="hide-default-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { provideCheckpoints } from 'vue-tinybase/custom-store' | ||
import { checkpoints1, checkpoints2, Checkpoints1Key, Checkpoints2Key } from './store' | ||
provideCheckpoints(Checkpoints1Key, checkpoints1) | ||
provideCheckpoints(Checkpoints2Key, checkpoints2) | ||
</script> | ||
``` | ||
|
||
</div> | ||
|
||
## injectCheckpoints {#inject-checkpoints} | ||
|
||
Inject a checkpoints object provided by [`provideCheckpoints`](/api/checkpoints/context#provide-checkpoints). | ||
|
||
<div class="hide-default-store"> | ||
|
||
- **Parameters** | ||
|
||
- `checkpointsKey` (`string | symbol`): Checkpoints object injection key. | ||
|
||
</div> | ||
|
||
- **Example** | ||
|
||
<div class="hide-custom-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { injectCheckpoints } from 'vue-tinybase' | ||
const store = injectCheckpoints() | ||
</script> | ||
``` | ||
|
||
</div> | ||
|
||
<div class="hide-default-store"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { injectCheckpoints } from 'vue-tinybase/custom-store' | ||
import { Checkpoints1Key } from './store' | ||
const checkpoints1 = injectCheckpoints(Checkpoints1Key) | ||
</script> | ||
``` | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"vitepress": "^1.2.3" | ||
}, | ||
"dependencies": { | ||
"@vueuse/core": "^10.11.0", | ||
"vue": "^3.4.31" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/public/vue-tinybase/src/@types/custom-store/checkpoints/composables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js' | ||
import type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js' | ||
|
||
export type UseCheckpointFunction = UseCheckpointFunctionWithSchemas & UseCheckpointFunctionWithoutSchemas | ||
|
||
export type { UseCheckpointFunction as UseCheckpointFunctionWithSchemas } from './with-schemas/composables.js' | ||
export type { UseCheckpointFunction as UseCheckpointFunctionWithoutSchemas } from './without-schemas/composables.js' |
1 change: 1 addition & 0 deletions
1
packages/public/vue-tinybase/src/@types/custom-store/checkpoints/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './composables.js' |
7 changes: 7 additions & 0 deletions
7
packages/public/vue-tinybase/src/@types/custom-store/checkpoints/with-schemas/composables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity' | ||
import type { Checkpoints, Id } from 'tinybase/with-schemas' | ||
|
||
export type UseCheckpointFunction = ( | ||
checkpoints: Checkpoints<any>, | ||
checkpointId: MaybeRefOrGetter<Id>, | ||
) => ComputedRef<string | undefined> |
7 changes: 7 additions & 0 deletions
7
...es/public/vue-tinybase/src/@types/custom-store/checkpoints/without-schemas/composables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { ComputedRef, MaybeRefOrGetter } from '@vue/reactivity' | ||
import type { Checkpoints, Id } from 'tinybase' | ||
|
||
export type UseCheckpointFunction = ( | ||
checkpoints: Checkpoints, | ||
checkpointId: MaybeRefOrGetter<Id>, | ||
) => ComputedRef<string | undefined> |
2 changes: 2 additions & 0 deletions
2
packages/public/vue-tinybase/src/@types/custom-store/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './composables.js' | ||
export * from './events.js' | ||
export * from './references.js' | ||
export * from './checkpoints/index.js' |
Oops, something went wrong.