Skip to content

Commit 44b624f

Browse files
committed
main 🧊 add use latest, use const, add usages in view
1 parent 8184e80 commit 44b624f

File tree

15 files changed

+186
-16
lines changed

15 files changed

+186
-16
lines changed

‎docs/functions/hooks/[name].md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Meta from '../../src/components/meta.vue'
33
import Api from '../../src/components/api.vue'
44
import Demo from '../../src/components/demo.vue'
55
import Contributors from '../../src/components/contributors.vue'
6+
import Code from '../../src/components/code.vue'
67
</script>
78

89
# {{ $params.name }}
@@ -11,15 +12,11 @@ import Contributors from '../../src/components/contributors.vue'
1112

1213
{{ $params.description }}
1314

14-
```typescript-vue
15-
import { {{ $params.name }} } from '@siberiacancode/reactuse';
16-
```
15+
<Code :code="$params.example" lang="typescript" />
1716

1817
## Usage
1918

20-
```typescript-vue
21-
{{ $params.usage }}
22-
```
19+
<Code :code="$params.usage" lang="typescript" />
2320

2421
## Demo
2522

‎docs/functions/hooks/[name].paths.ts renamed to ‎docs/functions/hooks/[name].paths.mts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { codeToHtml } from 'shiki'
2+
13
import { getHookFile, getHooks, matchJsdoc, parseHookJsdoc } from '../../src/utils';
24

35
export default {
@@ -16,19 +18,48 @@ export default {
1618

1719
const jsdoc = parseHookJsdoc(jsdocMatch);
1820

19-
if (!jsdoc.description || !jsdoc.usage) {
21+
if (!jsdoc.description || !jsdoc.usages.length) {
2022
console.error(`No content found for ${hook}`);
2123
return null;
2224
}
2325

26+
const usages = jsdoc.usages.reduce((acc, usage, index) => {
27+
if (index !== jsdoc.usages.length - 1) {
28+
acc += `${usage.description}\n// or\n`;
29+
} else {
30+
acc += usage.description;
31+
}
32+
return acc;
33+
}, '')
34+
35+
const usage = await codeToHtml(usages, {
36+
lang: 'typescript',
37+
themes: {
38+
light: 'github-light',
39+
dark: 'github-dark'
40+
},
41+
defaultColor: false
42+
});
43+
44+
45+
const example = await codeToHtml(`import { ${hook} } from '@siberiacancode/reactuse';`, {
46+
lang: 'typescript',
47+
themes: {
48+
light: 'github-light',
49+
dark: 'github-dark'
50+
},
51+
defaultColor: false
52+
});
53+
2454
return {
2555
params: {
2656
id: hook,
2757
name: hook,
58+
example,
2859
description: jsdoc.description.description,
2960
category: jsdoc.category?.name,
3061
lastModified: stats.mtime.getTime(),
31-
usage: jsdoc.usage.description,
62+
usage,
3263
apiParameters: jsdoc.apiParameters
3364
}
3465
};
@@ -46,3 +77,4 @@ export default {
4677
return params.filter(Boolean);
4778
}
4879
};
80+

‎docs/src/components/code.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup>
2+
import { onMounted, ref } from 'vue';
3+
4+
const props = defineProps({
5+
code: String,
6+
lang: String
7+
});
8+
9+
const container = ref(null);
10+
onMounted(() => {
11+
if (container.value) {
12+
container.value.innerHTML += props.code;
13+
}
14+
});
15+
</script>
16+
17+
<template>
18+
<div ref="container" :class="`language-${props.lang} vp-adaptive-theme vp-code`">
19+
<button title="Copy code" class="copy" />
20+
<span class="lang">{{ props.lang }}</span>
21+
</div>
22+
</template>

‎docs/src/utils/hooks/getHookItems.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const getHookItems = async (): Promise<HookItem[]> => {
2626

2727
const jsdoc = parseHookJsdoc(match[0].trim());
2828

29-
if (!jsdoc.description || !jsdoc.usage) {
29+
if (!jsdoc.description || !jsdoc.usages.length) {
3030
console.error(`No content found for ${hook}`);
3131
return null;
3232
}

‎docs/src/utils/parseHookJsdoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { parse } from 'comment-parser';
33
export const parseHookJsdoc = (file: string) => {
44
const jsdoc = parse(file)[0];
55
const description = jsdoc.tags.find(({ tag }) => tag === 'description');
6-
const usage = jsdoc.tags.find(({ tag }) => tag === 'example');
6+
const usages = jsdoc.tags.filter(({ tag }) => tag === 'example');
77
const deprecated = jsdoc.tags.find(({ tag }) => tag === 'deprecated');
88
const category = jsdoc.tags.find(({ tag }) => tag === 'category');
99
const apiParameters = jsdoc.tags.filter(
1010
({ tag }) => tag === 'param' || tag === 'overload' || tag === 'returns'
1111
);
1212

13-
return { description, usage, apiParameters, deprecated, category };
13+
return { description, usages, apiParameters, deprecated, category };
1414
};

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
],
1515
"license": "MIT",
16-
"homepage": "https://github.com/siberiacancode/reactuse",
16+
"homepage": "https://siberiacancode.github.io/reactuse/",
1717
"repository": {
1818
"type": "git",
1919
"url": "https://github.com/siberiacancode/reactuse.git"

‎src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './useBreakpoints/useBreakpoints';
66
export * from './useBrowserLanguage/useBrowserLanguage';
77
export * from './useClickOutside/useClickOutside';
88
export * from './useClipboard/useClipboard';
9+
export * from './useConst/useConst';
910
export * from './useCounter/useCounter';
1011
export * from './useCssVar/useCssVar';
1112
export * from './useDebounceCallback/useDebounceCallback';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useConst } from './useConst';
2+
3+
const Demo = () => {
4+
const mountTime = useConst(() => new Date().toTimeString())
5+
const obj = useConst({ a: Math.random() })
6+
7+
return (
8+
<div>
9+
<p>Mount time: <code>{mountTime}</code></p>
10+
<p>Value from constant object: <code>{obj.a}</code></p>
11+
</div>
12+
)
13+
};
14+
15+
export default Demo;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { renderHook } from '@testing-library/react';
2+
3+
import { useConst } from './useConst';
4+
5+
it('Should use const', () => {
6+
const { result } = renderHook(() => useConst('value'));
7+
8+
expect(result.current).toBe('value');
9+
});
10+
11+
it('should return the same constant value', () => {
12+
const { result, rerender } = renderHook(() => useConst('value'));
13+
expect(result.current).toBe('value');
14+
15+
rerender();
16+
17+
expect(result.current).toBe('value');
18+
});
19+
20+
it('Should call initializer function', () => {
21+
const init = vitest.fn(() => 99);
22+
const { result } = renderHook(() => useConst(init));
23+
24+
expect(result.current).toBe(99);
25+
expect(init).toHaveBeenCalledTimes(1);
26+
});

‎src/hooks/useConst/useConst.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useRef } from 'react';
2+
3+
/**
4+
* @name useConst
5+
* @description - Hook that returns the constant value
6+
* @category Utilities
7+
*
8+
* @template Value The type of the value
9+
* @param {(() => Value) | Value} initialValue The initial value of the constant
10+
* @returns {Value} The constant value
11+
*
12+
* @example
13+
* const value = useConst('value');
14+
*/
15+
export const useConst = <Value>(initialValue: (() => Value) | Value) =>
16+
useRef<Value>(initialValue instanceof Function ? initialValue() : initialValue).current;

0 commit comments

Comments
 (0)