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

palstek

v0.12.0

Published

A bunch of useful extensions for react projects

Downloads

184

Readme

______     _     _       _      
| ___ \   | |   | |     | |     
| |_/ /_ _| |___| |_ ___| | __  
|  __/ _` | / __| __/ _ \ |/ /  
| | | (_| | \__ \ ||  __/   < _ 
\_|  \__,_|_|___/\__\___|_|\_(_)
                                
                              
>> Palstek - a bunch of useful extensions for react projects.

Overview


Intro

Why "palstek"? - Because this is one of the most used knots on a sailboat. Just as useful, selected functions should be provided here. Especially designed as extension to functional react projects, react libraries and design systems.

It's designed as zero-dependency; no other packages do come with palstek. If you want to use all features (especially the hooks), you need to have react package as peerDependency installed.

Quick Start

  1. Install the package with npm i palstek into your project
  2. Use the functions. They support typescript.

Reference

utils

Utils contains a random collection of useful functions - often designed to bring in an own pattern into your react project.

resolveClassNames

You want to build reusable (libary-) components in react and are annoyed by merging your classnames with props.className(s)? This function is here to help. Use it in your components like:

    return <div className={resolveClassNames("my-component", props.className, someCondition && "modifier-class")}>{/*...*/}</div>

It will automatically filter out undefineds & falsy values. All valid strings are joined together to get valid multi-classNames. Also works with string arrays and css modules.

getRandomString

Get a string of random letters and numbers with a length, default is 5.

    const randomString = getRandomString(7); // will for example create '3udzdaf'

filterProps

This function is perfect for building atomic library components, where the vanilla props API should be preserved. Example:


type MyButtonProps = { appearance: "CTA" | "default" } & HTMLProps<HTMLButtonElement>;

export MyButton = (props: MyButtonProps) => (
    return <button {...filterProps(props, "appearance", "children")}>
        { props.appearance === "CTA" ? <b>{props.children}</b> : props.children }
    </button>
)

For a consumer of your library this is now still a full-featured HTML Button with access to it's complete props API. You only care about your feature additions.

autoFilterProps

This function works similar to the filterProps function but with one addition: To avoid big lists of custom props, you may want to use this pattern: all custom props begin with an underscore. Example:


type MyButtonProps = { _appearance: "CTA" | "default" } & HTMLProps<HTMLButtonElement>;

export MyButton = (props: MyButtonProps) => (
    return <button {...filterProps(props, "children")}>
        { props._appearance === "CTA" ? <b>{props.children}</b> : props.children }
    </button>
)

The autoFilterProps will automatically remove all members of the props object starting with an underscore. It also filters out following standard props (coming for example from react router):

  • history
  • location
  • match

stringTransform

This function collection is useful to transform strings in several common targets, such as to "kebab-case", "camelCase" and "PascalCase". Example:


const myString = "some-kebab-string";

const newString = stringTransform.toCamelCase(myString); // newString will be "someKebabString"

All functions support special characters (from languages like German or French).

Both functions toCamelCase and toPascalCase replace the characters -, _, and whitespace by default, but you can pass your own Regexp as optional parameter to modify the function to also replace a . or a , for example.

setToSession

Lightweight wrapper for sessionStorage.setItem(), will perform a JSON.stringify() for the value. Might get upgraded with custom event in future.

getFromSession

Lightweight wrapper for sessionStorage.getItem(), will perform a JSON.parse() for the value.

setToLocal

Lightweight wrapper for localStorage.setItem(), will perform a JSON.stringify() for the value. Might get upgraded with custom event in future.

getFromLocal

Lightweight wrapper for localStorage.getItem(), will perform a JSON.parse() for the value.


static

reset.css

This lightweight reset.css can be imported at first in every project to do some helpful normalizings to your project's styling.

hooks

usePrevious

Get access to a state's previous value. This can be helpful to implement a simple undo or to compare old and new values.

Example:

// inside a React Component

const [count, setCount] = useState(0); 
const previousCount = usePrevious(count); // will always be the previous value, like a "history - 1"

useCssVar

Provide local values insider your component's code to CSS as CSS Variable (needs browser support).

Example:

const MyComponent = () => {
  const [val, setVal] = useCssVar("--my-font-size", "2rem");

  return (
    <div style={{ fontSize: "var(--my-font-size)" }}>
        The value: {val}
        <button onClick={() => setVal(`${parseInt(val) + 1}rem`)}>increase</button>
    </div>
  );
};

Parameters:

  1. Variable name (has to start with '--')
  2. Variable value
  3. optional, an HtmlElement Reference on which to set the variable (default document.body)

Returns (similar to useState):

  1. The value
  2. Setter for the value

useOutsideClickHandler

A useful hook to manage "outside" clicks of DOM Elements. Based on React Ref and Event.composedPath. This pattern is quite solid and efficient, but does not work as soon as the DOM hierarchy does not represent the visual hierarchy (because of position: absolute of sub-elements or similar).

Example:

const MyComponent = () => {
  const outsideClickHandler = () => {
    console.log("outside");
  }

  const [elementRef] = useOutsideClickHandler(outsideClickHandler);

  return (
    <div ref={elementRef}>
      Dialog / Modal / Popup / ...
    </div>
  );
};

Parameters:

  1. Your click handler. Will get triggered once an outside click is detected.
  2. A condition (boolean). If false, the listener(s) won't get registered and/or unregistered. Default true.
  3. An optional list of event types, in case you want to listen to additional / other events than "click".

Returns:

  1. elementRef: The ref of the element which has to be inside the composedPath of the click(?) event. It's just a passthrough of React.useRef and works identical. Default's null.

useLocalStorage

A simple helper hook to access localStorage. Works like a useState hook. Listens to "storage" Event on window object, in case another tab/window accesses the same storage area (based on URL/domain).


const initValue = 15;

const [value, setValue] = useLocalStorage("myKey", initValue);

// will store '15' at key 'myKey' in localStorage

useSessionStorage

A simple helper hook to access sessionStorage. Works like a useState hook.


const initValue = 15;

const [value, setValue] = useSessionStorage("myKey", initValue);

// will store '15' at key 'myKey' in sessionStorage

Future Plans

  • Include the possibility to only install single functions instead of the whole package, like npm i palstek/resolveClassNames.

Contribution

If you want to contribute, feel free to do so. Just create your own branch and submit a PR with the changes.

Issues & Help

(...) palstek is not a module (ts2306)

There are inconsistencies when importing typings from node modules. This error should not occur if you use moduleResolution: node inside your tsconfig.

Credits

Credits go to:

  • https://www.joshwcomeau.com/css/custom-css-reset/ and https://piccalil.li/blog/a-modern-css-reset/ for inspiration on reset.css

Made with ❤ by @cmmdmx