0.1.26 • Published 6 months ago

native-variants v0.1.26

Weekly downloads
-
License
MIT
Repository
-
Last release
6 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

8 months ago

0.1.22

8 months ago

0.1.23

7 months ago

0.1.24

6 months ago

0.1.25

6 months ago

0.1.26

6 months ago

0.1.16

8 months ago

0.1.17

8 months ago

0.1.18

8 months ago

0.1.19

8 months ago

0.1.14

10 months ago

0.1.11

10 months ago

0.1.12

10 months ago

0.1.13

10 months ago

0.1.10

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.2

10 months ago

0.1.3

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago