Skip to content

Commit 9fd7030

Browse files
committed
feat(custom): v0.0.1
1 parent 987e057 commit 9fd7030

28 files changed

+8486
-13
lines changed

.cz-config.json

Whitespace-only changes.

.czrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"path": "cz-customizable",
3+
"config": {
4+
"cz-customizable": {
5+
"format": "{type}: {subject}"
6+
}
7+
}
8+
}

.editorconfig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# 表示是最顶层的配置文件
4+
root = true
5+
6+
# 匹配全部文件
7+
[*]
8+
# 设置字符集
9+
charset = utf-8
10+
# 缩进风格,可选 space、tab
11+
indent_style = space
12+
# 缩进的空格数
13+
indent_size = 2
14+
# 结尾换行符,可选 lf、cr、crlf
15+
end_of_line = lf
16+
# 在文件结尾插入新行
17+
insert_final_newline = true
18+
# 删除一行中的前后空格
19+
trim_trailing_whitespace = true
20+
21+
# 匹配 md 结尾的文件
22+
[*.md]
23+
trim_trailing_whitespace = false
24+
25+
# 匹配 package.json 和 .travis.yml
26+
[{package.json,.travis.yml}]
27+
indent_style = space
28+
indent_size = 2

.github/workflows/npm-publish.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: npm publish # 定义工作流名称
2+
3+
on:
4+
workflow_dispatch: # 允许手动触发工作流,默认是不开启的
5+
push: # 当有代码推送到仓库时触发
6+
branches:
7+
- main
8+
9+
jobs:
10+
build: # 工作流程中的一个作业
11+
runs-on: ubuntu-latest # 指定运行作业的虚拟环境
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
- name: Set up Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: 20
19+
registry-url: https://registry.npmjs.org/
20+
- name: enable pnpm
21+
run: corepack enable
22+
- name: install Dependencies
23+
run: |
24+
ls -al
25+
pnpm i
26+
pnpm build
27+
- name: Publish to NPM
28+
run: npm publish
29+
env:
30+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.lintstagedrc.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
'*.{js,ts}': [
3+
"npm run lint",
4+
"npm run format",
5+
"npm run lint:check",
6+
"npm run format:check",
7+
],
8+
'*.{json,md}': [
9+
"npm run format",
10+
"npm run format:check",
11+
],
12+
};

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"useTabs": false,
5+
"singleQuote": true,
6+
"trailingComma": "all"
7+
}

