@collapsed/react

中文文档
5.1.0发布于 2024/12/24MITTypeScriptESM + CJS近 30 天下载 1982

用于创建灵活和可访问扩展/缩放组件的回放自定义.

中文使用文档
文档目录9 节
  1. 特征
  2. 演示
  3. 安装
  4. 使用量
  5. 简单使用
  6. 自己来控制吧
  7. API 密码
  8. 替代解决方案
  9. 财务问题

页:1

你也许在找 反应相撞。本软件包(除 @ collapsed/ core (帮助) @ info)是一个WIP重写来创建"香草JS"核心.


@ collapsed/ react( 使用 Collapse)

用于创建可访问扩展/缩合组件的回放钩. 使用 CSS 转换自 0 改为 auto。 。 。 .

特征

  • 处理你元素的动画高度, auto 包括!
  • 你控制着UI useCollapse 提供必要的道具,您可以控制样式和元素.
  • 从盒子里可以进入 - 不需要担心你的倒闭/扩展组件能否进入,因为这能照顾你!
  • 不需要动画框架! 仅由 CSS 动画提供动力
  • 在类型脚本中写入

演示

看演示网站!

代码箱演示

安装

$ npm i @collapsed/react

使用量

简单使用

import React from "react";
import { useCollapse } from "@collapsed/react";

function Demo() {
  const { getCollapseProps, getToggleProps, isExpanded } = useCollapse();

  return (
    <div>
      <button {...getToggleProps()}>
        {isExpanded ? "Collapse" : "Expand"}
      </button>
      <section {...getCollapseProps()}>Collapsed content 🙈</section>
    </div>
  );
}

自己来控制吧

import React, { useState } from "react";
import { useCollapse } from "@collapsed/react";

function Demo() {
  const [isExpanded, setExpanded] = useState(false);
  const { getCollapseProps, getToggleProps } = useCollapse({ isExpanded });

  return (
    <div>
      <button
        {...getToggleProps({
          onClick: () => setExpanded((prevExpanded) => !prevExpanded),
        })}
      >
        {isExpanded ? "Collapse" : "Expand"}
      </button>
      <section {...getCollapseProps()}>Collapsed content 🙈</section>
    </div>
  );
}

API 密码

useCollapse 选择以下选项:

interface UseCollapseOptions {
  /** If true, the disclosure is expanded. */
  isExpanded?: boolean;
  /**
   * If true, the disclosure is expanded when it initially mounts.
   * @default false
   */
  defaultExpanded?: boolean;
  /** Handler called when the disclosure expands or collapses */
  onExpandedChange?: (state: boolean) => void;
  /** Handler called at each stage of the animation. */
  onTransitionStateChange?: (
    state:
      | "expandStart"
      | "expanding"
      | "expandEnd"
      | "collapseStart"
      | "collapsing"
      | "collapseEnd",
  ) => void;
  /** Timing function for the transition */
  easing?: string;
  /**
   * Duration of the expand/collapse animation.
   * If 'auto', the duration will be calculated based on the height of the collapse element
   */
  duration?: "auto" | number;
  /** Height in pixels that the collapse element collapses to */
  collapsedHeight?: number;
  /**
   * Unique identifier used to for associating elements appropriately for accessibility.
   */
  id?: string;
}

并返回以下 API :

interface CollapseAPI {
  isExpanded: boolean;
  setExpanded: (update: boolean | ((prev: boolean) => boolean)) => void;
  getToggleProps: <T extends HTMLElement>(
    props?: React.ComponentPropsWithoutRef<T> & { refKey?: string },
  ) => React.ComponentPropsWithRef<T>;
  getCollapseProps: <T extends HTMLElement>(
    props?: React.ComponentPropsWithoutRef<T> & { refKey?: string },
  ) => React.ComponentPropsWithRef<T>;
}

替代解决方案

  • 反应-春季 - JavaScript 动画基于库,有可能有更平滑的动画. 要创建可用的崩溃组件, 还需要做更多的工作 .
  • 反应-活体-高 - 另一个使用 CSS 向任何高度的动能过渡的库。 它提供组件,而不是钩.

财务问题

当我应用垂直 padding 到获得 getCollapseProps动画很简陋 而且不会一直崩溃 什么情况?

倒塌通过操纵 height 属性。 如果一个元素有垂直的粘贴,则会扩大元素的大小,即使它有 height: 0; overflow: hidden。 。 。 .

为了避免这种情况,只需将粘接物从元素移动到直接嵌入其中的元素.

// from
<div {...getCollapseProps({style: {padding: 20}})}
  This will do weird things
</div>

// to
<div {...getCollapseProps()}
  <div style={{padding: 20}}>
    Much better!
  </div>
</div>

版本与发布记录

@collapsed/react 共发布 6 个版本,首次发布于 2023/01/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.1.0latest2024/12/241.9 年前
5.0.02024/06/222 年前
4.1.0-experimental.79f518cexperimental2023/03/023 年前
4.0.12023/01/253 年前
4.0.02023/01/163 年前
4.0.0-beta.1next2023/01/163 年前

关于 @collapsed/react 的常见问题

@collapsed/react 是什么?

用于创建灵活和可访问扩展/缩放组件的回放自定义.

如何安装 @collapsed/react?

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

@collapsed/react 支持 TypeScript 吗?

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

@collapsed/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@collapsed/react 可以商用吗?

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

@collapsed/react 的下载量有多少?

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

@collapsed/react 还在维护吗?

最新版本 5.1.0 发布于 2024/12/24(1.9 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/01/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。