@brainfish-ai/components

中文文档
0.37.1发布于 2026/11/05MITTypeScriptES Module近 30 天下载 2653

Brainfish Components

原始使用文档
文档目录27 节
  1. Installation
  2. Usage
  3. Individual Component Imports (Recommended)
  4. Bulk Import (Legacy)
  5. Available Individual Imports
  6. Styles
  7. Development
  8. Getting started
  9. Developing
  10. Building
  11. Development Guide
  12. Color System
  13. Tailwind CSS Integration
  14. Component Patterns (shadcn/ui)
  15. Storybook Requirements
  16. Linting and Code formating for Typescript
  17. Testing
  18. Publishing your library to NPM
  19. Storybook
  20. Scripts
  21. Resources
  22. Bundler
  23. Code Formatter
  24. Storybook
  25. Testing
  26. Linting
  27. Compiler

Brainfish Components

Brainfish Components is a React component library that supports both bulk imports and individual component imports (similar to shadcn/ui) for optimal tree-shaking and bundle size optimization.

Installation

npm install @brainfish-ai/components
# or
yarn add @brainfish-ai/components

Usage

Import only the components you need for optimal bundle size:

// Import individual UI components
import { Button } from '@brainfish-ai/components/ui/button';
import { Dialog } from '@brainfish-ai/components/ui/dialog';
import { Input } from '@brainfish-ai/components/ui/input';

// Import other components
import { ChatSearch } from '@brainfish-ai/components/chat-search';
import { DatePicker } from '@brainfish-ai/components/date-picker';
import { Filter } from '@brainfish-ai/components/filter';

function MyApp() {
  return (
    <div>
      <Button>Click me</Button>
      <Input placeholder="Enter text..." />
      <DatePicker />
    </div>
  );
}

Bulk Import (Legacy)

Import all components at once (larger bundle size):

import { Button, Dialog, Input, ChatSearch } from '@brainfish-ai/components';

Available Individual Imports

UI Components

  • @brainfish-ai/components/ui/button
  • @brainfish-ai/components/ui/card
  • @brainfish-ai/components/ui/collapsible
  • @brainfish-ai/components/ui/command
  • @brainfish-ai/components/ui/dialog
  • @brainfish-ai/components/ui/dropdown-menu
  • @brainfish-ai/components/ui/input
  • @brainfish-ai/components/ui/label
  • @brainfish-ai/components/ui/popover
  • @brainfish-ai/components/ui/scroll-area
  • @brainfish-ai/components/ui/switch
  • @brainfish-ai/components/ui/textarea
  • @brainfish-ai/components/ui/tooltip

Complex Components

  • @brainfish-ai/components/chat-search
  • @brainfish-ai/components/combobox
  • @brainfish-ai/components/date-picker
  • @brainfish-ai/components/feedback
  • @brainfish-ai/components/filter
  • @brainfish-ai/components/generating-star
  • @brainfish-ai/components/markdown
  • @brainfish-ai/components/select

Styles

Don't forget to import the CSS:

import '@brainfish-ai/components/styles.css';

Development

Getting started

yarn install

Developing

To start developing run:

yarn start

This will build a version of your library, run the watcher and also run Storybook. To open Storybook manually open your Browser and navigate to http://localhost:6006. Start developing your components in src/components folder and update the src/index.js file accordingly. Always provide an YourComponent.story.tsx file, so your component will show up in Storybook.

Building

Build the library with individual component exports:

yarn build:components

Development Guide

This section explains the architecture and patterns used in this component library to help new developers get started quickly.

Color System

Our color system is built on CSS custom properties (variables) defined in src/global.css. All variables use the --bfc- prefix (Brainfish Components).

Color Token Scale

Colors follow a 100-1000 scale where lower numbers are lighter and higher numbers are darker:

--bfc-v2-color-blue-100: #e9f2fe;  /* lightest */
--bfc-v2-color-blue-500: #4688ec;  /* mid-tone */
--bfc-v2-color-blue-1000: #1c2b42; /* darkest */

Available Color Palettes

PaletteCSS Variable Pattern
Mono--bfc-v2-color-mono-{100-1000}
Red--bfc-v2-color-red-{100-1000}
Orange--bfc-v2-color-orange-{100-1000}
Yellow--bfc-v2-color-yellow-{100-1000}
Lime--bfc-v2-color-lime-{100-1000}
Green--bfc-v2-color-green-{100-1000}
Blue--bfc-v2-color-blue-{100-1000}
Teal--bfc-v2-color-teal-{100-1000}
Purple--bfc-v2-color-purple-{100-1000}
Magenta--bfc-v2-color-magenta-{100-1000}

