Coder Social home page Coder Social logo

dessert-box's Introduction

Dessert Box

Carefully packaged with sweets and atomic energy!

A <Box /> component to consume atoms created with vanilla-extract and sprinkles.

Shout out to the team at Seek for making these awesome libraries!

Usage

Install the package:

$ npm install dessert-box @radix-ui/react-polymorphic

@radix-ui/react-polymorphic is installed to get propert type hints.

Configure vanilla-extract and sprinkles and have your atoms ready, and also export a flat array with all of the pieces that make up your atoms fn:

// atoms.css.ts
import { createAtomicStyles, createAtomsFn } from "@vanilla-extract/sprinkles";

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

const responsiveStyles = createAtomicStyles({
  conditions: {
    mobile: {},
    tablet: { "@media": "screen and (min-width: 768px)" },
    desktop: { "@media": "screen and (min-width: 1024px)" },
  },
  properties: {
    padding: space,
    // ...
  },
  // ...
});

const colorStyles = createAtomicStyles({
  // ...
});

/**
 * Flat array including all property names used by our atoms
 */
export const usedProperties = [colorStyles, layoutStyles].flatMap((item) =>
  Object.keys(item)
);

export const atoms = createAtomsFn(responsiveStyles, colorStyles);

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

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

// yourApp.ts
const Box = createBox(atoms, usedProperties);

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>

TypeScript

This library is fully typed, and the component supports the as prop, and will properly type props based on the type of element we use and also based on our atoms.

Thanks to @radix-ui/react-polymorphic for helping to achieve this โœจ.

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.

dessert-box's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.