@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.
@@ -1,12 +1,12 @@
1
1
  import React, { createContext, FC } from 'react';
2
2
  import type { KitsBinding, KitsBindingProviderProps } from './types.binding';
3
3
 
4
- /** Binding of the nearest enclosing container or provider, if any. */
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
- * Gives every container in its subtree `binding` unless one passes its own, so a
9
- * host can wrap a mini app without the mini app passing anything.
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,
@@ -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
- const NO_OP_BINDING: ResolvedKitsBinding = {
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; containers use it unless a caller
42
- * passes or provides another. Until then `binding` does nothing, never throws.
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 ?? NO_OP_BINDING;
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 no-op.
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 NO_OP_BINDING;
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 { source, resolved } = useKitsBinding(binding);
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
- <KitsBindingContext.Provider value={source}>
87
- <Navigation
88
- screen={screen}
89
- theme={theme}
90
- options={options}
91
- binding={resolved}
92
- setCurrentContext={setCurrentContext}
93
- initialParams={initialParams}
94
- localize={localize}
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. Without a `binding` it
40
- * uses the enclosing container's.
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 { source, resolved } = useKitsBinding(binding);
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
- <KitsBindingContext.Provider value={source}>
101
- <ApplicationContext.Provider
102
- value={{
103
- navigator: navigator.current,
104
- theme: {
105
- ...theme,
106
- font: fontFamily,
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
- ...theme.colors,
109
- gradient: headerGradient,
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
- assets: {
112
- ...theme.assets,
113
- headerBackground,
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
- fonts: {
132
- regular: {
133
- fontFamily: navigationFontFamily,
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
- ref={navigationRef}
151
- onReady={() => {
152
- isReady.current = true;
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.Navigator
156
- initialRouteName="Stack"
157
- screenOptions={{
158
- headerMode: 'screen',
159
+ <Stack.Screen
160
+ name="Stack"
161
+ component={WidgetContent}
162
+ initialParams={{ screen: widget, setHeight }}
163
+ options={{
164
+ headerShown: false,
159
165
  }}
160
- >
161
- <Stack.Screen
162
- name="Stack"
163
- component={WidgetContent}
164
- initialParams={{ screen: widget, setHeight }}
165
- options={{
166
- headerShown: false,
167
- }}
168
- />
169
- <Stack.Screen
170
- name="Modal"
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 the no-op when the binding is the installed one', () => {
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
+ });
@@ -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 nearest enclosing one,
137
- * else `KitsHost`. `source` is what the container passes on to nested ones.
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 inherited = useContext(KitsBindingContext);
141
- const source = binding ?? inherited;
142
- const resolved = useMemo(() => resolveKitsBinding(source), [source]);
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 a no-op.
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 {
@@ -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 the nearest enclosing
67
- * container or `KitsBindingProvider`, then `KitsHost`. Keep it stable.
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 the enclosing
80
- * container's (the parent mini app's), then `KitsHost`. Keep it stable.
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/foundation",
3
- "version": "0.169.16-beta.6",
3
+ "version": "0.169.16-beta.7",
4
4
  "description": "React Native Component Kits",
5
5
  "keywords": [
6
6
  "@momo-kits/foundation"