1.3.1 • Published 6 years ago

react-dimensions v1.3.1

Weekly downloads
22,227
License
MIT
Repository
github
Last release
6 years ago

react-dimensions

React higher-order component to get the dimensions of a wrapper element and pass them as properties to the child element.

v^1.0.0 is for React v0.14 and above. Use ^0.1.0 for React v0.13

Why? How?

Some React components require a width to be set in pixels, and cannot be set to 100%. This is a challenge for responsive design. This component creates a wrapper <div> and sets the width and height to 100%, and then passes the dimensions of this div to your component.

Installation

Requires nodejs.

$ npm install react-dimensions

API

Dimensions(options)(MyComponent)

Wraps a react component and adds properties containerHeight and containerWidth. Useful for responsive design. Properties update on window resize. Note that the parent element must have either a height or a width, or nothing will be rendered

Can be used as a higher-order component or as an ES7 class decorator (see examples)

Parameters

  • options object=
    • options.getHeight function= A function that is passed an element and returns element height, where element is the wrapper div. Defaults to (element) => element.clientHeight
    • options.getWidth function= A function that is passed an element and returns element width, where element is the wrapper div. Defaults to (element) => element.clientWidth
    • options.containerStyle object= A style object for the <div> that will wrap your component. The dimensions of this div are what are passed as props to your component. The default style is { width: '100%', height: '100%', padding: 0, border: 0 } which will cause the div to fill its parent in most cases. If you are using a flexbox layout you will want to change this default style.
    • options.className string= Control the class name set on the wrapper <div>
    • options.elementResize boolean= Set true to watch the wrapper div for changes in size which are not a result of window resizing - e.g. changes to the flexbox and other layout. (optional, default false)

Examples

// ES2015
import React from 'react'
import Dimensions from 'react-dimensions'

class MyComponent extends React.Component {
  render() (
    <div
      containerWidth={this.props.containerWidth}
      containerHeight={this.props.containerHeight}
    >
    </div>
  )
}

export default Dimensions()(MyComponent) // Enhanced component
// ES5
var React = require('react')
var Dimensions = require('react-dimensions')

var MyComponent = React.createClass({
  render: function() {(
    <div
      containerWidth={this.props.containerWidth}
      containerHeight={this.props.containerHeight}
    >
    </div>
  )}
}

module.exports = Dimensions()(MyComponent) // Enhanced component

Returns function A higher-order component that can be used to enhance a react component Dimensions()(MyComponent)

getWrappedInstance()

Returns the underlying wrapped component instance. Useful if you need to access a method or property of the component passed to react-dimensions. Does not currently work for stateless function components see #30

Returns object The wrapped React component instance

Live Example

Will open a browser window for localhost:9966

npm i && npm i react react-dom && npm start

Tests

$ npm test
higlass-galaxycustom-ory-editor-core@adminide-stack/marketplace-module-browser@marty-editor/corecra-template-ifunpluscra-template-antd-ifunplus@cybertec/ory-editor-core@cybertec/react-page-core@infinitebrahmanuniverse/nolb-react-direact-horizontal-timeline-demo@everything-registry/sub-chunk-2551insights-coreinsights-webinsights-clienthiglass-itgloo-jaeger-uihiglass-no-github-depsfastmodel-layout-componentsexpressa-adminlearning_lockerkokolibmarijamicrosite-uiliwory-editor-coreory-editor-core-customolymp-uireact-horizontal-catalog-stepperreact-horizontal-timeline-tooltipreact-horizontal-timelinereact-horizontal-timeline-fixed3react-horizontal-timeline-no-uglifyreact-horizontal-timeline-no-uglify-versionreact-propersearchreact-propersearch-fieldreact-page-nm-corereact-styled-horizontal-timelinereact-sexy-tablepropersearchpropertablesws-frontendreact-backgroundsreact-fast-uitroposphere-uitest-pushlish-npm@touched/react-three-gba@splish-me/ory-editor-core@sandboxerdna/react-page-core@zalastax/nolb-react-di@tommy2gis/react-horizontal-timeline@beisen/fast-table@tidepool/viz@teamshirts/corealjesusg_jaeger-ui@workbench-stack/marketplace-module-browser@workbench-stack/preferences-module-browser@workbench-stack/extension-app-module-browser@workbench-stack/extension-mgmt-module-browser@workbench-stack/extension-module-browser@ag-components-react/filter-table@fernandobandeira/ory-editor-core@files-stack/client-react@files-stack/preferences-module-browser@files-stack/release-module-browser@favish/ory-editor-core@exadoctor/viz@eventgenius/ui-libcatreact-html@hackoregon/ui-maps@hackoregon/component-librarycs-react-horizontal-timeline@nhuthuy96/react-horizontal-timeline
2.0.0-alpha2

6 years ago

1.3.1

6 years ago

2.0.0-alpha1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

1.0.0

8 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago