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 🙏

© 2026 – Pkg Stats / Ryan Hefner

k2-react-utils

v0.15.6

Published

A collection of handy React/JavaScript utilities

Downloads

251

Readme

k2-react-utils

npm npm npm GitHub issues GitHub pull requests Build Status Coverage Status GitHub GitHub stars GitHub stars GitHub stars

A collection of standalone ES6 ReactJS utilities and hooks, written in TypeScript and transpiled to and bundled in ES5.

Table of Contents

Setup

This ES5 module is distributed via npm and should be installed as a production dependency.

Using yarn (preferred)

yarn add -E k2-react-utils

or via npm

npm i -S -E k2-react-utils

peerDependencies

Note: react@^16.8.0 would be required when using hook utils.

optionalDependencies

Type definitions come bundled in.

Documentation

k2-react-utils barrels (re-exports) the following utils and hooks as named exports:

<Content/> - source

A component that performs HTML sanitization.

Dependencies: react, react-dom

| Props | Type | Description | | ----- | ----------------- | ------------------------------------------- | | text | string (required) | Returns a DOM node with a <span/> wrapper |

Usage

import * as React from "react"; // standard TypeScript syntax
import { Content } from "k2-react-utils";

const Post: React.FunctionComponent<{}> = () => {
  const stringifiedMarkup: string =
    "<p>A Lion from Lannisport, and the sheep from the North</p>";

  return <Content text={stringifiedMarkup} />; // returns a DOM node
};

classify - source

A function that takes in an object of classes along with conditionals, and returns a string of classnames

Dependencies: none

| Parameters | Type | Description | | ----------- | ----------------- | ----------- | | classObject | Object (required) | - |

Usage

import * as React from "react"; // standard TypeScript syntax
import { classify } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  const classNames = classify({
    "js-active": true,
    "js-focus": false
  }); // returns "js-active"

  return <div className={classNames} />;
};

Alternatives

  • This util would be helpful when to working imperively with CSS classes, example when authoring libraries. styled-components is a much preferred alternative when styling React components.

connect - source

A re-implementation of react-redux's connect which suppresses @types/react-redux issues when using connect as a decorator to connect class components in TypeScript 2/3.

Dependencies: react, react-dom, react-redux

See connect's parameters here: https://react-redux.js.org/api/connect#connect-parameters

Usage

import * as React from 'react'; // standard TypeScript syntax
import { connect } from 'k2-react-utils';
import { IAppState, ILocale } from './models';

interface IStateProps{
    locale: ILocale
}

@connect((state: IAppState)=>({
    locale: state.locale
}))
class MyComponent: React.Component<IStateProps>{
    render(){
        const {locale}=this.props;
        return <div>{locale.region}<div/>;
    }
}

Alternatives

  • With [email protected] comes the useSelector hook which with less effort connect to your redux store.
  • The @connect util would be ideal when working with stateful class components that would need to be connected to the store.

deviceService - source

A service that identifies the device's current browser, operating system or platform (desktop or mobile) via three getters; getBrowserName, getOperatingSystem, getPlatform.

Usage

import * as React from "react"; // standard TypeScript syntax
import {
  getBrowserName,
  getOperatingSystem,
  getPlatform
} from "k2-react-utils";

getBrowserName(); // sample, "chrome"
getOperatingSystem(); // sample, "windows"
getPlatform(); // sample, "desktop"

fontUnitConverter (convertPixelsToRem, convertPixelsToEm) - source

A utility that converts pixels to rem/em units.

Dependencies: none

| Paramters | Type | Description | | ------------ | ---------------------------------- | ----------------------------------------------------- | | pixelValue | string (required) | Value to be converted into em/rem unitls | | baseFontSize | string (optional) - default '16px' | root pixel value, set on the <html> or <body> tag |

Usage

// font-settings
import { convertPixelsToRem, convertPixelsToEm } from 'k2-react-utils';

// either
const fontSizes = {
    f16: convertPixelsToRem('16px', '10px');
    f20: convertPixelsToRem('20px', '10px');
};

