@dessert-box/core

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

A library to easily consume your design tokens from a React component, meant to be used with [vanilla-extract][vanilla-extract].

原始使用文档
文档目录9 节
  1. Usage
  2. Variants
  3. API
  4. createBox(options: { atoms: AtomsFn, defaultClassName?: str…
  5. createBoxWithAtomsProp(options: { atoms: AtomsFn, defaultCl…
  6. Running the example app
  7. How does it work?
  8. Thanks
  9. FAQ

🍰 Dessert Box

A library to easily consume your design tokens from a React component, meant to be used with vanilla-extract.

This library will make consuming your sprinkles from a react component a breeze. It provides a zero-CSS-runtime <Box /> component (similar to the one in Braid or Chakra).

Try it on CodeSandbox!

It works by consuming atoms created with vanilla-extract) and sprinkles. Shout out to the team at Seek for making these awesome libraries!

  1. Step 1, create your Box with your atoms created with sprinkles:
// 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. Step 2, import it enjoy the sweetness:
// OtherFileOrComponent.tsx
import Box from './Box';

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

Wondering why using a Box component may be a good idea? or what is a Box component? Check the FAQ.

Wondering how to use variants with this library? Check out the variants section.

dessert-box

Try it on CodeSandbox!

Usage

Install the package:

$ npm install @dessert-box/react

Configure vanilla-extract and sprinkles and have your atoms ready:

// 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);

Check sprinkles docs for more context into how to create these atoms.

Now let's create our <Box /> using these atoms:

// 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>;
};

Notice we can pass every property, shorthand, or condition we can normally pass to our atomsFn function. For example, we could leverage the conditions for responsive design we have here:

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

If you need to render a tag different than a div, you can use the as prop:

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

Try it on CodeSandbox!

Variants

The official @vanilla-extract/recipes package has an excelent API for dealing with variants, this can be combined with our Box component to create variant-based components:

NOTE: (Assuming you already have created your Box component following the example above).

  1. First define your recipe using the recipe function:
// 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. Then use the recipes function to create variants and apply them to your 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;

For more context, refer to @vanilla-extract/recipe or feel free to open an issue in this project if the integration is not working as you'd expect!

API

createBox(options: { atoms: AtomsFn, defaultClassName?: string })

Creates a <Box /> component that takes atoms at the root level.

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

const Box = createBox({ atoms });

<Box padding="small" />;

createBoxWithAtomsProp(options: { atoms: AtomsFn, defaultClassName?: string })

Creates a <Box /> component that takes atoms as a prop called atoms.

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

const Box = createBoxWithAtomsProp({ atoms });

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

Running the example app

Run npm install then npm run build in the root folder (the one with this README file).

Then move into the example folder cd example and run npm install and npm start.

How does it work?

This works by depending on build-time generated CSS by sprinkles, and then using the atomsFn function to lookup classNames in runtime. So it does have a runtime footprint, but should be pretty minimal. I'm still experimenting to see if it's possible to remove that, but other approaches may lead to other constraints or similar runtime.

Thanks

  • Thanks to the team at Seek for vanilla-extract and sprinkles, this would not be possible without these great libs and the technical feats they accomplish.

FAQ

  • What is a Box component?

It's a generic element that allows you to prototype fast and takes a variety of styling props (think of exposing a lot of CSS attributes as props on a component).

  • Why should I use a Box component?

There are many versions and flavors of a Box component, some are more flexible, while others are more restrictive. The Box in this library falls into the latter category (restrictive), and it's more geared towards being the a lower level API of your Design System (or serving as inspiration for it).

This Box component is meant to be used as a primitive for consuming design tokens, giving you a nice balance between flexibility and constraints. You can use it as an lower level API to implement your other components (Buttons, Card, Layout components, ...), and also as a prototyping and general usage component:

  • As a prototyping tool, it allows you to use all of your design tokens to generate new designs and evaluate if you need to iterate on your foundations, or to validate if they work for your use cases.
  • For general usage you can still have the guarantee that users of your system won't do anything impossible (e.g.: using a value that is not part of the design tokens) but still have a productive experience working on 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 是什么?

A library to easily consume your design tokens from a React component, meant to be used with [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 和提交记录判断。