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

style-direct-club

v0.0.10

Published

Apply styles directly to your React Native components with props!

Downloads

12

Readme

style-direct-club

Apply styles directly to your React Native components with props!

  • 🛡️ Fully type-safe
  • 🚀 Fast prototyping
  • 🎭 Custom aliases
  • 🎨 Default styles
  • ⚡️ Custom props

Installation

yarn add style-direct-club

Usage

Style Direct Club allows you to style your components directly with style props. The styled function takes in a React Native component and returns the same component but with the style props hoisted up from the style prop.

import { styled } from 'style-direct-club';
import { MyButton } from './MyButton';

// Create a styled component
const MyStyledButton = styled(MyButton);

// Or use the built-in components
const View = styled.View;
const Text = styled.Text;

// Use props to style your components directly
function App() {
  return (
    <View flex={1} rowGap={10} justifyContent="center" alignItems="center">
      <Text color="red" fontSize={24}>
        Hello World!
      </Text>
      <Text color="blue" fontSize={16}>
        Styling is so easy!
      </Text>
      <MyStyledButton backgroundColor="blue">
        <Text color="white">Press Me!</Text>
      </MyStyledButton>
    </View>
  );
}

Options

Options allow you to customize your styling experience. Options can be passed to the styled function as an optional second argument, or with the withOptions function (for built-in components).

Aliases

By default, the name of each style prop will match the name of the style attrubute. Custom aliases allow you to define alternative names for each style prop. This is useful if you want short and concise prop names. These are fully customizable, so you can agree upon standards with your team and stay consistent!

import { styled } from 'style-direct-club';
import { View } from 'react-native';

const StyledView = styled(View, {
  aliases: {
    // Each key is your custom prop name and each value is the original style prop name
    bg: 'backgroundColor',
    p: 'padding',
    m: 'margin',
    mt: 'marginTop',
  },
});

function App() {
  return (
    <StyledView bg="blue" p={10} m={10} mt={20}>
      <Text color="white">Hello World!</Text>
    </StyledView>
  );
}

Note: This library comes with a set of default aliases for the built-in components.

import {
  defaultTextStyleAliases,
  defaultViewStyleAliases,
} from 'style-direct-club';

Default Styles

Use default styles to set common styles for your components. These styles will be applied to every instance of the component and can be overridden later on with props.

import { styled } from 'style-direct-club';
import { TouchableOpacity } from 'react-native';

const StyledTouchableOpacity = styled(TouchableOpacity, {
  defaultStyles: {
    backgroundColor: 'blue',
    padding: 10,
    borderRadius: 8,
  },
});

function App() {
  return (
    <StyledTouchableOpacity padding={5}>
      <Text color="white">Press Me!</Text>
    </StyledTouchableOpacity>
  );
}

Custom Props

Custom props allow you to apply multiple reusable styles with a single prop.

import { styled } from 'style-direct-club';
import { Text } from 'react-native';

const StyledText = styled(Text, {
  customProps: {
    sm: {
      fontSize: 12,
      marginBottom: 5,
    },
    xl: {
      fontSize: 36,
      marginBottom: 10,
      fontWeight: 'bold',
    },
  },
});

function App() {
  return <StyledText xl>Hello World!</StyledText>;
}

Sharing Options

Often times you'll want to share options across multiple components. Since options rely heavily on the type of the component being styled, you won't be able to pass the same options to different components. For example, the style properties for a View component are different than the style properties for a Text component. To solve for this, you can create multiple options objects, and pass them to each styled component where appropriate.

import { styled, Options } from 'style-direct-club';
import { View, Text } from 'react-native';

const viewOptions = {
  aliases: {
    bg: 'backgroundColor',
    p: 'padding',
    m: 'margin',
    mt: 'marginTop',
  },
} as const; // Use `as const` to preserve the type of the object

const textOptions = {
  aliases: {
    size: 'fontSize',
    weight: 'fontWeight',
  },
  customProps: {
    sm: {
      fontSize: 12,
      marginBottom: 5,
    },
    xl: {
      fontSize: 36,
      marginBottom: 10,
      fontWeight: 'bold',
    },
  },
} as const;

// These components will use the text options
const StyledText = styled(Text, textOptions);
const StyledTextInput = styled(TextInput, textOptions);

// These components will use the view options
const StyledView = styled(View, viewOptions);
const StyledPressable = styled(Pressable, viewOptions);

As an alterntative to the as const approach, you can type the options object with the Options<T> type. For example,

import { styled, Options } from 'style-direct-club';
import { View } from 'react-native';

const viewOptions: Options<View> = {
  aliases: {
    bg: 'backgroundColor',
    p: 'padding',
    m: 'margin',
    mt: 'marginTop',
  },
};

Built-in Components

Built-in components are provided for convenience. These components are the same as the original components (from react-native), but they have style props added to them. Here's a list of the built-in components:

  • styled.View
  • styled.Text
  • styled.TouchableOpacity
  • styled.Pressable
  • styled.TextInput
  • styled.Image

You can also choose to pass in options to built-in components via the withOptions function. For example, if you want to use the built-in Text component with custom options, you can do the following:

import { styled } from 'style-direct-club';

const Text = styled.Text.withOptions(myOptions);

Gotchas

  • The style prop is still supported, but style props will take priority over the style prop object.
  • If you want to add style props to a custom component, the component must pass its style prop to the underlying view.
  • Default styles and custom props will not respect aliases, you must use the original style prop name.