
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
@dessert-box/react
Advanced tools
A library to easily consume your design tokens from a React component, meant to be used with [vanilla-extract][vanilla-extract].
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).
It works by consuming atoms
created with vanilla-extract
) and sprinkles
. Shout out to the team at Seek for making these awesome libraries!
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;
// 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.
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>
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).
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];
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!
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" />;
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' }} />;
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
.
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.
sprinkles
, this would not be possible without these great libs and the technical feats they accomplish.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).
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:
FAQs
A library to easily consume your design tokens from a React component, meant to be used with [vanilla-extract][vanilla-extract].
The npm package @dessert-box/react receives a total of 15,483 weekly downloads. As such, @dessert-box/react popularity was classified as popular.
We found that @dessert-box/react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.