Giter Club home page Giter Club logo

macro-components's Introduction

macro-components

Create flexible layout and composite UI components without the need to define arbitrary custom props.

Build Status

npm i macro-components
import React from 'react'
import styled from 'styled-components'
import macro from 'macro-components'
import { space, fontSize, color } from 'styled-system'

// Define some styled-components
const Box = styled.div`${space} ${fontSize} ${color}`

// Ensure components have a `displayName`
Box.displayName = 'Box'

const Heading = styled.h2`${space} ${fontSize} ${color}`
Heading.displayName = 'Heading'

const Text = styled.div`${space} ${fontSize} ${color}`
Text.displayName = 'Text'

// Create a macro-component
const MediaObject = macro(({
  Image,
  Heading,
  Text
}) => (
  <Flex p={2} align='center'>
    <Box width={128}>
      {Image}
    </Box>
    <Box>
      {Heading}
      {Text}
    </Box>
  </Flex>
))

// Use the macro-component by passing the components as children
const App = props => (
  <div>
    <MediaObject>
      <Image src='kitten.png' />
      <Heading>
        Hello
      </Heading>
      <Text>
        This component keeps its tree structure but still allows for regular composition.
      </Text>
    </MediaObject>
  </div>
)

Features

  • Single component creator
  • Intended for use with libraries like styled-components & glamorous
  • Encapsulate layout structure in composable components
  • Help keep your component API surface area to a minimum
  • Works with any other React components

Motivation

Often it's best to use React composition and props.children to create UI that is composed of multiple elements, but sometimes you might want to create larger composite components with encapsulated tree structures for layout or create Bootstrap-like UI components such as panels, cards, or alerts. This library lets you create composite components with encapsulated DOM structures without the need to define arbitrary props APIs and that work just like any other React composition.

This can help ensure that your component API surface area remains small and easier to maintain.

If you find yourself creating composite React components that don't map to data structures, as described in Thinking in React, then this module is intended for you.

Usage

macro(elementFunction)

Returns a React component with a composable API that keeps tree layout structure.

const Banner = macro(({
  Heading,
  Subhead
}) => (
  <Box p={3} color='white' bg='blue'>
    {Heading}
    {Subhead}
  </Box>
)

By default, the elementFunction argument is called with an object of elements based on the element type or component displayName. Using the Banner component above would look something like the following.

<Banner>
  <Heading>Hello</Heading>
  <Subhead>Subhead</Subhead>
</Banner>

To ensure correct placement or for when there are multiples of the same child component type, use the name prop to specify which child element is inserted in a particular location in the tree.

<Banner>
  <Heading>Hello</Heading>
  <Heading name='Subhead'>Subhead</Heading>
</Banner>

elementFunction

The element function is similar to a React component, but receives an elements object as its first argument and props as its second one. The elements object is created from its children and is intended to make encapsulating composition and element structures easier.

Within the macro component, the element function is called with the elements object and props: elementFunction(elementsObject, props).

// example
const elFunc = ({ Heading, Text, }, props) => (
  <header>
    {Heading}
    {Text}
  </header>
)

const SectionHeader = macro(elFunc)

Omitting children

For any element not passed as a child to the macro component, the element function will render undefined and React will not render that element. This is useful for conditionally omitting optional children

const Message = macro({
  Icon,
  Text,
  CloseButton
}) => (
  <Flex p={2} bg='lightYellow'>
    {Icon}
    {Text}
    <Box mx='auto' />
    {CloseButton}
  </Flex>
)

// Omitting the Icon child element will render Message without an icon.
<Message>
  <Text>{props.message}</Text>
  <CloseButton
    onClick={props.dismissMessage}
  />
</Message>

Props passed to the root component

The second argument passed to the element function allows you to pass props to the root element or any other element within the component.

const Card = macro(({
  Image,
  Text
}, props) => (
  <Box p={2} bg={props.bg}>
    {Image}
    {Text}
  </Box>
))

// example usage
<Card bg='tomato'>
  <Image src='kittens.png' />
  <Text>Meow</Text>
</Card>

Clone Component

To apply default props to the elements passed in as children, use the Clone component in an element function.

import macro, { Clone } from 'macro-components'

const Header = macro(({ Heading, Text }) => (
  <Box p={2}>
    <Clone
      element={Heading}
      fontSize={6}
      mb={2}
    />
    <Clone
      element={Text}
      fontSize={3}
    />
  </Box>
))

Related

MIT License

macro-components's People

Contributors

jxnblk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.