2.2.0 • Published 4 years ago

@alexseitsinger/react-layouts v2.2.0

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
4 years ago

Layouts

Layout components to make constructing a page easier.

Installation

yarn add @alexseitsinger/react-layouts

Exports

LayoutProvider

Provides initial props to each layout component used throughout the app. Most of the props specified on the individual layout components will override the values provided by the provider.

Props
NameDescriptionDefaultRequired?
initialViewportHeightThie height to use before retrieving it from the DOM.0pxyes, if not specified on layout components
initialHeaderHeightInitial height to use for the header.0pxyes, if not specified on <HeaderLayout />
initialFooterHeightInitial height to use for the footer.0pxyes, if not specified on <FooterLayout />
sidebarWidthThe width to use for the sidebar.0pxyes, if not specified on <SidebarLayout />
onRenderHeaderInvoked to render the header.undefinedyes, if not specified on <HeaderLayout />
onRenderFooterInvoked to render the footer.undefinedyes, if not specified on <FooterLayout />
onRenderSidebarInvoked to render the sidebar.undefinedyes, if not specified on <SidebarLayout />
sidebarContainerStyleExtra css to apply.undefinedno
sidebarBodyStyleExtra css to apply.undefinedno
sidebarVoidStyleExtra css to apply.undefinedno
headerStyleExtra css to apply.undefinedno
footerStyleExtra css to apply.undefinedno

HeaderLayout

Renders a fixed header at the top of the page.

The header is initially rendered with a min-height matching the value of initialHeaderHeight. Then, once mounted, the header's actual height in the DOM is retrieved and set as height, then position: fixed gets set.

In the browser, all sizes are updated following each window resize.

Props
NameDescriptionDefaultRequired?
initialViewportHeightInitial height to use for the viewport.undefinedyes, if not specified on <LayoutProvider />
initialHeaderHeightThe height to use before retrieving it from the DOM.undefinedyes, if not specified on <LayoutProvider />
onRenderHeaderInvoked to render the inner body of the header.undefinedyes, if not specified on <LayoutProvider />
headerStyleAdditional css to apply to the rendered header body.undefinedno
childrenThe main page content to render below the header.undefinedyes

SidebarLayout

Container with space reserved for a fixed sidebar (on the right).

Props
NameDescriptionDefaultRequired?
initialViewportHeightInitial height to use for the viewport.undefinedyes, if not specified on <LayoutProvider />
sidebarWidthWidth of the sidebar.0pxyes, if not specified on <LayoutProvider />
sidebarContainerStyleExtra css to applyundefinedno
sidebarBodyStyleExtra css to applyundefinedno
sidebarVoidStyleExtra css to applyundefinedno
isMainStaticShould <main> element use height instead of min-heightfalseno
isFooterStaticShould <footer> element use height instead of min-heightfalseno
childrenThe content to render (to the left) of the sidebar.undefinedyes

FooterLayout

Includes a footer element below the main content. Footer height is removed from main element's possible height so it initially fits within the screen.

Props
NameDescriptionDefaultRequired?
initialFooterHeightIntitial height (px) to use for the footer before measuring and resizing in the DOM.undefinedyes, if not specified on <LayoutProvider />
onRenderFooterInvoked to render the footer.undefinedyes, if not specified on <LayoutProvider />
footerStyleExtra css to apply.undefinedno
isMainStaticShould <main> use height instead of min-height?falseno
isFooterStaticShould <footer> use height instead of min-height?falseno
childrenThe content to render (above) the footer, as the main page.undefinedyes

Main

A container with the remaining height (viewport - header) as either min-height or height if isStatic is true.

Props
NameDescriptionDefaultRequired?
isStaticShould <main> use height instead of min-height?falseno
childrenThe content to render within this element.nullyes

withLayout

HOC that supplies the wrapped component with the layout context props.

Props Provided
NameDescriptionOverwritable?
viewportHeightThe final measured height(px) of the viewport.no
headerHeightThe final measured height(px) of the header.no
footerHeightThe final measured height(px_ of the footer.no
mainHeightThe final measured height(px) of the main content (viewport - (header + footer)).no
initialHeaderHeightThe initial height(px) to use for the header before measuring.yes
initialFooterHeightThe initial height(px) to use for the footer before measuring.yes
onResizeFunction used to update <LayoutProvider /> from each layout component.no
onRenderHeaderFunction used by <HeaderLayout /> to render its content.yes
onRenderFooterFunction used by <FooterLayout /> to render its content.yes
onRenderSidebarFunction used by <SidebarLayout /> to render its content.yes
headerStyleExtra css to use.yes
footerStyleExtra css to use.yes
sidebarWidthThe width(px) to use.yes
sidebarContainerStyleExtra css to useyes
sidebarBodyStyleExtra css to use.yes
sidebarVoidStyleExtra css to use.yes

Viewport

Container with min-height set to the height of the browser window (or the initial value). If isStatic is true, will use height instead.

Example

// In app root
import { LayoutProvider } from "@alexseitsinger/react-layouts"

const App = () => (
  <LayoutProvider
    initialViewportHeight={"600px"}
    initialHeaderHeight={"30px"}
    initialFooterHeight={"10px"}
    sidebarWidth={"100px"}
    onRenderFooter={() => (
      <div>Footer here since it never changes</div>
      )}>
    <Router>
      <Route path={"/"} exact component={HomePage} />
      <Route path={"*"} exact component={NotFoundPage} />
    </Router>
  </LayoutProvider>
)
import { HeaderLayout, SidebarLayout, FooterLayout } from "@alexseitsinger/react-layouts"

function HomePage(props) {
  return (
    <HeaderLayout
      renderHeader={() => (
        <div>Header content</div>
      )}>
      <SidebarLayout
        onRenderSidebar={() => (
          <div>Sidebar content</div>
        )}>
        <FooterLayout>
          <div>This page content will include a footer below it</div>
        </FooterLayout>
      </SidebarLayout>
    </HeaderLayout>
  )
import { Viewport } from "@alexseitsinger/react-layouts"

function NotFoundPage(props) {
  return (
    <Viewport>
      Something that has the height of the browser.
    </Viewport>
  )
}