0.0.11 • Published 5 years ago

@nodeflair/react-chat v0.0.11

Weekly downloads
37
License
MIT
Repository
github
Last release
5 years ago

React Chat

Build Status

React Chat is presently in alpha and is not meant to be a viable substitute to delicate native chat frameworks.

React Chat currently does not support pagination of messages or rooms, hence might have performance issues when loading huge volume of chat data. Handles for pagination such as scroll pagination will be added soon.

Intro

React Chat is an extensible set of presentational chat components for mobile-first ReactJS web applications. It facilitates simple messaging interactions on-the-go.

React Chat is inspired by the designs at GiftedChat, Slack, WhatsApp and Telegram.

Besides this README, you may find documentation of the design and behaviours of components in Storybook.

Examples and demos will be provided through Storybook and the Examples page in the future. This is a work in progress.

Getting Started

React Chat requires at least React 16.3.0 as a peer dependency.

To install using npm:

npm install @hliejun/react-chat

To install using yarn:

yarn add @hliejun/react-chat

To opt-in to alpha versions, please explicitly state the version. Check on the latest version here.

Usage

React Chat comprises of:

  • ChatList

  • ChatRoom

ChatList

ChatList is a component used for interacting with a list of chat rooms or message groups. As a presentational component, it exposes the handles for various tasks such as displaying chat rooms, refreshing the view, filtering the list and more.

For instance, you can invoke ChatList with the following:

import { ChatList } from '@hliejun/react-chat';

class MyContainer extends React.Component {

  // ...

  render = () => {
    //...
    const { rooms } = this.state;
    //...
    return (
      <ChatList
        onItem={this.segueToChatRoom}
        rooms={this.parseRooms(rooms)}
        title='Chats'
      />
    );
  };

  // ...

  parseRooms = (rooms) => {
    // Fetch and parse data into shape of room objects
  };

  segueToChatRoom = (id) => {
    // Navigate to chat room of id when room is selected
  };

}

The following table indicates the props that ChatList accepts.

NameTypeDescription
classNamestringStyle classes passed down from container component
hideAvatarboolFlag to opt out from showing Avatar in ListItem
hideChevronboolFlag to opt out from showing chevron indicator as affordance to segueing
hideTitleBarboolFlag to opt out from showing the TitleBar
isLoadingboolFlag to display loading animations while the ChatList is not readily populated
layoutsee AppContextStyling enumeration to match ChatRoom layout
liveSearchboolFlag to indicate the intended search behaviour
menuActionssee ActionsAction handlers and metadata to populate chat ListItem menu
onAvatarfuncHandler for interaction with the Avatar in a ListItem
onInfofuncHandler for interaction with the identity Avatar in TitleBar
onItemfuncHandler for selecting a ListItem of a specific room
onMenufuncHandler for prompting the ListItem menu of a specific room
onRefreshfuncHandler for initiating a fetch or reload to repopulate the ChatList
onResultfuncHandler for selecting a search ListItem in the search results pane
onSearchfuncHandler for search or filter logic to fetch or reload searchResults
PlaceholderReact elementComponent to fill the body when rooms is empty
roomssee RoomsCollection of objects specifying the metadata of a room
searchHintstringPlaceholder for empty search field
searchPlaceholderReact elementPlaceholder for empty search results body
searchResultssee SearchCollection of objects describing the outcome of onSearch
selectedIdstringUnique identifier of the list item that is currently active
sizingsee AppContextDevice and sizing enumeration for layout responsiveness
subtitlestringSubtitle to display on TitleBar
themesee AppContextTheme enumeration for styling constituent components
titlestringTitle to display on TitleBar
usersee UsersObject containing metadata of a user or a user profile

ChatRoom

ChatRoom is a component used for interacting with a chat group, room, conversation, etc. Regardless of the room size, it offers generic handles for various messaging tasks such as typing, sending, searching, deleting, copying and more.

You can use ChatRoom this way:

import { ChatRoom } from '@hliejun/react-chat';

class MyContainer extends React.Component {

  // ...

  render = () => {
    //...
    const { input, messages, room, userId, users } = this.state;
    const { id, name } = room;
    //...
    return (
      <ChatRoom
        inputValue={input}
        messages={this.parseMessages(messages)}
        onInput={this.parseInput}
        onSend={this.sendMessage}
        roomId={id}
        roomName={name}
        userId={userId}
        users={users}
      />
    );
  };

  // ...

