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-style-mods

v3.0.7

Published

React library for creating style modifiers props

Downloads

583

Readme

react-style-mods

Effortlessly manage conditional inline styles in your React components!

Introduction

react-style-mods is a lightweight utility that simplifies the process of applying conditional inline styles (mods) to your React components based on props.

Features

  • Simple Mod Definitions: Define your styles once and reuse them across components.
  • Conditional Styles: Apply styles dynamically based on boolean props or parameters.
  • TypeScript Support: Enjoy full type safety when defining and using mods.
  • Zero Dependencies: Minimal overhead with no external dependencies.

Installation

Install via npm or yarn:

npm install react-style-mods

or

yarn add react-style-mods

Quick Start

1. Define Your Mods

Use defineStyleMods to create a set of style modifications:

import { defineStyleMods } from "react-style-mods";

const mods = defineStyleMods({
    // Static mod
    primary: { backgroundColor: "blue", color: "white" },

    // Dynamic mod with a parameter
    size: (size: "small" | "large") => ({
        fontSize: size === "small" ? "12px" : "24px",
    }),

    margin: (margin: number = 10) => ({
        margin: `${margin}px`,
    }),
});

2. Wrap Your Component

Wrap your component with withStyleMods to apply the mods:

import React from "react";
import { withStyleMods } from "react-style-mods";

const Button = ({ style, children, ...props }) => (
    <button style={style} {...props}>
        {children}
    </button>
);

const StyledButton = withStyleMods(mods, Button);

3. Use the Component with Mods

Now, you can apply mods directly via props:

<StyledButton primary margin>Primary Button</StyledButton>
<StyledButton size="large" margin>Large Button</StyledButton>
<StyledButton disabled margin={30}>Disabled Button</StyledButton>

The styles will be applied conditionally based on the props you pass with minimum runtime overhead.

TypeScript Support

react-style-mods provides full TypeScript support, ensuring that your mods and components are type-safe.

Using satisfies (TypeScript >= 4.9)

If you're using TypeScript 4.9 or newer, you can use the satisfies operator for even better type inference:

const mods = {
    primary: { backgroundColor: "blue", color: "white" },
    size: (size: "small" | "large") => ({
        fontSize: size === "small" ? "12px" : "24px",
    }),
} satisfies StyleModsDefinition;

API Reference

defineStyleMods(mods)

Defines a set of style modifications.

  • mods: An object where each key is a mod name, and each value is either:
    • A style object (React.CSSProperties).
    • A function that returns a style object, optionally taking a parameter.

withStyleMods(mods, Component)

Wraps a component to enable style mods.

  • mods: The mods defined using defineStyleMods.
  • Component: The React component to wrap.

createModsStylesFromProps(props, mods)

Creates a style object based on the props and mods provided.

Examples

Conditional Styling

<StyledButton primary>Primary Button</StyledButton>

Applies the primary styles when the primary prop is truthy.

Dynamic Styling with Parameters

<StyledButton size="small">Small Button</StyledButton>

Applies styles based on the value of the size prop.

Combining Mods

<StyledButton primary size="large">
    Large Primary Button
</StyledButton>

Combines multiple mods for complex styling.

License

MIT License