Semantic Color Tokens

Instead of using raw color values, use semantic tokens that adapt to themes:

TokenPurpose
--bfc-primaryPrimary brand color
--bfc-backgroundPage background
--bfc-foregroundPrimary text color
--bfc-mutedSubdued backgrounds
--bfc-muted-foregroundSubdued text
--bfc-borderBorder color
--bfc-destructive-bgError/danger backgrounds
--bfc-success-bgSuccess state backgrounds

Dark Mode

Dark mode is activated by adding the .dark class to the element. The color scale is automatically inverted (100 becomes the darkest, 1000 becomes the lightest), ensuring consistent contrast in both themes.

<!-- Light mode (default) -->
<html>...</html>

<!-- Dark mode -->
<html class="dark">...</html>

Tailwind CSS Integration

We use Tailwind CSS with a custom preset that maps our color system to Tailwind utilities.

Using the Tailwind Preset

Consumers of this library can import our Tailwind preset to use the same design tokens:

// tailwind.config.js
module.exports = {
  presets: [require('@brainfish-ai/components/tailwind')],
  // ... your config
};

Semantic Color Classes

Use semantic Tailwind classes instead of raw colors:

// Good - uses semantic colors that adapt to themes
<div className="bg-primary text-foreground border-border" />
<div className="bg-destructive text-destructive" />
<div className="bg-muted text-muted-foreground" />

// Bad - hardcoded colors won't adapt to dark mode
<div className="bg-[#a3e635] text-[#171717]" />

Typography Utilities

Custom typography classes are available for consistent text styles:

ClassFont SizeUse Case
.heading-xxl2remPage titles
.heading-xl1.75remSection headers
.heading-lg1.5remCard headers
.heading-m1.25remSubsections
.heading-sm1remSmall headers
.paragraph-lg1remBody text
.paragraph-md0.875remDefault text
.paragraph-sm0.75remCaptions, labels

The cn() Utility

Use cn() from @/lib/utils to merge Tailwind classes safely:

import { cn } from '@/lib/utils';

// Merges classes and handles conflicts properly
<div className={cn('bg-primary p-4', isActive && 'bg-accent', className)} />

Component Patterns (shadcn/ui)

Our UI components follow shadcn/ui patterns and conventions.

Component Location

  • Base UI components: src/components/ui/
  • Complex/composite components: src/components/

Component Structure

Components use class-variance-authority (CVA) for variant management:

import * as React from 'react';
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '@/lib/utils';

// Define variants with CVA
const buttonVariants = cva(
  'inline-flex items-center justify-center rounded-md text-sm font-medium',
  {
    variants: {
      variant: {
        default: 'bg-primary text-primary-foreground',
        destructive: 'bg-destructive text-destructive-foreground',
        outline: 'border border-input bg-background',
      },
      size: {
        default: 'h-9 px-4 py-2',
        sm: 'h-8 px-3 text-sm',
        lg: 'h-10 px-8',
      },
    },
    defaultVariants: {
      variant: 'default',
      size: 'default',
    },
  }
);

// Component with forwarded ref
export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, ...props }, ref) => (
    <button
      className={cn(buttonVariants({ variant, size, className }))}
      ref={ref}
      {...props}
    />
  )
);
Button.displayName = 'Button';

export { Button, buttonVariants };

Key Conventions

  1. Always use semantic colors - Never use raw hex values in components
  2. Forward refs - All components should forward refs for flexibility
  3. Use cn() for className - Always merge with incoming className prop
  4. Export variants - Export the CVA variants for reuse
  5. Set displayName - Always set displayName for better debugging

Storybook Requirements

Every component MUST have an accompanying Storybook story file.

Why Stories Matter

  • Enable independent component testing and development
  • Serve as living documentation for the component API
  • Allow visual regression testing
  • Help designers and developers collaborate

Story File Convention

For a component Button.tsx, create Button.stories.tsx in the same directory:

src/components/ui/
├── button.tsx
├── button.stories.tsx  <-- Required!
└── button.test.tsx     <-- Optional unit tests

Story Structure

import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './button';

const meta: Meta<typeof Button> = {
  title: 'UI/Button',
  component: Button,
  tags: ['autodocs'],
};

