react-native-molecules 0.5.0-beta.2 → 0.5.0-beta.4
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.
- package/README.md +1 -1
- package/components/Button/Button.tsx +2 -2
- package/components/ElementGroup/ElementGroup.tsx +16 -14
- package/components/Surface/Surface.android.tsx +3 -2
- package/components/Surface/Surface.ios.tsx +8 -6
- package/components/Surface/utils.ts +0 -24
- package/components/TextInput/TextInput.tsx +2 -2
- package/components/Tooltip/TooltipTrigger.tsx +25 -16
- package/package.json +1 -10
- package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +5 -2
- package/utils/extractPropertiesFromStyles.ts +25 -0
- package/utils/lodash.ts +77 -5
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ React Native Molecules
|
|
|
4
4
|
|
|
5
5
|
<h3 align="center">
|
|
6
6
|
Material 3-powered primitives for React Native + Web.<br/>
|
|
7
|
-
<a href="https://
|
|
7
|
+
<a href="https://molecules.taylordb.ai">Explore the docs</a>
|
|
8
8
|
</h3>
|
|
9
9
|
|
|
10
10
|
---
|
|
@@ -12,11 +12,11 @@ import { type StyleProp, type TextStyle, View, type ViewProps, type ViewStyle }
|
|
|
12
12
|
import { useActionState } from '../../hooks';
|
|
13
13
|
import type { MD3Elevation } from '../../types/theme';
|
|
14
14
|
import { resolveStateVariant } from '../../utils';
|
|
15
|
+
import { extractPropertiesFromStyles } from '../../utils/extractPropertiesFromStyles';
|
|
15
16
|
import { ActivityIndicator } from '../ActivityIndicator';
|
|
16
17
|
import { Icon, type IconType } from '../Icon';
|
|
17
18
|
import { StateLayer } from '../StateLayer';
|
|
18
19
|
import { Surface, type SurfaceProps } from '../Surface';
|
|
19
|
-
import { extractProperties } from '../Surface/utils';
|
|
20
20
|
import { Text } from '../Text';
|
|
21
21
|
import { TouchableRipple } from '../TouchableRipple';
|
|
22
22
|
import type { ButtonSize, ButtonVariant } from './types';
|
|
@@ -209,7 +209,7 @@ const Button = (
|
|
|
209
209
|
defaultStyles;
|
|
210
210
|
|
|
211
211
|
// for mobile
|
|
212
|
-
const { borderRadius } =
|
|
212
|
+
const { borderRadius } = extractPropertiesFromStyles(
|
|
213
213
|
[defaultStyles.root, styleProp],
|
|
214
214
|
['borderRadius'],
|
|
215
215
|
);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Children, cloneElement, forwardRef, memo, type ReactElement, useMemo } from 'react';
|
|
2
2
|
import { View, type ViewProps, type ViewStyle } from 'react-native';
|
|
3
3
|
|
|
4
|
+
import { extractPropertiesFromStyles } from '../../utils/extractPropertiesFromStyles';
|
|
4
5
|
import { isNil } from '../../utils/lodash';
|
|
5
6
|
import { elementGroupStyles } from './utils';
|
|
6
7
|
|
|
@@ -44,24 +45,25 @@ export const ElementGroup = (
|
|
|
44
45
|
borderTopRightRadius,
|
|
45
46
|
borderBottomLeftRadius,
|
|
46
47
|
borderBottomRightRadius,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
} = extractPropertiesFromStyles(
|
|
49
|
+
[elementGroupStyles.root, style],
|
|
50
|
+
[
|
|
51
|
+
'borderRadius',
|
|
52
|
+
'borderTopLeftRadius',
|
|
53
|
+
'borderTopRightRadius',
|
|
54
|
+
'borderBottomLeftRadius',
|
|
55
|
+
'borderBottomRightRadius',
|
|
56
|
+
],
|
|
57
|
+
);
|
|
56
58
|
|
|
57
59
|
return {
|
|
58
|
-
containerStyle: [
|
|
60
|
+
containerStyle: [elementGroupStyles.root, style],
|
|
59
61
|
borderRadius: borderRadiusProp || _borderRadius,
|
|
60
62
|
borderRadiuses: {
|
|
61
|
-
borderTopLeftRadius: borderTopLeftRadius
|
|
62
|
-
borderTopRightRadius: borderTopRightRadius
|
|
63
|
-
borderBottomLeftRadius: borderBottomLeftRadius
|
|
64
|
-
borderBottomRightRadius: borderBottomRightRadius
|
|
63
|
+
borderTopLeftRadius: borderTopLeftRadius,
|
|
64
|
+
borderTopRightRadius: borderTopRightRadius,
|
|
65
|
+
borderBottomLeftRadius: borderBottomLeftRadius,
|
|
66
|
+
borderBottomRightRadius: borderBottomRightRadius,
|
|
65
67
|
},
|
|
66
68
|
};
|
|
67
69
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -4,8 +4,9 @@ import { useUnistyles } from 'react-native-unistyles';
|
|
|
4
4
|
|
|
5
5
|
import { inputRange } from '../../styles/shadow';
|
|
6
6
|
import type { MD3Elevation } from '../../types/theme';
|
|
7
|
+
import { extractPropertiesFromStyles } from '../../utils/extractPropertiesFromStyles';
|
|
7
8
|
import { BackgroundContextWrapper } from './BackgroundContextWrapper';
|
|
8
|
-
import { defaultStyles,
|
|
9
|
+
import { defaultStyles, getElevationAndroid } from './utils';
|
|
9
10
|
|
|
10
11
|
export type Props = ViewProps & {
|
|
11
12
|
/**
|
|
@@ -43,7 +44,7 @@ const Surface = ({ elevation = 1, style, children, testID, ...props }: Props, re
|
|
|
43
44
|
elevation: getElevationAndroid(elevation, inputRange, elevationLevel),
|
|
44
45
|
},
|
|
45
46
|
] as StyleProp<ViewStyle>,
|
|
46
|
-
surfaceBackground:
|
|
47
|
+
surfaceBackground: extractPropertiesFromStyles(
|
|
47
48
|
[defaultStyles.root as ViewStyle, style],
|
|
48
49
|
['backgroundColor'],
|
|
49
50
|
).backgroundColor,
|
|
@@ -3,8 +3,9 @@ import { Animated, type StyleProp, View, type ViewStyle } from 'react-native';
|
|
|
3
3
|
import { useUnistyles } from 'react-native-unistyles';
|
|
4
4
|
|
|
5
5
|
import type { MD3Elevation } from '../../types/theme';
|
|
6
|
+
import { extractPropertiesFromStyles } from '../../utils/extractPropertiesFromStyles';
|
|
6
7
|
import { BackgroundContextWrapper } from './BackgroundContextWrapper';
|
|
7
|
-
import { defaultStyles,
|
|
8
|
+
import { defaultStyles, getStyleForShadowLayer } from './utils';
|
|
8
9
|
|
|
9
10
|
export type Props = ComponentPropsWithRef<typeof View> & {
|
|
10
11
|
/**
|
|
@@ -78,14 +79,15 @@ const Surface = ({ elevation = 1, style, children, testID, ...props }: Props, re
|
|
|
78
79
|
})();
|
|
79
80
|
|
|
80
81
|
const { surfaceBackground, sharedStyle, layer0Style, layer1Style } = useMemo(() => {
|
|
81
|
-
const { position, alignSelf, top, left, right, bottom, borderRadius } =
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
const { position, alignSelf, top, left, right, bottom, borderRadius } =
|
|
83
|
+
extractPropertiesFromStyles(
|
|
84
|
+
[defaultStyles.root as ViewStyle, style],
|
|
85
|
+
['position', 'alignSelf', 'top', 'left', 'right', 'bottom', 'borderRadius'],
|
|
86
|
+
);
|
|
85
87
|
const absoluteStyle = { position, alignSelf, top, right, bottom, left };
|
|
86
88
|
|
|
87
89
|
return {
|
|
88
|
-
surfaceBackground:
|
|
90
|
+
surfaceBackground: extractPropertiesFromStyles(
|
|
89
91
|
[defaultStyles.root as ViewStyle, style],
|
|
90
92
|
['backgroundColor'],
|
|
91
93
|
).backgroundColor,
|
|
@@ -15,30 +15,6 @@ export const defaultStyles = getRegisteredComponentStylesWithFallback(
|
|
|
15
15
|
defaultStylesDefault,
|
|
16
16
|
);
|
|
17
17
|
|
|
18
|
-
// TODO - abstract this
|
|
19
|
-
export function extractProperties(
|
|
20
|
-
_objectsArray: Record<string, any>,
|
|
21
|
-
propertiesToExtract: string[],
|
|
22
|
-
) {
|
|
23
|
-
const extracted: Record<string, any> = {};
|
|
24
|
-
|
|
25
|
-
const objectsArray = _objectsArray.flat();
|
|
26
|
-
|
|
27
|
-
for (let i = objectsArray.length - 1; i >= 0; i--) {
|
|
28
|
-
const obj = objectsArray[i];
|
|
29
|
-
|
|
30
|
-
for (const prop of propertiesToExtract) {
|
|
31
|
-
if (!obj) continue;
|
|
32
|
-
if (prop in obj) {
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
extracted[prop] = obj[prop];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return extracted;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
18
|
const _shadowColor = '#000';
|
|
43
19
|
|
|
44
20
|
const iOSShadowOutputRanges = [
|
|
@@ -3,7 +3,7 @@ import React, {
|
|
|
3
3
|
memo,
|
|
4
4
|
type PropsWithoutRef,
|
|
5
5
|
type ReactNode,
|
|
6
|
-
type
|
|
6
|
+
type Ref,
|
|
7
7
|
useCallback,
|
|
8
8
|
useContext,
|
|
9
9
|
useEffect,
|
|
@@ -50,7 +50,7 @@ type Element = ReactNode | ((props: ElementProps) => ReactNode);
|
|
|
50
50
|
|
|
51
51
|
export type Props = Omit<TextInputProps, 'ref'> &
|
|
52
52
|
WithElements<Element> & {
|
|
53
|
-
ref?:
|
|
53
|
+
ref?: Ref<NativeTextInput | null>;
|
|
54
54
|
/**
|
|
55
55
|
* Variant of the TextInput.
|
|
56
56
|
* - `flat` - flat input with an underline.
|
|
@@ -30,27 +30,36 @@ const TooltipTrigger = memo(({ children }: { children: ReactElement }) => {
|
|
|
30
30
|
() => triggerRef?.current,
|
|
31
31
|
);
|
|
32
32
|
|
|
33
|
-
const onPress = useCallback(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
const onPress = useCallback(
|
|
34
|
+
(e: unknown) => {
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
children?.props?.onPress?.(e);
|
|
37
|
+
},
|
|
38
|
+
[children?.props],
|
|
39
|
+
);
|
|
37
40
|
|
|
38
|
-
const onLongPress = useCallback(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
const onLongPress = useCallback(
|
|
42
|
+
(e: unknown) => {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
children?.props?.onLongPress?.(e);
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
if (isWeb) return;
|
|
47
|
+
onOpen();
|
|
48
|
+
},
|
|
49
|
+
[children?.props, isWeb, onOpen],
|
|
50
|
+
);
|
|
45
51
|
|
|
46
|
-
const onPressOut = useCallback(
|
|
47
|
-
|
|
52
|
+
const onPressOut = useCallback(
|
|
53
|
+
(e: unknown) => {
|
|
54
|
+
// @ts-ignore
|
|
48
55
|
|
|
49
|
-
|
|
56
|
+
children?.props?.onPressOut?.(e);
|
|
50
57
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
if (isWeb) return;
|
|
59
|
+
onClose();
|
|
60
|
+
},
|
|
61
|
+
[children?.props, isWeb, onClose],
|
|
62
|
+
);
|
|
54
63
|
|
|
55
64
|
const onHoverIn = useCallback(() => {
|
|
56
65
|
// @ts-ignore
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-molecules",
|
|
3
|
-
"version": "0.5.0-beta.
|
|
3
|
+
"version": "0.5.0-beta.4",
|
|
4
4
|
"author": "Thet Aung <thetaung.dev@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -47,10 +47,6 @@
|
|
|
47
47
|
"color": "^4.2.3",
|
|
48
48
|
"date-fns": "^4.1.0",
|
|
49
49
|
"eventemitter2": "^6.4.9",
|
|
50
|
-
"lodash.get": "^4.4.2",
|
|
51
|
-
"lodash.keyby": "^4.6.0",
|
|
52
|
-
"lodash.memoize": "^4.1.2",
|
|
53
|
-
"lodash.omitby": "^4.6.0",
|
|
54
50
|
"use-sync-external-store": "^1.2.0"
|
|
55
51
|
},
|
|
56
52
|
"peerDependencies": {
|
|
@@ -64,11 +60,6 @@
|
|
|
64
60
|
"@react-native-documents/picker": "^10.1.2",
|
|
65
61
|
"@types/color": "^3.0.3",
|
|
66
62
|
"@types/jest": "^29.1.2",
|
|
67
|
-
"@types/lodash": "^4.17.13",
|
|
68
|
-
"@types/lodash.get": "^4.4.7",
|
|
69
|
-
"@types/lodash.keyby": "^4.6.7",
|
|
70
|
-
"@types/lodash.memoize": "^4.1.7",
|
|
71
|
-
"@types/lodash.omitby": "^4.6.7",
|
|
72
63
|
"@types/react": "~19.1.10",
|
|
73
64
|
"@types/use-sync-external-store": "^1.5.0",
|
|
74
65
|
"react": "19.1.0",
|
|
@@ -10,9 +10,12 @@ import {
|
|
|
10
10
|
type ShortcutsManagerProps,
|
|
11
11
|
} from './utils';
|
|
12
12
|
|
|
13
|
+
const defaultScopes: ShortcutsManagerProps['scopes'] = [];
|
|
14
|
+
const defaultShortcuts: Shortcut[] = [];
|
|
15
|
+
|
|
13
16
|
const _ShortcutsManager = ({ shortcuts, scopes, children }: ShortcutsManagerProps) => {
|
|
14
|
-
const shortcutsRef = useRef<Shortcut[]>(shortcuts ||
|
|
15
|
-
const scopesRef = useRef(scopes);
|
|
17
|
+
const shortcutsRef = useRef<Shortcut[]>(shortcuts || defaultShortcuts);
|
|
18
|
+
const scopesRef = useRef(scopes || defaultScopes);
|
|
16
19
|
const parentContextRef = useContext(ShortcutsManagerContext);
|
|
17
20
|
|
|
18
21
|
const contextValue = useMemo(() => {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { StyleSheet } from 'react-native-unistyles';
|
|
2
|
+
|
|
3
|
+
// TODO - abstract this
|
|
4
|
+
export function extractPropertiesFromStyles(
|
|
5
|
+
_objectsArray: Record<string, any>,
|
|
6
|
+
propertiesToExtract: string[],
|
|
7
|
+
) {
|
|
8
|
+
const extracted: Record<string, any> = {};
|
|
9
|
+
|
|
10
|
+
const objectsArray = StyleSheet.flatten(_objectsArray);
|
|
11
|
+
|
|
12
|
+
for (let i = objectsArray.length - 1; i >= 0; i--) {
|
|
13
|
+
const obj = objectsArray[i];
|
|
14
|
+
|
|
15
|
+
for (const prop of propertiesToExtract) {
|
|
16
|
+
if (!obj) continue;
|
|
17
|
+
if (prop in obj) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
extracted[prop] = obj[prop];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return extracted;
|
|
25
|
+
}
|
package/utils/lodash.ts
CHANGED
|
@@ -1,8 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
type MemoizeCache<K = any, V = any> = {
|
|
2
|
+
has(key: K): boolean;
|
|
3
|
+
get(key: K): V | undefined;
|
|
4
|
+
set(key: K, value: V): unknown;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type MemoizeFn = {
|
|
8
|
+
<T extends (...args: any[]) => any>(
|
|
9
|
+
func: T,
|
|
10
|
+
resolver?: (...args: Parameters<T>) => any,
|
|
11
|
+
): MemoizedFunction<T>;
|
|
12
|
+
Cache: { new (): MemoizeCache<any, any> };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export interface MemoizedFunction<T extends (...args: any[]) => any> {
|
|
16
|
+
(...args: Parameters<T>): ReturnType<T>;
|
|
17
|
+
cache: MemoizeCache<any, ReturnType<T>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const memoize: MemoizeFn = ((
|
|
21
|
+
func: (...args: any[]) => any,
|
|
22
|
+
resolver?: (...args: any[]) => any,
|
|
23
|
+
) => {
|
|
24
|
+
if (typeof func !== 'function') {
|
|
25
|
+
throw new TypeError('Expected a function');
|
|
26
|
+
}
|
|
27
|
+
const memoized = function (this: unknown, ...args: any[]) {
|
|
28
|
+
const key = resolver ? resolver.apply(this, args) : args[0];
|
|
29
|
+
const { cache } = memoized as MemoizedFunction<typeof func>;
|
|
30
|
+
if (cache.has(key)) {
|
|
31
|
+
return cache.get(key);
|
|
32
|
+
}
|
|
33
|
+
const result = func.apply(this, args);
|
|
34
|
+
cache.set(key, result);
|
|
35
|
+
return result;
|
|
36
|
+
} as MemoizedFunction<typeof func>;
|
|
37
|
+
|
|
38
|
+
(memoized as MemoizedFunction<typeof func>).cache = new memoize.Cache();
|
|
39
|
+
return memoized;
|
|
40
|
+
}) as MemoizeFn;
|
|
41
|
+
|
|
42
|
+
memoize.Cache = Map;
|
|
43
|
+
|
|
44
|
+
type Iteratee<T> = ((item: T) => string | number | symbol) | keyof T;
|
|
45
|
+
|
|
46
|
+
export const keyBy = <T extends Record<string, any>>(
|
|
47
|
+
collection: T[],
|
|
48
|
+
iteratee: Iteratee<T>,
|
|
49
|
+
): Record<string, T> => {
|
|
50
|
+
const result: Record<string, T> = {};
|
|
51
|
+
if (!Array.isArray(collection)) return result;
|
|
52
|
+
|
|
53
|
+
const getter = typeof iteratee === 'function' ? iteratee : (item: T) => item[iteratee];
|
|
54
|
+
for (const item of collection) {
|
|
55
|
+
const key = getter(item);
|
|
56
|
+
if (key != null) {
|
|
57
|
+
result[String(key)] = item;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type Predicate = (value: any, key: string) => boolean;
|
|
64
|
+
|
|
65
|
+
export const omitBy = <T extends Record<string, any>>(
|
|
66
|
+
object: T,
|
|
67
|
+
predicate: Predicate,
|
|
68
|
+
): Partial<T> => {
|
|
69
|
+
if (object == null) return {};
|
|
70
|
+
const result: Partial<T> = {};
|
|
71
|
+
for (const [key, value] of Object.entries(object)) {
|
|
72
|
+
if (!predicate(value, key)) {
|
|
73
|
+
result[key as keyof T] = value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
};
|
|
6
78
|
|
|
7
79
|
export const isNil = (value: unknown): value is null | undefined => value == null;
|
|
8
80
|
export const noop = () => {};
|