@dessert-box/core

中文文档
0.2.0发布于 2022/06/06MITTypeScriptESM + CJS零依赖近 30 天下载 6.5万

一个库,可以轻松地从 React 组件中消耗出您的设计符号,意在使用 [vanilla-extract][vanilla-extract].

中文使用文档
文档目录9 节
  1. 使用量
  2. 备选案文
  3. API 密码
  4. 创建框( 选项 : {原子: 原子Fn, 默认分类Name: string })
  5. 创建 BoxWith Atoms Prop( 选项 : {原子: AtomsFn, 默认类Name: string })
  6. 运行示例应用程序
  7. 怎么会这样?
  8. 谢谢
  9. 财务问题

甜点盒

从 React 组件轻松消耗您设计符号的库, 用于 香草摘录。 。 。 .

这个图书馆会消耗你的 喷洒 从一个反应组件 微风。 它提供零CSS运行时间 <Box /> 组件(类似于 布罗德 或者说 查克罗) (中文(简体) ).

试试密码箱!

它通过消费发挥作用 atoms 创建于 vanilla-extract和 sprinkles向"求求"的团队大喊大叫 制作这些很棒的图书馆!

  1. 第一步,用您的 atoms 用喷出物创建 :
// Box.tsx
import { createBox } from '@dessert-box/react';
import { atoms } from './sprinkles.css';

const { Box } = createBox({
  atoms,
  // optional: pass your CSS reset className here
  // useful if you want to scope your reset to your Box element
  defaultClassName: 'resetClassName',
});

export default Box;
  1. 步骤2,导入它享受甜蜜:
// OtherFileOrComponent.tsx
import Box from './Box';

const MyComponent = () => {
  return <Box padding="large">What a sweet treat!</Box>;
};

想知道为什么使用Box组件可能是一个好主意吗? 或者Box组件是什么? 检查 财务问题。 。 。 .

想知道如何使用 variants 这个图书馆? 看看这个 变体 节.

dessert-box

试试密码箱!

使用量

安装软件包 :

$ npm install @dessert-box/react

配置 香草摘录 财务报告和审定财务报表 sprinkles 并准备好你的原子:

// atoms.css.ts
import { defineProperties, createSprinkles } from '@vanilla-extract/sprinkles';

const space = {
  none: 0,
  small: 4,
  medium: 8,
  large: 16,
};

const colors = {
  primary: 'blue',
  // ...
};

const atomicStyles = defineProperties({
  conditions: {
    mobile: {},
    tablet: { '@media': 'screen and (min-width: 768px)' },
    desktop: { '@media': 'screen and (min-width: 1024px)' },
  },
  properties: {
    padding: space,
    backgroundColor: colors,
    // ...
  },
  // ...
});

export const atoms = createSprinkles(atomicStyles);

检查 sprinkles 文档 用于创建这些原子.

现在让我们创造我们 <Box /> 使用这些原子:

// Box.ts
import { createBox } from '@dessert-box/react';
import { atoms } from './sprinkles.css';

const { Box } = createBox({ atoms });

export default Box;
// otherFile.tsx
import Box from './Box';

const App = () => {
  return <Box padding="large">Hello</Box>;
};

注意,我们可以通过每一个财产, 简称,或者条件 我们通常可以通过我们 atomsFn 函数。 例如,我们可以利用这些条件来设计应对能力:

<Box padding={{ mobile: 'none', tablet: 'small', desktop: 'large' }} />

如果需要使标记与标记不同 div,可以使用 as 道具 :

<Box as="a" href="https://example.com" padding="small">
  Link to example
</Box>

试试密码箱!

备选案文

官员 @vanilla-extract/recipes (英语) 软件包有处理变体的优秀API,这可以和我们的 Box 要创建的组件 基于变体的组件数字 :

