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

styled-rn

v1.0.7

Published

Styled React Native Components

Downloads

290

Readme

GitHub license Npm package total downloads GitHub forks GitHub stars

TypeScript

💄 Styled-RN

Styled Components for React Native the way they should have been.

Inspired by this article


Intro

Why 💄 styled-rn it better than 💅🏼 styled-components/native ?

Well, first of all, using styled components should be fun, but using css to style a React Native app is cumbersome and often ends up to be very messy.

Also:

  • styled-rn gives you access to ALL React Native style props
  • styled-rn is faster because it does not do tedious string template processing
  • styled-rn is easier to use (again, no messy string templates)
  • styled-rn is fully typed and has a nice API
  • styled-rn supports custom props, theme via ThemeProvider, multiple style objects and more..
  • styled-rn has a shorter name ;)

Installing:

npm i styled-rn

Dependencies

Styled-RN depends on react-native-safe-area-context to provide insets for the ctx prop. See below for more details.

npm i react-native-safe-area-context

Usage:

import { styled } from "styled-rn";

// Basic usage
export const Container = styled.View({
  flexDirection: "row",
  backgroundColor: "cyan",
});

// Use with any component
export const CoolAndBoldComponent = styled(CoolComponent, {
  fontWeight: "bold",
});

// Pass props to the styled component (attrs)
export const NonWrappingText = styled.Text(
  {
    color: "blue",
  },
  {
    attrs: {
      numberOfLines: 1,
      ellipsizeMode: "tail",
    },
  }
);

Theming:

You will need to do a few things in order to propagate the theme prop into all of your styled components:

  1. Define your theme object and the type for it
  2. Augment the Theme type
  3. Wrap your app in ThemeProvider

First, define your theme:

// mytheme.ts

export const theme = {
    primary: "blue",
    background: "white",
    ...
} as const;

export type MyAppTheme = typeof theme;

TODO: add example on how to use multiple themes

Now you need to augment the default Theme type with your own type. In order to do that, create a definitions file at the root of your source tree (e.g. global.d.ts) and add the following to it:

import { MyAppTheme } from "./mytheme.ts";

declare module "styled-rn" {
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
  interface Theme extends MyAppTheme {}
}

And finally, the wrapping, as usual:

// App.tsx

import { ThemeProvider } from "styled-rn";
import { theme } from "./mytheme.ts";

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      {/* your app components */}
    </ThemeProvider>;
  )
}

And that's it! You can now access your theme in any styled component:

export const Button = styled.TouchableOpacity(({ theme }) => ({
  backgroundColor: theme.background,
}));

If you want to use custom props in your styled component, make sure that your custom props interface extends StyledProps. E.g.

// Important:         👇
interface ButtonProps extends StyledProps {
  disabled?: boolean;
}

export const Button = styled.TouchableOpacity(({ disabled, theme }: ButtonProps) => ({
  opacity: disabled ? 0.3 : 1,
  backgroundColor: theme.background,
}));

// Using conditional styles with custom props
interface SmartComponentProps extends StyledProps {
  active?: boolean;
}
export const SmartComponent = styled.TouchableOpacity(({ active, theme }: SmartComponentProps) => [
  {
    fontSize: 16,
  },
  active && {
    fontWeight: theme.activeFontWeight,
    color: theme.activeColor,
  },
]);

You can also use theme in your components by calling the useTheme() hook or by wrapping your component with withTheme() HOC.

import { useTheme } from "styled-rn";

export const MyComponent = () => {
  const theme = useTheme();
};

Context

There is also a special ctx prop which you access easily from your styled component. It contains insets and window objects, which are the values returned by useSafeAreaInsets() and useWindowDimensions() respectively.

For example:

const TopBackButtonContainer = styled.TouchableOpacity(
  ({ theme, ctx }) =>
    ({
      color: theme.primary,
      position: "absolute",
      top: ctx.insets.top || theme.spacing[1],
      left: 20,
      zIndex: 10,
    })
);

Known Issues

  • When component returns the style from inline function sometimes the style object has to be type casted to ViewStyle, TextStyle, etc... Otherwise, you'll get a weird TS error. It looks like a TS bug. If you don't want to cast your styles, make sure that you pass props to the function and USE THE PROPS in your styles.

Contributing

Issues and Pull Requests are always welcome.