@qc/react-block

0.2.2 • Public • Published

@qc/react-block

Build Status Coverage Status License Downloads

npm badge

A React component that renders a div with the semantics of representing a block structure or region.

A div is a block-level element but by default it does not create a block formatting context (BFC). Certain CSS properties can be applied to elements to give it a BFC. Some of the methods are listed below.

  1. Block elements where overflow has a value other than visible.
  2. Elements with contain set to layout, content, or strict.
  3. Elements with display's inside value set to flow-root.

A BFC provides the following benefits, among others:

  • Prevents top and bottom margin bleedthrough.
  • Lets the background fill the entire area behind an element's content.

See this Block Formatting Context for Block-Level Elements Codepen to see BFC in action.

Note: Included with the BFC CSS is box-sizing: border-box since this is the box-sizing most sensible for a block.

One of the most common use cases is to use Block as the root of a custom React component or as representing different regions within a component. See the examples below.

Installation

npm install --save @qc/react-block

or

yarn add @qc/react-block

Example Usage

As the Root

import React from 'react'

import Block from '@qc/react-block'

import '@qc/react-block/dist/styles/Block.css'


export default function MyBlockComponent(props) {
  return (
    <Block className="My" style={{ backgroundColor: '#bedfed' }}>
      <h1>My Block Component</h1>
      {/*
      Note: The margins of the `h1` and `p` won't bleed-through
      like they would with default styled `div`s.  Also, the
      background color will fill the area behind the margins.
      */}
      <p>My component demo'ing the Block component.</p>
    </Block>
  )
}

Component Regions

import React from 'react'

import Block from '@qc/react-block'

import '@qc/react-block/dist/styles/Block.css'


export default function Panel(props) {
  return (
    <Block className="Panel">
      <Block className="Panel-Head">
        <Title>Panel's Title</Title>
      </Block>
      <Block className="Panel-Body">
        <h1>...</h1>
        <p>...</p>
        <h2>...</h2>
        <p>...</p>
      </Block>
      <Block className="Panel-Foot">
        <button>...</button>
      </Block>
    </Block>
  )
}

Custom Component Type

divs are not the only element supported. Any one of the following elements may be used by setting the compType property.

  • address
  • article
  • aside
  • blockquote
  • details
  • div
  • dl
  • fieldset
  • figcaption
  • figure
  • footer
  • form
  • header
  • main
  • nav
  • section
import React from 'react'

import Block from '@qc/react-block'

import '@qc/react-block/dist/styles/Block.css'


export default function SiteFooter(props) {
  return (
    <Block compType="footer">
      ...
    </Block>
  )
}

Just Using Block CSS

The key to the Block component is in the CSS — not the JavaScript. All that needs to be done is to include the Block CSS class in any component.

import React from 'react'

import '@qc/react-block/dist/styles/Block.css'


export default function SiteFooter(props) {
  return (
    <footer className="Block">
      ...
    </footer>
  )
}

Excluding replaced elements.

Use ES Modules

This package also comes with the source and an ES variation. Instead of

import Block from '@qc/react-block'

use

import Block from '@qc/react-block/es/Block'

or

import Block from '@qc/react-block/src/Block'

to import the component.

If you do this, then you will need to be sure to transpile the code to a syntax compatible with the browsers you plan to support.

The source is using object spread syntax. In order to transpile it with babel, you must include the object spread transform plugin.

Why Multiple BFC Methods?

Why are multiple BFC methods employed in the included CSS?

This is to help ensure the block is still given a BFC if in the event that the CSS is overridden in a way that would have removed the BFC. For instance, in browsers that support contain: layout or display: flow-root, the following will still have a BFC.

<Block style={{overflow: 'visible'}}>
  ...
</Block>

Other Packages from QC

Maintainers

License

ISC

Package Sidebar

Install

npm i @qc/react-block

Weekly Downloads

0

Version

0.2.2

License

ISC

Unpacked Size

12.7 kB

Total Files

7

Last publish

Collaborators

  • qc