react-peer-chat 0.2.1 → 0.2.3

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/build/icons.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import React from "react";
2
- import './icons.css';
3
2
  export declare function BiSolidMessageDetail(props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>): React.JSX.Element;
4
3
  export declare function BiSolidMessageX(props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>): React.JSX.Element;
5
4
  export declare function GrSend(props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>): React.JSX.Element;
package/build/icons.js CHANGED
@@ -1,28 +1,27 @@
1
1
  import React from "react";
2
- import './icons.css';
3
2
  export function BiSolidMessageDetail(props) {
4
- return React.createElement("span", Object.assign({}, props),
5
- React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'scale-down' },
3
+ return React.createElement("span", { ...props },
4
+ React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down' },
6
5
  React.createElement("path", { d: "M20 2H4c-1.103 0-2 .894-2 1.992v12.016C2 17.106 2.897 18 4 18h3v4l6.351-4H20c1.103 0 2-.894 2-1.992V3.992A1.998 1.998 0 0 0 20 2zm-6 11H7v-2h7v2zm3-4H7V7h10v2z" })));
7
6
  }
8
7
  export function BiSolidMessageX(props) {
9
- return React.createElement("span", Object.assign({}, props),
10
- React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'scale-down' },
8
+ return React.createElement("span", { ...props },
9
+ React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down' },
11
10
  React.createElement("path", { d: "M20 2H4c-1.103 0-2 .894-2 1.992v12.016C2 17.106 2.897 18 4 18h3v4l6.351-4H20c1.103 0 2-.894 2-1.992V3.992A1.998 1.998 0 0 0 20 2zm-3.293 11.293-1.414 1.414L12 11.414l-3.293 3.293-1.414-1.414L10.586 10 7.293 6.707l1.414-1.414L12 8.586l3.293-3.293 1.414 1.414L13.414 10l3.293 3.293z" })));
12
11
  }
13
12
  export function GrSend(props) {
14
- return React.createElement("span", Object.assign({}, props),
15
- React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'scale-down invert' },
13
+ return React.createElement("span", { ...props },
14
+ React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down rpc-invert' },
16
15
  React.createElement("path", { fill: "none", stroke: "#000", strokeWidth: 2, d: "M22,3 L2,11 L20.5,19 L22,3 Z M10,20.5 L13,16 M15.5,9.5 L9,14 L9.85884537,20.0119176 C9.93680292,20.5576204 10.0751625,20.5490248 10.1651297,20.009222 L11,15 L15.5,9.5 Z" })));
17
16
  }
