@momo-kits/foundation 0.169.16-beta.6 → 0.169.16-beta.7
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/Application/KitsBindingProvider.tsx +3 -3
- package/Application/KitsHost.ts +7 -6
- package/Application/NavigationContainer.tsx +10 -13
- package/Application/WidgetContainer.tsx +74 -77
- package/Application/__tests__/KitsHost.test.ts +80 -1
- package/Application/hooks.ts +7 -8
- package/Application/types.binding.ts +1 -1
- package/Application/types.ts +6 -4
- package/package.json +1 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import React, { createContext, FC } from 'react';
|
|
2
2
|
import type { KitsBinding, KitsBindingProviderProps } from './types.binding';
|
|
3
3
|
|
|
4
|
-
/**
|
|
4
|
+
/** A host's `KitsBindingProvider` hand-off; kit containers never set it. */
|
|
5
5
|
const KitsBindingContext = createContext<KitsBinding | undefined>(undefined);
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Host hand-off: a mini app container (MMApplication) that doesn't render the
|
|
9
|
+
* mini app's kit containers gives them `binding`, unless one passes its own.
|
|
10
10
|
*/
|
|
11
11
|
const KitsBindingProvider: FC<KitsBindingProviderProps> = ({
|
|
12
12
|
binding,
|
package/Application/KitsHost.ts
CHANGED
|
@@ -10,7 +10,8 @@ type BindingMethod = keyof KitsBinding;
|
|
|
10
10
|
const NO_REQUEST: KitsRequest = { cancel: () => {} };
|
|
11
11
|
const NO_SUBSCRIPTION: KitsSubscription = { remove: () => {} };
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
/** What the kit does without a host: every service is inert. */
|
|
14
|
+
const DefaultKitsBinding: ResolvedKitsBinding = {
|
|
14
15
|
dismiss: () => {},
|
|
15
16
|
trackEvent: () => {},
|
|
16
17
|
getLanguage: () => NO_REQUEST,
|
|
@@ -38,12 +39,12 @@ const resolvedBindings = new WeakSet<KitsBinding>();
|
|
|
38
39
|
let installed: KitsBinding | undefined;
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
|
-
* The host app installs its binding here once;
|
|
42
|
-
*
|
|
42
|
+
* The host app installs its binding here once; a container uses it unless one
|
|
43
|
+
* is passed in or handed off. Until then `binding` is `DefaultKitsBinding`.
|
|
43
44
|
*/
|
|
44
45
|
const KitsHost = {
|
|
45
46
|
get binding(): KitsBinding {
|
|
46
|
-
return installed ??
|
|
47
|
+
return installed ?? DefaultKitsBinding;
|
|
47
48
|
},
|
|
48
49
|
/** The last install wins. The kit borrows the binding and never closes it. */
|
|
49
50
|
install(binding: KitsBinding) {
|
|
@@ -56,7 +57,7 @@ const KitsHost = {
|
|
|
56
57
|
|
|
57
58
|
/**
|
|
58
59
|
* Object that implements `method`, looked up at call time: `binding`, else the
|
|
59
|
-
* installed host binding unless that is `binding` itself, else the
|
|
60
|
+
* installed host binding unless that is `binding` itself, else the default.
|
|
60
61
|
*/
|
|
61
62
|
const getMethodOwner = (
|
|
62
63
|
binding: KitsBinding | undefined,
|
|
@@ -70,7 +71,7 @@ const getMethodOwner = (
|
|
|
70
71
|
if (isOtherHost && typeof host[method] === 'function') {
|
|
71
72
|
return host;
|
|
72
73
|
}
|
|
73
|
-
return
|
|
74
|
+
return DefaultKitsBinding;
|
|
74
75
|
};
|
|
75
76
|
|
|
76
77
|
/**
|
|
@@ -30,7 +30,6 @@ import { HeaderBackground } from './Components/HeaderBackground';
|
|
|
30
30
|
import { useScreenLayout } from '../Layout';
|
|
31
31
|
import { getHeaderRowOptions } from '../Layout/utils';
|
|
32
32
|
import { ReservedRegionsProvider } from '../Layout/ReservedRegions';
|
|
33
|
-
import { KitsBindingContext } from './KitsBindingProvider';
|
|
34
33
|
|
|
35
34
|
const Stack = createStackNavigator();
|
|
36
35
|
|
|
@@ -54,7 +53,7 @@ const NavigationContainer: React.FC<NavigationContainerProps> = ({
|
|
|
54
53
|
}) => {
|
|
55
54
|
const context = useContext<any>(MiniAppContext);
|
|
56
55
|
const [currentContext, setCurrentContext] = useState<any>({});
|
|
57
|
-
const
|
|
56
|
+
const resolved = useKitsBinding(binding);
|
|
58
57
|
|
|
59
58
|
useEffect(() => {
|
|
60
59
|
const sub = resolved.observeConfig('font_scale_config', data => {
|
|
@@ -83,17 +82,15 @@ const NavigationContainer: React.FC<NavigationContainerProps> = ({
|
|
|
83
82
|
>
|
|
84
83
|
<ReservedRegionsProvider>
|
|
85
84
|
<MiniAppContext.Provider value={mergedContext}>
|
|
86
|
-
<
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/>
|
|
96
|
-
</KitsBindingContext.Provider>
|
|
85
|
+
<Navigation
|
|
86
|
+
screen={screen}
|
|
87
|
+
theme={theme}
|
|
88
|
+
options={options}
|
|
89
|
+
binding={resolved}
|
|
90
|
+
setCurrentContext={setCurrentContext}
|
|
91
|
+
initialParams={initialParams}
|
|
92
|
+
localize={localize}
|
|
93
|
+
/>
|
|
97
94
|
</MiniAppContext.Provider>
|
|
98
95
|
</ReservedRegionsProvider>
|
|
99
96
|
</SafeAreaProvider>
|
|
@@ -16,7 +16,6 @@ import { createStackNavigator } from '@react-navigation/stack';
|
|
|
16
16
|
import ModalScreen from './ModalScreen';
|
|
17
17
|
import { StyleSheet, View } from 'react-native';
|
|
18
18
|
import { useKitsBinding } from './hooks';
|
|
19
|
-
import { KitsBindingContext } from './KitsBindingProvider';
|
|
20
19
|
|
|
21
20
|
const Stack = createStackNavigator();
|
|
22
21
|
|
|
@@ -36,8 +35,8 @@ const WidgetContent: React.FC = ({ route }: any) => {
|
|
|
36
35
|
|
|
37
36
|
/**
|
|
38
37
|
* Root of an embedded widget: its own navigator with the widget screen and the
|
|
39
|
-
* `Modal` route, sized to the widget's content height.
|
|
40
|
-
*
|
|
38
|
+
* `Modal` route, sized to the widget's content height. Its binding is never
|
|
39
|
+
* inherited from an enclosing container (see `useKitsBinding`).
|
|
41
40
|
*/
|
|
42
41
|
const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
43
42
|
widget,
|
|
@@ -51,7 +50,7 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
51
50
|
const isReady = useRef(false);
|
|
52
51
|
const navigator = useRef(new Navigator(navigationRef, isReady, true));
|
|
53
52
|
const [currentContext, setCurrentContext] = useState({});
|
|
54
|
-
const
|
|
53
|
+
const resolved = useKitsBinding(binding);
|
|
55
54
|
|
|
56
55
|
useEffect(() => {
|
|
57
56
|
const sub = resolved.observeConfig('font_scale_config', data => {
|
|
@@ -97,86 +96,84 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
97
96
|
<View style={[styles.container, { height: height ?? '100%' }]}>
|
|
98
97
|
<SafeAreaProvider>
|
|
99
98
|
<MiniAppContext.Provider value={mergedContext}>
|
|
100
|
-
<
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
theme
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
<ApplicationContext.Provider
|
|
100
|
+
value={{
|
|
101
|
+
navigator: navigator.current,
|
|
102
|
+
theme: {
|
|
103
|
+
...theme,
|
|
104
|
+
font: fontFamily,
|
|
105
|
+
colors: {
|
|
106
|
+
...theme.colors,
|
|
107
|
+
gradient: headerGradient,
|
|
108
|
+
},
|
|
109
|
+
assets: {
|
|
110
|
+
...theme.assets,
|
|
111
|
+
headerBackground,
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
translate,
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<NavigationIndependentTree>
|
|
118
|
+
<ReactNavigationContainer
|
|
119
|
+
theme={{
|
|
120
|
+
dark: false,
|
|
107
121
|
colors: {
|
|
108
|
-
|
|
109
|
-
|
|
122
|
+
primary: theme.colors.primary,
|
|
123
|
+
background: theme.colors.background.default,
|
|
124
|
+
card: theme.colors.background.surface,
|
|
125
|
+
text: theme.colors.text.default,
|
|
126
|
+
border: theme.colors.border.default,
|
|
127
|
+
notification: theme.colors.error.primary,
|
|
110
128
|
},
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
},
|
|
116
|
-
translate,
|
|
117
|
-
}}
|
|
118
|
-
>
|
|
119
|
-
<NavigationIndependentTree>
|
|
120
|
-
<ReactNavigationContainer
|
|
121
|
-
theme={{
|
|
122
|
-
dark: false,
|
|
123
|
-
colors: {
|
|
124
|
-
primary: theme.colors.primary,
|
|
125
|
-
background: theme.colors.background.default,
|
|
126
|
-
card: theme.colors.background.surface,
|
|
127
|
-
text: theme.colors.text.default,
|
|
128
|
-
border: theme.colors.border.default,
|
|
129
|
-
notification: theme.colors.error.primary,
|
|
129
|
+
fonts: {
|
|
130
|
+
regular: {
|
|
131
|
+
fontFamily: navigationFontFamily,
|
|
132
|
+
fontWeight: '400',
|
|
130
133
|
},
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
fontWeight: '400',
|
|
135
|
-
},
|
|
136
|
-
medium: {
|
|
137
|
-
fontFamily: navigationFontFamily,
|
|
138
|
-
fontWeight: '400',
|
|
139
|
-
},
|
|
140
|
-
bold: {
|
|
141
|
-
fontFamily: navigationFontFamily,
|
|
142
|
-
fontWeight: '400',
|
|
143
|
-
},
|
|
144
|
-
heavy: {
|
|
145
|
-
fontFamily: navigationFontFamily,
|
|
146
|
-
fontWeight: '400',
|
|
147
|
-
},
|
|
134
|
+
medium: {
|
|
135
|
+
fontFamily: navigationFontFamily,
|
|
136
|
+
fontWeight: '400',
|
|
148
137
|
},
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
138
|
+
bold: {
|
|
139
|
+
fontFamily: navigationFontFamily,
|
|
140
|
+
fontWeight: '400',
|
|
141
|
+
},
|
|
142
|
+
heavy: {
|
|
143
|
+
fontFamily: navigationFontFamily,
|
|
144
|
+
fontWeight: '400',
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
}}
|
|
148
|
+
ref={navigationRef}
|
|
149
|
+
onReady={() => {
|
|
150
|
+
isReady.current = true;
|
|
151
|
+
}}
|
|
152
|
+
>
|
|
153
|
+
<Stack.Navigator
|
|
154
|
+
initialRouteName="Stack"
|
|
155
|
+
screenOptions={{
|
|
156
|
+
headerMode: 'screen',
|
|
153
157
|
}}
|
|
154
158
|
>
|
|
155
|
-
<Stack.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
<Stack.Screen
|
|
160
|
+
name="Stack"
|
|
161
|
+
component={WidgetContent}
|
|
162
|
+
initialParams={{ screen: widget, setHeight }}
|
|
163
|
+
options={{
|
|
164
|
+
headerShown: false,
|
|
159
165
|
}}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
component={ModalScreen}
|
|
172
|
-
options={getModalOptions()}
|
|
173
|
-
initialParams={{ screen: widget }}
|
|
174
|
-
/>
|
|
175
|
-
</Stack.Navigator>
|
|
176
|
-
</ReactNavigationContainer>
|
|
177
|
-
</NavigationIndependentTree>
|
|
178
|
-
</ApplicationContext.Provider>
|
|
179
|
-
</KitsBindingContext.Provider>
|
|
166
|
+
/>
|
|
167
|
+
<Stack.Screen
|
|
168
|
+
name="Modal"
|
|
169
|
+
component={ModalScreen}
|
|
170
|
+
options={getModalOptions()}
|
|
171
|
+
initialParams={{ screen: widget }}
|
|
172
|
+
/>
|
|
173
|
+
</Stack.Navigator>
|
|
174
|
+
</ReactNavigationContainer>
|
|
175
|
+
</NavigationIndependentTree>
|
|
176
|
+
</ApplicationContext.Provider>
|
|
180
177
|
</MiniAppContext.Provider>
|
|
181
178
|
</SafeAreaProvider>
|
|
182
179
|
</View>
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
import { createElement, type ReactElement } from 'react';
|
|
2
|
+
import { act, create } from 'react-test-renderer';
|
|
3
|
+
import { useKitsBinding } from '../hooks';
|
|
4
|
+
import { KitsBindingProvider } from '../KitsBindingProvider';
|
|
1
5
|
import { KitsHost, resolveKitsBinding } from '../KitsHost';
|
|
2
6
|
import type { KitsBinding } from '../types.binding';
|
|
3
7
|
|
|
8
|
+
type ContainerProps = {
|
|
9
|
+
name: string;
|
|
10
|
+
binding?: KitsBinding;
|
|
11
|
+
children?: ReactElement;
|
|
12
|
+
};
|
|
13
|
+
|
|
4
14
|
/** Host binding that records each call as `[method, ...args]`. */
|
|
5
15
|
const createRecordingBinding = (calls: unknown[][]): KitsBinding => {
|
|
6
16
|
return {
|
|
@@ -13,6 +23,28 @@ const createRecordingBinding = (calls: unknown[][]): KitsBinding => {
|
|
|
13
23
|
};
|
|
14
24
|
};
|
|
15
25
|
|
|
26
|
+
/** Binding whose `trackEvent` records `name`. */
|
|
27
|
+
const createNamedBinding = (name: string, calls: unknown[][]): KitsBinding => {
|
|
28
|
+
return {
|
|
29
|
+
trackEvent: eventName => {
|
|
30
|
+
calls.push([name, eventName]);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Resolves its binding as a kit container does and tracks `name` on render. */
|
|
36
|
+
const Container = ({ name, binding, children }: ContainerProps) => {
|
|
37
|
+
useKitsBinding(binding).trackEvent(name, {});
|
|
38
|
+
return children ?? null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Renders `element` once, synchronously. */
|
|
42
|
+
const render = (element: ReactElement) => {
|
|
43
|
+
act(() => {
|
|
44
|
+
create(element);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
16
48
|
describe('KitsHost', () => {
|
|
17
49
|
afterEach(() => {
|
|
18
50
|
KitsHost.uninstall();
|
|
@@ -40,7 +72,7 @@ describe('KitsHost', () => {
|
|
|
40
72
|
expect(calls).toEqual([['own.trackEvent', 'event'], ['host.vibrateLight']]);
|
|
41
73
|
});
|
|
42
74
|
|
|
43
|
-
it('uses
|
|
75
|
+
it('uses DefaultKitsBinding when the binding is the installed one', () => {
|
|
44
76
|
const calls: unknown[][] = [];
|
|
45
77
|
const host = createRecordingBinding(calls);
|
|
46
78
|
KitsHost.install(host);
|
|
@@ -74,3 +106,50 @@ describe('KitsHost', () => {
|
|
|
74
106
|
expect(hostCancel).toHaveBeenCalledTimes(1);
|
|
75
107
|
});
|
|
76
108
|
});
|
|
109
|
+
|
|
110
|
+
describe('useKitsBinding', () => {
|
|
111
|
+
afterEach(() => {
|
|
112
|
+
KitsHost.uninstall();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('never inherits the binding of an enclosing container', () => {
|
|
116
|
+
const calls: unknown[][] = [];
|
|
117
|
+
KitsHost.install(createNamedBinding('host', calls));
|
|
118
|
+
const inner = createElement(Container, { name: 'inner' });
|
|
119
|
+
const outer = createElement(Container, {
|
|
120
|
+
name: 'outer',
|
|
121
|
+
binding: createNamedBinding('A', calls),
|
|
122
|
+
children: inner,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
render(outer);
|
|
126
|
+
|
|
127
|
+
expect(calls).toEqual([
|
|
128
|
+
['A', 'outer'],
|
|
129
|
+
['host', 'inner'],
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('uses the host hand-off of KitsBindingProvider unless a prop is passed', () => {
|
|
134
|
+
const calls: unknown[][] = [];
|
|
135
|
+
KitsHost.install(createNamedBinding('host', calls));
|
|
136
|
+
const inner = createElement(Container, { name: 'inner' });
|
|
137
|
+
const outer = createElement(Container, {
|
|
138
|
+
name: 'outer',
|
|
139
|
+
binding: createNamedBinding('A', calls),
|
|
140
|
+
children: inner,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
render(
|
|
144
|
+
createElement(KitsBindingProvider, {
|
|
145
|
+
binding: createNamedBinding('handOff', calls),
|
|
146
|
+
children: outer,
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(calls).toEqual([
|
|
151
|
+
['A', 'outer'],
|
|
152
|
+
['handOff', 'inner'],
|
|
153
|
+
]);
|
|
154
|
+
});
|
|
155
|
+
});
|
package/Application/hooks.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
} from './utils';
|
|
16
16
|
import { KitsBindingContext } from './KitsBindingProvider';
|
|
17
17
|
import { resolveKitsBinding } from './KitsHost';
|
|
18
|
-
import type { KitsBinding } from './types.binding';
|
|
18
|
+
import type { KitsBinding, ResolvedKitsBinding } from './types.binding';
|
|
19
19
|
|
|
20
20
|
const FOREGROUND_FLAG_DURATION = 100;
|
|
21
21
|
|
|
@@ -133,14 +133,13 @@ const useNormalizedSharedValue = (
|
|
|
133
133
|
};
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
|
-
* Binding of a container: its `binding` prop, else the
|
|
137
|
-
* else `KitsHost`.
|
|
136
|
+
* Binding of a container: its `binding` prop, else the host's hand-off through
|
|
137
|
+
* `KitsBindingProvider`, else `KitsHost`. Never an enclosing container's.
|
|
138
138
|
*/
|
|
139
|
-
const useKitsBinding = (binding?: KitsBinding) => {
|
|
140
|
-
const
|
|
141
|
-
const source = binding ??
|
|
142
|
-
|
|
143
|
-
return { source, resolved };
|
|
139
|
+
const useKitsBinding = (binding?: KitsBinding): ResolvedKitsBinding => {
|
|
140
|
+
const handedOff = useContext(KitsBindingContext);
|
|
141
|
+
const source = binding ?? handedOff;
|
|
142
|
+
return useMemo(() => resolveKitsBinding(source), [source]);
|
|
144
143
|
};
|
|
145
144
|
|
|
146
145
|
export {
|
|
@@ -66,7 +66,7 @@ export type KitsOpenWebRequest = {
|
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* Host services the kit calls, mirroring native `KitsBinding`. A method left out
|
|
69
|
-
* falls back to the one installed in `KitsHost`, then to
|
|
69
|
+
* falls back to the one installed in `KitsHost`, then to `DefaultKitsBinding`.
|
|
70
70
|
* Callbacks fire only on success; containers borrow the binding and never close it.
|
|
71
71
|
*/
|
|
72
72
|
export interface KitsBinding {
|
package/Application/types.ts
CHANGED
|
@@ -63,8 +63,9 @@ export type NavigationContainerProps = {
|
|
|
63
63
|
options?: NavigationOptions;
|
|
64
64
|
theme?: Theme;
|
|
65
65
|
/**
|
|
66
|
-
* Optional. Host services for this mini app; defaults to
|
|
67
|
-
*
|
|
66
|
+
* Optional. Host services for this mini app; defaults to a host's
|
|
67
|
+
* `KitsBindingProvider`, then `KitsHost`, never an enclosing container's.
|
|
68
|
+
* Keep it stable.
|
|
68
69
|
*/
|
|
69
70
|
binding?: KitsBinding;
|
|
70
71
|
initialParams?: any;
|
|
@@ -76,8 +77,9 @@ export type WidgetContainerProps = {
|
|
|
76
77
|
widget: React.ComponentType<any>;
|
|
77
78
|
theme?: Theme;
|
|
78
79
|
/**
|
|
79
|
-
* Optional. Host services for this widget's app; defaults to
|
|
80
|
-
*
|
|
80
|
+
* Optional. Host services for this widget's app; defaults to a host's
|
|
81
|
+
* `KitsBindingProvider`, then `KitsHost`, never an enclosing container's.
|
|
82
|
+
* Keep it stable.
|
|
81
83
|
*/
|
|
82
84
|
binding?: KitsBinding;
|
|
83
85
|
localize: LocalizeProps;
|