-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c66695
commit 7677af5
Showing
11 changed files
with
218 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@sa/hooks", | ||
"version": "2.0.0", | ||
"exports": { | ||
".": "./src/index.ts" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"./src/*" | ||
] | ||
} | ||
} | ||
} |
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,5 @@ | ||
import useBoolean from './use-boolean'; | ||
import useLoading from './use-loading'; | ||
import useContext from './use-context'; | ||
|
||
export { useBoolean, useLoading, useContext }; |
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,30 @@ | ||
import { ref } from 'vue'; | ||
|
||
/** | ||
* boolean | ||
* @param initValue init value | ||
*/ | ||
export default function useBoolean(initValue = false) { | ||
const bool = ref(initValue); | ||
|
||
function setBool(value: boolean) { | ||
bool.value = value; | ||
} | ||
function setTrue() { | ||
setBool(true); | ||
} | ||
function setFalse() { | ||
setBool(false); | ||
} | ||
function toggle() { | ||
setBool(!bool.value); | ||
} | ||
|
||
return { | ||
bool, | ||
setBool, | ||
setTrue, | ||
setFalse, | ||
toggle | ||
}; | ||
} |
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,108 @@ | ||
import { inject, provide } from 'vue'; | ||
import type { InjectionKey } from 'vue'; | ||
|
||
type ContextFn<T> = () => T; | ||
|
||
/** | ||
* use context | ||
* @param contextName context name | ||
* @param fn context function | ||
* @example | ||
* ```ts | ||
* // there are three vue files: A.vue, B.vue, C.vue, and A.vue is the parent component of B.vue and C.vue | ||
* | ||
* // context.ts | ||
* import { ref } from 'vue'; | ||
* import { useContext } from '@sa/hooks'; | ||
* | ||
* export const { setupStore, useStore } = useContext('demo', () => { | ||
* const count = ref(0); | ||
* | ||
* function increment() { | ||
* count.value++; | ||
* } | ||
* | ||
* function decrement() { | ||
* count.value--; | ||
* } | ||
* | ||
* return { | ||
* count, | ||
* increment, | ||
* decrement | ||
* }; | ||
* }) | ||
* ``` | ||
* | ||
* // A.vue | ||
* ```vue | ||
* <template> | ||
* <div>A</div> | ||
* </template> | ||
* <script setup lang="ts"> | ||
* import { setupStore } from './context'; | ||
* | ||
* setupStore(); | ||
* // const { increment } = setupStore(); // also can control the store in the parent component | ||
* </script> | ||
* ``` | ||
* // B.vue | ||
* ```vue | ||
* <template> | ||
* <div>B</div> | ||
* </template> | ||
* <script setup lang="ts"> | ||
* import { useStore } from './context'; | ||
* | ||
* const { count, increment } = useStore(); | ||
* </script> | ||
* ``` | ||
* | ||
* // C.vue is same as B.vue | ||
*/ | ||
export default function useContext<T>(contextName: string, fn: ContextFn<T>) { | ||
const { useProvide, useInject } = createContext<T>(contextName); | ||
|
||
const context = fn(); | ||
|
||
/** | ||
* setup store in the parent component | ||
*/ | ||
function setupStore() { | ||
return useProvide(context); | ||
} | ||
|
||
/** | ||
* use store in the child component | ||
*/ | ||
function useStore() { | ||
return useInject(); | ||
} | ||
|
||
return { | ||
setupStore, | ||
useStore | ||
}; | ||
} | ||
|
||
/** | ||
* create context | ||
*/ | ||
function createContext<T>(contextName: string) { | ||
const injectKey: InjectionKey<T> = Symbol(contextName); | ||
|
||
function useProvide(context: T) { | ||
provide(injectKey, context); | ||
|
||
return context; | ||
} | ||
|
||
function useInject() { | ||
return inject(injectKey) as T; | ||
} | ||
|
||
return { | ||
useProvide, | ||
useInject | ||
}; | ||
} |
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,15 @@ | ||
import useBoolean from './use-boolean'; | ||
|
||
/** | ||
* loading | ||
* @param initValue init value | ||
*/ | ||
export default function useLoading(initValue = false) { | ||
const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(initValue); | ||
|
||
return { | ||
loading, | ||
startLoading, | ||
endLoading | ||
}; | ||
} |
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,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"lib": ["DOM", "ESNext"], | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"jsx": "preserve", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"noUnusedLocals": true, | ||
"strictNullChecks": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"types": ["node"] | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
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
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 @@ | ||
<script setup lang="ts"></script> | ||
|
||
<template> | ||
<LookForward /> | ||
</template> | ||
|
||
<style scoped></style> |