react-native-twc 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +23 -7
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { clsx } from "clsx";
3
3
  import React from "react";
4
4
  import { twMerge } from "tailwind-merge";
5
- import { jsxDEV } from "react/jsx-dev-runtime";
5
+ import { jsx } from "react/jsx-runtime";
6
6
  function filterProps(props, shouldForwardProp) {
7
7
  const filteredProps = {};
8
8
  const keys = Object.keys(props);
@@ -19,23 +19,39 @@ var createTwc = (config = {}) => {
19
19
  const defaultShouldForwardProp = config.shouldForwardProp || ((prop) => prop[0] !== "$");
20
20
  const wrap = (Component) => {
21
21
  const createTemplate = (attrs, shouldForwardProp = defaultShouldForwardProp) => {
22
+ const componentCache = new Map;
22
23
  const template = (stringsOrFn, ...values) => {
23
24
  const isClassFn = typeof stringsOrFn === "function";
24
- const tplClassName = !isClassFn && String.raw({ raw: stringsOrFn }, ...values);
25
+ const cacheKey = isClassFn ? stringsOrFn.toString() : String.raw({ raw: stringsOrFn }, ...values);
26
+ if (componentCache.has(cacheKey)) {
27
+ return componentCache.get(cacheKey);
28
+ }
29
+ const tplClassName = !isClassFn ? cacheKey : undefined;
30
+ const hasAttrs = attrs !== undefined;
31
+ const isAttrsFunction = typeof attrs === "function";
32
+ const staticAttrs = !isAttrsFunction && hasAttrs ? attrs : undefined;
25
33
  const ForwardedComponent = React.forwardRef((p, ref) => {
26
34
  const { className: classNameProp, ...rest } = p;
27
- const resolvedAttrs = typeof attrs === "function" ? attrs(p) : attrs ? attrs : {};
28
- const filteredProps = filterProps({ ...resolvedAttrs, ...rest }, shouldForwardProp);
35
+ let finalProps;
36
+ if (!hasAttrs) {
37
+ finalProps = filterProps(rest, shouldForwardProp);
38
+ } else if (isAttrsFunction) {
39
+ const resolvedAttrs = attrs(p);
40
+ finalProps = filterProps({ ...resolvedAttrs, ...rest }, shouldForwardProp);
41
+ } else {
42
+ finalProps = filterProps({ ...staticAttrs, ...rest }, shouldForwardProp);
43
+ }
29
44
  const baseClassName = isClassFn ? stringsOrFn(p) : tplClassName;
30
45
  const finalClassName = typeof baseClassName === "function" ? (renderProps) => compose(baseClassName(renderProps), typeof classNameProp === "function" ? classNameProp(renderProps) : classNameProp) : compose(baseClassName, classNameProp);
31
46
  const Comp = Component;
32
- return /* @__PURE__ */ jsxDEV(Comp, {
47
+ return /* @__PURE__ */ jsx(Comp, {
33
48
  ref,
34
49
  className: finalClassName,
35
- ...filteredProps
36
- }, undefined, false, undefined, this);
50
+ ...finalProps
51
+ });
37
52
  });
38
53
  ForwardedComponent.displayName = `twc(${Component.displayName || Component.name || "Component"})`;
54
+ componentCache.set(cacheKey, ForwardedComponent);
39
55
  return ForwardedComponent;
40
56
  };
41
57
  template.transientProps = (fnOrArray) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-twc",
3
- "version": "1.0.3",
3
+ "version": "1.1.1",
4
4
  "description": "Create reusable React Native + NativeWind components with Tailwind CSS syntax.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -17,10 +17,10 @@
17
17
  "scripts": {
18
18
  "test": "bun test",
19
19
  "build": "rm -rf dist && bun run build:js && bun run build:dts",
20
- "build:js": "bun build src/index.tsx --outdir dist --format esm --external react --external react-native --external clsx --external tailwind-merge",
20
+ "build:js": "NODE_ENV=production bun build src/index.tsx --outdir dist --format esm --external react --external react-native --external clsx --external tailwind-merge",
21
21
  "build:dts": "tsc -p tsconfig.build.json",
22
22
  "check-types": "tsc --noEmit",
23
- "prepublishOnly": "bun run build"
23
+ "prepublishOnly": "bun run build && bun test"
24
24
  },
25
25
  "keywords": [
26
26
  "react-native",