7.1.0 • Published 2 years ago

bpk-react-utils v7.1.0

Weekly downloads
620
License
Apache-2.0
Repository
github
Last release
2 years ago

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

PropertyPropTypeRequiredDefault Value
childrennodetrue-
isOpenbooltrue-
beforeClosefuncfalsenull
onClosefuncfalsenoop
onOpenfuncfalsenoop
onRenderfuncfalsenoop
renderTargetfuncfalsenull
targetoneOf(function, node)falsenull
closeOnEscPressedboolfalsetrue

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

PropertyPropTypeRequiredDefault Value
childrennodetrue-
appearClassNamestringtrue-
appearActiveClassNamestringtrue-
transitionTimeoutnumbertrue-

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
}
@infinitebrahmanuniverse/nolb-bp@everything-registry/sub-chunk-1259@zalastax/nolb-bpbpk-component-autosuggestbpk-component-badgebpk-component-banner-alertbpk-component-barchartbpk-component-blockquotebpk-component-cardbpk-component-carouselbpk-component-checkboxbpk-component-chipbpk-component-close-buttonbpk-component-codebpk-component-content-cardsbpk-component-breadcrumbbpk-component-breakpointbpk-component-calendarbpk-component-content-containerbpk-component-datatablebpk-component-datepickerbpk-component-description-listbpk-component-dialogbpk-component-drawerbpk-component-fieldsetbpk-component-flarebpk-component-floating-notificationbpk-component-navigation-stackbpk-component-nudgerbpk-component-overlaybpk-component-paginationbpk-component-panelbpk-component-paragraphbpk-component-inputbpk-component-labelbpk-component-linkbpk-component-listbpk-component-loading-buttonbpk-component-form-validationbpk-component-graphic-promotionbpk-component-gridbpk-component-grid-togglebpk-component-headingbpk-component-horizontal-navbpk-component-mapbpk-component-mobile-scroll-containerbpk-component-modalbpk-component-navigation-barbpk-component-selectbpk-component-skip-linkbpk-component-sliderbpk-component-spinnerbpk-component-split-inputbpk-component-star-ratingbpk-component-switchbpk-component-tablebpk-component-textbpk-component-textareabpk-component-theme-togglebpk-component-router-linkbpk-component-rtl-togglebpk-component-scrollable-calendarbpk-component-section-listbpk-component-iconbpk-component-imagebpk-component-infinite-scrollbpk-component-accordionbpk-component-aria-livebpk-component-phone-inputbpk-component-popoverbpk-component-pricebpk-component-progressbpk-component-radiobpk-component-ratingbpk-component-ticketbpk-component-tooltipbpk-scrim-utilsbpk-storybook-utils@olliecurtis/bpk-component-scrollable-calendar
7.0.1

2 years ago

7.1.0

2 years ago

5.0.0

2 years ago

7.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.2

2 years ago

4.1.0

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.1.3

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.13

3 years ago

3.0.12

3 years ago

3.0.11

3 years ago

3.0.10

3 years ago

3.0.9

3 years ago

3.0.8

3 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.2-css.0

4 years ago

3.0.1-css.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-css.0

4 years ago

2.9.4-css.0

4 years ago

2.9.4

4 years ago

2.9.3

4 years ago

2.9.3-css.0

4 years ago

2.9.2

4 years ago

2.9.1

4 years ago

2.9.0

4 years ago

2.9.0-css.0

4 years ago

2.8.4-css.0

4 years ago

2.8.4

4 years ago

2.8.3-css.0

5 years ago

2.8.3

5 years ago

2.8.2-css.0

5 years ago

2.8.2

5 years ago

2.8.0-css.0

5 years ago

2.8.1-alpha.39

5 years ago

2.8.0

5 years ago

2.7.10

5 years ago

2.7.10-alpha.40

5 years ago

2.7.9-alpha.76

5 years ago

2.7.9

5 years ago

2.7.9-alpha.72

5 years ago

2.7.9-alpha.53

5 years ago

2.7.9-alpha.11

5 years ago

2.7.8

5 years ago

2.7.7

5 years ago

2.7.6

5 years ago

2.7.5

5 years ago

2.7.4

5 years ago

2.7.3

5 years ago

2.7.2

5 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.13

6 years ago

2.6.12

6 years ago

2.6.11

6 years ago

2.6.10

6 years ago

2.6.9

6 years ago

2.6.8

6 years ago

2.6.7

6 years ago

2.6.6

6 years ago

2.6.5

6 years ago

2.6.4

6 years ago

2.6.3

6 years ago

2.6.2

6 years ago

2.6.1

6 years ago

2.6.0

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.8

6 years ago

2.3.7

7 years ago

2.3.6

7 years ago

2.3.5

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.6

7 years ago

1.4.5

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.0.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago