0.1.26 • Published 5 months ago

native-variants v0.1.26

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Simplify and streamline style management in React Native with TypeScript support

Install native-variants using npm or yarn:

npm install native-variants
//or
yarn add native-variants

native-variants provides a powerful utility to define, organize, and apply component styling for React Native. It supports multiple slots, variants, default settings, and compound variants, enabling a clean and reusable way to manage styles.

Here’s a quick example of how to use native-variants to style a button component.

import { nv, type VariantProps } from "native-variants";

const buttonVariants = nv({
  slots: ["root", "text"], // Define slots for styling
  base: {
    root: { paddingHorizontal: 16, paddingVertical: 12 }, // Base styles for root
    text: { color: "white", textAlign: "center" }, // Base styles for text
  },
  variants: {
    variant: {
      solid: {
        root: { backgroundColor: "#ff0006" },
        text: { color: "white" },
      },
      ghost: {
        root: { backgroundColor: "transparent" },
        text: { color: "#ff0006" },
      },
    },
  },
  defaultVariants: {
    variant: "solid", // Default variant configuration
  },
  compoundVariants: [
    {
      variant: "ghost",
      css: {
        root: { borderWidth: 1, borderColor: "#fff006" },
      },
    },
  ],
});

Create a styled Button component:

import React from "react";
import { Text, TouchableOpacity } from "react-native";

export interface ButtonProps
  extends React.ComponentPropsWithoutRef<typeof TouchableOpacity>,
    VariantProps<typeof buttonVariants> {}

export const Button = React.forwardRef<
  React.ComponentRef<typeof TouchableOpacity>,
  ButtonProps
>(({ children, variant, ...props }, ref) => {
  const { root, text } = buttonVariants({ variant });

  return (
    <TouchableOpacity {...props} ref={ref} style={root}>
      <Text style={text}>{children}</Text>
    </TouchableOpacity>
  );
});

Button.displayName = "Button";
import { Button } from "./Button";

export default function App() {
  return (
    <>
      <Button variant="solid">Solid Button</Button>
      <Button variant="ghost">Ghost Button</Button>
    </>
  );
}
  1. Multi-Slot Styling: Define styles for multiple slots (e.g., root, text).
  2. Variant Management: Easily handle variations like solid or ghost.
  3. Default Variants: Define fallback styles for missing configurations.
  4. Compound Variants: Apply conditional styles based on combined properties.

Feel free to contribute by submitting issues or pull requests. For questions, reach out to the maintainer:

Email: matheussdev3@gmail.com Maintainer: matheussatoshi

This library is licensed under the MIT License.

0.1.21

7 months ago

0.1.22

7 months ago

0.1.23

6 months ago

0.1.24

5 months ago

0.1.25

5 months ago

0.1.26

5 months ago

0.1.16

7 months ago

0.1.17

7 months ago

0.1.18

7 months ago

0.1.19

7 months ago

0.1.14

9 months ago

0.1.11

9 months ago

0.1.12

9 months ago

0.1.13

9 months ago

0.1.10

9 months ago

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.2

9 months ago

0.1.3

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago