# InstinctHub React UI

A comprehensive React component library with 130+ components for building modern web applications.

## 🚀 Quick Start

### Prerequisites

- **Node.js** (Recommended version: >= 16.x)
- **npm** (Comes with Node.js) or **yarn**

## 🤖 AI-Powered Component Discovery

**NEW**: InstinctHub React UI now includes AI-powered component discovery through Model Context Protocol (MCP) integration!

### 🌐 For Claude.ai Users (Web)
1. Go to [Claude.ai](https://claude.ai) → Settings → Integrations
2. Add integration: `InstinctHub React UI`
3. URL: `https://ui.instincthub.com/api/mcp`
4. Start asking: *"Find form components"*, *"Generate a login form"*, *"Help me integrate components"*

### 💻 For Claude Code CLI Users
The project includes a local MCP server for full CLI integration:
```bash
# MCP tools are automatically available:
# - mcp__search_components
# - mcp__get_component_docs  
# - mcp__recommend_components
# - mcp__generate_code
# - mcp__integration_help
```

**[→ Full MCP Integration Guide](./src/__examples__/src/app/api/README.md)**

## Installation

To use the `@instincthub/react-ui` package, another project needs to install the packages listed in the `peerDependencies` section of its `package.json`.

### Step 1: Install Peer Dependencies

`@instincthub/react-ui` has several peer dependencies that need to be installed separately:

```sh
npm install @emotion/react@^11.14.0 @emotion/styled@^11.14.0 @mui/icons-material@^7.0.0 @mui/material@^7.0.0 @reduxjs/toolkit@^2.6.1 @types/redux-logger@^3.0.13 jspdf@^3.0.1 next@^15.2.1 next-auth@beta primereact@^10.9.3 react@^19.0.0 react-dom@^19.0.0 react-redux@^9.2.0 redux-logger@^3.0.6 react-syntax-highlighter@^15.6.1 styled-components@^6.1.16
```

Or using yarn:

```sh
yarn add @emotion/react@^11.14.0 @emotion/styled@^11.14.0 @mui/icons-material@^7.0.0 @mui/material@^7.0.0 @reduxjs/toolkit@^2.6.1 @types/redux-logger@^3.0.13 jspdf@^3.0.1 next@^15.2.1 next-auth@beta primereact@^10.9.3 react@^19.0.0 react-dom@^19.0.0 react-redux@^9.2.0 redux-logger@^3.0.6 react-syntax-highlighter@^15.6.1 styled-components@^6.1.16```

Or using pnmpm:

```sh
pnpm add @emotion/react@^11.14.0 @emotion/styled@^11.14.0 @mui/icons-material@^7.0.0 @mui/material@^7.0.0 @reduxjs/toolkit@^2.6.1 @types/redux-logger@^3.0.13 jspdf@^3.0.1 next@^15.2.1 next-auth@beta primereact@^10.9.3 react@^19.0.0 react-dom@^19.0.0 react-redux@^9.2.0 redux-logger@^3.0.6 react-syntax-highlighter@^15.6.1 styled-components@^6.1.16```

### Step 2: Install the Package

Now, install `@instincthub/react-ui`:

```sh
npm install @instincthub/react-ui
```

Or using yarn:

```sh
yarn add @instincthub/react-ui
```

## Development Setup

If you are contributing or testing locally, install the required dependencies:

```sh
npm install
```

Then, build the package using:

```sh
npm run rollup
```

Run the unit tests (Vitest with jsdom). Tests live in `__tests__/` folders next to the code they cover (`src/**/__tests__/*.test.ts`):

```sh
npm test
```

## Linking the Package (For Local Development)

To test `@instincthub/react-ui` locally within another project:

```sh
npm link
```

In your consuming project, run:

```sh
npm link @instincthub/react-ui
```

To unlink:

```sh
npm unlink @instincthub/react-ui
```

## Available Scripts

<!-- AUTO-GENERATED from package.json scripts -->
| Command | Description |
|---------|-------------|
| `npm run dev` | Start the example Next.js app with hot reload |
| `npm run build` | Build the example Next.js app |
| `npm run start` | Start the built example app |
| `npm run rollup` | Build the library for distribution |
| `npm test` | Run the unit tests once (Vitest + jsdom) |
| `npm run test:watch` | Run the unit tests in watch mode |
| `npm run link-ui` | Rebuild and `npm link` for local development (macOS/Linux) |
| `npm run link-ui-win` | Rebuild and `npm link` for local development (Windows) |
| `npm run unlink-ui-linux` | Unlink the package (macOS/Linux) |
| `npm run yalc-link-ui` | Rebuild and publish to local yalc store |
| `npm run yalc-link-ui-win` | Rebuild and publish to local yalc store (Windows) |
| `npm run yalc-update-ui` | Rebuild and push updates to all yalc consumers |
| `npm run deploy` | Build + publish to npm (the docs site deploys from GitHub Actions on push to main) |
<!-- END AUTO-GENERATED -->

## Core Features

- **Form Components**: A comprehensive set of form elements including text fields, date pickers, phone inputs, and more
- **UI Components**: Action dropdowns, modals, tooltips, and other interactive UI elements
- **Styling Assets**: Pre-built CSS files for consistent styling including dark mode support
- **Utility Functions**: API helpers, form validation, and other common utilities

## Usage

### Importing Components

```jsx
import { TextField, PasswordField, NewSubmitBtn } from "@instincthub/react-ui";

function LoginForm() {
  const [formData, setFormData] = useState({
    email: "",
    password: "",
  });

  const handleChange = (e) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value,
    });
  };

  return (
    <form>
      <TextField
        label="Email Address"
        name="email"
        value={formData.email}
        onChange={handleChange}
        required
      />

      <PasswordField
        label="Password"
        name="password"
        value={formData.password}
        onChange={handleChange}
        required
      />

      <NewSubmitBtn text="Login" />
    </form>
  );
}
```

### Using Utility Functions

```jsx
import { openToast, fetchAPI } from "@instincthub/react-ui";

// Display a toast notification
openToast("Operation successful!", "success");

// Make an API call
const fetchData = async () => {
  try {
    const response = await fetchAPI("/api/users", "GET");
    return response.data;
  } catch (error) {
    openToast("Failed to fetch data", "error");
  }
};
```

## Component Documentation

### Form Components

#### TextField

Standard text input field with label.

```jsx
<TextField
  label="Username"
  name="username"
  value={username}
  onChange={handleChange}
  required={true}
  placeholder="Enter your username"
  disabled={false}
/>
```

#### PasswordField

Secure password input with optional visibility toggle.

```jsx
<PasswordField
  label="Password"
  name="password"
  value={password}
  onChange={handleChange}
  required={true}
  showToggle={true}
/>
```

#### DateInput

Date picker component.

```jsx
<DateInput
  label="Birth Date"
  name="birthDate"
  value={birthDate}
  onChange={handleChange}
  required={true}
/>
```

#### PhoneNumberInput

International phone number input with country code selection.

```jsx
<PhoneNumberInput
  label="Phone Number"
  name="phone"
  value={phone}
  onChange={handleChange}
  required={true}
/>
```

#### NewSubmitBtn

Form submission button with loading state support.

```jsx
<NewSubmitBtn
  text="Submit"
  loading={isSubmitting}
  disabled={!isValid}
  onClick={handleSubmit}
/>
```

#### FormError

Display form validation errors.

```jsx
<FormError error={errors.username} />
```

#### FilterObjects

Filter and sort collections of objects.

```jsx
<FilterObjects data={users} filterKey="role" onChange={handleFilterChange} />
```

### UI Components

#### ActionDropdown

Dropdown menu for actions.

```jsx
<ActionDropdown
  type="email"
  names="assigned_email"
  labels="School Assigned Email"
  requireds={true}
/>
```

#### IHubTextEditor

Notion-style rich text editor: slash commands and a "+" block menu, drag-to-reorder blocks (including into banners, callouts and toggles), tables with row/column handles, image/video/audio/PDF/file uploads, embeds, section banners, callouts, toggles, CTA buttons, and pasted HTML (including email templates) that keeps its inline styles.

```jsx
<IHubTextEditor
  content={html}
  onChange={setHtml}
  upload={{ presignEndpoint: `${API_HOST_URL}uploads/presign/`, token }}
/>
```

Uploads can come from `onFileUpload`, the `upload` prop, or these environment variables:

<!-- AUTO-GENERATED from process.env reads in src/components/ui/editor/ihub-editor/upload -->
| Variable | Required | Description | Example |
|----------|----------|-------------|---------|
| `NEXT_PUBLIC_IHUB_EDITOR_PRESIGN_URL` | No | Presign endpoint: receives `POST { filename, content_type, size, kind }`, returns `{ url, cdnUrl }` (recommended) | `https://api.example.com/uploads/presign/` |
| `NEXT_PUBLIC_IHUB_EDITOR_UPLOAD_URL` | No | Multipart upload endpoint; JSON response must include `url`, `file`, `location` or `cdnUrl` | `https://api.example.com/uploads/` |
<!-- END AUTO-GENERATED -->

Block, table and media styles are in `src/assets/css/ui/ihub-text-editor-blocks.css`, which is included by `ui-index.css`. Full reference: [docs/static-docs/components/IHubTextEditor.md](docs/static-docs/components/IHubTextEditor.md).

### Utility Functions

#### openToast

Display toast notifications.

```jsx
openToast("Message sent successfully!", "success");
openToast("Operation failed", "error");
openToast("Please wait", "info");
```

#### openConfirmModal

Show a confirmation dialog.

```jsx
openConfirmModal({
  title: "Delete Item",
  message: "Are you sure you want to delete this item?",
  onConfirm: handleDelete,
  onCancel: () => console.log("Cancelled"),
});
```

#### handleFormErrors

Process and display form validation errors.

```jsx
try {
  // Form submission logic
} catch (error) {
  const errors = handleFormErrors(error);
  setFormErrors(errors);
}
```

#### fetchAPI

Make API requests with consistent error handling.

```jsx
const fetchData = async () => {
  const response = await fetchAPI("/api/data", "GET");
  return response.data;
};

const createItem = async (item) => {
  const response = await fetchAPI("/api/items", "POST", item);
  return response.data;
};
```

## Next.js Integration

This package works seamlessly with Next.js applications. For optimal performance, consider these best practices:

```jsx
// In your _app.js or layout.js
import "@instincthub/react-ui/assets/css/styles.css"; // Import styles once at the application root
```

## TypeScript Support

InstinctHub UI is built with TypeScript and includes full type definitions for all components and utilities.

```tsx
import { TextField, TextFieldProps } from "@instincthub/react-ui";

// Types are available for all props
const CustomTextField: React.FC<TextFieldProps> = (props) => {
  return <TextField {...props} className="custom-field" />;
};
```

## Browser Support

InstinctHub UI is compatible with all modern browsers including:

- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch: `git checkout -b feature/amazing-feature`
3. Add or update tests and make sure `npm test` passes
4. Commit your changes: `git commit -m 'Add some amazing feature'`
5. Push to the branch: `git push origin feature/amazing-feature`
6. Open a Pull Request

## License

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

## Credits

Developed and maintained by InstinctHub.
