Skip to content

Commit d8878f5

Browse files
authored
Live cursors Javascript example, users cursor not rendering.
2 parents d0bbd2c + 03757c0 commit d8878f5

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

examples/spaces-live-cursors/javascript/src/script.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,23 @@ async function connect() {
4040
const space = await spaces.get(spaceName);
4141
const parentRef = document.getElementById('live-cursors');
4242

43-
if (!parentRef || !(parentRef instanceof HTMLDivElement)) return;
43+
if (!parentRef || !(parentRef instanceof HTMLDivElement)) {
44+
return;
45+
}
4446

45-
await space.enter({
46-
name: mockNames[Math.floor(Math.random() * mockNames.length)],
47-
userColors: { cursorColor: colors[Math.floor(Math.random() * colors.length)] },
48-
});
47+
parentRef.style.cursor = 'default';
4948

5049
space.cursors.subscribe('update', async (cursorUpdate) => {
5150
const members = await space.members.getAll();
5251
const member = members.find((member) => member.connectionId === cursorUpdate.connectionId);
5352

54-
if (!member) return;
53+
if (!member) {
54+
return;
55+
}
56+
57+
if (client.connection.id === cursorUpdate.connectionId) {
58+
return;
59+
}
5560

5661
if (cursorUpdate.data?.['state'] === 'leave') {
5762
document.getElementById(`member-cursor-${member.connectionId}`)?.remove();
@@ -83,6 +88,14 @@ async function connect() {
8388
parentRef.appendChild(memberCursorContainer);
8489
});
8590

91+
const mockName = mockNames[Math.floor(Math.random() * mockNames.length)];
92+
const userColor = { cursorColor: colors[Math.floor(Math.random() * colors.length)] };
93+
94+
await space.enter({
95+
name: mockName,
96+
userColors: userColor,
97+
});
98+
8699
const updateCursorPosition = async (position: CursorPosition) => {
87100
await space.cursors.set({ position: { x: position.left, y: position.top } });
88101
};

examples/spaces-live-cursors/react/src/components/LiveCursors.tsx

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type Member = Omit<SpaceMember, "profileData"> & {
88
profileData: { userColors: { cursorColor: string; }; name: string };
99
};
1010

11-
// 💡 This component is used to render the cursor of the user
11+
// 💡 This component is used to update the location of the user's cursor
1212
const YourCursor = ({
1313
self,
1414
parentRef,
@@ -24,28 +24,7 @@ const YourCursor = ({
2424

2525
useTrackCursor(setCursorPosition, parentRef);
2626

27-
if (!self) return null;
28-
if (!cursorPosition || cursorPosition.state === "leave") return null;
29-
30-
const { cursorColor } = self.profileData.userColors;
31-
32-
return (
33-
<div
34-
className="uk-position-absolute uk-animation-fade pointer-events-none"
35-
style={{
36-
top: `${cursorPosition?.top || 0}px`,
37-
left: `${cursorPosition?.left || 0}px`,
38-
}}
39-
>
40-
<CursorSvg cursorColor={cursorColor} />
41-
<div
42-
style={{ backgroundColor: cursorColor }}
43-
className="uk-badge uk-position-relative text-white transform translate-x-4 -translate-y-1 px-3 py-2"
44-
>
45-
You
46-
</div>
47-
</div>
48-
);
27+
return null;
4928
};
5029

5130
// 💡 This component is used to render the cursors of other users in the space

0 commit comments

Comments
 (0)