@liveblocks/react 0.17.1 → 0.17.4

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/index.d.ts CHANGED
@@ -52,32 +52,122 @@ declare type RoomProviderProps<
52
52
  children: React.ReactNode;
53
53
  } & RoomInitializers<TPresence, TStorage>
54
54
  >;
55
- declare function createRoomContext<
55
+ declare type RoomContext<
56
56
  TPresence extends JsonObject,
57
- TStorage extends LsonObject = LsonObject,
58
- TUserMeta extends BaseUserMeta = BaseUserMeta,
59
- TRoomEvent extends Json = never
60
- >(
61
- client: Client
62
- ): {
63
- RoomProvider: (props: RoomProviderProps<TPresence, TStorage>) => JSX.Element;
64
- useBatch: () => (callback: () => void) => void;
65
- useBroadcastEvent: () => (
66
- event: TRoomEvent,
67
- options?: BroadcastOptions
68
- ) => void;
69
- useErrorListener: (callback: (err: Error) => void) => void;
70
- useEventListener: (
57
+ TStorage extends LsonObject,
58
+ TUserMeta extends BaseUserMeta,
59
+ TRoomEvent extends Json
60
+ > = {
61
+ /**
62
+ * Makes a Room available in the component hierarchy below.
63
+ * When this component is unmounted, the current user leave the room.
64
+ * That means that you can't have 2 RoomProvider with the same room id in your react tree.
65
+ */
66
+ RoomProvider(props: RoomProviderProps<TPresence, TStorage>): JSX.Element;
67
+ /**
68
+ * Returns a function that batches modifications made during the given function.
69
+ * All the modifications are sent to other clients in a single message.
70
+ * All the modifications are merged in a single history item (undo/redo).
71
+ * All the subscribers are called only after the batch is over.
72
+ */
73
+ useBatch(): (callback: () => void) => void;
74
+ /**
75
+ * Returns a callback that lets you broadcast custom events to other users in the room
76
+ *
77
+ * @example
78
+ * const broadcast = useBroadcastEvent();
79
+ *
80
+ * broadcast({ type: "CUSTOM_EVENT", data: { x: 0, y: 0 } });
81
+ */
82
+ useBroadcastEvent(): (event: TRoomEvent, options?: BroadcastOptions) => void;
83
+ /**
84
+ * useErrorListener is a react hook that lets you react to potential room connection errors.
85
+ *
86
+ * @example
87
+ * useErrorListener(er => {
88
+ * console.error(er);
89
+ * })
90
+ */
91
+ useErrorListener(callback: (err: Error) => void): void;
92
+ /**
93
+ * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
94
+ *
95
+ * @example
96
+ * useEventListener(({ connectionId, event }) => {
97
+ * if (event.type === "CUSTOM_EVENT") {
98
+ * // Do something
99
+ * }
100
+ * });
101
+ */
102
+ useEventListener(
71
103
  callback: (eventData: { connectionId: number; event: TRoomEvent }) => void
72
- ) => void;
73
- useHistory: () => History;
74
- useList: <TKey extends Extract<keyof TStorage, string>>(
104
+ ): void;
105
+ /**
106
+ * Returns the room.history
107
+ */
108
+ useHistory(): History;
109
+ /**
110
+ * Returns a function that undoes the last operation executed by the current client.
111
+ * It does not impact operations made by other clients.
112
+ */
113
+ useUndo(): () => void;
114
+ /**
115
+ * Returns a function that redoes the last operation executed by the current client.
116
+ * It does not impact operations made by other clients.
117
+ */
118
+ useRedo(): () => void;
119
+ /**
120
+ * Returns the LiveList associated with the provided key.
121
+ * The hook triggers a re-render if the LiveList is updated, however it does not triggers a re-render if a nested CRDT is updated.
122
+ *
123
+ * @param key The storage key associated with the LiveList
124
+ * @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
125
+ *
126
+ * @example
127
+ * const animals = useList("animals"); // e.g. [] or ["🦁", "🐍", "🦍"]
128
+ */
129
+ useList<TKey extends Extract<keyof TStorage, string>>(
130
+ key: TKey
131
+ ): TStorage[TKey] | null;
132
+ /**
133
+ * Returns the LiveMap associated with the provided key. If the LiveMap does not exist, a new empty LiveMap will be created.
134
+ * The hook triggers a re-render if the LiveMap is updated, however it does not triggers a re-render if a nested CRDT is updated.
135
+ *
136
+ * @param key The storage key associated with the LiveMap
137
+ * @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
138
+ *
139
+ * @example
140
+ * const shapesById = useMap("shapes");
141
+ */
142
+ useMap<TKey extends Extract<keyof TStorage, string>>(
75
143
  key: TKey
76
- ) => TStorage[TKey] | null;
77
- useMap: <TKey_1 extends Extract<keyof TStorage, string>>(
78
- key: TKey_1
79
- ) => TStorage[TKey_1] | null;
80
- useMyPresence: () => [
144
+ ): TStorage[TKey] | null;
145
+ /**
146
+ * Returns the LiveObject associated with the provided key.
147
+ * The hook triggers a re-render if the LiveObject is updated, however it does not triggers a re-render if a nested CRDT is updated.
148
+ *
149
+ * @param key The storage key associated with the LiveObject
150
+ * @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
151
+ *
152
+ * @example
153
+ * const object = useObject("obj");
154
+ */
155
+ useObject<TKey extends Extract<keyof TStorage, string>>(
156
+ key: TKey
157
+ ): TStorage[TKey] | null;
158
+ /**
159
+ * Returns the presence of the current user of the current room, and a function to update it.
160
+ * It is different from the setState function returned by the useState hook from React.
161
+ * You don't need to pass the full presence object to update it.
162
+ *
163
+ * @example
164
+ * const [myPresence, updateMyPresence] = useMyPresence();
165
+ * updateMyPresence({ x: 0 });
166
+ * updateMyPresence({ y: 0 });
167
+ *
168
+ * // At the next render, "myPresence" will be equal to "{ x: 0, y: 0 }"
169
+ */
170
+ useMyPresence(): [
81
171
  TPresence,
82
172
  (
83
173
  overrides: Partial<TPresence>,
@@ -86,46 +176,128 @@ declare function createRoomContext<
86
176
  }
87
177
  ) => void
88
178
  ];
89
- useObject: <TKey_2 extends Extract<keyof TStorage, string>>(
90
- key: TKey_2
91
- ) => TStorage[TKey_2] | null;
92
- useOthers: () => Others<TPresence, TUserMeta>;
93
- useRedo: () => () => void;
94
- useRoom: () => Room<TPresence, TStorage, TUserMeta, TRoomEvent>;
95
- useSelf: () => User<TPresence, TUserMeta> | null;
96
- useStorage: () => [root: LiveObject<TStorage> | null];
97
- useUndo: () => () => void;
98
- useUpdateMyPresence: () => (
179
+ /**
180
+ * Returns an object that lets you get information about all the the users currently connected in the room.
181
+ *
182
+ * @example
183
+ * const others = useOthers();
184
+ *
185
+ * // Example to map all cursors in JSX
186
+ * {
187
+ * others.map((user) => {
188
+ * if (user.presence?.cursor == null) {
189
+ * return null;
190
+ * }
191
+ * return <Cursor key={user.connectionId} cursor={user.presence.cursor} />
192
+ * })
193
+ * }
194
+ */
195
+ useOthers(): Others<TPresence, TUserMeta>;
196
+ /**
197
+ * Returns the Room of the nearest RoomProvider above in the React component
198
+ * tree.
199
+ */
200
+ useRoom(): Room<TPresence, TStorage, TUserMeta, TRoomEvent>;
201
+ /**
202
+ * Gets the current user once it is connected to the room.
203
+ *
204
+ * @example
205
+ * const user = useSelf();
206
+ */
207
+ useSelf(): User<TPresence, TUserMeta> | null;
208
+ /**
209
+ * Returns the LiveObject instance that is the root of your entire Liveblocks
210
+ * Storage.
211
+ *
212
+ * @example
213
+ * const [root] = useStorage();
214
+ */
215
+ useStorage(): [root: LiveObject<TStorage> | null];
216
+ /**
217
+ * useUpdateMyPresence is similar to useMyPresence but it only returns the function to update the current user presence.
218
+ * If you don't use the current user presence in your component, but you need to update it (e.g. live cursor), it's better to use useUpdateMyPresence to avoid unnecessary renders.
219
+ *
220
+ * @example
221
+ * const updateMyPresence = useUpdateMyPresence();
222
+ * updateMyPresence({ x: 0 });
223
+ * updateMyPresence({ y: 0 });
224
+ *
225
+ * // At the next render, the presence of the current user will be equal to "{ x: 0, y: 0 }"
226
+ */
227
+ useUpdateMyPresence(): (
99
228
  overrides: Partial<TPresence>,
100
229
  options?: {
101
230
  addToHistory: boolean;
102
231
  }
103
232
  ) => void;
104
- deprecated_useList: {
105
- <TValue extends Lson>(key: string): LiveList<TValue> | null;
106
- <TValue_1 extends Lson>(
107
- key: string,
108
- items: TValue_1[]
109
- ): LiveList<TValue_1> | null;
110
- };
111
- deprecated_useMap: {
112
- <TKey_3 extends string, TValue_2 extends Lson>(key: string): LiveMap<
113
- TKey_3,
114
- TValue_2
115
- > | null;
116
- <TKey_4 extends string, TValue_3 extends Lson>(
117
- key: string,
118
- entries: readonly (readonly [TKey_4, TValue_3])[] | null
119
- ): LiveMap<TKey_4, TValue_3> | null;
120
- };
121
- deprecated_useObject: {
122
- <TData extends LsonObject>(key: string): LiveObject<TData> | null;
123
- <TData_1 extends LsonObject>(
124
- key: string,
125
- initialData: TData_1
126
- ): LiveObject<TData_1> | null;
127
- };
233
+ /**
234
+ * Returns the LiveList associated with the provided key.
235
+ * The hook triggers a re-render if the LiveList is updated, however it does not triggers a re-render if a nested CRDT is updated.
236
+ *
237
+ * @param key The storage key associated with the LiveList
238
+ * @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
239
+ *
240
+ * @example
241
+ * const animals = useList("animals"); // e.g. [] or ["🦁", "🐍", "🦍"]
242
+ */
243
+ deprecated_useList<TValue extends Lson>(key: string): LiveList<TValue> | null;
244
+ /**
245
+ * @deprecated We no longer recommend initializing the
246
+ * items from the useList() hook. For details, see https://bit.ly/3Niy5aP.
247
+ */
248
+ deprecated_useList<TValue extends Lson>(
249
+ key: string,
250
+ items: TValue[]
251
+ ): LiveList<TValue> | null;
252
+ /**
253
+ * Returns the LiveMap associated with the provided key. If the LiveMap does not exist, a new empty LiveMap will be created.
254
+ * The hook triggers a re-render if the LiveMap is updated, however it does not triggers a re-render if a nested CRDT is updated.
255
+ *
256
+ * @param key The storage key associated with the LiveMap
257
+ * @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
258
+ *
259
+ * @example
260
+ * const shapesById = useMap("shapes");
261
+ */
262
+ deprecated_useMap<TKey extends string, TValue extends Lson>(
263
+ key: string
264
+ ): LiveMap<TKey, TValue> | null;
265
+ /**
266
+ * @deprecated We no longer recommend initializing the
267
+ * entries from the useMap() hook. For details, see https://bit.ly/3Niy5aP.
268
+ */
269
+ deprecated_useMap<TKey extends string, TValue extends Lson>(
270
+ key: string,
271
+ entries: readonly (readonly [TKey, TValue])[] | null
272
+ ): LiveMap<TKey, TValue> | null;
273
+ /**
274
+ * Returns the LiveObject associated with the provided key.
275
+ * The hook triggers a re-render if the LiveObject is updated, however it does not triggers a re-render if a nested CRDT is updated.
276
+ *
277
+ * @param key The storage key associated with the LiveObject
278
+ * @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
279
+ *
280
+ * @example
281
+ * const object = useObject("obj");
282
+ */
283
+ deprecated_useObject<TData extends LsonObject>(
284
+ key: string
285
+ ): LiveObject<TData> | null;
286
+ /**
287
+ * @deprecated We no longer recommend initializing the fields from the
288
+ * useObject() hook. For details, see https://bit.ly/3Niy5aP.
289
+ */
290
+ deprecated_useObject<TData extends LsonObject>(
291
+ key: string,
292
+ initialData: TData
293
+ ): LiveObject<TData> | null;
128
294
  };
295
+ declare function createRoomContext<
296
+ TPresence extends JsonObject,
297
+ TStorage extends LsonObject = LsonObject,
298
+ TUserMeta extends BaseUserMeta = BaseUserMeta,
299
+ TRoomEvent extends Json = never
300
+ >(client: Client): RoomContext<TPresence, TStorage, TUserMeta, TRoomEvent>;
129
301
 
130
302
  /**
131
303
  * NOTE:
package/index.js CHANGED
@@ -75,9 +75,9 @@ function createRoomContext(client$1) {
75
75
  return client$1;
76
76
  }
77
77
  : useClient;
78
- var RoomContext = React__namespace.createContext(null);
78
+ var RoomCtx = React__namespace.createContext(null);
79
79
  function useRoom() {
80
- var room = React__namespace.useContext(RoomContext);
80
+ var room = React__namespace.useContext(RoomCtx);
81
81
  if (null == room)
82
82
  throw new Error("RoomProvider is missing from the react tree");
83
83
  return room;
@@ -285,7 +285,7 @@ function createRoomContext(client$1) {
285
285
  [_client, roomId]
286
286
  ),
287
287
  React__namespace.createElement(
288
- RoomContext.Provider,
288
+ RoomCtx.Provider,
289
289
  { value: room },
290
290
  props.children
291
291
  )
package/index.mjs CHANGED
@@ -58,9 +58,9 @@ function useRerender() {
58
58
  function createRoomContext(client) {
59
59
  let useClient$1;
60
60
  useClient$1 = "__legacy" !== client ? () => client : useClient;
61
- const RoomContext = React.createContext(null);
61
+ const RoomCtx = React.createContext(null);
62
62
  function useRoom() {
63
- const room = React.useContext(RoomContext);
63
+ const room = React.useContext(RoomCtx);
64
64
  if (null == room)
65
65
  throw new Error("RoomProvider is missing from the react tree");
66
66
  return room;
@@ -241,11 +241,7 @@ function createRoomContext(client) {
241
241
  ),
242
242
  [_client, roomId]
243
243
  ),
244
- React.createElement(
245
- RoomContext.Provider,
246
- { value: room },
247
- props.children
248
- )
244
+ React.createElement(RoomCtx.Provider, { value: room }, props.children)
249
245
  );
250
246
  },
251
247
  useBatch: function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/react",
3
- "version": "0.17.1",
3
+ "version": "0.17.4",
4
4
  "description": "A set of React hooks and providers to use Liveblocks declaratively.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "license": "Apache-2.0",
31
31
  "peerDependencies": {
32
- "@liveblocks/client": "0.17.1",
32
+ "@liveblocks/client": "0.17.4",
33
33
  "react": "^16.14.0 || ^17 || ^18"
34
34
  },
35
35
  "devDependencies": {