bpk-react-utils

7.1.0 • Public • Published

bpk-react-utils

Miscellaneous React based utilities for use in Backpack components.

Installation

npm install bpk-react-utils --save-dev

Portal.js

Render's children into a new component tree and appends it to document.body. Useful for Modals, Popovers, Tooltips etc where it's necessary in overcoming z-index issues when absolutely positioning elements.

Usage

import { Portal } from 'bpk-react-utils';
import BpkButton from 'bpk-component-button';
import { BpkCode } from 'bpk-component-code';
import React, { Component } from 'react';

class MyComponent extends Component {
  constructor() {
    super();

    this.state = {
      isOpen: false,
    };

  }

  onOpen = () => {
    this.setState({
      isOpen: true,
    });
  }

  onClose = () => {
    this.setState({
      isOpen: false,
    });
  }

  render() {
    return (
      <div>
        <BpkButton onClick={this.onOpen}>Open portal</BpkButton>
        <Portal
          isOpen={this.state.isOpen}
          onClose={this.onClose}
        >
          <div>I'm now appended to <BpkCode>document.body</BpkCode></div>
        </Portal>
      </div>
    );
  }
}

Props

Property PropType Required Default Value
children node true -
isOpen bool true -
beforeClose func false null
onClose func false noop
onOpen func false noop
onRender func false noop
renderTarget func false null
target oneOf([function, node]) false null
closeOnEscPressed bool false true

cssModules.js

A helpful utility which permits backwards compatibility with hard coded classes and CSS modules.

Usage

import React from 'react';
import { cssModules } from 'bpk-react-utils';

import STYLES from './MyComponent.scss';

const getClassName = cssModules(STYLES);

const MyComponent = (props) => (
  <div className={getClassName('MyComponent')}>
    <div className={getClassName('MyComponent__inner')}>
      {props.children}
    </div>
  </div>
);

With CSS modules:

<div class="_35WloynrPDta8fhSfoHEgE">
  <div class="_1ghNYY7jOYzUneVCT4piQ9">
    Some text.
  </div>
</div>

Without CSS modules:

<div class="MyComponent">
  <div class="MyComponent__inner">
    Some text.
  </div>
</div>

The returned function accepts multiple class names and ignores values other than strings. e.g:

import { cssModules } from 'bpk-react-utils';

import STYLES from './MyComponent.scss';

const getClassNames = cssModules(STYLES);

const MyComponent = (props) => (
  <div className={getClassName('MyComponent', props.disabled && 'MyComponent--disabled')}>
   {props.children}
  </div>
);

Will result in MyComponent MyComponent--disabled if props.disabled is true or MyComponent otherwise.

TransitionInitialMount.js

A wrapper around react-transition-group which makes it easy to transition a components initial mount. All you need to provide is two class names and a timeout.

Usage

import React from 'react';
import { TransitionInitialMount } from 'bpk-react-utils';

const MyComponent = (props) => (
  <TransitionInitialMount
    appearClassName="my-transition-class--appear"
    appearActiveClassName="my-transition-class--appear-active"
    transitionTimeout={300}
  >
    <div>Some text.</div>
  </TransitionInitialMount>
);
@import '~bpk-mixins/index.scss';

.my-transition-class {
  transition: opacity $bpk-duration-sm ease-in-out;
  opacity: 1;

  &--appear {
    opacity: 0;
  }

  &--appear-active {
    opacity: 1;
  }
}

Props

Property PropType Required Default Value
children node true -
appearClassName string true -
appearActiveClassName string true -
transitionTimeout number true -

isRTL

Returns true if the browser is showing content right-to-left.

Usage

import React from 'react';
import { isRTL } from 'bpk-react-utils';

if (isRTL()) {
  // do RTL stuff
} else {
  // do LTR stuff
}

isDeviceIphone

Returns true if the device is an iPhone.

Usage

import React from 'react';
import { isDeviceIphone } from 'bpk-react-utils';

if (isDeviceIphone()) {
  // do iPhone specific stuff
} else {
  // do other browser/device stuff
}

isDeviceIpad

Returns true if the device is an iPad.

Usage

import React from 'react';
import { isDeviceIpad } from 'bpk-react-utils';

if (isDeviceIpad()) {
  // do iPad specific stuff
} else {
  // do other browser/device stuff
}

isDeviceIos

Returns true if the platform is iOS (iPhone/iPad).

Usage

import React from 'react';
import { isDeviceIos } from 'bpk-react-utils';

if (isDeviceIos()) {
  // do iOS specific stuff
} else {
  // do other browser/device stuff
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.2-css.00css
2.8.1-alpha.390canary
7.1.040latest

Version History

VersionDownloads (Last 7 Days)Published
7.1.040
7.0.10
7.0.00
6.0.116
6.0.00
5.0.00
4.1.29
4.1.10
4.1.00
4.0.20
4.0.10
4.0.00
3.1.30
3.1.10
3.1.00
3.0.130
3.0.120
3.0.110
3.0.100
3.0.90
3.0.80
3.0.70
3.0.60
3.0.50
3.0.40
3.0.30
3.0.2-css.00
3.0.25
3.0.1-css.00
3.0.10
3.0.0-css.00
3.0.00
2.9.4-css.00
2.9.45
2.9.3-css.00
2.9.30
2.9.20
2.9.10
2.9.0-css.00
2.9.00
2.8.4-css.00
2.8.40
2.8.3-css.00
2.8.30
2.8.2-css.00
2.8.20
2.8.0-css.00
2.8.1-alpha.390
2.8.00
2.7.100
2.7.10-alpha.400
2.7.9-alpha.760
2.7.90
2.7.9-alpha.720
2.7.9-alpha.530
2.7.9-alpha.110
2.7.80
2.7.70
2.7.60
2.7.50
2.7.40
2.7.30
2.7.20
2.7.10
2.7.00
2.6.130
2.6.120
2.6.110
2.6.100
2.6.90
2.6.80
2.6.70
2.6.60
2.6.50
2.6.40
2.6.30
2.6.20
2.6.10
2.6.00
2.5.10
2.5.00
2.4.30
2.4.20
2.4.10
2.4.00
2.3.80
2.3.70
2.3.60
2.3.50
2.3.40
2.3.30
2.3.20
2.3.10
2.3.00
2.2.20
2.2.10
2.2.00
2.1.10
2.1.00
2.0.20
2.0.10
2.0.00
1.4.60
1.4.50
1.4.40
1.4.30
1.4.20
1.4.10
1.4.00
1.3.30
1.3.20
1.3.10
1.3.00
1.2.20
1.2.10
1.2.00
1.0.00
0.1.00
0.0.10

Package Sidebar

Install

npm i bpk-react-utils

Weekly Downloads

75

Version

7.1.0

License

Apache-2.0

Unpacked Size

71.9 kB

Total Files

23

Last publish

Collaborators

  • ojcurt
  • frugoman
  • gert-jan.vercauteren
  • skyscanner-koalasquad
  • anambl
  • mattface