README-ZH.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
一个实用的 React Hooks 集合,为你的日常开发提供便利。
2+
3+
## 安装
4+
5+
```bash
6+
# 使用 npm
7+
npm install r-hooks
8+
9+
# 使用 yarn
10+
yarn add r-hooks
11+
12+
# 使用 pnpm
13+
pnpm add r-hooks
14+
```
15+
16+
## 可用的 Hooks
17+
18+
### useTheme
19+
20+
用于管理主题(暗色/亮色/系统)的 Hook,支持持久化存储。
21+
22+
```tsx
23+
import { useTheme, ThemeProvider } from 'r-hooks';
24+
25+
function App() {
26+
const { theme, setTheme, resolvedTheme } = useTheme();
27+
28+
return (
29+
<div>
30+
<button onClick={() => setTheme('dark')}>暗色</button>
31+
<button onClick={() => setTheme('light')}>亮色</button>
32+
<button onClick={() => setTheme('system')}>跟随系统</button>
33+
</div>
34+
);
35+
}
36+
```
37+
38+
### useResizeObserver
39+
40+
用于监听元素尺寸变化的 Hook。
41+
42+
```tsx
43+
import { useResizeObserver } from 'r-hooks';
44+
45+
function Component() {
46+
const [ref, size] = useResizeObserver();
47+
48+
return (
49+
<div ref={ref}>
50+
宽度: {size.width}, 高度: {size.height}
51+
</div>
52+
);
53+
}
54+
```
55+
56+
### useThrottle
57+
58+
用于节流函数调用的 Hook。
59+
60+
```tsx
61+
import { useThrottle } from 'r-hooks';
62+
63+
function Component() {
64+
const throttledFn = useThrottle(() => {
65+
// 你的函数
66+
}, 1000);
67+
}
68+
```
69+
70+
### useDebounce
71+
72+
用于防抖函数调用的 Hook。
73+
74+
```tsx
75+
import { useDebounce } from 'r-hooks';
76+
77+
function Component() {
78+
const debouncedFn = useDebounce(() => {
79+
// 你的函数
80+
}, 1000);
81+
}
82+
```
83+
84+
### useScroll
85+
86+
用于追踪滚动位置的 Hook。
87+
88+
```tsx
89+
import { useScroll } from 'r-hooks';
90+
91+
function Component() {
92+
const { x, y, direction } = useScroll();
93+
94+
return (
95+
<div>
96+
横向滚动: {x}, 纵向滚动: {y}, 方向: {direction}
97+
</div>
98+
);
99+
}
100+
```
101+
102+
### useMediaQuery
103+
104+
用于响应媒体查询的 Hook。
105+
106+
```tsx
107+
import { useMediaQuery } from 'r-hooks';
108+
109+
function Component() {
110+
const isMobile = useMediaQuery('(max-width: 768px)');
111+
112+
return isMobile ? <移动端视图 /> : <桌面端视图 />;
113+
}
114+
```
115+
116+
### useQueryParams
117+
118+
用于管理 URL 查询参数的 Hook。
119+
120+
```tsx
121+
import { useQueryParams } from 'r-hooks';
122+
123+
function Component() {
124+
const [params, setParams] = useQueryParams();
125+
126+
return (
127+
<div>
128+
<button onClick={() => setParams({ page: 2 })}>跳转到第 2 页</button>
129+
</div>
130+
);
131+
}
132+
```
133+
134+
### useAsync
135+
136+
用于处理异步操作的 Hook。
137+
138+
```tsx
139+
import { useAsync } from 'r-hooks';
140+
141+
function Component() {
142+
const [execute, { data, loading, error }] = useAsync(async () => {
143+
const response = await fetch('/api/data');
144+
return response.json();
145+
});
146+
147+
return (
148+
<div>
149+
{loading && <div>加载中...</div>}
150+
{error && <div>错误: {error.message}</div>}
151+
{data && <div>数据: {JSON.stringify(data)}</div>}
152+
</div>
153+
);
154+
}
155+
```
156+
157+
### useClickOutside
158+
159+
用于检测元素外部点击的 Hook。
160+
161+
```tsx
162+
import { useClickOutside } from 'r-hooks';
163+
164+
function Component() {
165+
const ref = useRef(null);
166+
167+
useClickOutside(ref, () => {
168+
// 处理外部点击
169+
});
170+
171+
return <div ref={ref}>点击我外部</div>;
172+
}
173+
```
174+
175+
### useWindowSize
176+
177+
用于追踪窗口尺寸的 Hook。
178+
179+
```tsx
180+
import { useWindowSize } from 'r-hooks';
181+
182+
function Component() {
183+
const { width, height } = useWindowSize();
184+
185+
return (
186+
<div>
187+
窗口尺寸: {width}x{height}
188+
</div>
189+
);
190+
}
191+
```
192+
193+
### usePagination
194+
195+
用于管理分页状态的 Hook。
196+
197+
```tsx
198+
import { usePagination } from 'r-hooks';
199+
200+
function Component() {
201+
const { pagination, paginationState, setPageIndex, setPageSize, nextPage, prevPage, hasNextPage, hasPrevPage } =
202+
usePagination({
203+
initialPage: 1,
204+
initialPageSize: 10,
205+
total: 100,
206+
});
207+
208+
return (
209+
<div>
210+
<button onClick={prevPage} disabled={!hasPrevPage}>
211+
上一页
212+
</button>
213+
<span>第 {pagination.pageIndex} 页</span>
214+
<button onClick={nextPage} disabled={!hasNextPage}>
215+
下一页
216+
</button>
217+
</div>
218+
);
219+
}
220+
```
221+
222+
## 开发
223+
224+
```bash
225+
# 安装依赖
226+
pnpm install
227+
228+
# 开始开发
229+
pnpm dev
230+
231+
# 构建
232+
pnpm build
233+
234+
# 代码检查
235+
pnpm lint
236+
```
237+
238+
## 许可证
239+
240+
ISC © coderlzw

0 commit comments

Comments
 (0)