@colorye/react-native-css 0.3.0

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.
@@ -0,0 +1,89 @@
1
+ import React from "react";
2
+ import type { StyleProp, ViewStyle, TextStyle, ImageStyle } from "react-native";
3
+
4
+ export interface StyleMapping {
5
+ className?: string;
6
+ contentContainerClassName?: string;
7
+ [key: string]: string | undefined;
8
+ }
9
+
10
+ export interface GroupState {
11
+ pressed: boolean;
12
+ hovered?: boolean;
13
+ focus?: boolean;
14
+ }
15
+
16
+ export const GroupContext: React.Context<GroupState>;
17
+ export const InheritContext: React.Context<Record<string, any> | undefined>;
18
+
19
+ export function setGlobalStylesheet(sheet: Record<string, any>): void;
20
+ export function getGlobalStylesheet(): Record<string, any>;
21
+
22
+ /**
23
+ * Wraps any React Native or third-party component to support className mapping.
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * import { FlashList } from "@shopify/flash-list";
28
+ * import { cssInterop } from "@colorye/react-native-css";
29
+ *
30
+ * const StyledFlashList = cssInterop(FlashList, {
31
+ * className: "style",
32
+ * contentContainerClassName: "contentContainerStyle",
33
+ * });
34
+ * ```
35
+ */
36
+ export function cssInterop<P extends object>(
37
+ Component: React.ComponentType<P>,
38
+ mapping?: StyleMapping
39
+ ): React.ForwardRefExoticComponent<
40
+ React.PropsWithoutRef<P> & {
41
+ className?: string;
42
+ contentContainerClassName?: string;
43
+ inheritStyle?: any;
44
+ } & React.RefAttributes<any>
45
+ >;
46
+
47
+ /**
48
+ * Alias for cssInterop.
49
+ */
50
+ export function remapProps<P extends object>(
51
+ Component: React.ComponentType<P>,
52
+ mapping: StyleMapping
53
+ ): React.ForwardRefExoticComponent<
54
+ React.PropsWithoutRef<P> & {
55
+ className?: string;
56
+ contentContainerClassName?: string;
57
+ inheritStyle?: any;
58
+ } & React.RefAttributes<any>
59
+ >;
60
+
61
+ export namespace Runtime {
62
+ export function getFlattenStyle(declarations: any): any;
63
+ export function getStyle(
64
+ stylesheet: any,
65
+ args: [inheritStyle?: any, className?: string, style?: any, elementName?: string]
66
+ ): any;
67
+ export function getInheritStyle(declarations: any): Record<string, any> | undefined;
68
+ export function mergeStyles(inheritStyle: any, staticStyles: any, inlineStyle: any): any;
69
+ export function clearCache(): void;
70
+ }
71
+
72
+ export function getStylesheet(css: string, filename?: string): string;
73
+ export function writeStylesheetJSON(content: string, filename?: string): void;
74
+ export function transform(args: { src: string; filename: string; options?: any }): any;
75
+
76
+ declare const _default: {
77
+ cssInterop: typeof cssInterop;
78
+ remapProps: typeof remapProps;
79
+ setGlobalStylesheet: typeof setGlobalStylesheet;
80
+ getGlobalStylesheet: typeof getGlobalStylesheet;
81
+ GroupContext: typeof GroupContext;
82
+ InheritContext: typeof InheritContext;
83
+ Runtime: typeof Runtime;
84
+ getStylesheet: typeof getStylesheet;
85
+ transform: typeof transform;
86
+ writeStylesheetJSON: typeof writeStylesheetJSON;
87
+ };
88
+
89
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "GroupContext", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _interop.GroupContext;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "InheritContext", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _interop.InheritContext;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "Runtime", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _transformerRuntime["default"];
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "cssInterop", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _interop.cssInterop;
28
+ }
29
+ });
30
+ exports["default"] = void 0;
31
+ Object.defineProperty(exports, "getGlobalStylesheet", {
32
+ enumerable: true,
33
+ get: function get() {
34
+ return _interop.getGlobalStylesheet;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "remapProps", {
38
+ enumerable: true,
39
+ get: function get() {
40
+ return _interop.remapProps;
41
+ }
42
+ });
43
+ Object.defineProperty(exports, "setGlobalStylesheet", {
44
+ enumerable: true,
45
+ get: function get() {
46
+ return _interop.setGlobalStylesheet;
47
+ }
48
+ });
49
+ var _interop = require("./interop.js");
50
+ var _transformerRuntime = _interopRequireDefault(require("./transformer-runtime.js"));
51
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
52
+ var _default = exports["default"] = {
53
+ cssInterop: _interop.cssInterop,
54
+ remapProps: _interop.remapProps,
55
+ setGlobalStylesheet: _interop.setGlobalStylesheet,
56
+ getGlobalStylesheet: _interop.getGlobalStylesheet,
57
+ GroupContext: _interop.GroupContext,
58
+ InheritContext: _interop.InheritContext,
59
+ Runtime: _transformerRuntime["default"]
60
+ };
@@ -0,0 +1,260 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.InheritContext = exports.GroupContext = void 0;
7
+ exports.cssInterop = cssInterop;
8
+ exports["default"] = void 0;
9
+ exports.getGlobalStylesheet = getGlobalStylesheet;
10
+ exports.remapProps = remapProps;
11
+ exports.setGlobalStylesheet = setGlobalStylesheet;
12
+ var _react = _interopRequireWildcard(require("react"));
13
+ var _transformerRuntime = _interopRequireDefault(require("./transformer-runtime.js"));
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
15
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
16
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
17
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
20
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
21
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
22
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
23
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
24
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
25
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
26
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
27
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
28
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
29
+ var InheritContext = exports.InheritContext = (0, _react.createContext)(undefined);
30
+ var GroupContext = exports.GroupContext = (0, _react.createContext)({
31
+ pressed: false,
32
+ hovered: false,
33
+ focus: false
34
+ });
35
+ var globalStylesheet = {};
36
+ try {
37
+ var raw = require("./exported-stylesheet.json");
38
+ var current = raw;
39
+ while ((_current = current) !== null && _current !== void 0 && _current["default"] && _typeof(current["default"]) === "object" && !current[":root"]) {
40
+ var _current;
41
+ current = current["default"];
42
+ }
43
+ globalStylesheet = current || {};
44
+ } catch (_unused) {
45
+ // Fallback to empty if not yet compiled
46
+ }
47
+ function setGlobalStylesheet(sheet) {
48
+ if (sheet && _typeof(sheet) === "object") {
49
+ var _Runtime$clearCache;
50
+ var _current2 = sheet;
51
+ while ((_current3 = _current2) !== null && _current3 !== void 0 && _current3["default"] && _typeof(_current2["default"]) === "object" && !_current2[":root"]) {
52
+ var _current3;
53
+ _current2 = _current2["default"];
54
+ }
55
+ globalStylesheet = _current2;
56
+ (_Runtime$clearCache = _transformerRuntime["default"].clearCache) === null || _Runtime$clearCache === void 0 || _Runtime$clearCache.call(_transformerRuntime["default"]);
57
+ }
58
+ }
59
+ function getGlobalStylesheet() {
60
+ return globalStylesheet;
61
+ }
62
+
63
+ /**
64
+ * cssInterop(Component, mapping)
65
+ *
66
+ * Wraps any React Native or third-party component to support className mapping.
67
+ *
68
+ * @example
69
+ * cssInterop(FlashList, {
70
+ * className: "style",
71
+ * contentContainerClassName: "contentContainerStyle",
72
+ * });
73
+ *
74
+ * @param {React.ComponentType} Component
75
+ * @param {Record<string, string>} mapping Mapping of class props to style props
76
+ * @returns {React.ForwardRefExoticComponent}
77
+ */
78
+ function cssInterop(Component) {
79
+ var mapping = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
80
+ className: "style"
81
+ };
82
+ var InteropComponent = (0, _react.forwardRef)(function (props, ref) {
83
+ var parentInherit = (0, _react.useContext)(InheritContext);
84
+ var parentGroup = (0, _react.useContext)(GroupContext);
85
+ var inheritStyle = props.inheritStyle || parentInherit;
86
+ var _React$useState = _react["default"].useState(false),
87
+ _React$useState2 = _slicedToArray(_React$useState, 2),
88
+ groupPressed = _React$useState2[0],
89
+ setGroupPressed = _React$useState2[1];
90
+ var nextProps = _objectSpread({}, props);
91
+ if (ref) {
92
+ nextProps.ref = ref;
93
+ }
94
+ var sheet = getGlobalStylesheet();
95
+ var currentInherit = parentInherit;
96
+ var isGroupProvider = false;
97
+
98
+ // Check if this component acts as a group container
99
+ for (var _i = 0, _Object$keys = Object.keys(mapping); _i < _Object$keys.length; _i++) {
100
+ var classProp = _Object$keys[_i];
101
+ var val = props[classProp];
102
+ if (typeof val === "string" && (val === "group" || val.split(/\s+/).includes("group"))) {
103
+ isGroupProvider = true;
104
+ break;
105
+ }
106
+ }
107
+ if (isGroupProvider) {
108
+ var originalOnPressIn = props.onPressIn;
109
+ var originalOnPressOut = props.onPressOut;
110
+ nextProps.onPressIn = function (e) {
111
+ setGroupPressed(true);
112
+ originalOnPressIn === null || originalOnPressIn === void 0 || originalOnPressIn(e);
113
+ };
114
+ nextProps.onPressOut = function (e) {
115
+ setGroupPressed(false);
116
+ originalOnPressOut === null || originalOnPressOut === void 0 || originalOnPressOut(e);
117
+ };
118
+ }
119
+ var _loop = function _loop() {
120
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
121
+ classProp = _Object$entries$_i[0],
122
+ styleProp = _Object$entries$_i[1];
123
+ var classValue = props[classProp];
124
+ var hasClass = typeof classValue === "string" && classValue.length > 0;
125
+ var isPrimaryStyle = styleProp === "style";
126
+ var hasInherit = isPrimaryStyle && Boolean(inheritStyle);
127
+ if (hasClass) {
128
+ var _props$accessibilityS;
129
+ var hasActiveVariants = /(^|\s)(active|pressed):/.test(classValue);
130
+ var hasDisabledVariants = /(^|\s)disabled:/.test(classValue);
131
+ var hasGroupActiveVariants = /(^|\s)group-(active|pressed):/.test(classValue);
132
+ var isDisabled = Boolean(props.disabled || ((_props$accessibilityS = props.accessibilityState) === null || _props$accessibilityS === void 0 ? void 0 : _props$accessibilityS.disabled));
133
+ if ((hasActiveVariants || hasDisabledVariants || hasGroupActiveVariants) && isPrimaryStyle) {
134
+ var classes = classValue.trim().split(/\s+/);
135
+ var normalClasses = [];
136
+ var activeClasses = [];
137
+ var disabledClasses = [];
138
+ var groupActiveClasses = [];
139
+ var _iterator = _createForOfIteratorHelper(classes),
140
+ _step;
141
+ try {
142
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
143
+ var cls = _step.value;
144
+ if (cls.startsWith("active:") || cls.startsWith("pressed:")) {
145
+ var baseCls = cls.replace(/^(active|pressed):/, "");
146
+ activeClasses.push(baseCls);
147
+ } else if (cls.startsWith("disabled:")) {
148
+ var _baseCls = cls.replace(/^disabled:/, "");
149
+ disabledClasses.push(_baseCls);
150
+ } else if (cls.startsWith("group-active:") || cls.startsWith("group-pressed:")) {
151
+ var _baseCls2 = cls.replace(/^group-(active|pressed):/, "");
152
+ groupActiveClasses.push(_baseCls2);
153
+ } else {
154
+ normalClasses.push(cls);
155
+ }
156
+ }
157
+ } catch (err) {
158
+ _iterator.e(err);
159
+ } finally {
160
+ _iterator.f();
161
+ }
162
+ var normalStyle = _transformerRuntime["default"].getStyle(sheet, [hasInherit ? inheritStyle : undefined, normalClasses.join(" "), _typeof(props[styleProp]) === "object" ? props[styleProp] : undefined]);
163
+ var activeStyle = activeClasses.length > 0 ? _transformerRuntime["default"].getStyle(sheet, [undefined, activeClasses.join(" "), undefined]) : undefined;
164
+ var disabledStyle = disabledClasses.length > 0 ? _transformerRuntime["default"].getStyle(sheet, [undefined, disabledClasses.join(" "), undefined]) : undefined;
165
+ var groupActiveStyle = hasGroupActiveVariants && parentGroup.pressed && groupActiveClasses.length > 0 ? _transformerRuntime["default"].getStyle(sheet, [undefined, groupActiveClasses.join(" "), undefined]) : undefined;
166
+ if (hasActiveVariants || typeof props[styleProp] === "function") {
167
+ nextProps[styleProp] = function (state) {
168
+ var isPressed = state && state.pressed || false;
169
+ var userStyle = typeof props[styleProp] === "function" ? props[styleProp](state) : null;
170
+ var currentStyles = [normalStyle, groupActiveStyle];
171
+ if (isPressed && !isDisabled && activeStyle) {
172
+ currentStyles.push(activeStyle);
173
+ }
174
+ if (isDisabled && disabledStyle) {
175
+ currentStyles.push(disabledStyle);
176
+ }
177
+ if (userStyle) {
178
+ currentStyles.push(userStyle);
179
+ }
180
+ return currentStyles.filter(Boolean);
181
+ };
182
+ } else {
183
+ var currentStyles = [normalStyle, groupActiveStyle, isDisabled ? disabledStyle : undefined, _typeof(props[styleProp]) === "object" ? props[styleProp] : undefined].filter(Boolean);
184
+ nextProps[styleProp] = currentStyles.length === 1 ? currentStyles[0] : currentStyles.length > 1 ? currentStyles : undefined;
185
+ }
186
+ var inheritable = _transformerRuntime["default"].getInheritStyle(normalStyle);
187
+ if (inheritable) {
188
+ currentInherit = parentInherit ? _objectSpread(_objectSpread({}, parentInherit), inheritable) : inheritable;
189
+ }
190
+ } else {
191
+ // Dynamic className present - runtime resolution
192
+ var computedStyle = _transformerRuntime["default"].getStyle(sheet, [hasInherit ? inheritStyle : undefined, classValue, props[styleProp]]);
193
+ if (computedStyle !== undefined) {
194
+ nextProps[styleProp] = computedStyle;
195
+ if (isPrimaryStyle) {
196
+ var _inheritable = _transformerRuntime["default"].getInheritStyle(computedStyle);
197
+ if (_inheritable) {
198
+ currentInherit = parentInherit ? _objectSpread(_objectSpread({}, parentInherit), _inheritable) : _inheritable;
199
+ }
200
+ }
201
+ }
202
+ }
203
+ delete nextProps[classProp];
204
+ } else if (isPrimaryStyle) {
205
+ // Static inlined style or plain style prop with inheritance
206
+ var ownStyle = props[styleProp];
207
+ if (typeof ownStyle === "function") {
208
+ if (hasInherit) {
209
+ nextProps[styleProp] = function (state) {
210
+ var res = ownStyle(state);
211
+ return [inheritStyle, res];
212
+ };
213
+ } else {
214
+ nextProps[styleProp] = ownStyle;
215
+ }
216
+ } else {
217
+ var flatOwnStyle = ownStyle ? _transformerRuntime["default"].getFlattenStyle(ownStyle) : undefined;
218
+ if (hasInherit) {
219
+ var merged = _transformerRuntime["default"].mergeStyles(inheritStyle, flatOwnStyle, undefined);
220
+ if (merged !== undefined) {
221
+ nextProps[styleProp] = merged;
222
+ }
223
+ }
224
+
225
+ // Inheritable styles come from either the component's own style or inherited from parent
226
+ var combinedForInherit = hasInherit ? _transformerRuntime["default"].mergeStyles(inheritStyle, flatOwnStyle, undefined) : flatOwnStyle;
227
+ var ownInheritable = _transformerRuntime["default"].getInheritStyle(combinedForInherit);
228
+ if (ownInheritable) {
229
+ currentInherit = parentInherit ? _objectSpread(_objectSpread({}, parentInherit), ownInheritable) : ownInheritable;
230
+ }
231
+ }
232
+ }
233
+ };
234
+ for (var _i2 = 0, _Object$entries = Object.entries(mapping); _i2 < _Object$entries.length; _i2++) {
235
+ _loop();
236
+ }
237
+ delete nextProps.inheritStyle;
238
+ var element = _react["default"].createElement(Component, nextProps);
239
+ if (isGroupProvider) {
240
+ element = _react["default"].createElement(GroupContext.Provider, {
241
+ value: _objectSpread(_objectSpread({}, parentGroup), {}, {
242
+ pressed: groupPressed
243
+ })
244
+ }, element);
245
+ }
246
+ if (currentInherit && currentInherit !== parentInherit) {
247
+ return _react["default"].createElement(InheritContext.Provider, {
248
+ value: currentInherit
249
+ }, element);
250
+ }
251
+ return element;
252
+ });
253
+ var name = Component.displayName || Component.name || "Component";
254
+ InteropComponent.displayName = "CssInterop(".concat(name, ")");
255
+ return InteropComponent;
256
+ }
257
+ function remapProps(Component, mapping) {
258
+ return cssInterop(Component, mapping);
259
+ }
260
+ var _default = exports["default"] = cssInterop;