export default meta;
type Story = StoryObj<typeof Button>;

// Show all variants
export const Default: Story = {
  args: { children: 'Click me' },
};

export const Destructive: Story = {
  args: { variant: 'destructive', children: 'Delete' },
};

export const AllSizes: Story = {
  render: () => (
    <div className="flex gap-4">
      <Button size="sm">Small</Button>
      <Button size="default">Default</Button>
      <Button size="lg">Large</Button>
    </div>
  ),
};

Running Storybook

yarn storybook

Then open http://localhost:6006 in your browser.


Linting and Code formating for Typescript

Linting and code formating is done via ESLint and Prettier using eslint-plugin-react and eslint-config-prettier. You can modify linting rules by overriding them in the .eslintrc.cjs file.

yarn lint

or (if automatic fixing is possible)

yarn lint:fix

Testing

Testing is done with Vitest and @testing-library/react You can refer to Button.test.js as an example.

yarn test

Publishing your library to NPM

To release your library to NPM or your private Registry, make sure you have an active account at NPM, your .npmrc file is correctly setup and the registry url at publishConfig in package.json file is set to your repository url, then:

yarn release

Storybook

For custom layouts, styling and more information about Storybook, please refer to Storybook documentation.

Deploy Storybook to GitHub Pages

Make sure the homepage url in package.json file is set to your githup pages url, then:

yarn deploy

Scripts

  • yarn start : Only serves Storybook.
  • yarn build : Builds your library (build can be found in dist folder).
  • yarn storybook:build : Builds the static Storybook in case you want to deploy it.
  • yarn test : Runs the tests.
  • yarn test:coverage: Runs the test and shows the coverage.
  • yarn lint : Runs the linter, Typescript typecheck and stylelint.
  • yarn lint:fix : Runs the linter, Typescript typecheck and stylelint and fixes automatic fixable issues.
  • yarn eslint: Runs only the JavaScript linter.
  • yarn eslint:fix: Runs only the JavaScript linter and fixes automatic fixable issues.
  • yarn stylelint: Runs only the style linter.
  • yarn stylelint:fix: Runs only the style linter and fixes automatic fixable issues.
  • yarn check-types: Runs typescript type checker.
  • yarn ci: Runs Linting, tests and type checker all together.
  • yarn release : Publishes your Library on NPM or your private Registry (depending on your config in your .npmrc file).
  • yarn storybook: Same as yarn start, to serve storybook.
  • yarn storybook:build: Generates the build for storybook in storybook-static folder, that can be deployed wherever you need.
  • yarn storybook:deploy: Builds and deploys Storybook to GitHub Pages.

Resources

Bundler

Code Formatter

Storybook

Testing

Linting

Compiler

版本与发布记录

@brainfish-ai/components 共发布 377 个版本,首次发布于 2024/12/23;过去 12 个月发布了 149 个版本。

版本发布日期距今
0.37.1latest2026/11/053 天前
0.37.02026/11/044 天前
0.36.12026/11/035 天前
0.36.02026/10/309 天前
0.35.202026/10/2712 天前
0.35.192026/10/1920 天前

其他发布通道:beta → 0.15.0-beta.3,rc → 0.16.6-rc.3,alpha → 0.18.4-alpha.7。

关于 @brainfish-ai/components 的常见问题

@brainfish-ai/components 是什么?

Brainfish Components

如何安装 @brainfish-ai/components?

在项目目录运行 npm install @brainfish-ai/components,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:clsx(^2.x)、react(18.x || 19.x)、react-dom(18.x || 19.x)、usehooks-ts(^3.x)、tailwind-merge(^2.x)、tailwindcss-animate(^1.x)、@phosphor-icons/react(^2.x)、class-variance-authority(^0.7.x)、@tailwindcss/container-queries(0.1.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@brainfish-ai/components 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brainfish-ai/components 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@brainfish-ai/components 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 22.x || 24.x。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@brainfish-ai/components 可以商用吗?

@brainfish-ai/components 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brainfish-ai/components 的下载量有多少?

根据 npm 官方下载统计,@brainfish-ai/components 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,653 次,日均约 88 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brainfish-ai/components 还在维护吗?

最新版本 0.37.1 发布于 2026/11/05(3 天前);至今共发布 377 个版本,其中过去 12 个月发布了 149 个;首次发布于 2024/12/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。