@guokit-dev/im-react 0.0.0-stage → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +158 -2
- package/dist/index.cjs +5279 -0
- package/dist/index.mjs +5257 -0
- package/dist/style.css +571 -0
- package/dist/types/core/chatController.d.ts +121 -0
- package/dist/types/core/externalStore.d.ts +25 -0
- package/dist/types/core/features.d.ts +8 -0
- package/dist/types/core/format.d.ts +32 -0
- package/dist/types/core/groupAvatarCache.d.ts +37 -0
- package/dist/types/core/imStore.d.ts +13 -0
- package/dist/types/core/index.d.ts +11 -0
- package/dist/types/core/media.d.ts +37 -0
- package/dist/types/core/memberCache.d.ts +31 -0
- package/dist/types/core/mention.d.ts +31 -0
- package/dist/types/core/pinyinIndex.d.ts +15 -0
- package/dist/types/core/strings.d.ts +315 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/ui/chat/ChatPage.d.ts +11 -0
- package/dist/types/ui/chat/Composer.d.ts +29 -0
- package/dist/types/ui/chat/MessageList.d.ts +17 -0
- package/dist/types/ui/chat/MessageRow.d.ts +24 -0
- package/dist/types/ui/chat/bubbles.d.ts +26 -0
- package/dist/types/ui/chat/emojis.d.ts +2 -0
- package/dist/types/ui/contacts/AddFriendDialog.d.ts +2 -0
- package/dist/types/ui/contacts/ContactsPage.d.ts +9 -0
- package/dist/types/ui/contacts/FriendGroups.d.ts +6 -0
- package/dist/types/ui/contacts/FriendSettings.d.ts +4 -0
- package/dist/types/ui/contacts/NewFriendsPage.d.ts +2 -0
- package/dist/types/ui/contacts/UserCard.d.ts +8 -0
- package/dist/types/ui/contacts/index.d.ts +5 -0
- package/dist/types/ui/contacts/routes.d.ts +4 -0
- package/dist/types/ui/contacts/shared.d.ts +33 -0
- package/dist/types/ui/context.d.ts +44 -0
- package/dist/types/ui/conversation/ConversationList.d.ts +13 -0
- package/dist/types/ui/group/GroupChats.d.ts +2 -0
- package/dist/types/ui/group/GroupInfo.d.ts +9 -0
- package/dist/types/ui/group/GroupMembers.d.ts +11 -0
- package/dist/types/ui/group/GroupNotice.d.ts +8 -0
- package/dist/types/ui/group/SelectFriends.d.ts +8 -0
- package/dist/types/ui/group/routes.d.ts +4 -0
- package/dist/types/ui/group/shared.d.ts +47 -0
- package/dist/types/ui/hooks.d.ts +9 -0
- package/dist/types/ui/layout/ImWorkspace.d.ts +8 -0
- package/dist/types/ui/layout/Layers.d.ts +19 -0
- package/dist/types/ui/layout/frame.d.ts +14 -0
- package/dist/types/ui/layout/useWide.d.ts +4 -0
- package/dist/types/ui/menu/Report.d.ts +7 -0
- package/dist/types/ui/navigator.d.ts +111 -0
- package/dist/types/ui/privacy/PrivacyPage.d.ts +7 -0
- package/dist/types/ui/privacy/routes.d.ts +4 -0
- package/dist/types/ui/routes.d.ts +7 -0
- package/dist/types/ui/theme.d.ts +30 -0
- package/dist/types/ui/types.d.ts +44 -0
- package/dist/types/ui/viewer/ImageViewer.d.ts +6 -0
- package/dist/types/ui/viewer/VideoPlayer.d.ts +5 -0
- package/dist/types/ui/widgets/Avatar.d.ts +16 -0
- package/dist/types/ui/widgets/GroupAvatar.d.ts +21 -0
- package/dist/types/ui/widgets/Icon.d.ts +43 -0
- package/dist/types/ui/widgets/IndexedList.d.ts +14 -0
- package/dist/types/ui/widgets/Menu.d.ts +36 -0
- package/dist/types/ui/widgets/Page.d.ts +16 -0
- package/dist/types/ui/widgets/UnreadBadge.d.ts +11 -0
- package/dist/types/ui/widgets/feedback.d.ts +23 -0
- package/dist/types/ui/widgets/useFocusTrap.d.ts +2 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 济宁市果壳信息科技有限公司
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @guokit-dev/im-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
guokit IM 的 React UI 组件:开箱即用的整页会话与聊天,可换皮。建在 JS SDK [`@guokit-dev/im`](../js/README.md) 之上,页面行为与文案同 Flutter 版 [`guokit_im_ui`](../flutter_ui/README.md)。
|
|
4
|
+
|
|
5
|
+
- **整页 `ImWorkspace`**:撑满父容器,容器宽 ≥ 768px 时是「导航条 + 会话列表 + 聊天主区 + 右侧抽屉」,窄了自动变成单栏页面栈(底部消息 / 通讯录 / 隐私三个标签)。断点看的是容器,不是窗口,嵌在页面局部也能切对
|
|
6
|
+
- **会话列表**:置顶、免打扰、删除、全部已读、@我提示、未读角标、断线横条;右上角「+」菜单(发起群聊、添加朋友),群聊用九宫格头像
|
|
7
|
+
- **聊天页**:文字、图片、视频、语音(只收听,Web 不录)、文件、位置、业务卡片;撤回、举报、失败重发、对方已读、正在输入、群 @ 与 @所有人
|
|
8
|
+
- **输入框**:Enter 发送、Shift+Enter 换行,输入法组字时的 Enter 不发;表情;粘贴截图、拖文件进聊天区、工具栏选图片视频与文件,发送前在附件条里预览;「+」菜单放宿主扩展项,超过服务端上限的直接拦下
|
|
9
|
+
- **通讯录与资料卡**(0.28.0 起):首字母分节与索引条、好友分组筛选、搜索备注或昵称、新的朋友(角标)、群聊列表;添加朋友(按完整账号或搜索号);资料卡分好友 / 陌生人两态,备注与分组、分组管理、拉黑、举报、删除
|
|
10
|
+
- **群资料**(0.28.0 起):成员格、全部成员与操作菜单(设管理员、禁言时长、移出、转让群主)、邀请与发起群聊、群名与群公告、免打扰与置顶、全员禁言、仅管理员可邀请、清空记录、退群与解散
|
|
11
|
+
- **隐私**(0.28.0 起):加好友方式、黑名单
|
|
12
|
+
- **换皮**:一组 CSS 变量,`theme` 属性或宿主 CSS 都能改;文案可整体或部分替换
|
|
13
|
+
- **零运行时依赖**,样式是纯 CSS,不要求宿主用 Tailwind 或任何 CSS-in-JS
|
|
14
|
+
|
|
15
|
+
消息搜索、客服会话(C 期)随后续版本加入。
|
|
16
|
+
|
|
17
|
+
需要 IM 服务端 ≥ v1.30.0(按搜索号查人、黑名单带昵称;只用会话与聊天时 ≥ v1.19.0 即可),JS SDK ≥ 0.28.0。
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i @guokit-dev/im @guokit-dev/im-react
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
peer 依赖:`react` / `react-dom` ≥ 18.2,`@guokit-dev/im` ≥ 0.28.0 且 < 1。
|
|
26
|
+
|
|
27
|
+
## 最小接入
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { GuokitIm, ImKickedOutError } from '@guokit-dev/im'
|
|
31
|
+
import { GuokitImUi, ImWorkspace } from '@guokit-dev/im-react'
|
|
32
|
+
import '@guokit-dev/im-react/style.css'
|
|
33
|
+
|
|
34
|
+
const im = new GuokitIm({
|
|
35
|
+
baseUrl: 'https://im.example.com',
|
|
36
|
+
// 向自家后端取 IM 令牌(自家后端用 API Key 调签令牌接口),原样返回 { token, expires_at }
|
|
37
|
+
tokenProvider: async () => {
|
|
38
|
+
const r = await fetch('/my-api/im-token', { method: 'POST' }).then((r) => r.json())
|
|
39
|
+
if (r.banned) throw new ImKickedOutError()
|
|
40
|
+
return r.data
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
await im.login(currentUser.id)
|
|
44
|
+
|
|
45
|
+
export function MessagesPage() {
|
|
46
|
+
return (
|
|
47
|
+
<div style={{ height: '100vh' }}>
|
|
48
|
+
<GuokitImUi im={im} me={{ nickname: currentUser.name, avatarUrl: currentUser.avatar }}
|
|
49
|
+
callbacks={{ onKickedOut: () => router.push('/login') }}>
|
|
50
|
+
<ImWorkspace />
|
|
51
|
+
</GuokitImUi>
|
|
52
|
+
</div>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `im` 由宿主创建、登录、登出;组件只订阅,卸载时取消订阅。还没登录时组件显示空态
|
|
58
|
+
- `me` 是本人昵称与头像(SDK 不提供本人资料),聊天页本人头像用
|
|
59
|
+
- `ImWorkspace` 撑满父容器,父容器要有高度
|
|
60
|
+
- 组件样式整体在 `@layer gkim` 里,宿主有全局 reset 时见下文「与宿主全局样式共存」
|
|
61
|
+
|
|
62
|
+
完整可跑的例子见 [demo/web](../demo/web)。
|
|
63
|
+
|
|
64
|
+
## 单页组件
|
|
65
|
+
|
|
66
|
+
宿主自己拼版时,不用 `ImWorkspace`,直接放单页组件(仍要包在 `GuokitImUi` 里):
|
|
67
|
+
|
|
68
|
+
| 组件 | 说明 |
|
|
69
|
+
|---|---|
|
|
70
|
+
| `ImConversationList` | 会话列表。props:`onOpen(conversation)`、`selectedId`、`showAddMenu`(默认 true,右上角「+」菜单);`showSearch` 随 C 期生效 |
|
|
71
|
+
| `ImChat` | 聊天页。目标三选一:`conversation`(会话对象)、`peer`(对方 userId)、`group`(群号);传空或多传同步抛 `TypeError` |
|
|
72
|
+
| `ImContacts` | 通讯录。props:`onSelect(userId)`,不传时打开内置资料卡 |
|
|
73
|
+
| `ImUserCard` | 资料卡。props:`userId`;陌生人的昵称头像 SDK 拿不到,取单聊会话里的对方资料,都没有只显示 userId |
|
|
74
|
+
| `ImGroupInfo` | 群资料。props:`groupId` |
|
|
75
|
+
| `ImPrivacy` | 隐私:加好友方式与黑名单 |
|
|
76
|
+
| `ImUnreadBadge` | 总未读角标,放宿主自己的导航上 |
|
|
77
|
+
| `ImAvatar` / `ImGroupAvatar` | 头像,配色与 Flutter 版一致;群头像用群头像图,没有时拼前 9 个成员的九宫格(进入视口才拉成员,同一实例共用缓存) |
|
|
78
|
+
|
|
79
|
+
单页模式下点会话:传了 `onOpen` 走它,否则回调 `callbacks.onOpenConversation(target)`,由宿主决定去哪。单页组件里打开的资料卡、群资料、新的朋友、群成员等页面以右侧抽屉或对话框浮在 `GuokitImUi` 里,不用宿主接路由;要开聊天时回调 `onOpenConversation`。
|
|
80
|
+
|
|
81
|
+
Hooks:`useIm()`、`useConversations()`、`useTotalUnread()`、`useConnection()`、`useFriends()`(好友快照,`friendsChanged` 时刷新)、`useImNavigator()`(`open(route)` / `back()` / `close()`,`ImWorkspace` 内用)。
|
|
82
|
+
|
|
83
|
+
### 默认路由(0.28.0 行为变化)
|
|
84
|
+
|
|
85
|
+
点头像、群聊右上角按钮,**没传回调时**由「不响应」改为打开内置资料卡与群资料;群聊右上角按钮改为始终显示。点自己的头像、客服与系统发送者(`@cs`、`@cs:<id>`、`@system`)的头像不响应也不回调。传了 `callbacks.onAvatarTap` / `onGroupInfoTap` 的宿主不受影响,口径同 Flutter `guokit_im_ui` 0.25.0。
|
|
86
|
+
|
|
87
|
+
## 换皮
|
|
88
|
+
|
|
89
|
+
`theme` 属性写成 `.gkim-root` 上的行内 CSS 变量,没写的用默认值:
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
<GuokitImUi im={im} theme={{ accent: '#0F766E', avatarShape: 'rounded', bubbleRadius: 8 }}>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
| theme 字段 | CSS 变量 | 默认 |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `accent` | `--gkim-accent` | `#2563EB`,按钮、选中态、本人气泡都跟它 |
|
|
98
|
+
| `selfBubble` / `selfBubbleText` | `--gkim-bubble-self-bg` / `--gkim-bubble-self-fg` | 跟 accent / `#FFFFFF` |
|
|
99
|
+
| `peerBubble` / `peerBubbleText` | `--gkim-bubble-peer-bg` / `--gkim-bubble-peer-fg` | `#FFFFFF` / `#1A1D23` |
|
|
100
|
+
| `bubbleRadius` | `--gkim-bubble-radius` | `12px`,靠头像的角固定 4px |
|
|
101
|
+
| `avatarShape` | `--gkim-avatar-radius` | `'circle'`(50%),`'rounded'` 为 25% |
|
|
102
|
+
| `groupAvatarShape` | `--gkim-group-avatar-radius` | `'rounded'` |
|
|
103
|
+
| `avatarSize` | `--gkim-avatar-size` | `40px`,会话列表是 1.2 倍 |
|
|
104
|
+
| `chatBackground` | `--gkim-chat-bg` | `#F5F6F8` |
|
|
105
|
+
| `timeDividerSize` | `--gkim-time-divider-size` | `12px` |
|
|
106
|
+
|
|
107
|
+
另有 `--gkim-text` / `-text-2` / `-text-3`、`--gkim-divider`、`--gkim-border`、`--gkim-danger`、`--gkim-surface` / `-surface-2`、`--gkim-font`、`--gkim-accent-soft`(由 accent 派生的浅底)等,宿主可以直接写 CSS 覆盖:
|
|
108
|
+
|
|
109
|
+
```css
|
|
110
|
+
.gkim-root { --gkim-divider: #e2e8f0; }
|
|
111
|
+
.gkim-bubble { font-size: 15px; }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
类名统一 `gkim-` 前缀、BEM 写法,算公开 API,改名会写进 CHANGELOG。
|
|
115
|
+
|
|
116
|
+
## 插槽与文案
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
<GuokitImUi
|
|
120
|
+
im={im}
|
|
121
|
+
messageRenderers={{ custom: (msg) => (msg.content.data?.order ? <OrderBubble msg={msg} /> : null) }}
|
|
122
|
+
moreItems={[{ icon: <BagIcon />, label: '发商品', onClick: (target) => openProductPicker(target) }]}
|
|
123
|
+
menuItems={(msg) => [{ label: '收藏', onClick: (m) => favorite(m) }]}
|
|
124
|
+
strings={{ send: '发出', inputPlaceholder: '说点什么…' }}
|
|
125
|
+
callbacks={{ onAvatarTap: (userId) => openProfile(userId), onCardTap: (card) => openCard(card) }}
|
|
126
|
+
>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- `messageRenderers`:按消息 type 覆盖气泡内容,返回 `null` 走默认
|
|
130
|
+
- `moreItems`:「+」菜单追加项。`target.to` 单聊是对方 userId、群聊为空串,群聊用 `{ groupId: target.groupId }` 发
|
|
131
|
+
- `menuItems`:追加到消息悬停「更多」与右键菜单末尾
|
|
132
|
+
- `strings`:键名同 Flutter `ImUiStrings`,浅合并;默认值导出为 `defaultStrings`
|
|
133
|
+
- `callbacks`:传了走宿主,没传用内置行为(`onAvatarTap`、`onGroupInfoTap` 见上文「默认路由」)。`onCardTap` 没传时新标签页打开卡片的 `web_link`(其次 `app_link`);`onLocationTap` 没传时位置气泡不可点;`onNavigate(route)` 在内置视图栈变化时回调,宿主要接浏览器后退键时用
|
|
134
|
+
|
|
135
|
+
## 与宿主全局样式共存
|
|
136
|
+
|
|
137
|
+
组件样式整体包在 `@layer gkim` 里。按 CSS 层叠规则,**不在任何 layer 里的声明总是压过 layer 里的**,与引入顺序无关;layer 之间则后声明的层优先。所以:
|
|
138
|
+
|
|
139
|
+
- **Tailwind v4**(preflight 在原生 `@layer base`)及其他用 `@layer` 的 reset:把 `style.css` 放在宿主 CSS 之后引入,`gkim` 层排在它们后面,reset 冲不掉组件样式
|
|
140
|
+
- **Tailwind v3、normalize.css、Bootstrap reboot** 这类写在 layer 外的全局 reset,会压过组件样式(如 `button { background-color: transparent }` 让发送按钮看不见、`* { border-width: 0 }` 让气泡边框消失)。把它们放进一个排在前面的 layer:
|
|
141
|
+
|
|
142
|
+
```css
|
|
143
|
+
@layer reset, gkim;
|
|
144
|
+
@import 'normalize.css' layer(reset); /* Tailwind v3:@import 'tailwindcss/base' layer(reset); */
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
宿主自己写来覆盖组件的 CSS 不放进 layer 就会生效(见上文「换皮」)。
|
|
148
|
+
|
|
149
|
+
## SSR(Next.js 等)
|
|
150
|
+
|
|
151
|
+
产物首行带 `'use client'`,import 时不碰 `window` 与 IndexedDB,可以在服务端组件树里引用。`GuokitIm` 实例要在客户端创建(如 `useEffect` 里,或放在只在客户端加载的模块里)。
|
|
152
|
+
|
|
153
|
+
## 浏览器
|
|
154
|
+
|
|
155
|
+
Chrome / Edge ≥ 111、Safari ≥ 16.4、Firefox ≥ 113(用到 `@layer` 与 `color-mix()`)。
|
|
156
|
+
|
|
157
|
+
## 跨域
|
|
158
|
+
|
|
159
|
+
浏览器直连 IM:IM 后台「IM 设置 → 允许的 Origin」要加上你站点的完整 origin(同时管 REST 与长连接),否则接口与长连接都会被拦。
|