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

@waveplay/snazzy

v1.3.0

Published

Flexible storage for Node, React Native, and the browser

Downloads

125

Readme

GitHub license Tests npm minizipped size

Synthetic sugar for styling on React Native

Getting started

Install the package:

npm install @waveplay/snazzy

Instead of using StyleSheet.create(), use sheet() to create your styles:

import { sheet } from '@waveplay/snazzy'

const styles = sheet({
	container: {
		flex: 1,
		backgroundColor: 'white'
	}
})

// Alternatively, you can use the default export
import snazzy from '@waveplay/snazzy'

const styles = snazzy.sheet({
	// ...
})

You can now use destructuring to access your styles:

<View {...styles.container} />

See the sample project for more usage examples.

Individual styles

If you'd like to create individual styles, you can use css():

import { css } from '@waveplay/snazzy'

const containerStyle = css({
	flex: 1,
	backgroundColor: 'white'
})

... and use it like this:

<View {...containerStyle} />

This is useful for creating styles as functions:

import { css } from '@waveplay/snazzy'

const containerStyle = (color) =>
	css({
		flex: 1,
		backgroundColor: color
	})

... and use it like this:

<View {...containerStyle('white')} />

Merge styles

You can merge styles using the merge() function:

import { css, merge } from '@waveplay/snazzy'

const containerStyle = css({
	flex: 1,
	backgroundColor: 'white'
})

const textStyle = css({
	color: 'black'
})

const mergedStyle = merge(containerStyle, textStyle)

Raw styles

Sometimes you may need to use raw style objects. You can use cssRaw() instead of css() to create these:

import { cssRaw } from '@waveplay/snazzy'

const containerStyle = cssRaw({
	flex: 1,
	backgroundColor: 'white'
})

This is useful for keeping the same syntax but making it compatible with more elements such as HTML:

<div {...containerStyle} />

Conditional raw styles

Alternatively, you can pass a raw option to css(). The value can be a boolean, 'native', or 'web':

const containerStyle = css(
	{
		flex: 1,
		backgroundColor: 'white'
	},
	{
		raw: 'web' // This will be a raw style object on web, but a style object on native
	}
)

Custom backends

Snazzy is powered by React Native's StyleSheet API by default. If you'd like to use a different backend, you can create your own backend by implementing the SnazzyBackend interface:

import { SnazzyBackend } from '@waveplay/snazzy/backend'
import type { StyleProp } from 'react-native'
import type { RawSheet, SnazzyOptionsBackend, StyleType } from '@waveplay/snazzy/backend'

class CustomBackend implements SnazzyBackend {
	create<T extends Record<string, unknown>>(style: T, options: SnazzyOptionsBackend): RawSheet<T> {
		// ...
	}

	merge<T extends StyleType>(...styles: StyleProp<T>[]): StyleProp<T> {
		// ...
	}
}

You can then create a new instance of Snazzy with your custom backend:

import { Snazzy } from '@waveplay/snazzy/core'

const snazz = new Snazzy({
	backend: new CustomBackend()
})
export default snazz

Don't forget to also export individual function bindings to keep the same import syntax:

export const css: typeof snazz.css = snazz.css.bind(snazz)
export const cssRaw: typeof snazz.cssRaw = snazz.cssRaw.bind(snazz)
export const merge: typeof snazz.merge = snazz.merge.bind(snazz)
export const sheet: typeof snazz.sheet = snazz.sheet.bind(snazz)

Transformers

Transformers are functions that can modify the styles. You can create your own transformers by implementing the SnazzyTransformer interface and passing it to the transformers option in a new instance of Snazzy:

import { Snazzy } from '@waveplay/snazzy/core'
import { DefaultBackend } from '@waveplay/snazzy/backend/default'
import type { SnazzyOptions, StyleType } from '@waveplay/snazzy/backend'

// This transformer will change the background color of the style to pink if the id is 'title'
const exampleTransformer = <T extends StyleType>(style: T, options: SnazzyOptions) => {
	if (options?.id === 'title') {
		style.backgroundColor = 'pink'
	}

	return style
}

const snazz = new Snazzy({
	backend: new DefaultBackend(),
	transformers: [exampleTransformer]
})

Default instance

When you use import snazzy from '@waveplay/snazzy', you're importing the default instance of Snazzy. This instance uses StyleSheet as its backend.

This may be considered unnecessary overhead if you're creating new instances of Snazzy in your code instead of using the default instance. Import from @waveplay/snazzy/core instead to avoid this overhead.

import { Snazzy } from '@waveplay/snazzy/core'

const snazzy = new Snazzy({
	// ... your custom options
})

Credits

This project was originally developed for WavePlay.

License

The MIT License.