From 30953969889241c8abebde5da617474efe3b9b12 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Thu, 14 Oct 2021 13:11:09 +0200 Subject: [PATCH] Add floating example (#70) --- packages/react-chat-example/package.json | 3 +- packages/react-chat-example/src/index.tsx | 96 +++++++++++++++++-- packages/react-chat/src/components/Chat.tsx | 2 +- .../react-chat/src/components/ReactChat.tsx | 10 +- yarn.lock | 1 + 5 files changed, 102 insertions(+), 10 deletions(-) diff --git a/packages/react-chat-example/package.json b/packages/react-chat-example/package.json index 1a30bb6..88ac117 100644 --- a/packages/react-chat-example/package.json +++ b/packages/react-chat-example/package.json @@ -26,7 +26,8 @@ "react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^5.2.0", - "stream-browserify": "^3.0.0" + "stream-browserify": "^3.0.0", + "styled-components": "^5.3.1" }, "devDependencies": { "@testing-library/react-hooks": "^7.0.1", diff --git a/packages/react-chat-example/src/index.tsx b/packages/react-chat-example/src/index.tsx index 455f24e..5792914 100644 --- a/packages/react-chat-example/src/index.tsx +++ b/packages/react-chat-example/src/index.tsx @@ -1,6 +1,7 @@ import { community, lightTheme, ReactChat } from "@dappconnect/react-chat"; -import React from "react"; +import React, { useRef, useState } from "react"; import ReactDOM from "react-dom"; +import styled from "styled-components"; const fetchMetadata = async (link: string) => { const response = await fetch("https://localhost:3000", { @@ -21,13 +22,96 @@ const fetchMetadata = async (link: string) => { } }; +function DragDiv() { + console.log(); + const [x, setX] = useState(0); + const [y, setY] = useState(0); + const [width, setWidth] = useState(window.innerWidth - 50); + const [height, setHeight] = useState(window.innerHeight - 50); + const [showChat, setShowChat] = useState(true); + const ref = useRef(null); + const moved = useRef(false); + const setting = useRef(""); + + const onMouseMove = (e: MouseEvent) => { + if (setting.current === "position") { + e.preventDefault(); + setX(e.x - 20); + setY(e.y - 20); + } + if (setting.current === "size") { + setWidth(e.x - x); + setHeight(e.y - y); + e.preventDefault(); + } + moved.current = true; + }; + + const onMouseUp = () => { + document.removeEventListener("mousemove", onMouseMove); + document.removeEventListener("mouseup", onMouseUp); + if (!moved.current) [setShowChat((prev) => !prev)]; + moved.current = false; + }; + + return ( + + { + setting.current = "position"; + document.addEventListener("mousemove", onMouseMove); + document.addEventListener("mouseup", onMouseUp); + }} + /> + {showChat && ( + <> +
+ +
+ { + setting.current = "size"; + document.addEventListener("mousemove", onMouseMove); + document.addEventListener("mouseup", onMouseUp); + }} + > + + )} +
+ ); +} + +const SizeSet = styled.div` + margin-left: auto; + margin-right: 0px; + width: 10px; + height: 10px; + background-color: light-grey; + border: 1px solid; +`; + +const Bubble = styled.div` + width: 50px; + height: 50px; + border-radius: 50%; + background-color: lightblue; + border: 1px solid; +`; + +const Drag = styled.div` + position: absolute; + overflow: hide; +`; + ReactDOM.render(
- +
, document.getElementById("root") ); diff --git a/packages/react-chat/src/components/Chat.tsx b/packages/react-chat/src/components/Chat.tsx index fe9fd7e..c180303 100644 --- a/packages/react-chat/src/components/Chat.tsx +++ b/packages/react-chat/src/components/Chat.tsx @@ -89,6 +89,6 @@ export function Chat({ theme, community, fetchMetadata }: ChatProps) { const ChatWrapper = styled.div` width: 100%; - height: 100vh; + height: 100%; display: flex; `; diff --git a/packages/react-chat/src/components/ReactChat.tsx b/packages/react-chat/src/components/ReactChat.tsx index 7cc7ff8..54fa272 100644 --- a/packages/react-chat/src/components/ReactChat.tsx +++ b/packages/react-chat/src/components/ReactChat.tsx @@ -1,5 +1,6 @@ import React, { useRef } from "react"; import { ThemeProvider } from "styled-components"; +import styled from "styled-components"; import { NarrowProvider } from "../contexts/narrowProvider"; import { CommunityData } from "../helpers/communityMock"; @@ -20,15 +21,20 @@ export function ReactChat({ theme, community, fetchMetadata }: ReactChatProps) { return ( -
+ -
+
); } + +const Wrapper = styled.div` + height: 100%; + overflow: hidden; +`; diff --git a/yarn.lock b/yarn.lock index aeb9342..9e2ff6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -258,6 +258,7 @@ __metadata: source-map-loader: ^3.0.0 stream-browserify: ^3.0.0 style-loader: ^3.3.0 + styled-components: ^5.3.1 ts-loader: ^9.2.5 ts-node: ^10.1.0 typescript: ^4.3.5