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

react-style-utils

v0.0.1

Published

react-style-utils

Downloads

6

Readme

React Style Utils

面向跨端提供相同的样式特性供给算法。

  1. 不生成具体的样式或 className ,而是生成中间数据。
  2. 针对 React 运行机制设计,所有的特性构成方法,皆区分:属性初始化解析提取 两个入口。

属性初始化 :如,通过 flex 函数帮助快速建立 flex 特性的声明

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 函数返回的数据进行样式提取:

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

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