Coder Social home page Coder Social logo

compound-builder's Introduction

compound-builder

NPM version NPM downloads Size

It's small but powerful. You can build compound components with declarative code.

Also, it supports type safety as shown in the picture below.

compound-builder is typesafe


๐Ÿ•น Demo Repository ๐Ÿ•น
Sample Counter

Basic example

import { compoundBuilder } from "compound-builder";
import constate from "constate";

import useSampleLayout from "./index.hook";
import { SampleBody, SampleFooter, SampleHeader } from "./index.style";

export const [SampleProvider, useCount, useCountIncrement, useCountDecrement] =
  constate(
    useSampleLayout,
    (value) => value.count,
    (value) => value.increment,
    (value) => value.decrement
  );

const SampleLayout = compoundBuilder(SampleProvider, {
  // You can specify any key you want. (e.g. chicken, pizza)
  Header: SampleHeader,
  Body: SampleBody,
  Footer: SampleFooter,
});

export default SampleLayout;

When used as shown above, the developed compound component will be affected by the Sample Provider context. This means that you can access the context through hooks like useCount.

If you develop compound components through a builder, you can use them as follows:

import SampleLayout from "@/components/Organisms/layouts/sample";
import CounterSection from "@/components/Organisms/sections/sample/CounterSection";

const SamplePage = () => {
  return (
    <SampleLayout>
      <SampleLayout.Header>
        <SampleHeaderSection />
      </SampleLayout.Header>

      <SampleLayout.Body>
        <CounterSection />
      </SampleLayout.Body>

      <SampleLayout.Footer>
        <SampleFooterSection />
      </SampleLayout.Footer>
    </SampleLayout>
  );
};

export default SamplePage;

Installation

npm:

npm i compound-builder

Yarn:

yarn add compound-builder

API

compoundBuilder<T>(wrapperComponent, Record<string, React.component<T>)

If you add sub-components to the compound component to be specified, you can handle the types as they are when using them.

Contributing

If you find a bug, please create an issue providing instructions to reproduce it. It's always very appreciable if you find the time to fix it. In this case, please submit a PR.

If you're a beginner, it'll be a pleasure to help you contribute. You can start by reading the beginner's guide to contributing to a GitHub project.

When working on this codebase, please use yarn. Run yarn examples to run examples.

License

MIT ยฉ KZ-Lucas

compound-builder's People

Contributors

lucas0530 avatar

Stargazers

 avatar ๋ฐ•์ƒํ›ˆ avatar

Watchers

 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.