@clutch-creator/toolkit

中文文档
3.3.1发布于 2025/11/27MITTypeScriptES Module近 30 天下载 2746

Toolkit components and utils used in clutch projects

原始使用文档
文档目录14 节
  1. Installation
  2. Overview
  3. API Reference
  4. State Management Hooks
  5. Utilities
  6. Error Classes
  7. Controls Types
  8. Package Exports
  9. Components
  10. Development Workflow
  11. 🚀 Deployment
  12. License
  13. Contributing
  14. 🆘 Support

Clutch Toolkit

npm version License: MIT TypeScript React Next.js Build Status

A comprehensive toolkit providing React components, utilities, and state management solutions for Next.js applications built with the Clutch framework.

Installation

npm install @clutch-creator/toolkit
# or
yarn add @clutch-creator/toolkit
# or
bun add @clutch-creator/toolkit

Overview

The Clutch Toolkit is designed to provide developers with a set of reusable components and utilities that integrate seamlessly with Next.js applications. It includes state management, SEO optimization, image handling, and various utility functions.

API Reference

State Management Hooks

useRegisterState(name, value)

Registers a state to be globally bindable in clutch.

Type Signature:

useRegisterState<T>(name: string, value: T): (newValue: T) => void

Example:

import { useRegisterState } from '@clutch-creator/toolkit';
import { useState } from 'react';