  parseMessages = (messages) => {
    // Fetch and parse data into shape of message objects
  };

  parseInput = (nextInput) => {
    // Input control to parse HTML string into desirable values
    // ...
    // When done parsing, reload ChatRoom with new input state
    // Pure components will not reload if unaffected by input
    this.setState({
      input: parsedInput
    });
  };

  sendMessage = (input) => {
    // Call API to submit message
  };

}

The following table indicates the props that ChatRoom accepts.

NameTypeDescription
attachOptionssee MediaAction handlers for media attachments
classNamestringStyle classes passed down from container component
hideAvatarboolFlag to opt out from showing Avatar in Message item
inputDatasee MediaObject describing attached media due for posting
inputHintstringPlaceholder for input field
inputValuestringHTML-formatted string for rendering into input field
layoutsee AppContextStyling enumeration describing layout of messages
liveSearchboolFlag to indicate the intended search behaviour
menuActionssee ActionsAction handlers and metadata to populate chat Message menu
messagessee MessagesCollection of objects representing Message content
onAttachfuncHandler for attaching media to input
onAvatarfuncHandler for interaction with the Avatar in a Message
onContentfuncHandler for selecting a message Content
onInfofuncHandler for interaction with the identity Avatar in TitleBar
onInputfuncHandler for intercepting and updating input field value
onMenufuncHandler for prompting the content Menu of a specific message
onRefreshfuncHandler for initiating a fetch or reload to repopulate the Message list
onResultfuncHandler for selecting a search ListItem in the search results pane
onReturnfuncHandler for interacting with the return button to perform a segue
onSearchfuncHandler for search or filter logic to fetch or reload searchResults
onSendfuncHandler for submitting input value
roomAvatarstringLink to room avatar image
roomIdstringUnique identifier of the room for actions contextual to the room
roomNamestringName or title of the chat room
searchHintstringPlaceholder for empty search field
searchPlaceholderReact elementPlaceholder for empty search results body
searchResultssee SearchCollection of objects describing the outcome of onSearch
sizingsee AppContextDevice and sizing enumeration for layout responsiveness
subtitlestringSubtitle to display on TitleBar
themesee AppContextTheme enumeration for styling constituent components
userIdstringUnique identifier of the current user
userssee UsersIndexed collection of objects containing metadata of all users involved in the chat room

Actions

(WIP) Here are the PropTypes declaration for menu actions for the respective components.

{/* ListItem Menu Actions */}

menuActions: PropTypes.arrayOf(PropTypes.shape({
  action: PropTypes.func.isRequired,
  icon: PropTypes.string,
  label: PropTypes.string.isRequired,
  type: PropTypes.oneOf([
    'archive',
    'delete',
    'info',
    'pin',
    'star',
    'unread'
  ]).isRequired
}))

{/* Message Menu Actions */}

menuActions: PropTypes.arrayOf(PropTypes.shape({
  action: PropTypes.func.isRequired,
  icon: PropTypes.string,
  label: PropTypes.string.isRequired,
  type: PropTypes.oneOf([
    'copy',
    'delete',
    'forward',
    'info',
    'pin',
    'reply'
  ]).isRequired
})),

AppContext

(WIP) Here are the PropTypes declaration for app context options.

{/* Layout */}

layout: PropTypes.oneOf([
  'aligned',
  'staggered'
])

{/* Sizing */}

sizing: PropTypes.oneOf([
  'desktop',
  'mobile',
  'tablet'
])

{/* Theme */}

theme: PropTypes.oneOf([
  'dark',
  'light'
])

Messages

(WIP) Here are the PropTypes declaration for messages.

{/* Messages */}

messages: PropTypes.arrayOf(PropTypes.shape({
  data: PropTypes.shape({
    coordinates: PropTypes.shape({
      lat: PropTypes.string,
      lng: PropTypes.string
    }),
    galleryId: PropTypes.string,
    markdown: PropTypes.string,
    metadata: PropTypes.object,
    source: PropTypes.string.isRequired,
    type: PropTypes.oneOf([
      'audio',
      'file',
      'gif',
      'image',
      'link',
      'location',
      'markdown',
      'pdf',
      'video'
    ]).isRequired
  }),
  eventContent: PropTypes.element,
  eventName: PropTypes.string,
  isDelivered: PropTypes.bool,
  isLoading: PropTypes.bool,
  isRead: PropTypes.bool,
  messageId: PropTypes.string.isRequired,
  senderId: PropTypes.string,
  text: PropTypes.string,
  timeStamp: PropTypes.string.isRequired,
  type: PropTypes.oneOf([
    'event',
    'media',
    'system',
    'text'
  ]).isRequired
})).isRequired

