# react-style-utils

> react-style-utils

Latest version **0.0.1** (published 2022-03-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-style-utils
pnpm add react-style-utils
yarn add react-style-utils
bun add react-style-utils
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-03-25 |
| First published | 2022-03-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jiankai Zeng <janpoem@163.com> |
| Maintainers | janpoem |
| Keywords | react, app, app-provider |

## Links

- npm: https://www.npmjs.com/package/react-style-utils
- Repository: https://gitee.com/janpoem/react-app-provider
- npm.io page: https://npm.io/package/react-style-utils

## 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

- 0.0.1 (latest) — 2022-03-25

## README

# React Style Utils

面向跨端提供相同的样式特性供给算法。

1. 不生成具体的样式或 className ，而是生成中间数据。
2. 针对 React 运行机制设计，所有的特性构成方法，皆区分：`属性初始化` 和 `解析提取` 两个入口。

`属性初始化` ：如，通过 `flex` 函数帮助快速建立 flex 特性的声明

```tsx
const Test: React.FC = () => (
  <View flex={flex('col-nowrap', 'between')}>
    <View>cell 1</View>
    <View>cell 2</View>
  </View>
)
```

`flex` 函数，只将 flex 声明内容进行存储，不做任何过滤或转化，生成一个浅层的数据结构（单层）以满足浅比较的需要。React 机制，这个 Test 组件内的 props 或 state 变化时，都会触发 `flex` 函数被循环调用。

`解析提取` ：然后基于对 `flex` 函数返回的数据进行样式提取：

```ts
const useFlex = (props) => {
  const ref = useRef(null);

  useEffect(() => {
    if (ref.current == null || !shallowEqual(props, ref.current)) {
      ref.current = props;
      // 实际对 flex props 进行解析
      handleFlex(parseFlexProps(props));
      // h5 或 小程序，可以返回 className，不同的项目环境，className 也会不一样。
      // native 则是返回相应的 Stylesheet
    }
  }, [props]);
}
```

比如，针对 `windi-css` flexbox 生成相关的 className

```ts
const dirClasses = {
  [Flex.row]   : 'flex-row',
  [Flex.col]   : 'flex-col',
  [Flex.rowRev]: 'flex-row-reverse',
  [Flex.colRev]: 'flex-col-reverse',
};

const wrapClasses = {
  0             : undefined,
  [Flex.nowrap] : 'flex-nowrap',
  [Flex.wrap]   : 'flex-wrap',
  [Flex.wrapRev]: 'flex-wrap-reverse',
};

const justifyClass = ({ content, items, self }: CISParseResult): string => {
  return [
    content && `justify-${content}`,
    items && `justify-items-${items}`,
    self && `justify-self-${self}`,
  ].filter(Boolean).join(' ');
};

const alignClass = ({ content, items, self }: CISParseResult): string => {
  return [
    content && `content-${content}`,
    items && `items-${items}`,
    self && `self-${self}`,
  ].filter(Boolean).join(' ');
};

const windiFlexbox = (
  res: FlexParsed | undefined,
  isInlineFlex = false,
): string | undefined => {
  if (res == null) return undefined;
  return clsx(
    isInlineFlex ? 'inline-flex' : 'flex',
    dirClasses[res.dir] ?? null,
    wrapClasses[res.wrap] ?? null,
    justifyClass(res.justify),
    alignClass(res.align),
  );
};
```

### 已实现特性

- [x] flex
- [ ] flex cell - basis、grow、shrink、order
- [ ] position
- [ ] grid
- [ ] display - `visible` 、多行截取

优先实现参考：

- [yoga layout](https://yogalayout.com/)，RN 中，Flex 和 Position 是极其频繁使用的，写 Stylesheet.create 写到吐。

### 测试覆盖

```
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                  
 flex.ts  |     100 |      100 |     100 |     100 |                  
----------|---------|----------|---------|---------|-------------------
```

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