npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ui-chat

v0.1.1

Published

A simple chat UI for React

Downloads

7

Readme

React UI Chat

React UI Chat is a customizable chat component for React applications. It provides a simple and elegant interface for building chat features in your web projects.

1711210041623

Features

  • 📝 Easy-to-use chat interface.
  • 🎨 Customizable styling and theming.
  • 📦 Lightweight and dependency-free.
  • ⚙️ Extensible with plugins and hooks.

Installation

You can install react-ui-chat via npm:

npm install react-ui-chat

Usage

This is a basic example of how to show the Chat component:

import { Chat } from 'react-ui-chat'
import "react-ui-chat/tailwind.css" // if you are not using tailwind
import { TMessage } from 'react-ui-chat/types' // if you are using typescript

const initialMessages: TMessage[] = [
  {
    id: 1,
    message: 'I need help with my order',
    date: new Date().toISOString(),
    type: "sent",
  },
  {
    id: 2,
    message: 'Hello, how can I help you?',
    date: new Date().toISOString(),
    type: "receive",
  },
]

const Example1 = () => <Chat initialMessages={initialMessages}/>

export default Example1

Let's see a more advanced example with a simulated chat but updating the delivery status:

import { Chat } from 'react-ui-chat'
import "react-ui-chat/tailwind.css" // if you are not using tailwind
import { TMessage } from 'react-ui-chat/types' // if you are using typescript

import { useEffect, useState } from 'react'

type Conversation = {
  id: string | number,
  text: string,
  owner: string,
  date: string,
  status: TMessage['status']
}

// simulate a chat between two users, passing ALL messages
function Example3() {
  const [conversation, setConversation] = useState<Conversation[]>([])

  // simulate the message delivery status
  useEffect(() => {
    if (!conversation.length || conversation[conversation.length - 1].status === 'delivered') return;

    const time = setTimeout(() => {
      setConversation((prev) => {
        return prev.map((message) => {
          if (message.status === 'sending') {
            return {
              ...message,
              status: 'delivered'
            }
          }
          return message
        })
      })
    }, 1000)

    return () => clearTimeout(time)
  }, [conversation])

  // simulate the message sending
  const handleOnMessageSend = (message: TMessage, owner: string) => {
    setConversation([
      ...conversation,
      {
        id: conversation.length + 1,
        text: message.message,
        owner,
        date: message.date,
        status: 'sending',
      }
    ])
  }

  // reformat the conversation to be used in the Chat component
  const messagesByOwner: (owner: string) => TMessage[] = (owner) => 
    conversation.map((message, index) => ({
      id: index +1,
      message: message.text,
      date: message.date,
      type: message.owner === owner ? 'sent' : 'receive',
      status: message.status
    }))
  

  return (
    <div className="container">
      <Chat 
        messages={messagesByOwner('Albert')}
        onMessageSend={(message) => handleOnMessageSend(message, 'Albert')}
      />
      <Chat 
        messages={messagesByOwner('Nina')}
        onMessageSend={(message) => handleOnMessageSend(message, 'Nina')}
      />
    </div>
  )
}

export default Example3

It's not enough? You can also see a real example with a chat using a socket.io server here

Props

The Chat component accepts the following props:

  • initialMessages: An array of messages to display when the chat is first rendered.
  • messages: An array of messages to display in the chat. If this prop is provided, the chat will be controlled.
  • messageReceived: A message object that was received. This prop is used to update the chat only with new messages.
  • onMessageSend: A function that is called when the user sends a message. It receives the message object as an argument.

Types

The react-ui-chat package exports the following types:

  • TMessage: An object representing a chat message. It has the following main properties:
    • id: A unique identifier for the message.
    • message: The text content of the message.
    • date: The date and time when the message was sent.
    • type: The type of the message. It can be either "sent" or "receive".
    • status: The status of the message. It can be either "sending", "delivered", "read", or "error".

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.