// or for ems
const fontSizes = {
    f16: convertPixelsToEm('16px');
    f18: convertPixelsToEm('18px');
};

// Styled Component
import styled from 'styled-components';
import { fontSizes } from './fontSettings';

const MyComponent = styled`
    font-size: ${fontSizes.f16}; // DOM output => font-size: 1.6rem;
`;

hostEnv - source

A util that returns an object denoting the current hostname and a boolean value, isLocal.

Dependencies: none

| Paramters | Type | Description | | --------- | ------------------------------------------------- | ----------- | | host | string (optional), default window.location.host | - |

Usage

import * as React from "react"; // standard TypeScript syntax
import { hostEnv } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  React.useEffect(() => {
    window.fetch(getUrl());
  }, []);

  const getUrl = (): string => {
    if (hostEnv.isLocal) {
      return "http//dev.somesite.com/api/people";
    } else if (hostEnv.host === "uat1.somesite.com") {
      return "http//uat.somesite.com/api/people";
    }

    return "/api/people";
  };

  return <div />;
};

Alternatives

  • Consider using client-side environment variables configurable via an .env file.

convertXmlToJson - source

A function that converts xml into json.

Dependencies: xml-js

| Parameters | Type | Description | | ---------- | -------------- | ----------- | | xmlNode | xml (required) | - |

Usage

import { convertXmlToJson } from "k2-react-utils";

const xmlNode = `<xml>
    <title>Aerys</title>
</xml>`;

convertXmlToJson(xmlNode);

useBrowserStorage - source

A hook that performs getting, setting and clearing of values to localStorage and sessionStorage.

Dependencies: react, react-dom

| Parameters | Type | Description | | ----------- | -------------------------------------- | -------------------------------------------------------- | | storageType | 'LOCAL' | 'SESSION' (required) | context, point to localStorage, or sessionStorage | | key | string | number | boolean (required) | property name to used in either local or session storage |

Usage

import * as React from "react"; // standard TypeScript syntax
import { useEffect } from "react";
import { useBrowserStorage } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  const [isUndead, setIsUndead, clearIsUndead] = useBrowserStorage<boolean>(
    "SESSION",
    "isUndead"
  );

  useEffect(() => {
    setIsUndead(false);

    () => {
      clearIsUndead();
    };
  }, []);

  return <div>Xhoan Daxos is Undead: {isUndead}</div>;
};

useDevice - source

A hook that identifies the device's current browser, operating system or platform (desktop or mobile).

Dependencies: react

Usage

import * as React from "react"; // standard TypeScript syntax
import { useDevice } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  const { browserName, operatingSystem, platform } = useDevice();

  return (
    <table>
      <tbody>
        <tr>
          <td>Browser</td>
          <td>{browserName}</td> // sample, "chrome"
        </tr>
        <tr>
          <td>OS</td>
          <td>{operatingSystem}</td> // sample, "windows"
        </tr>
        <tr>
          <td>OS</td>
          <td>{platform}</td> // sample, "desktop"
        </tr>
      </tbody>
    </table>
  );
};

useScroll - source

A hook that returns the y-position (integer) on scroll.

Dependencies: react, react-dom

Usage

import * as React from "react"; // standard TypeScript syntax
import { useScroll } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  const { verticalScrollPosition } = useScroll();

  return <div>Vertical scroll position {verticalScrollPosition}</div>; // 0
};

useViewport - source

A hook that returns the current viewport's width, height and the document's scrollable height (as integers).

Dependencies: react, react-dom

Usage

import * as React from "react"; // standard TypeScript syntax
import { useViewport } from "k2-react-utils";

const MyComponent: React.FunctionComponent<{}> = () => {
  const { viewportWidth, viewportHeight, documentHeight } = useViewport();

  return (
    <div>
      width: {viewportWidth}, height: {viewportHeight}, documentHeight:{" "}
      {documentHeight},
    </div>
  );
};

Development

  • Run yarn on the root of the repository.
  • Run yarn start to start the project.
  • Run yarn test:watch to ammend tests.