function Counter() {
  const [counter, setCounter] = useState(0);

  // Register the state so it can be accessed globally in Clutch
  useRegisterState<number>('count', counter);

  const increment = () => setCounter(prev => prev + 1);
  const reset = () => setCounter(0);

  return (
    <div>
      <button onClick={increment}>Increment</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}

useRegisterAction(options)

Registers actions for components that can be triggered by events.

Type Signature:

useRegisterAction<T extends (...args: unknown[]) => unknown>(
  options: TRegisterActionOptions<T>
): void

interface TRegisterActionOptions<T> {
  name: string;
  action: T;
  props?: Record<string, unknown>;
  wrapper?: React.FunctionComponent<{
    children: React.ReactNode;
    [key: string]: unknown;
  }>;
  styleSelectors?: TStyleSelector[];
}

interface TStyleSelector {
  name: string;
  value: string;
}

Example:

import { useRegisterAction } from '@clutch-creator/toolkit';
import { useState } from 'react';

type TSetToggled = (toggled: boolean) => void;

function TodoItem({ id, text, completed }) {
  const [toggled, setToggled] = useState(false);

  useRegisterAction<TSetToggled>({
    name: 'setToggled',
    action: (newValue) => {
      setToggled((value) => newValue);
    },
    props: {
      'data-toggled': toggled
    },
    styleSelectors: [
      { name: 'Toggled', value: '&:[data-toggled=true]' }
    ]
  });

  return <div className={completed ? 'completed' : ''}>{text}</div>;
}

useRegisterSelect(setVisibility, activeTrail)

Registers a handler to be called when the element is selected in clutch.

Type Signature:

useRegisterSelect(
  setVisibility: (shouldBeVisible: boolean) => void,
  activeTrail?: boolean
): null

Example:

import { useRegisterSelect } from '@clutch-creator/toolkit';
import { useState } from 'react';

function ConditionalContent({ children }) {
  const [isVisible, setIsVisible] = useState(true);

  useRegisterSelect(
    (shouldBeVisible) => setIsVisible(shouldBeVisible),
    true // activeTrail - whether it should be called when a child is selected
  );

  if (!isVisible) return null;

  return <div className="conditional-content">{children}</div>;
}

Utilities

updateUrlSearchParams(newParams, router)

Updates URL search parameters while preserving existing ones. Useful for filtering, pagination, and state management through URLs.

Type Signature:

updateUrlSearchParams(
  newParams: Record<string, unknown>,
  router: NextRouter
): Record<string, unknown>

Example:

import { updateUrlSearchParams } from '@clutch-creator/toolkit';
import { useRouter } from 'next/router';

function ProductFilters() {
  const router = useRouter();

  const handleCategoryFilter = (category: string) => {
    updateUrlSearchParams({ category, page: 1 }, router);
    // URL: /products?category=electronics&page=1
  };

  const handlePriceRange = (min: number, max: number) => {
    updateUrlSearchParams({
      priceMin: min.toString(),
      priceMax: max.toString()
    }, router);
    // URL: /products?category=electronics&page=1&priceMin=100&priceMax=500
  };

  const clearFilters = () => {
    updateUrlSearchParams({
      category: null,
      priceMin: null,
      priceMax: null
    }, router);
    // URL: /products
  };

  return (
    <div>
      <button onClick={() => handleCategoryFilter('electronics')}>
        Electronics
      </button>
      <button onClick={() => handlePriceRange(100, 500)}>
        $100-$500
      </button>
      <button onClick={clearFilters}>Clear Filters</button>
    </div>
  );
}

clutchElementConfig(element, config)

Registers React components for use in the Clutch visual editor with optional configuration.

Type Signature:

clutchElementConfig(
  element: React.FunctionComponent,
  config: {
    icon?: string;
    styleSelectors?: { name?: string; value: string }[];
  }
): void

Example:

import { clutchElementConfig } from '@clutch-creator/toolkit';

const Button = ({ children, variant = 'primary', ...props }) => (
  <button className={`btn btn-${variant}`} {...props}>
    {children}
  </button>
);

const Card = ({ title, children, ...props }) => (
  <div className="card" {...props}>
    <h3 className="card-title">{title}</h3>
    <div className="card-content">{children}</div>
  </div>
);

// Register components with Clutch editor
clutchElementConfig(Button, {
  icon: '🔘',
  styleSelectors: [
    { name: 'Hover', value: '&:hover' },
    { name: 'Disabled', value: '&:disabled' },
  ]
});

clutchElementConfig(Card, {
  icon: '🃏',
  styleSelectors: [
    { name: 'Card Hover', value: '&:hover' },
  ]
});

logger

Conditional logging utility with different methods for development and debugging.

Type Signature:

logger: {
  log(...args: unknown[]): void;      // Only in development
  debug(...args: unknown[]): void;    // Only when window.CLUTCH_DEBUG = true
  warn(...args: unknown[]): void;     // In development or server
  error(...args: unknown[]): void;    // In development or server
}

Example:

import { logger } from '@clutch-creator/toolkit';

function DataProcessor({ data }) {
  // Always logs in development
  logger.log('Processing data:', data);

  // Only logs when window.CLUTCH_DEBUG = true
  logger.debug('Debug info:', data);

  try {
    const processed = processData(data);
    logger.log('Data processed successfully:', processed);
    return processed;
  } catch (error) {
    logger.error('Error processing data:', error);
    logger.warn('Falling back to default data');
    return getDefaultData();
  }
}

// In browser console to enable debug logging:
// window.CLUTCH_DEBUG = true;

cloneChildren(children, props)

Clones React children and applies the given props to each child element. Filters out debug-related props and handles both single children and arrays of children.

Type Signature:

cloneChildren(
  children: React.ReactNode,
  props: Record<string, unknown>
): React.ReactNode

Example:

import { cloneChildren } from '@clutch-creator/toolkit';

function WrapperComponent({ children, className, onClick }) {
  // Clone children and add common props
  const clonedChildren = cloneChildren(children, {
    className: `child-element ${className}`,
    onClick: onClick,
    'data-wrapper': true
  });

  return <div className="wrapper">{clonedChildren}</div>;
}

// Usage
function App() {
  return (
    <WrapperComponent className="highlighted" onClick={() => console.log('clicked')}>
      <button>Button 1</button>
      <button>Button 2</button>
    </WrapperComponent>
  );
}
// Both buttons will receive className="child-element highlighted" and onClick handler

Error Classes

MissingEnvVariableError

Thrown when required environment variables are missing.

Type Signature:

class MissingEnvVariableError extends Error {
  constructor(envName: string);
}

Example:

import { MissingEnvVariableError } from '@clutch-creator/toolkit';

function initializeApp() {
  const apiKey = process.env.NEXT_PUBLIC_API_KEY;
  const dbUrl = process.env.DATABASE_URL;

  if (!apiKey) {
    throw new MissingEnvVariableError('NEXT_PUBLIC_API_KEY');
  }

  if (!dbUrl) {
    throw new MissingEnvVariableError('DATABASE_URL');
  }

  // Initialize app with environment variables
  return { apiKey, dbUrl };
}

// Usage with error handling
try {
  const config = initializeApp();
} catch (error) {
  if (error instanceof MissingEnvVariableError) {
    console.error('Configuration error:', error.message);
    // Handle missing environment variable
  }
}

InvalidEnvVariableError

Thrown when environment variables have invalid values.

Type Signature:

class InvalidEnvVariableError extends Error {
  constructor(envName: string);
}

Example:

import { InvalidEnvVariableError } from '@clutch-creator/toolkit';

function validateConfig() {
  const port = process.env.PORT;
  const nodeEnv = process.env.NODE_ENV;

  if (port && isNaN(parseInt(port))) {
    throw new InvalidEnvVariableError('PORT');
  }

  if (nodeEnv && !['development', 'production', 'test'].includes(nodeEnv)) {
    throw new InvalidEnvVariableError('NODE_ENV');
  }

  return {
    port: port ? parseInt(port) : 3000,
    nodeEnv: nodeEnv || 'development',
  };
}

// Usage
try {
  const config = validateConfig();
} catch (error) {
  if (error instanceof InvalidEnvVariableError) {
    console.error('Invalid configuration:', error.message);
    process.exit(1);
  }
}

Controls Types

The toolkit exports comprehensive TypeScript types for various control types used in the Clutch editor:

import type { Controls } from '@clutch-creator/toolkit';

// Available control types:
// Array, Checkbox, Code, Color, Combobox, Component, File, Input,
// Json, Media, Number, Object, RichText, Select, Styles, Svg,
// TextArea, Url, Action

Example:

import type { Controls } from '@clutch-creator/toolkit';

type TSomeComponentProps = {
  // props.image will use a media control
  image: Controls["Media"],
  /**
   * You can also annotate a prop to set a control
   * @control CustomControl
   */
  anotherProp: string
}

export function SomeComponent = (props: TSomeComponentProps) {
  // ...
}

Package Exports

The toolkit is organized into modular exports for specific use cases:

Components

// Individual component imports
import { Image } from '@clutch-creator/toolkit/components/Image';
import { ClientImage } from '@clutch-creator/toolkit/components/ClientImage';
import { Link } from '@clutch-creator/toolkit/components/Link';
import { Seo } from '@clutch-creator/toolkit/components/Seo';
import { RichText } from '@clutch-creator/toolkit/components/RichText';
import { Svg } from '@clutch-creator/toolkit/components/Svg';
import { ApplyHooks } from '@clutch-creator/toolkit/components/ApplyHooks';
import { NotFoundRedirect } from '@clutch-creator/toolkit/components/NotFoundRedirect';
import { Slot } from '@clutch-creator/toolkit/components/Slot';
  • Image: Advanced Next.js Image wrapper with automatic placeholder generation and optimization
  • ClientImage: Client-side image component for dynamic image loading
  • Link: Enhanced Next.js Link component with support for complex URL parameter management
  • Seo: Comprehensive SEO component supporting Open Graph, Twitter Cards, and structured data
  • RichText: Flexible rich text renderer supporting both string and JSX content
  • Svg: SVG component wrapper for dynamic SVG rendering
  • ApplyHooks: Component for applying multiple hooks to children components
  • NotFoundRedirect: Component for handling 404 redirects
  • Slot: Utility component for passing props to children (uses cloneChildren internally)

Development Workflow

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests: bun run test
  5. Run linting: bun run lint
  6. Create a changeset: bun run changeset (if your changes should trigger a release)
  7. Commit your changes: git commit -m 'feat: add amazing feature'
  8. Push to your branch: git push origin feature/amazing-feature
  9. Create a Pull Request

To run toolkit locally in a clutch project, create a symlink of toolkit:

bun link

Then on the project dir:

bun link @clutch-creator/toolkit

You'll also need to add some configuration to the project next config, make sure the root path is back enough to where it englobes the toolkit location:

const nextConfig: NextConfig = withClutch({
  transpilePackages: ['@clutch-creator/toolkit'],
  turbo: {
    root: path.join(__dirname, '../../../../..'),
  },
  outputFileTracingRoot: path.join(__dirname, '../../../../..'),
});

🚀 Deployment

This project uses Changesets for automated versioning and publishing:

  1. Create a changeset: bun run changeset
  2. Push changes to main branch
  3. GitHub Actions will automatically:
    • Create a "Release PR" with version updates
    • When merged, publish NPM packages and create GitHub releases

License

MIT

Contributing

This toolkit is maintained by the Clutch team. For issues and feature requests, please visit our GitHub repository.

🆘 Support


Made with ❤️ by the Clutch team

Clutch is the next-generation visual builder that empowers creative professionals with total design freedom, advanced functionality, and top-tier performance. Learn more at clutch.io.

版本与发布记录

@clutch-creator/toolkit 共发布 49 个版本,首次发布于 2025/07/23;过去 12 个月发布了 3 个版本。

版本发布日期距今
3.3.1latest2025/11/2711 个月前
3.3.02025/11/2611 个月前
3.2.02025/11/1811 个月前
3.1.12025/11/061 年前
3.1.02025/11/051 年前
3.0.02025/11/031 年前

关于 @clutch-creator/toolkit 的常见问题

@clutch-creator/toolkit 是什么?

Toolkit components and utils used in clutch projects

如何安装 @clutch-creator/toolkit?

在项目目录运行 npm install @clutch-creator/toolkit,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(^16.0.1)、react(^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@clutch-creator/toolkit 支持 TypeScript 吗?

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

@clutch-creator/toolkit 支持 ESM 还是 CommonJS?

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

@clutch-creator/toolkit 可以商用吗?

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

@clutch-creator/toolkit 的下载量有多少?

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

@clutch-creator/toolkit 还在维护吗?

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