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

react-cool-utils

v1.0.0

Published

Helpful utility functions that you usually need in any react project.

Downloads

3

Readme

react-cool-utils

Helpful utility functions that you usually need in any react project.

classNames()

Creates string with unique css class names. Accepts non fixed amount of arguments. Possible values are: strings, arrays, objects, null and undefined. Function understands space separated classes in strings, inside arrays and in property names of objects.

import { ReactElement } from 'react';
import { classNames } from 'react-cool-utils';

function Example(): ReactElement {
  return (
    <div className={classNames(
      'static-class another-class',
      'one-more-class',
      {
        ['conditional-class']: condition()
      },
      cssClassesFromUtilityFunctionAsArray(),
    )}/>
  );
}

dataAttrs()

Creates an object with data attributes. Convert property names to lowercase kebab-case strings with data- prefix. All values will be converted to strings.

import { ReactElement, useMemo } from 'react';
import { dataAttrs } from 'react-cool-utils';

type ExampleProps = {
  id: number;
  kind: string;
};

function Example({ id, kind }: ExampleProps): ReactElement {
  const dataset = useMemo(() => dataAttrs({
    elementId: id,
    exampleKind: kind,
    'wrong-style_property': false,
  }), [id, kind]);
  
  /*
  dataset object will be:
  {
    'data-element-id': '123',
    'data-example-kind': 'test',
    'data-wrong-style-property': 'false',
  }
  */

  return (
    <div {...dataset}/>
  );
}

customStyle()

Just type safe function for creating CSSProperties object with custom css properties inside. So it's not needed anymore to use as type casting.

import { ReactElement, useMemo } from 'react';
import { customStyle } from 'react-cool-utils';

type ExampleProps = {
  columns: number;
};

function Example({ columns }: ExampleProps): ReactElement {
  const style = useMemo(() => customStyle({
    backgroundColor: 'red',
    '--example-columnsAmount': columns,
  }), [columns]);

  return (
    <div style={style}/>
  );
}

generateKey()

Receives as object and returns uuid string for it. Returns same value for same object on each call.

import { generateKey } from 'react-cool-utils';

const data = { someProperty: 'someValue' };
const uuid1 = generateKey(data);
const uuid2 = generateKey(data);

console.log(uuid1 === uuid2); // true

withReactKey()

This function adds a special property with a unique string to the passed object. If the passed object already contains this property, its value will be kept unchanged. For the case when a new object will be passed, but with a property and value generated earlier for another object, the function can work in two modes: new and keep.

  • keep (default): the property will be kept unchanged;
  • new: a new value will be generated for the property.

The property name is a private symbol, so this property will not be included in JSON when sending data over the network.

getReactKey()

Returns previously generated unique string from special property (created by withReactKey() function) or generates new key for passed object.

import { getReactKey, withReactKey } from 'react-cool-utils';

const dataWithKey = withReactKey({
  some: 'property',
});
const key = getReactKey(dataWithKey); // retunrns unique string

renderMany()

Function allows to render arrays and any other iterables as single React.Fragment. Additionally, instead of iterable object, may accept positive integer. It allows to render something specified amount of times.

  • may render custom fallback in case of empty iterable or 0 times;
  • for iterable of objects automatically provides keys;
With integer
import { renderMany } from 'react-cool-utils';

function Example(): ReactElement {
  // returns a Fragment with 5 span elements inside
  return renderMany(5, (_, index) => (
    <span>{index}</span>
  ));
}
With iterable
import { useMemo } from 'react';
import { renderMany } from 'react-cool-utils';

function Example(): ReactElement {
  const data = useMemo((): Set<object> => createExampleMap(), []);
  // returns a Fragment with generated span elements inside
  return renderMany(data, (element, index, key) => (
    // key is unique for each element
    <div key={key}>{index}</div>
  ));
}
With fallback
import { useMemo } from 'react';
import { renderMany } from 'react-cool-utils';

function Example(): ReactElement {
  // returns a Fragment with generated fallback
  return renderMany([], (element, index) => (
    <div key={key}>{index}</div>
  ), () => (
    <div>I'm fallback</div>
  ));
}