注: (假设您已经创建了自己的 Box (按以上例子排列).

  1. 首先用 recipe 函数 :
// Button.css.ts
import { recipe } from '@vanilla-extract/recipes';
import { atoms } from '../atoms.css';

export const buttonRecipe = recipe({
  variants: {
    kind: {
      primary: atoms({ background: 'blue50' }),
      secondary: atoms({ background: 'yellow' }),
    },
    size: {
      md: atoms({ fontSize: 'large' }),
      lg: atoms({ fontSize: 'extraLarge' }),
    },
  },
});

export type ButtonVariants = Parameters<typeof buttonRecipe>[0];
  1. 那就用那个 recipes 函数创建变量并将其应用到您的 Box数字 :
// Button.tsx
import { Box } from './Box';
import { buttonRecipe, ButtonVariants } from './Button.css';

type Props = {
  children: React.ReactNode;
} & ButtonVariants;

export const Button = ({
  children,
  size = 'md',
  kind = 'secondary',
}: Props) => {
  return (
    <Box as="button" className={buttonRecipe({ size, kind })}>
      {children}
    </Box>
  );
};

export default Button;

关于更多背景,参见: @vanilla-extract/recipe (英语) 或者觉得自由 在该项目中打开一个问题 如果融合没有你预期的那样有效!

API 密码

创建框( 选项 : {原子: 原子Fn, 默认分类Name: string })

创建一个 <Box /> 在根位取出原子的组件.

import { createBox } from '@dessert-box/react';
import { atoms } from './atoms.css';

const Box = createBox({ atoms });

<Box padding="small" />;

创建 BoxWith Atoms Prop( 选项 : {原子: AtomsFn, 默认类Name: string })

创建一个 <Box /> 将原子作为道具的组件 atoms。 。 。 .

import { createBoxWithAtomsProp } from '@dessert-box/react';
import { atoms } from './atoms.css';

const Box = createBoxWithAtomsProp({ atoms });

<Box atoms={{ padding: 'small' }} />;

运行示例应用程序

运行 npm install 接下来 npm run build 在根文件夹中(有此 README 文件的) .

然后移动到示例文件夹中 cd example 运行 npm install 财务报告和审定财务报表 npm start。 。 。 .

怎么会这样?

这个工程依赖于构建时生成的 CSS 喷洒,然后使用 atomsFn 在运行时间中查找类名。 所以它确实有一个运行时间的足迹, 但应该非常小。 我仍然在尝试看是否有可能去除它, 但其他的方法可能导致其他的限制或类似的运行时间.

谢谢

  • 多亏了Speaking的团队 香草摘录 财务报告和审定财务报表 sprinkles没有这些伟大的自由 和技术成就,这是不可能实现的.

财务问题

  • Box 组件是什么?

这是一种通用的元素,可以让你快速地原型并取取出各种造型道具(考虑将许多CSS属性暴露在一个组件上作为道具).

  • 我为什么要用Box组件?

Box 组件有很多版本和口味,有些更多 灵活,而其他人则更多 限制性。这个库中的"盒子"属于后一类(限制性),它更适合成为您设计系统的下级API(或作为它的灵感).

这个Box组件是用来作为消费设计符号的原始,在灵活性和约束之间给予你一个很好的平衡. 您可以使用它作为较低等的 API 执行您的其他组件(按钮,卡,布局组件,.),也可以作为原型和一般用法组件:

  • 作为一个原型化工具,它允许您使用所有的设计符来生成新的设计并评估您是否需要在您的地基上进行拓扑,或者验证它们是否为您的使用案例工作.
  • 对于一般的用法,你仍然可以保证你的系统的用户不会做任何不可能的事(例如:使用一个不是设计符号的一部分的值),但仍有在UI上工作的生产性经验.

版本与发布记录

@dessert-box/core 共发布 2 个版本,首次发布于 2021/09/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.0latest2022/06/064 年前
0.1.02021/09/205 年前

关于 @dessert-box/core 的常见问题

@dessert-box/core 是什么?

一个库,可以轻松地从 React 组件中消耗出您的设计符号,意在使用 [vanilla-extract][vanilla-extract].

如何安装 @dessert-box/core?

在项目目录运行 npm install @dessert-box/core,或使用 pnpm add / yarn add / bun add。

@dessert-box/core 支持 TypeScript 吗?

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

@dessert-box/core 支持 ESM 还是 CommonJS?

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

@dessert-box/core 可以商用吗?

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

@dessert-box/core 的下载量有多少?

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

@dessert-box/core 还在维护吗?

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