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

stylesheets-for-viewports

v1.0.1

Published

Lightweight react native lib for using different stylesheets based on current viewport (width, scale, etc.)

Downloads

23

Readme

stylesheets-for-viewports 📱🖥️

Version

Lightweight react native library for using different stylesheets based on current viewport (width, scale, etc.).

Demo

Live Demos

Check out the Live Demo here (Expo Snack).

Installation

If you use expo, use:

expo install stylesheets-for-viewports

Otherwise, for npm use:

npm i stylesheets-for-viewports --save

And for yarn use:

yarn add stylesheets-for-viewports

Usage

First, wrap your main app with the context provider, like so:

import {
  ViewportContextProvider,
  IViewportConfig,
} from "stylesheets-for-viewports";

// define your viewports via typescript:
export type MyViewports = "MOBILE" | "TABLET" | "DESKTOP";

// define your viewport config:
const myViewportConfig: IViewportConfig<MyViewports> = [
  { name: "MOBILE", lowestWidth: undefined },
  { name: "TABLET", lowestWidth: 768 },
  { name: "DESKTOP", lowestWidth: 1280 },
];

export default function App() {
  return (
    <ViewportContextProvider<MyViewports> viewports={myViewportConfig}>
      <MyApp />
    </ViewportContextProvider>
  );
}

After that, you can use the responsive stylesheets, like so:

import { Text, View } from "react-native";
import {
  IResponsiveStyles,
  useResponsiveStyles,
} from "stylesheets-for-viewports";
// also import MyViewports ;)

export const TestComponent = () => {
  const styles = useResponsiveStyles(responsiveStyles);
  return (
    <View style={styles.container}>
      <Text style={styles.text}>TestComponent</Text>
    </View>
  );
};

const responsiveStyles: IResponsiveStyles<MyViewports, "container" | "text"> = {
  DESKTOP: {
    container: {
      backgroundColor: "blue",
    },
    text: { fontSize: 14 },
  },
  MOBILE: {
    container: { backgroundColor: "lightblue" },
    text: { fontSize: 10 },
  },
  // if you don't want to provide a style for every single viewport,
  // you can alternatively also provide an 'OTHER' entry, which kicks in...
  // for all other viewports:
  OTHER: { container: { backgroundColor: "grey" }, text: { fontSize: 8 } },
};

Props

Besides the type parameter V, the ViewportContextProvider accepts 2 props:

| prop | required | type | description | | -------------------- | -------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | viewports | yes | IViewportConfig<V> | Define your viewports here, by providing the names and their lowest width value in pixels. Make sure the viewports are sorted in an ascending way, from lowest to highest width. The smallest viewport needs to have its lowestWidth set to undefined. Check the example above. | | getCurrentViewport | no | (windowDimensions: ScaledSize, viewports: IViewportConfig<V>) => V | This optional prop provides a way of replacing the built-in function, that determines which viewport is the current one. While the built-in version only checks the current screen width, with the custom version you can add more complexity to the logic (like considering the scale factor for instance, etc.) |

useResponsiveDimensions hook

The library also exports the internally used useResponsiveDimensions hook. Feel free to use it, it returns the following object:

{
  dimensions: ScaledSize; // <-- the current width, scale, etc.
  viewport: V; // <-- the current viewport
}

If you enjoy using this...