All Projects → jxnblk → Macro Components

jxnblk / Macro Components

Licence: mit
Create flexible layout and composite UI components without the need to define arbitrary custom props

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Macro Components

tsstyled
A small, fast, and simple CSS-in-JS solution for React.
Stars: ✭ 52 (-89.28%)
Mutual labels:  components, styled-components, style
Handycontrols
Contains some simple and commonly used WPF controls based on HandyControl
Stars: ✭ 347 (-28.45%)
Mutual labels:  style, components, styled-components
Stylable
Stylable - CSS for components
Stars: ✭ 1,109 (+128.66%)
Mutual labels:  style, components
Snacks
The Instacart Component Library
Stars: ✭ 65 (-86.6%)
Mutual labels:  style, components
components
Components library for the Blockchain.com ecosystem 💍 🔥
Stars: ✭ 20 (-95.88%)
Mutual labels:  components, styled-components
Ui
React Styled Components with bootstrap grid system
Stars: ✭ 89 (-81.65%)
Mutual labels:  components, styled-components
Leaf Ui
🍃 Leaf-UI: A react component library built using styled-components
Stars: ✭ 98 (-79.79%)
Mutual labels:  components, styled-components
jss-material-ui
A enhanced styling engine for material-ui
Stars: ✭ 15 (-96.91%)
Mutual labels:  styled-components, style
Nano Style
React functional CSS-in-JS
Stars: ✭ 85 (-82.47%)
Mutual labels:  style, components
claxed
Classes with the same style of Styled-Components
Stars: ✭ 17 (-96.49%)
Mutual labels:  components, styled-components
lab-cli
Command line utilities and exporting module for Compositor Lab
Stars: ✭ 52 (-89.28%)
Mutual labels:  components, styled-components
styled-mixin
Simple wrapper for creating styled-components mixins
Stars: ✭ 17 (-96.49%)
Mutual labels:  components, styled-components
Horror
😱 React HTML elements with CSS-in-JS
Stars: ✭ 78 (-83.92%)
Mutual labels:  components, styled-components
Design System
Priceline.com Design System
Stars: ✭ 604 (+24.54%)
Mutual labels:  components, styled-components
Re Jok
A React UI Component library built with styled-components
Stars: ✭ 102 (-78.97%)
Mutual labels:  components, styled-components
Reactackle
Open-source components library built with React and Styled-Components.
Stars: ✭ 278 (-42.68%)
Mutual labels:  components, styled-components
React95
🌈🕹 Refreshed Windows 95 style UI components for your React app
Stars: ✭ 4,877 (+905.57%)
Mutual labels:  components, styled-components
Phpinsights
🔰 Instant PHP quality checks from your console
Stars: ✭ 4,442 (+815.88%)
Mutual labels:  style
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (-5.98%)
Mutual labels:  style
Tachyons Components
React UI components powered by Tachyons with a styled-components like API
Stars: ✭ 409 (-15.67%)
Mutual labels:  components

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}`
Box.displayName = 'Box'

const Image = styled.img`
  max-width: 100%;
  height: auto;
  ${space}
`
Image.displayName = 'Image'

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

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

// create a macro function with the UI components you intend to use
const macro = Macro({
  Image,
  Heading,
  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>
))
import MediaObject from './MediaObject'

// get the macro component's child components
const { Image, Heading, Text } = MediaObject

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

Note: Macro components are intended to only work with specific child components. If you're wanting to define slots, see the Alternatives section below.

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(componentsObject)(elementFunction)

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

const Banner = Macro({
  // pass a components object
  Heading,
  Subhead
})(({
  // the element function receives child elements
  // named according to the components object
  Heading,
  Subhead
}) => (
  <Box p={3} color='white' bg='blue'>
    {Heading}
    {Subhead}
  </Box>
)

The elementFunction argument is called with an object of elements based on the componentsObject passed to the Macro function. Using the Banner component above would look something like the following.

import Banner from './Banner'

const App = () => (
  <Banner>
    <Banner.Heading>Hello</Banner.Heading>
    <Banner.Subhead>Subhead</Banner.Subhead>
  </Banner>
)

componentsObject

The components object is used to defined which components the macro component will accept as children.

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 Heading = styled.h2``
const Text = styled.div``

const componentsObj = {
  Heading,
  Text
}

const SectionHeader = Macro(componentsObj)(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 macro = Macro({ Icon, Text, CloseButton })

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

const { Text, CloseButton } = Message

// Omitting the Icon child element will render Message without an icon.
const message = (
  <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 macro = Macro({ Image, Text })

const Card = macro(({
  Image,
  Text
}, props) => (
  <Box p={2} bg={props.bg}>
    {Image}
    {Text}
  </Box>
))
// example usage
<Card bg='tomato'>
  <Card.Image src='kittens.png' />
  <Card.Text>Meow</Card.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'
import { Heading, Text } from './ui'

const macro = Macro({ Heading, Text })

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

Using a Component Multiple Times

To use the same component twice, give it a unique key in the componentsObject.

import React from 'react'
import Macro from 'macro-components'
import { Heading } from './ui'

const macro = Macro({
  Heading: Heading,
  Subhead: Heading
})

const Header = macro(({ Heading, Subhead }) => (
  <Box p={2}>
    {Heading}
    {Subhead}
  </Box>
))
<Header>
  <Header.Heading>Hello</Header.Heading>
  <Header.Subhead>Subhead</Header.Subhead>
</Header>

Alternatives

To create layout components that are not coupled to specific child components, using props or ordered children is probably a simpler approach.

The solutions below allow you to pass any arbitrary components as props or children.

See this discussion for more.

// using custom props
const MyLayout = ({
  left,
  right
}) => (
  <Flex>
    <Box width={128}>
      {left}
    </Box>
    <Box width={1}>
      {right}
    </Box>
  </Flex>
)

<MyLayout
  left={(
    <Image src='kitten.png' />
  )}
  right={(
    <Text>Meow</Text>
  )}
/>
// using ordered children
const Header = props => {
  const [ first, second ] = React.Children.toArray(props.children)
  return (
    <Box p={3}>
      {first}
      {second}
    </Box>
  )
}

<Header>
  <Heading>First</Heading>
  <Text>Second</Text>
</Header>
// using a children object
const Header = ({
  children: {
    left,
    right
  }
}) => (
  <Flex>
    <Box>
      {left}
    </Box>
    <Box width={1}>
      {right}
    </Box>
  </Flex>
)

<Header>
  {{
    left: (
      <Image src='kitten.png' />
    ),
    right: (
      <Text>Meow</Text>
    )
  }}
</Header>

Related

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].