@momo-kits/foundation 0.169.19-beta.4 → 0.169.19-beta.5
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/hooks.ts +3 -0
- package/ComponentCounter/__tests__/hooks.test.tsx +227 -0
- package/ComponentCounter/__tests__/utils.test.ts +249 -0
- package/ComponentCounter/hooks.ts +44 -0
- package/ComponentCounter/index.tsx +57 -0
- package/ComponentCounter/styles.ts +25 -0
- package/ComponentCounter/utils.ts +73 -0
- package/Layout/FloatingButton.tsx +7 -19
- package/index.ts +1 -0
- package/package.json +1 -1
package/Application/hooks.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
ScreenContext,
|
|
7
7
|
TrackingScopeContext,
|
|
8
8
|
} from '../Context';
|
|
9
|
+
import { useCountComponent } from '../ComponentCounter/hooks';
|
|
9
10
|
import {
|
|
10
11
|
type AnimatedCompatValue,
|
|
11
12
|
type NavigationTreeState,
|
|
@@ -27,6 +28,8 @@ const useComponentId = (componentName: string, accessibilityLabel?: string) => {
|
|
|
27
28
|
|
|
28
29
|
const scope = scopeName ? `${scopeName}/` : '';
|
|
29
30
|
|
|
31
|
+
useCountComponent(screen.screenName ?? 'unknown', componentName);
|
|
32
|
+
|
|
30
33
|
const componentId = useMemo(() => {
|
|
31
34
|
if (accessibilityLabel) {
|
|
32
35
|
return accessibilityLabel;
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { act, create, type ReactTestRenderer } from 'react-test-renderer';
|
|
3
|
+
import { useComponentCounts, useCountComponent } from '../hooks';
|
|
4
|
+
|
|
5
|
+
type Counts = Record<string, number>;
|
|
6
|
+
|
|
7
|
+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =
|
|
8
|
+
true;
|
|
9
|
+
|
|
10
|
+
let screenSeed = 0;
|
|
11
|
+
const nextScreen = () => {
|
|
12
|
+
screenSeed += 1;
|
|
13
|
+
return `screen-${screenSeed}`;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const Probe: React.FC<{ screen: string; name: string }> = ({
|
|
17
|
+
screen,
|
|
18
|
+
name,
|
|
19
|
+
}) => {
|
|
20
|
+
useCountComponent(screen, name);
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const Reader: React.FC<{ screen: string; read: (counts: Counts) => void }> = ({
|
|
25
|
+
screen,
|
|
26
|
+
read,
|
|
27
|
+
}) => {
|
|
28
|
+
read(useComponentCounts(screen));
|
|
29
|
+
return null;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type Probes = { screen: string; name: string }[];
|
|
33
|
+
|
|
34
|
+
const mount = (watched: string, probes: Probes) => {
|
|
35
|
+
let latest: Counts = {};
|
|
36
|
+
const read = (counts: Counts) => {
|
|
37
|
+
latest = counts;
|
|
38
|
+
};
|
|
39
|
+
const tree = (list: Probes) => (
|
|
40
|
+
<>
|
|
41
|
+
<Reader screen={watched} read={read} />
|
|
42
|
+
{list.map((probe, index) => (
|
|
43
|
+
<Probe key={index} screen={probe.screen} name={probe.name} />
|
|
44
|
+
))}
|
|
45
|
+
</>
|
|
46
|
+
);
|
|
47
|
+
let renderer: ReactTestRenderer | undefined;
|
|
48
|
+
act(() => {
|
|
49
|
+
renderer = create(tree(probes));
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
counts: () => latest,
|
|
53
|
+
update: (list: Probes) => {
|
|
54
|
+
act(() => renderer?.update(tree(list)));
|
|
55
|
+
},
|
|
56
|
+
unmount: () => {
|
|
57
|
+
act(() => renderer?.unmount());
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const same = (screen: string, name: string, size: number): Probes => {
|
|
63
|
+
return Array.from({ length: size }, () => ({ screen, name }));
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe('useCountComponent: counting mounts', () => {
|
|
67
|
+
it.each(Array.from({ length: 10 }, (_, index) => index + 1))(
|
|
68
|
+
'counts %i mounted components of one type',
|
|
69
|
+
size => {
|
|
70
|
+
const screen = nextScreen();
|
|
71
|
+
const view = mount(screen, same(screen, 'Button', size));
|
|
72
|
+
expect(view.counts()).toEqual({ Button: size });
|
|
73
|
+
view.unmount();
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
it('reports nothing for a screen without components', () => {
|
|
78
|
+
const screen = nextScreen();
|
|
79
|
+
const view = mount(screen, []);
|
|
80
|
+
expect(view.counts()).toEqual({});
|
|
81
|
+
view.unmount();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('useCountComponent: unmount cleanup', () => {
|
|
86
|
+
it.each([1, 2, 3, 4, 5])('clears %i components on unmount', size => {
|
|
87
|
+
const screen = nextScreen();
|
|
88
|
+
const view = mount(screen, same(screen, 'Switch', size));
|
|
89
|
+
view.unmount();
|
|
90
|
+
const reader = mount(screen, []);
|
|
91
|
+
expect(reader.counts()).toEqual({});
|
|
92
|
+
reader.unmount();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it.each([2, 3, 4, 5])(
|
|
96
|
+
'drops one of %i components when it is removed',
|
|
97
|
+
size => {
|
|
98
|
+
const screen = nextScreen();
|
|
99
|
+
const view = mount(screen, same(screen, 'Tag', size));
|
|
100
|
+
view.update(same(screen, 'Tag', size - 1));
|
|
101
|
+
expect(view.counts()).toEqual({ Tag: size - 1 });
|
|
102
|
+
view.unmount();
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
it('removes the entry instead of leaving a zero', () => {
|
|
107
|
+
const screen = nextScreen();
|
|
108
|
+
const view = mount(screen, same(screen, 'Tag', 1));
|
|
109
|
+
view.update([]);
|
|
110
|
+
expect(Object.keys(view.counts())).toEqual([]);
|
|
111
|
+
view.unmount();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('useCountComponent: grouping by type', () => {
|
|
116
|
+
it.each([
|
|
117
|
+
['Button/Pay', 'Button'],
|
|
118
|
+
['Button/Cancel', 'Button'],
|
|
119
|
+
['Tag/Label', 'Tag'],
|
|
120
|
+
['Input/Money/Amount', 'Input'],
|
|
121
|
+
['Switch', 'Switch'],
|
|
122
|
+
])('counts "%s" under "%s"', (name, type) => {
|
|
123
|
+
const screen = nextScreen();
|
|
124
|
+
const view = mount(screen, same(screen, name, 1));
|
|
125
|
+
expect(view.counts()).toEqual({ [type]: 1 });
|
|
126
|
+
view.unmount();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it.each([2, 3, 4, 5, 6])(
|
|
130
|
+
'merges %i differently labelled buttons into one entry',
|
|
131
|
+
size => {
|
|
132
|
+
const screen = nextScreen();
|
|
133
|
+
const probes = Array.from({ length: size }, (_, index) => ({
|
|
134
|
+
screen,
|
|
135
|
+
name: `Button/Label ${index}`,
|
|
136
|
+
}));
|
|
137
|
+
const view = mount(screen, probes);
|
|
138
|
+
expect(view.counts()).toEqual({ Button: size });
|
|
139
|
+
view.unmount();
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
it('keeps the count when a label changes but the type does not', () => {
|
|
144
|
+
const screen = nextScreen();
|
|
145
|
+
const view = mount(screen, same(screen, 'Button/A', 2));
|
|
146
|
+
view.update(same(screen, 'Button/B', 2));
|
|
147
|
+
expect(view.counts()).toEqual({ Button: 2 });
|
|
148
|
+
view.unmount();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('moves the count when the type changes', () => {
|
|
152
|
+
const screen = nextScreen();
|
|
153
|
+
const view = mount(screen, same(screen, 'Button/A', 1));
|
|
154
|
+
view.update(same(screen, 'Tag/A', 1));
|
|
155
|
+
expect(view.counts()).toEqual({ Tag: 1 });
|
|
156
|
+
view.unmount();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('useCountComponent: mixed types', () => {
|
|
161
|
+
const sizes = [0, 1, 2, 3];
|
|
162
|
+
const pairs = sizes.flatMap(buttons => sizes.map(tags => [buttons, tags]));
|
|
163
|
+
|
|
164
|
+
it.each(pairs)('%i buttons with %i tags', (buttons, tags) => {
|
|
165
|
+
const screen = nextScreen();
|
|
166
|
+
const probes = [
|
|
167
|
+
...same(screen, 'Button/x', buttons),
|
|
168
|
+
...same(screen, 'Tag/y', tags),
|
|
169
|
+
];
|
|
170
|
+
const view = mount(screen, probes);
|
|
171
|
+
const expected: Counts = {};
|
|
172
|
+
if (buttons > 0) {
|
|
173
|
+
expected.Button = buttons;
|
|
174
|
+
}
|
|
175
|
+
if (tags > 0) {
|
|
176
|
+
expected.Tag = tags;
|
|
177
|
+
}
|
|
178
|
+
expect(view.counts()).toEqual(expected);
|
|
179
|
+
view.unmount();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('useCountComponent: screen isolation', () => {
|
|
184
|
+
it.each([1, 2, 3])('keeps %i other screens out of the count', others => {
|
|
185
|
+
const screen = nextScreen();
|
|
186
|
+
const probes = [
|
|
187
|
+
...same(screen, 'Button', 2),
|
|
188
|
+
...Array.from({ length: others }, () => ({
|
|
189
|
+
screen: nextScreen(),
|
|
190
|
+
name: 'Button',
|
|
191
|
+
})),
|
|
192
|
+
];
|
|
193
|
+
const view = mount(screen, probes);
|
|
194
|
+
expect(view.counts()).toEqual({ Button: 2 });
|
|
195
|
+
view.unmount();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('counts the same screen from separate roots together', () => {
|
|
199
|
+
const screen = nextScreen();
|
|
200
|
+
const first = mount(screen, same(screen, 'Tag', 1));
|
|
201
|
+
const second = mount(screen, same(screen, 'Tag', 2));
|
|
202
|
+
expect(second.counts()).toEqual({ Tag: 3 });
|
|
203
|
+
first.unmount();
|
|
204
|
+
second.unmount();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe('useComponentCounts: snapshot stability', () => {
|
|
209
|
+
it('returns the same object between unrelated renders', () => {
|
|
210
|
+
const screen = nextScreen();
|
|
211
|
+
const seen: Counts[] = [];
|
|
212
|
+
const view = mount(screen, same(screen, 'Tag', 1));
|
|
213
|
+
seen.push(view.counts());
|
|
214
|
+
view.update(same(screen, 'Tag', 1));
|
|
215
|
+
seen.push(view.counts());
|
|
216
|
+
expect(seen[1]).toBe(seen[0]);
|
|
217
|
+
view.unmount();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('returns a stable empty object for an unknown screen', () => {
|
|
221
|
+
const first = mount(nextScreen(), []);
|
|
222
|
+
const second = mount(nextScreen(), []);
|
|
223
|
+
expect(first.counts()).toBe(second.counts());
|
|
224
|
+
first.unmount();
|
|
225
|
+
second.unmount();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Fiber,
|
|
3
|
+
countViews,
|
|
4
|
+
getInstanceFiber,
|
|
5
|
+
getScopeRoot,
|
|
6
|
+
} from '../utils';
|
|
7
|
+
|
|
8
|
+
const HOST = 5;
|
|
9
|
+
const TEXT = 6;
|
|
10
|
+
const COMPOSITE = 0;
|
|
11
|
+
const KIT_CONTEXT = { name: 'ComponentContext' };
|
|
12
|
+
const OTHER_CONTEXT = { name: 'OtherContext' };
|
|
13
|
+
|
|
14
|
+
type Spec = {
|
|
15
|
+
tag?: number;
|
|
16
|
+
type?: unknown;
|
|
17
|
+
children?: Spec[];
|
|
18
|
+
capture?: (fiber: Fiber) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const build = (spec: Spec, parent: Fiber | null = null): Fiber => {
|
|
22
|
+
const fiber: Fiber = {
|
|
23
|
+
tag: spec.tag ?? COMPOSITE,
|
|
24
|
+
type: spec.type ?? null,
|
|
25
|
+
child: null,
|
|
26
|
+
sibling: null,
|
|
27
|
+
return: parent,
|
|
28
|
+
alternate: null,
|
|
29
|
+
};
|
|
30
|
+
let previous: Fiber | null = null;
|
|
31
|
+
(spec.children ?? []).forEach(childSpec => {
|
|
32
|
+
const child = build(childSpec, fiber);
|
|
33
|
+
if (previous) {
|
|
34
|
+
previous.sibling = child;
|
|
35
|
+
} else {
|
|
36
|
+
fiber.child = child;
|
|
37
|
+
}
|
|
38
|
+
previous = child;
|
|
39
|
+
});
|
|
40
|
+
spec.capture?.(fiber);
|
|
41
|
+
return fiber;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const host = (...children: Spec[]): Spec => ({ tag: HOST, children });
|
|
45
|
+
const kit = (...children: Spec[]): Spec => ({
|
|
46
|
+
type: KIT_CONTEXT,
|
|
47
|
+
children,
|
|
48
|
+
});
|
|
49
|
+
const hosts = (count: number): Spec[] => {
|
|
50
|
+
return Array.from({ length: count }, () => host());
|
|
51
|
+
};
|
|
52
|
+
const root = (...children: Spec[]) => build(host(...children));
|
|
53
|
+
const count = (rootFiber: Fiber, skip: Fiber | null = null) => {
|
|
54
|
+
return countViews(rootFiber, KIT_CONTEXT, skip);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
describe('countViews: flat host views', () => {
|
|
58
|
+
it.each(Array.from({ length: 21 }, (_, index) => index))(
|
|
59
|
+
'counts %i sibling views as not kit',
|
|
60
|
+
size => {
|
|
61
|
+
expect(count(root(...hosts(size)))).toEqual({ total: size, kit: 0 });
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('countViews: views inside a kit provider', () => {
|
|
67
|
+
it.each(Array.from({ length: 15 }, (_, index) => index + 1))(
|
|
68
|
+
'counts %i views under one kit provider as kit',
|
|
69
|
+
size => {
|
|
70
|
+
expect(count(root(kit(...hosts(size))))).toEqual({
|
|
71
|
+
total: size,
|
|
72
|
+
kit: size,
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
it('recognises a provider exposing the context through _context', () => {
|
|
78
|
+
const provider: Spec = {
|
|
79
|
+
type: { _context: KIT_CONTEXT },
|
|
80
|
+
children: hosts(2),
|
|
81
|
+
};
|
|
82
|
+
expect(count(root(provider))).toEqual({ total: 2, kit: 2 });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('recognises a provider that is the context itself', () => {
|
|
86
|
+
expect(count(root(kit(host(), host(), host())))).toEqual({
|
|
87
|
+
total: 3,
|
|
88
|
+
kit: 3,
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('ignores providers of another context', () => {
|
|
93
|
+
const other: Spec = { type: OTHER_CONTEXT, children: hosts(4) };
|
|
94
|
+
expect(count(root(other))).toEqual({ total: 4, kit: 0 });
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('countViews: nesting depth', () => {
|
|
99
|
+
const chain = (depth: number): Spec => {
|
|
100
|
+
let spec = host();
|
|
101
|
+
for (let level = 1; level < depth; level += 1) {
|
|
102
|
+
spec = host(spec);
|
|
103
|
+
}
|
|
104
|
+
return spec;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
it.each(Array.from({ length: 12 }, (_, index) => index + 1))(
|
|
108
|
+
'counts a host chain %i levels deep',
|
|
109
|
+
depth => {
|
|
110
|
+
expect(count(root(chain(depth)))).toEqual({ total: depth, kit: 0 });
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
it('counts a kit subtree nested inside a kit subtree once per view', () => {
|
|
115
|
+
expect(count(root(kit(host(kit(host(host()))))))).toEqual({
|
|
116
|
+
total: 3,
|
|
117
|
+
kit: 3,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('countViews: kit and non-kit mix', () => {
|
|
123
|
+
const sizes = [0, 1, 2, 3];
|
|
124
|
+
const pairs = sizes.flatMap(plain => sizes.map(inKit => [plain, inKit]));
|
|
125
|
+
|
|
126
|
+
it.each(pairs)('%i plain views next to %i kit views', (plain, inKit) => {
|
|
127
|
+
const tree = root(...hosts(plain), kit(...hosts(inKit)));
|
|
128
|
+
expect(count(tree)).toEqual({ total: plain + inKit, kit: inKit });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('keeps siblings after a kit subtree out of kit', () => {
|
|
132
|
+
expect(count(root(kit(host()), host()))).toEqual({ total: 2, kit: 1 });
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('counts a plain view that wraps a kit component', () => {
|
|
136
|
+
expect(count(root(host(kit(host()))))).toEqual({ total: 2, kit: 1 });
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('countViews: tags that are not host views', () => {
|
|
141
|
+
it('ignores text instances', () => {
|
|
142
|
+
expect(count(root({ tag: TEXT }, host()))).toEqual({ total: 1, kit: 0 });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('ignores composite wrappers but counts their host children', () => {
|
|
146
|
+
const wrapper: Spec = { tag: COMPOSITE, children: hosts(2) };
|
|
147
|
+
expect(count(root(wrapper))).toEqual({ total: 2, kit: 0 });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('returns zero for an empty root', () => {
|
|
151
|
+
expect(count(root())).toEqual({ total: 0, kit: 0 });
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('countViews: skipped subtree', () => {
|
|
156
|
+
it.each([0, 1, 2, 3, 4])('skips a subtree with %i descendants', size => {
|
|
157
|
+
let skipped: Fiber | null = null;
|
|
158
|
+
const badge: Spec = {
|
|
159
|
+
tag: HOST,
|
|
160
|
+
children: hosts(size),
|
|
161
|
+
capture: fiber => {
|
|
162
|
+
skipped = fiber;
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
const tree = root(host(), badge, host());
|
|
166
|
+
expect(count(tree, skipped)).toEqual({ total: 2, kit: 0 });
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('skips a fiber through its alternate', () => {
|
|
170
|
+
let skipped: Fiber | null = null;
|
|
171
|
+
const badge: Spec = {
|
|
172
|
+
tag: HOST,
|
|
173
|
+
children: hosts(2),
|
|
174
|
+
capture: fiber => {
|
|
175
|
+
skipped = fiber;
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
const tree = root(host(), badge);
|
|
179
|
+
const current = tree.child?.sibling as Fiber;
|
|
180
|
+
const alternate = { ...current, alternate: skipped };
|
|
181
|
+
tree.child!.sibling = alternate;
|
|
182
|
+
expect(count(tree, skipped)).toEqual({ total: 1, kit: 0 });
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('counts everything when nothing is skipped', () => {
|
|
186
|
+
expect(count(root(host(), host(host())), null)).toEqual({
|
|
187
|
+
total: 3,
|
|
188
|
+
kit: 0,
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('getInstanceFiber', () => {
|
|
194
|
+
const fiber = build(host());
|
|
195
|
+
const devFiber = build(host());
|
|
196
|
+
|
|
197
|
+
it('returns null for null', () => {
|
|
198
|
+
expect(getInstanceFiber(null)).toBeNull();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('returns null for an object without a handle', () => {
|
|
202
|
+
expect(getInstanceFiber({})).toBeNull();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('reads __internalInstanceHandle', () => {
|
|
206
|
+
expect(getInstanceFiber({ __internalInstanceHandle: fiber })).toBe(fiber);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('falls back to the dev handle', () => {
|
|
210
|
+
const owner = { _internalFiberInstanceHandleDEV: devFiber };
|
|
211
|
+
expect(getInstanceFiber(owner)).toBe(devFiber);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('prefers __internalInstanceHandle over the dev handle', () => {
|
|
215
|
+
const owner = {
|
|
216
|
+
__internalInstanceHandle: fiber,
|
|
217
|
+
_internalFiberInstanceHandleDEV: devFiber,
|
|
218
|
+
};
|
|
219
|
+
expect(getInstanceFiber(owner)).toBe(fiber);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe('getScopeRoot', () => {
|
|
224
|
+
it('returns null for null', () => {
|
|
225
|
+
expect(getScopeRoot(null)).toBeNull();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('returns null when there is no host ancestor', () => {
|
|
229
|
+
const tree = build({ children: [{ children: [{}] }] });
|
|
230
|
+
expect(getScopeRoot(tree.child?.child ?? null)).toBeNull();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('returns the direct host parent', () => {
|
|
234
|
+
const tree = build(host(host()));
|
|
235
|
+
expect(getScopeRoot(tree.child)).toBe(tree);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('skips composite parents up to the nearest host', () => {
|
|
239
|
+
const tree = build(host({ children: [{ children: [host()] }] }));
|
|
240
|
+
const leaf = tree.child?.child?.child ?? null;
|
|
241
|
+
expect(getScopeRoot(leaf)).toBe(tree);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('returns the nearest host, not the topmost', () => {
|
|
245
|
+
const tree = build(host(host(host())));
|
|
246
|
+
const leaf = tree.child?.child ?? null;
|
|
247
|
+
expect(getScopeRoot(leaf)).toBe(tree.child);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useEffect, useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
3
|
+
type Counts = Record<string, number>;
|
|
4
|
+
type Snapshot = Record<string, Counts>;
|
|
5
|
+
|
|
6
|
+
const EMPTY_COUNTS: Counts = {};
|
|
7
|
+
const listeners = new Set<() => void>();
|
|
8
|
+
let snapshot: Snapshot = {};
|
|
9
|
+
|
|
10
|
+
const changeCount = (screenName: string, componentName: string, delta: number) => {
|
|
11
|
+
const current = snapshot[screenName] ?? EMPTY_COUNTS;
|
|
12
|
+
const value = (current[componentName] ?? 0) + delta;
|
|
13
|
+
const nextScreen = { ...current, [componentName]: value };
|
|
14
|
+
if (value <= 0) {
|
|
15
|
+
delete nextScreen[componentName];
|
|
16
|
+
}
|
|
17
|
+
snapshot = { ...snapshot, [screenName]: nextScreen };
|
|
18
|
+
listeners.forEach(listener => listener());
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const subscribe = (listener: () => void) => {
|
|
22
|
+
listeners.add(listener);
|
|
23
|
+
return () => {
|
|
24
|
+
listeners.delete(listener);
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Registers one mounted kit component under its screen, removed on unmount. */
|
|
29
|
+
const useCountComponent = (screenName: string, componentName: string) => {
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const type = componentName.split('/')[0];
|
|
32
|
+
changeCount(screenName, type, 1);
|
|
33
|
+
return () => changeCount(screenName, type, -1);
|
|
34
|
+
}, [screenName, componentName]);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Live per-component counts of the kit components mounted on a screen. */
|
|
38
|
+
const useComponentCounts = (screenName: string): Counts => {
|
|
39
|
+
return useSyncExternalStore(subscribe, () => {
|
|
40
|
+
return snapshot[screenName] ?? EMPTY_COUNTS;
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { useCountComponent, useComponentCounts };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import React, { FC, useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Text, TouchableOpacity, View } from 'react-native';
|
|
3
|
+
import { ComponentContext, ScreenContext } from '../Context';
|
|
4
|
+
import { useComponentCounts } from './hooks';
|
|
5
|
+
import {
|
|
6
|
+
type ViewCounts,
|
|
7
|
+
countViews,
|
|
8
|
+
getInstanceFiber,
|
|
9
|
+
getScopeRoot,
|
|
10
|
+
} from './utils';
|
|
11
|
+
import styles from './styles';
|
|
12
|
+
|
|
13
|
+
/** Floating badge with the kit components and native views on this screen. */
|
|
14
|
+
const ComponentCounterBadge: FC = () => {
|
|
15
|
+
const screen = useContext<any>(ScreenContext);
|
|
16
|
+
const counts = useComponentCounts(screen.screenName ?? 'unknown');
|
|
17
|
+
const [isExpanded, setExpanded] = useState(false);
|
|
18
|
+
const [views, setViews] = useState<ViewCounts>({ total: 0, kit: 0 });
|
|
19
|
+
const containerRef = useRef<View>(null);
|
|
20
|
+
|
|
21
|
+
const scanViews = () => {
|
|
22
|
+
const self = getInstanceFiber(containerRef.current);
|
|
23
|
+
const root = getScopeRoot(self);
|
|
24
|
+
if (root) {
|
|
25
|
+
setViews(countViews(root, ComponentContext, self));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
useEffect(scanViews, [counts, isExpanded]);
|
|
30
|
+
|
|
31
|
+
const entries = Object.entries(counts).sort((a, b) => b[1] - a[1]);
|
|
32
|
+
const total = entries.reduce((sum, [, count]) => sum + count, 0);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<View ref={containerRef} style={styles.container} pointerEvents="box-none">
|
|
36
|
+
<TouchableOpacity
|
|
37
|
+
style={styles.badge}
|
|
38
|
+
onPress={() => {
|
|
39
|
+
scanViews();
|
|
40
|
+
setExpanded(!isExpanded);
|
|
41
|
+
}}>
|
|
42
|
+
<Text style={styles.title}>{`Views: ${views.total}`}</Text>
|
|
43
|
+
<Text style={styles.row}>{`In kit: ${views.kit}`}</Text>
|
|
44
|
+
<Text style={styles.row}>
|
|
45
|
+
{`Not kit: ${views.total - views.kit}`}
|
|
46
|
+
</Text>
|
|
47
|
+
<Text style={styles.title}>{`Kit components: ${total}`}</Text>
|
|
48
|
+
{isExpanded &&
|
|
49
|
+
entries.map(([name, count]) => (
|
|
50
|
+
<Text key={name} style={styles.row}>{`${name}: ${count}`}</Text>
|
|
51
|
+
))}
|
|
52
|
+
</TouchableOpacity>
|
|
53
|
+
</View>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export { ComponentCounterBadge };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { StyleSheet } from 'react-native';
|
|
2
|
+
import { Colors, Radius, Spacing } from '../Consts';
|
|
3
|
+
|
|
4
|
+
export default StyleSheet.create({
|
|
5
|
+
container: {
|
|
6
|
+
position: 'absolute',
|
|
7
|
+
right: Spacing.M,
|
|
8
|
+
bottom: Spacing.Size3XL,
|
|
9
|
+
alignItems: 'flex-end',
|
|
10
|
+
},
|
|
11
|
+
badge: {
|
|
12
|
+
backgroundColor: Colors.black_17,
|
|
13
|
+
borderRadius: Radius.S,
|
|
14
|
+
paddingHorizontal: Spacing.M,
|
|
15
|
+
paddingVertical: Spacing.S,
|
|
16
|
+
},
|
|
17
|
+
title: {
|
|
18
|
+
color: Colors.black_01,
|
|
19
|
+
fontWeight: '600',
|
|
20
|
+
},
|
|
21
|
+
row: {
|
|
22
|
+
color: Colors.black_01,
|
|
23
|
+
marginTop: Spacing.XXS,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
type Fiber = {
|
|
2
|
+
tag: number;
|
|
3
|
+
type: unknown;
|
|
4
|
+
child: Fiber | null;
|
|
5
|
+
sibling: Fiber | null;
|
|
6
|
+
return: Fiber | null;
|
|
7
|
+
alternate: Fiber | null;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type FiberOwner = {
|
|
11
|
+
__internalInstanceHandle?: Fiber;
|
|
12
|
+
_internalFiberInstanceHandleDEV?: Fiber;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type ViewCounts = { total: number; kit: number };
|
|
16
|
+
|
|
17
|
+
const HOST_COMPONENT_TAG = 5;
|
|
18
|
+
|
|
19
|
+
const getInstanceFiber = (instance: unknown): Fiber | null => {
|
|
20
|
+
const owner = instance as FiberOwner | null;
|
|
21
|
+
const fiber = owner?.__internalInstanceHandle;
|
|
22
|
+
return fiber ?? owner?._internalFiberInstanceHandleDEV ?? null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const getScopeRoot = (fiber: Fiber | null): Fiber | null => {
|
|
26
|
+
let node = fiber?.return ?? null;
|
|
27
|
+
while (node && node.tag !== HOST_COMPONENT_TAG) {
|
|
28
|
+
node = node.return;
|
|
29
|
+
}
|
|
30
|
+
return node;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const isProviderOf = (fiber: Fiber, context: unknown) => {
|
|
34
|
+
const providerContext = (fiber.type as { _context?: unknown })?._context;
|
|
35
|
+
return fiber.type === context || providerContext === context;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const isSkipped = (fiber: Fiber, skip: Fiber | null) => {
|
|
39
|
+
return !!skip && (fiber === skip || fiber.alternate === skip);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Native views under `root`, ignoring the `skip` subtree. Views below a
|
|
44
|
+
* `context` provider (the one every kit component wraps itself in) count as kit.
|
|
45
|
+
*/
|
|
46
|
+
const countViews = (
|
|
47
|
+
root: Fiber,
|
|
48
|
+
context: unknown,
|
|
49
|
+
skip: Fiber | null,
|
|
50
|
+
): ViewCounts => {
|
|
51
|
+
const counts: ViewCounts = { total: 0, kit: 0 };
|
|
52
|
+
|
|
53
|
+
const visit = (first: Fiber | null, isInKit: boolean) => {
|
|
54
|
+
let node = first;
|
|
55
|
+
while (node) {
|
|
56
|
+
if (!isSkipped(node, skip)) {
|
|
57
|
+
const isKit = isInKit || isProviderOf(node, context);
|
|
58
|
+
if (node.tag === HOST_COMPONENT_TAG) {
|
|
59
|
+
counts.total += 1;
|
|
60
|
+
counts.kit += isKit ? 1 : 0;
|
|
61
|
+
}
|
|
62
|
+
visit(node.child, isKit);
|
|
63
|
+
}
|
|
64
|
+
node = node.sibling;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
visit(root.child, false);
|
|
69
|
+
return counts;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type { Fiber, ViewCounts };
|
|
73
|
+
export { getInstanceFiber, getScopeRoot, countViews };
|
|
@@ -64,11 +64,8 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
64
64
|
const labelFontFamily = exportFontFamily('bold');
|
|
65
65
|
const maxWidth = useSharedValue(0);
|
|
66
66
|
const minWidth = size === 'small' ? SMALL_SIZE : LARGE_SIZE;
|
|
67
|
-
const iconSize = size === 'small' ? SMALL_ICON_SIZE : LARGE_ICON_SIZE;
|
|
68
67
|
const opacityAnimated = useSharedValue(0);
|
|
69
68
|
const widthAnimated = useSharedValue<number | null>(null);
|
|
70
|
-
const paddingLeftAnimated = useSharedValue(Spacing.M);
|
|
71
|
-
const collapsedPaddingLeft = useSharedValue(Spacing.M);
|
|
72
69
|
const lastOffset = useSharedValue(0);
|
|
73
70
|
const lastDirection = useSharedValue<string | null>(null);
|
|
74
71
|
const [isLabelShown, setIsLabelShown] = useState(true);
|
|
@@ -92,9 +89,6 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
92
89
|
opacityAnimated.value = withTiming(0, {
|
|
93
90
|
duration: ANIMATION_DURATION,
|
|
94
91
|
});
|
|
95
|
-
paddingLeftAnimated.value = withTiming(collapsedPaddingLeft.value, {
|
|
96
|
-
duration: ANIMATION_DURATION,
|
|
97
|
-
});
|
|
98
92
|
widthAnimated.value = withTiming(
|
|
99
93
|
minWidth,
|
|
100
94
|
{ duration: ANIMATION_DURATION },
|
|
@@ -106,9 +100,6 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
106
100
|
opacityAnimated.value = withTiming(1, {
|
|
107
101
|
duration: ANIMATION_DURATION,
|
|
108
102
|
});
|
|
109
|
-
paddingLeftAnimated.value = withTiming(Spacing.M, {
|
|
110
|
-
duration: ANIMATION_DURATION,
|
|
111
|
-
});
|
|
112
103
|
widthAnimated.value = withTiming(
|
|
113
104
|
maxWidth.value,
|
|
114
105
|
{ duration: ANIMATION_DURATION },
|
|
@@ -125,7 +116,6 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
125
116
|
|
|
126
117
|
const containerStyle = useAnimatedStyle(() => ({
|
|
127
118
|
width: widthAnimated.value ?? undefined,
|
|
128
|
-
paddingLeft: paddingLeftAnimated.value,
|
|
129
119
|
}));
|
|
130
120
|
|
|
131
121
|
const labelStyle = useAnimatedStyle(() => ({
|
|
@@ -135,14 +125,8 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
135
125
|
const handleLayout = (event: LayoutChangeEvent) => {
|
|
136
126
|
const layout = event.nativeEvent.layout;
|
|
137
127
|
if (widthAnimated.value != null) return;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
maxWidth.value = expandedWidth;
|
|
141
|
-
widthAnimated.value = expandedWidth;
|
|
142
|
-
// collapsed it is a circle as wide as it is tall: center the icon from the
|
|
143
|
-
// rendered height so it stays centered when sizes are scaled but padding is not
|
|
144
|
-
collapsedPaddingLeft.value =
|
|
145
|
-
(layout.height - (layout.height * iconSize) / minWidth) / 2;
|
|
128
|
+
maxWidth.value = layout.width;
|
|
129
|
+
widthAnimated.value = layout.width;
|
|
146
130
|
};
|
|
147
131
|
|
|
148
132
|
if (renderComponent) {
|
|
@@ -174,7 +158,11 @@ export const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
|
174
158
|
]}
|
|
175
159
|
>
|
|
176
160
|
<TouchableOpacity style={styles.content} onPress={onPress}>
|
|
177
|
-
<Icon
|
|
161
|
+
<Icon
|
|
162
|
+
source={icon}
|
|
163
|
+
size={size === 'small' ? SMALL_ICON_SIZE : LARGE_ICON_SIZE}
|
|
164
|
+
color={iconColor}
|
|
165
|
+
/>
|
|
178
166
|
{isLabelShown && !!label && (
|
|
179
167
|
<Animated.Text
|
|
180
168
|
allowFontScaling={false}
|
package/index.ts
CHANGED