18
17
  export function BsFillMicFill(props) {
19
- return React.createElement("span", Object.assign({}, props),
18
+ return React.createElement("span", { ...props },
20
19
  React.createElement("svg", { fill: "currentColor", width: '1rem', height: '1rem' },
21
20
  React.createElement("path", { d: "M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z" }),
22
21
  React.createElement("path", { d: "M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5z" })));
23
22
  }
24
23
  export function BsFillMicMuteFill(props) {
25
- return React.createElement("span", Object.assign({}, props),
24
+ return React.createElement("span", { ...props },
26
25
  React.createElement("svg", { fill: "currentColor", width: '1rem', height: '1rem' },
27
26
  React.createElement("path", { d: "M13 8c0 .564-.094 1.107-.266 1.613l-.814-.814A4.02 4.02 0 0 0 12 8V7a.5.5 0 0 1 1 0v1zm-5 4c.818 0 1.578-.245 2.212-.667l.718.719a4.973 4.973 0 0 1-2.43.923V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 1 0v1a4 4 0 0 0 4 4zm3-9v4.879L5.158 2.037A3.001 3.001 0 0 1 11 3z" }),
28
27
  React.createElement("path", { d: "M9.486 10.607 5 6.12V8a3 3 0 0 0 4.486 2.607zm-7.84-9.253 12 12 .708-.708-12-12-.708.708z" })));
package/build/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import React, { CSSProperties, DetailedHTMLProps, HTMLAttributes, ReactNode, RefObject } from 'react';
2
2
  import { PeerOptions } from 'peerjs';
3
- import './index.css';
4
3
  type Message = {
5
4
  id: string;
6
5
  text: string;
package/build/index.js CHANGED
@@ -1,29 +1,7 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __rest = (this && this.__rest) || function (s, e) {
11
- var t = {};
12
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
- t[p] = s[p];
14
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
- t[p[i]] = s[p[i]];
18
- }
19
- return t;
20
- };
21
1
  import React, { useEffect, useRef, useState } from 'react';
22
2
  import useStorage, { removeStorage } from './storage';
23
3
  import { BiSolidMessageDetail, BiSolidMessageX, BsFillMicFill, BsFillMicMuteFill, GrSend } from './icons';
24
- import './index.css';
25
- export default function Chat(_a) {
26
- var { name, peerId, remotePeerId, peerOptions, text = true, voice = true, dialogOptions, onError = () => console.error("Can not access microphone!"), children } = _a, props = __rest(_a, ["name", "peerId", "remotePeerId", "peerOptions", "text", "voice", "dialogOptions", "onError", "children"]);
4
+ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = true, voice = true, dialogOptions, onError = () => console.error("Can not access microphone!"), children, ...props }) {
27
5
  const [peer, setPeer] = useState();
28
6
  const [opponentName, setOpponentName] = useState();
29
7
  const [notification, setNotification] = useState(false);
@@ -39,11 +17,10 @@ export default function Chat(_a) {
39
17
  remotePeerId = `rpc-${remotePeerId}`;
40
18
  const handleRemoteStream = (remoteStream) => streamRef.current.srcObject = remoteStream;
41
19
  function addMessage(message, send = false) {
42
- var _a, _b;
43
20
  setMessages(old => old.concat(message));
44
21
  if (send)
45
- (_a = connRef.current) === null || _a === void 0 ? void 0 : _a.send({ type: 'message', message });
46
- else if (!((_b = dialogRef.current) === null || _b === void 0 ? void 0 : _b.open))
22
+ connRef.current?.send({ type: 'message', message });
23
+ else if (!dialogRef.current?.open)
47
24
  setNotification(true);
48
25
  }
49
26
  function handleConnection(conn, setName = false) {
@@ -70,12 +47,10 @@ export default function Chat(_a) {
70
47
  setPeer(undefined);
71
48
  return;
72
49
  }
73
- (function loadPeer() {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const Peer = (yield import('peerjs')).default;
76
- const peer = new Peer(`rpc-${peerId}`, peerOptions);
77
- setPeer(peer);
78
- });
50
+ (async function loadPeer() {
51
+ const Peer = (await import('peerjs')).default;
52
+ const peer = new Peer(`rpc-${peerId}`, peerOptions);
53
+ setPeer(peer);
79
54
  })();
80
55
  }, [audio]);
81
56
  useEffect(() => {
@@ -110,58 +85,55 @@ export default function Chat(_a) {
110
85
  });
111
86
  peer.on('connection', handleConnection);
112
87
  return () => {
113
- var _a, _b, _c;
114
- (_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(track => track.stop());
115
- (_b = connRef.current) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
116
- (_c = connRef.current) === null || _c === void 0 ? void 0 : _c.close();
117
- call === null || call === void 0 ? void 0 : call.removeAllListeners();
118
- call === null || call === void 0 ? void 0 : call.close();
88
+ localStream.current?.getTracks().forEach(track => track.stop());
89
+ connRef.current?.removeAllListeners();
90
+ connRef.current?.close();
91
+ call?.removeAllListeners();
92
+ call?.close();
119
93
  peer.removeAllListeners();
120
94
  peer.destroy();
121
95
  };
122
96
  }, [peer]);
123
97
  useEffect(() => {
124
- var _a, _b;
125
98
  if (dialog)
126
- (_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.show();
99
+ dialogRef.current?.show();
127
100
  else
128
- (_b = dialogRef.current) === null || _b === void 0 ? void 0 : _b.close();
101
+ dialogRef.current?.close();
129
102
  }, [dialog]);
130
103
  useEffect(() => {
131
104
  const container = containerRef.current;
132
105
  if (container)
133
106
  container.scrollTop = container.scrollHeight;
134
107
  }, [dialog, opponentName, messages]);
135
- return React.createElement("div", Object.assign({ className: 'main' }, props),
108
+ return React.createElement("div", { className: 'rpc-main', ...props },
136
109
  typeof children === 'function' ? children({ notification, messages, addMessage, dialogRef, audio, setAudio }) : React.createElement(React.Fragment, null,
137
110
  text && React.createElement("div", null,
138
111
  dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) })
139
- : React.createElement("div", { className: 'notification' },
112
+ : React.createElement("div", { className: 'rpc-notification' },
140
113
  React.createElement(BiSolidMessageDetail, { onClick: () => {
141
114
  setNotification(false);
142
115
  setDialog(true);
143
116
  } }),
144
- notification && React.createElement("span", { className: 'badge' })),
145
- React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'dialog' : ''} position-${(dialogOptions === null || dialogOptions === void 0 ? void 0 : dialogOptions.position) || 'center'}`, style: dialogOptions === null || dialogOptions === void 0 ? void 0 : dialogOptions.style },
146
- React.createElement("div", { className: 'heading' }, "Chat"),
117
+ notification && React.createElement("span", { className: 'rpc-badge' })),
118
+ React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'dialog' : ''} position-${dialogOptions?.position || 'center'}`, style: dialogOptions?.style },
119
+ React.createElement("div", { className: 'rpc-heading' }, "Chat"),
147
120
  React.createElement("hr", null),
148
121
  React.createElement("div", null,
149
- React.createElement("div", { ref: containerRef, className: 'message-container' }, opponentName && messages.map(({ id, text }, i) => React.createElement("div", { key: i },
122
+ React.createElement("div", { ref: containerRef, className: 'rpc-message-container' }, opponentName && messages.map(({ id, text }, i) => React.createElement("div", { key: i },
150
123
  React.createElement("strong", null,
151
124
  id === peerId ? 'You' : opponentName,
152
125
  ": "),
153
126
  React.createElement("span", null, text)))),
154
127
  React.createElement("hr", null),
155
- React.createElement("form", { className: 'input-container', onSubmit: e => {
156
- var _a;
128
+ React.createElement("form", { className: 'rpc-input-container', onSubmit: e => {
157
129
  e.preventDefault();
158
- const text = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.value;
130
+ const text = inputRef.current?.value;
159
131
  if (text) {
160
132
  inputRef.current.value = '';
161
133
  addMessage({ id: peerId, text }, true);
162
134
  }
163
135
  } },
164
- React.createElement("input", { ref: inputRef, className: 'input', placeholder: 'Enter a message' }),
136
+ React.createElement("input", { ref: inputRef, className: 'rpc-input', placeholder: 'Enter a message' }),
165
137
  React.createElement("button", { type: 'submit' },
166
138
  React.createElement(GrSend, null)))))),
167
139
  voice && React.createElement("div", null, audio ? React.createElement(BsFillMicFill, { title: "Turn mic off", onClick: () => setAudio(false) }) : React.createElement(BsFillMicMuteFill, { title: "Turn mic on", onClick: () => setAudio(true) }))),
package/build/storage.js CHANGED
@@ -10,7 +10,7 @@ const getStorage = (key, fallbackValue, local = false) => {
10
10
  throw new Error("Value doesn't exist");
11
11
  value = JSON.parse(value);
12
12
  }
13
- catch (_a) {
13
+ catch {
14
14
  if (fallbackValue !== undefined) {
15
15
  value = fallbackValue;
16
16
  setStorage(key, value, local);
@@ -23,7 +23,7 @@ const getStorage = (key, fallbackValue, local = false) => {
23
23
  return value;
24
24
  };
25
25
  export default function useStorage(key, initialValue, { local = false, save = false } = {}) {
26
- save || (save = getStorage('mode') !== 'online');
26
+ save ||= getStorage('mode') !== 'online';
27
27
  const [storedValue, setStoredValue] = useState(save ? getStorage(key, initialValue, local) : initialValue);
28
28
  const setValue = (value) => {
29
29
  setStoredValue((old) => {
@@ -1,14 +1,14 @@
1
- .main {
1
+ .rpc-main {
2
2
  display: flex;
3
3
  align-items: center;
4
4
  column-gap: 0.5rem;
5
5
  }
6
6
 
7
- .notification {
7
+ .rpc-notification {
8
8
  position: relative;
9
9
  }
10
10
 
11
- .notification .badge {
11
+ .rpc-notification .rpc-badge {
12
12
  background-color: red;
13
13
  position: absolute;
14
14
  top: 0.25rem;
@@ -18,7 +18,7 @@
18
18
  border-radius: 100%;
19
19
  }
20
20
 
21
- .dialog {
21
+ .rpc-dialog {
22
22
  display: flex;
23
23
  flex-direction: column;
24
24
  row-gap: 0.25rem;
@@ -30,52 +30,52 @@
30
30
  font-size: smaller;
31
31
  }
32
32
 
33
- .position-left {
33
+ .rpc-position-left {
34
34
  left: 0.5rem;
35
35
  translate: -100%;
36
36
  }
37
37
 
38
- .position-center {
38
+ .rpc-position-center {
39
39
  translate: -50%;
40
40
  }
41
41
 
42
- .position-right {
42
+ .rpc-position-right {
43
43
  left: 0.5rem;
44
44
  }
45
45
 
46
- .message-container {
46
+ .rpc-message-container {
47
47
  height: 6rem;
48
48
  overflow-y: scroll;
49
49
  padding-inline: 0.5rem;
50
50
  margin-bottom: 0.25rem;
51
51
  }
52
52
 
53
- .message-container::-webkit-scrollbar {
53
+ .rpc-message-container::-webkit-scrollbar {
54
54
  width: 2.5px;
55
55
  }
56
56
 
57
- .message-container::-webkit-scrollbar-track {
57
+ .rpc-message-container::-webkit-scrollbar-track {
58
58
  background: gray;
59
59
  }
60
60
 
61
- .message-container::-webkit-scrollbar-thumb {
61
+ .rpc-message-container::-webkit-scrollbar-thumb {
62
62
  background-color: white;
63
63
  }
64
64
 
65
- .heading {
65
+ .rpc-heading {
66
66
  text-align: center;
67
67
  font-weight: bold;
68
68
  padding-inline: 0.5rem;
69
69
  }
70
70
 
71
- .input-container {
71
+ .rpc-input-container {
72
72
  display: flex;
73
73
  column-gap: 0.25rem;
74
74
  padding-inline: 0.5rem;
75
75
  padding-block: 0.4rem;
76
76
  }
77
77
 
78
- .input {
78
+ .rpc-input {
79
79
  background-color: black;
80
80
  border: none;
81
81
  outline: 1px solid rgba(255, 255, 255, 0.8);
@@ -83,6 +83,14 @@
83
83
  padding-inline: 0.25rem;
84
84
  }
85
85
 
86
- .input:focus {
86
+ .rpc-input:focus {
87
87
  outline: 2px solid white;
88
+ }
89
+
90
+ .rpc-scale-down {
91
+ scale: calc(2/3);
92
+ }
93
+
94
+ .rpc-invert {
95
+ filter: invert(100%);
88
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "An easy to use react component for impleting peer-to-peer chatting.",
5
5
  "main": "./build/index.js",
6
6
  "type": "module",
package/build/icons.css DELETED
@@ -1,11 +0,0 @@
1
- * {
2
- -webkit-tap-highlight-color: transparent;
3
- }
4
-
5
- .scale-down {
6
- scale: calc(2/3);
7
- }
8
-
9
- .invert {
10
- filter: invert(100%);
11
- }