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-chatbot-tailwind

v1.0.2

Published

**react-chatbot-tailwind** is a React component for integrating a customizable chatbot into your web applications. It supports modal and non-modal configurations, message threading, and dynamic message handling.

Downloads

10

Readme

react-chatbot-tailwind

react-chatbot-tailwind is a React component for integrating a customizable chatbot into your web applications. It supports modal and non-modal configurations, message threading, and dynamic message handling.

Installation

You can install the package via npm or yarn:

npm install react-chatbot-tailwind
# or
yarn add react-chatbot-tailwind
# or
pnpm install react-chatbot-tailwind

Import the package where you want to use it:

import { Chatbot } from 'react-chatbot-tailwind';

IMPORTANT

Then import the CSS file in your project:

import "react-chatbot-tailwind/dist/style.css";

Usage

Basic Example

Here's a basic example of how to use the Chatbot component:

import React from 'react';
import { Chatbot } from 'react-chatbot-tailwind';
import "react-chatbot-tailwind/dist/style.css"; //IMPORTANT

const App = () => {
  return (
    <div>
      <h1>Welcome to My Website</h1>
      <Chatbot modal={true} />
    </div>
  );
};

export default App;

Usage with Streaming API

// pages/index.tsx
"use client";
import { useState } from "react";
import Chatbot from "react-chatbot-tailwind";
import "react-chatbot-tailwind/dist/style.css";

type MessageType = {
  source: "bot" | "user";
  text: string;
};

export default function Home() {
  const [messages, setMessages] = useState<MessageType[]>([
    { text: "Hello, how can I assist you today?", source: "bot" },
  ]);
  const [loading, setLoading] = useState<boolean>(false);
  const [eventSource, setEventSource] = useState<EventSource | null>(null);

  const getResponse = () => {
    const eventSource = new EventSource("/api/chat");
    setEventSource(eventSource);
    setLoading(true);
    let currentMessage = "";
    eventSource.onmessage = (event) => {
      const parseData = event.data.replace("data: ", "");
      const message = parseData.replace("\n\n", "");
      currentMessage += message;
      setMessages((prev) => {
        const updatedMessages = [...prev];
        updatedMessages[updatedMessages.length - 1] = {
          text: currentMessage,
          source: "bot",
        };
        return updatedMessages;
      });

      if (currentMessage.includes("###STOP###")) {
        setLoading(false);
        eventSource.close();
      }
    };

    eventSource.onerror = () => {
      setLoading(false);
      eventSource.close();
    };

    return () => {
      setLoading(false);
    };
  };

  const handleStopStream = () => {
    eventSource && eventSource.close();
    setLoading(false);
  };

  return (
    <main className="flex flex-col items-center justify-between w-full">
      <div className="grow w-full">
        <Chatbot
          modal
          title="Chatbot"
          messages={messages}
          setMessages={setMessages}
          loading={loading}
          setLoading={setLoading}
          botMessageClassName="bg-red-600"
          userMessageClassName="bg-blue-600 text-white"
          botMessageNotchClassName="border-red-600"
          userMessageNotchClassName="border-blue-600"
          getResponse={getResponse}
          handleStopStream={handleStopStream}
        />
      </div>
    </main>
  );
}

API

Props

  • title: Title of the chatbot.
  • modal (optional): Boolean to toggle modal view.
  • messages: Array of message objects containing source and text.
  • setMessages: State setter function for messages.
  • loading: Boolean indicating if the bot is processing a message.
  • setLoading: State setter function for loading state.
  • className (optional): Additional class names for the component.
  • botMessageClassName (optional): Class names for bot messages.
  • userMessageClassName (optional): Class names for user messages.
  • botMessageNotchClassName (optional): Class names for bot message notch.
  • userMessageNotchClassName (optional): Class names for user message notch.
  • getResponse: Function to initiate fetching a response from the bot.
  • handleStopStream: Function to stop the bot response stream.

Styling

The appearance of the chatbot can be customized using Tailwind CSS classes. Feel free to adjust the styles to match your application's design.

Features

  • Modal and Non-Modal Modes: Choose between modal and non-modal configurations based on your UI/UX preferences.
  • Dynamic Messaging: Handles dynamic messages from users and provides responses using server-side events.
  • Scrolling Behavior: Automatically scrolls to the latest messages for a seamless chat experience.

License

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

Example

To see a working example, refer to the provided code snippets in the Usage section. Customize the Chatbot component with different styles, messages, and interaction logic as per your requirements.