# styles-merge

> A JavaScript utility for merging style objects together

Latest version **1.2.1** (published 2018-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install styles-merge
pnpm add styles-merge
yarn add styles-merge
bun add styles-merge
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2018-05-01 |
| First published | 2018-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 152.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Srishan Bhattarai |
| Maintainers | srishanbhattarai |
| Keywords | react, react-native, utility, javsacript, styles, stylesmerge |

## Links

- npm: https://www.npmjs.com/package/styles-merge
- Homepage: https://github.com/srishanbhattarai/styles-merge
- Issues: https://github.com/srishanbhattarai/styles-merge/issues
- npm.io page: https://npm.io/package/styles-merge

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2018-05-01
- 1.2.0 — 2018-04-27
- 1.1.0 — 2018-04-27
- 1.0.0 — 2018-04-27

## README

# Stylesmerge &middot; [![npm](https://img.shields.io/npm/v/styles-merge.svg)](https://www.npmjs.com/package/styles-merge) [![Build Status](https://travis-ci.org/srishanbhattarai/styles-merge.svg?branch=master)](https://travis-ci.org/srishanbhattarai/styles-merge) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

A declarative JavaScript utility to merge style objects together - developed primarily for React Native styles.

## Installation

Install with `npm`:

```bash
npm install styles-merge --save
```

Or, if you prefer `yarn`:

```bash
yarn add styles-merge
```

After installation, you can import the function as follows:

```js
import stylesmerge from 'styles-merge'
```

## Usage

The `stylesmerge` function can be used in two primary ways, conditionally and non-conditionally. In either case, you get the
final 'merged' object by calling `.eval()`

Conditional usages leverage `.ifThen()` and `.ifThenElse()`:

```js
import stylesmerge from 'styles-merge'


const styles = stylesmerge()
  .ifThen(noOfObjects < 10, { flexDirection: 'row' })
  .ifThenElse(isDiscountSeason, { backgroundColor: 'red' }, { backgroundColor: 'green' })
  .eval()
```

Pass an initial set of arguments to `stylesmerge()` for non-conditional usage.

```js
import stylesmerge from 'styles-merge'


const styles = stylesmerge(styles.containerStyle, styles.fontStyle)
```

Furthermore, you can mix and match both the conditional and non-conditional patterns.

## React Native Usage

Here's a moderately "real world" dumb component that utilizes this library:

```js
import React from 'react'
import PropTypes from 'prop-types'
import { View } from 'react-native'
import stylesmerge from 'styles-merge'

/**
 * Container is a generic element to wrap children
 * in a flex View.
 *
 * @returns {jsx}
 */
const Container = ({ children, centerChildren, flex, style, ...props }) => {
  const styles = stylesmerge({ flex })
    .ifThen(style !== undefined, style)
    .ifThen(centerChildren === true, { alignItems: 'center', justifyContent: 'center' })
    .eval()

  return <View style={styles}>{children}</View>
}

Container.propTypes = {
  children: PropTypes.element.isRequired,
  centerChildren: PropTypes.bool,
  style: View.propTypes.style,
  flex: PropTypes.number
}

Container.defaultProps = {
  flex: 1,
  centerChildren: false
}

export default Container
```

## License

MIT

---
_Source: https://npm.io/package/styles-merge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
