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-template-tags

v0.0.4

Published

A collection of utility components for React.

Downloads

2

Readme

Installation

You can install this package using npm:

npm install react-template-tags

Template Tags

Template tags are reusable components in React that simplify common patterns like conditional rendering, iteration, and more. They provide a declarative way to express logic in JSX, improving code readability and maintainability.

Each Component

The Each component iterates over an array and renders each item using the provided render function.

import { Each } from 'react-templates-tags';

<Each of={[1, 2, 3]} render={(item, index) => <p key={index}>{item}</p>} />;

If Component

The If component renders its children only if the condition is true.

import { If } from 'react-templates-tags';

<If condition={true}>
  <p>This will be rendered if the condition is true.</p>
</If>;

IfElse Component

The IfElse component renders either children or elseChildren based on the condition.

import { IfElse } from 'react-templates-tags';

<IfElse condition={true}>
  <p>This will be rendered if the condition is true.</p>
  <p>Otherwise, this will be rendered.</p>
</IfElse>;

Show Component

The Show component renders the first child with isTrue set to true, or the last child if none match.

import { Show } from 'react-templates-tags';

<Show>
  <Show.When isTrue={true}>
    <p>This will be rendered if isTrue is true.</p>
  </Show.When>
  <Show.Else>
    <p>This will be rendered if isTrue is false.</p>
  </Show.Else>
</Show>;

Switch Component

The Switch component renders the case corresponding to the provided value or the default case if specified.

import { Switch } from 'react-templates-tags';

<Switch
  value={'two'}
  cases={{ one: <p>Case One</p>, two: <p>Case Two</p> }}
  defaultCase={<p>Default Case</p>}
/>;

While Component

The While component renders its children as long as the condition is true.

import { While } from 'react-templates-tags';

<While condition={true}>
  <p>This will be rendered as long as the condition is true.</p>
</While>;

Utility Functions

This package contains a collection of utility functions for common tasks in JavaScript/TypeScript development.

Usage

import {
  classNames,
  stripTags,
  isClient,
  isEmpty,
  toCamelCase,
  randomIntFromInterval,
  uid,
} from 'react-templates-tags';

// Usage examples
const combinedClassNames = classNames('button', 'primary', { active: isActive });
const cleanString = stripTags('<p>Hello, <b>world</b>!</p>');
const isBrowser = isClient;
const emptyCheck = isEmpty([]);
const camelCaseString = toCamelCase('hello world');
const randomNum = randomIntFromInterval(1, 10);
const uniqueId = uid(8);

classNames(...args: (string | undefined | null)[]): string

Concatenates multiple class names into a single string, filtering out any falsy values and removing extra spaces.

  • args: Class names to concatenate.
  • Returns: Concatenated string of class names.

Example

<div className={combinedClassNames}>...</div>

or

<div className={classNames('button', 'primary', { active: isActive })}>...</div>

stripTags(str: string): string

Removes HTML tags from a given string.

  • str: The string from which to remove HTML tags.
  • Returns: The modified string with HTML tags removed.

isClient(): boolean

Checks if the current environment is a client-side environment.

  • Returns: True if the current environment is a client-side environment, false otherwise.

isEmpty(value: any): boolean

Checks if a given value is empty.

  • value: The value to check.
  • Returns: True if the value is empty, false otherwise.

toCamelCase(input: string): string

Transforms a string from a space-separated format to camel case.

  • input: The input string to be transformed.
  • Returns: The transformed string in camel case.

randomIntFromInterval(min: number = 1, max: number = 5): number

Generates a random integer between a given minimum and maximum value (inclusive).

  • min: The minimum value of the range (default is 1).
  • max: The maximum value of the range (default is 5).
  • Returns: A random integer between min and max.

uid(length: number = 5): string

Generates a unique identifier (UID) of a specified length.

  • length: The length of the UID. Default is 5.
  • Returns: The generated UID.