npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bpk-react-utils

v7.1.0

Published

Utilities for Backpack's React components.

Downloads

7,641

Readme

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
}