Media

(WIP) Here are the PropTypes declaration for media-related objects.

{/* Attach Options */}

attachOptions: PropTypes.arrayOf(PropTypes.shape({
  action: PropTypes.func.isRequired,
  icon: PropTypes.string,
  label: PropTypes.string.isRequired,
  type: PropTypes.oneOf([
    'audio',
    'file',
    'gif',
    'image',
    'link',
    'location',
    'markdown',
    'pdf',
    'video'
  ]).isRequired
}))

{/* Input Data */}

inputData: PropTypes.shape({
  coordinates: PropTypes.shape({
    lat: PropTypes.string,
    lng: PropTypes.string
  }),
  galleryId: PropTypes.string,
  markdown: PropTypes.string,
  metadata: PropTypes.object,
  source: PropTypes.string.isRequired,
  type: PropTypes.oneOf([
    'audio',
    'file',
    'gif',
    'image',
    'link',
    'location',
    'markdown',
    'pdf',
    'video'
  ]).isRequired
})

Rooms

(WIP) Here are the PropTypes declaration for rooms.

{/* Rooms */}

rooms: PropTypes.arrayOf(PropTypes.shape({
  avatar: PropTypes.string,
  description: PropTypes.string,
  id: PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  status: PropTypes.oneOf([
    'archive',
    'pin',
    'star',
    'new'
  ]),
  subtitle: PropTypes.string,
  timeStamp: PropTypes.string
})).isRequired

Search

(WIP) Here are the PropTypes declaration for search results.

{/* Search Results */}

PropTypes.arrayOf(PropTypes.shape({
  avatar: PropTypes.string,
  description: PropTypes.string,
  id: PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  timeStamp: PropTypes.string
}))

Users

(WIP) Here are the PropTypes declaration for user(s).

{/* User */}

user: PropTypes.shape({
  avatar: PropTypes.string,
  description: PropTypes.string,
  email: PropTypes.string,
  id: PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  phone: PropTypes.string,
  website: PropTypes.string
})

{/* Users */}

users: PropTypes.objectOf(PropTypes.shape({
  avatar: PropTypes.string,
  description: PropTypes.string,
  email: PropTypes.string,
  id: PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  phone: PropTypes.string,
  website: PropTypes.string
})).isRequired

Style Classes

(WIP) Formats and variants off CSS classes.

Development

(WIP) Code Design, Linters, Storybook, RollUp, AppContext, Classnames. DOMPurify.

TODOs

Summary of upcoming TODOs:

Features

  • Animations
  • Children buttons in TitleBar
  • Dividers
  • Expose child components for use
  • Media preview
  • Message markdown
  • Message replies
  • Refresh
  • Scroll pagination
  • Themes and abstract colors usage

Performance

  • Recycle views
  • Reduce bundle size with UglifyJS
  • Split bundles for selective import
  • Switch to PureComponent(s)
  • Trim style classes

Code Quality

  • Add Jest tests
  • Refactor all stories
  • Switch to SCSS for stories

Documentation

  • Add example screenshots
  • Complete README
  • Complete Storybook
  • Examples with ChatKit

Maintenance

  • Setup issues

Credits and Licenses

This project is licensed under the MIT License. See LICENSE for copyright credits to the respective sources. The following people and resources have helped this project greatly in one way or another:

Throttling without Lodash Author: Jhey Tompkins Web: Throttling and Debouncing in Javascript

Default to Polymer SVG Icons Author: PolymerElements and David Francisco Repo (polymerelements): https://github.com/PolymerElements/iron-icons Repo (dmfrancisco): https://github.com/dmfrancisco/react-icons

Web URL Regex Author: Diego Perini Gist: https://gist.github.com/dperini/729294

SASS Mixins Authors: Richard Torres and Engage Interactive Repo (engageinteractive): https://github.com/engageinteractive/core Gist (richardtorres314): https://gist.github.com/richardtorres314/26b18e12958ba418bb37993fdcbfc1bd

SASS Material Colors Author: minusfive Repo: https://github.com/minusfive/sass-material-colors

Support

(WIP) Issues and contact.

0.0.11

5 years ago

0.0.9

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago