LoginSignup
32
20

More than 1 year has passed since last update.

【Reactでchat機能を実装するためのUIライブラリchat-ui-kit-reactが好みのUI過ぎるので使い方を解説する】

Last updated at Posted at 2021-08-05

まずはinstallして、雛形をコピペで貼り付けて、実際にどのようなUIなのか試してみるといい。

個人的に非常に好みです。
参照記事: https://reposhub.com/react/ui-frameworks/chatscope-chat-ui-kit-react.html

【雛形】

npm install @chatscope/chat-ui-kit-react
npm install @chatscope/chat-ui-kit-styles
import styles from "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css";
import {
  MainContainer,
  ChatContainer,
  MessageList,
  Message,
  MessageInput,
    Avatar
} from "@chatscope/chat-ui-kit-react";

<div style={{ position: "relative", height: "500px" }}>
  <MainContainer>
    <ChatContainer>
      <MessageList>
        <Message
          model={{
            message: "Hello my friend",
            sentTime: "just now",
            sender: "Joe",
            position: "normal",
            direction: "incoming"
          }}
        />
      </MessageList>
      <MessageInput placeholder="Type message here" />
    </ChatContainer>
  </MainContainer>
</div>;

以下のようなUIが表示される。
50120F91-9558-4EC6-9C35-A917E43509CD.jpeg
一つ一つ解説していく。

【Formについて】

・画像の下側の「ファイル参照ボタン・入力フォーム・送信ボタン」これらは
<MessageInput placeholder="Type message here" />というComponentにまとめられている。
D17D63EC-AEB7-4655-8E23-5A655E20B8E1_4_5005_c.jpeg

・このFormをカスタマイズするには以下のようにすればいい。
attachButton={false}で左下のファイル参照ボタンが消える。
sendButton={false}で送信ボタンを消すことができる。

<MessageInput placeholder="Type message here" attachButton={false} sendButton={false}/>

744551E9-6495-4B4A-9A00-C20B8949939F_4_5005_c.jpeg

【Messageについて】

・Hello my frendと表示されているMessageは、以下の部分で構成されている。

<Message
     model={{
       message: "Hello my friend",
       sentTime: "just now",
       sender: "Joe",
    position: "normal",
    direction: "incoming"
     }}
 />

つまり、以下のように単純に3つ置くだけでMessageは3つ表示される。

<div style={{ position: "relative", height: "500px" }}>
                <MainContainer>
                    <ChatContainer>
                    <MessageList>
                        <Message
                        model={{
                            message: "Hello my friend",
                            sentTime: "just now",
                            sender: "Joe",
                            position: "normal",
                            direction: "incoming"
                        }}
                        />
                        <Message
                        model={{
                            message: "Hello my friend",
                            sentTime: "just now",
                            sender: "Joe",
                            position: "normal",      
                            direction: "incoming"
                        }}
                        />
                        <Message
                        model={{
                            message: "Hello my friend",
                            sentTime: "just now",
                            sender: "Joe",
                            position: "normal",
                            direction: "incoming"
                        }}
                        />
                    </MessageList>
                    <MessageInput placeholder="Type message here"/>
                    </ChatContainer>
                </MainContainer>
            </div>

EE37A38D-3641-4347-8550-9558368D131A.jpeg
「positionオプション」は[ first, normal, last ]などがある。
firstとnormalはmessageの型枠は同じだがfirstのみひとつ上のmessageと少し間を開けてくれる。
lastは他2つと少し形を変えてlast Messageであることを視覚化してくれる。
以下の画像は、上から順にnormal, first, lastのpositionを設定したものである。
70EA24B2-78C4-4C98-86C4-759F26355E7B_4_5005_c.jpeg

・「directionオプション」はMessageの表示位置を相手か自分かに設定できるものだ。
direction: "incoming"で左に表示。
direction: "outgoing"で右に表示。
8B1868E9-17A9-4EDD-9177-0BA515B5FCB9.jpeg

【Avatarについて】

このライブラリではAvatar、つまりユーザーアイコンを表示するのもサポートしてくれる。
例えば以下のようなUiをつくることができる。
5A276F7A-929F-4809-81C2-178A27BBD00D.jpeg

上記の左側はいかのようなCodeになっている。(import時にAvatarComponentを使えるようにしておかないとAvatarがないというエラーが出る)

import logo from './logo192.png';
<Message
      model={{
           message: "Hello my friend",
           sentTime: "just now",
           sender: "Joe",
           position: "first"                       
           direction: "incoming"  
       }}>
      <Avatar src={logo} name="React-logo"/>
</Message>
 <Message
      model={
           message: "Hello my friend",
           sentTime: "just now",
           sender: "Joe",
           position: "first",
           direction: "incoming"
      }}>
      <Avatar src={logo} name="React-logo"/>
</Message>
<Message
      model={{
           message: "Hello my friend",
           sentTime: "just now",
           sender: "Joe",
           position: "first",
           direction: "incoming"
      }}>
      <Avatar src={logo} name="React-logo"/>
</Message>

また、Avatar用のspaceを空けてくれるoptionもあります。

<Message model={{
            message: "Hello my friend",
            sentTime: "just now",
            sender: "Akane",
            direction: "incoming",
            position: "last"
 }} avatarSpacer={true} />

avatarSpacer={true} とすると以下のようなAvatarスペースの空いたMessageが追加されます。
501B505D-C824-40AE-BE8A-AC63A3BAA87E.jpeg

以上。
さらに詳しくはDocumentを参照してください。ほかにもいろいろできそうなので。
https://chatscope.io/storybook/react/?path=/docs/documentation-introduction--page

32
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
20