@openpeepshq/rn-components 0.12.3 → 0.12.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/package.json +3 -3
- package/src/components/CapabilityMatrix.tsx +44 -6
- package/src/components/conversations/ConversationMessageBubble.tsx +3 -6
- package/src/components/custom/common/popover.tsx +4 -1
- package/src/components/custom/modals/media/document-picker-sheet.tsx +10 -3
- package/src/components/custom/modals/media/image-picker-sheet.test.tsx +201 -0
- package/src/components/custom/modals/media/image-picker-sheet.tsx +27 -17
- package/src/components/custom/modals/media/send-media-sheet.tsx +4 -1
- package/src/components/groups/GroupMembersList.test.tsx +156 -0
- package/src/components/groups/GroupMembersList.tsx +4 -1
- package/src/components/jams/JamCallParticipant.tsx +6 -1
- package/src/components/jams/JamDesktopFooter.tsx +6 -1
- package/src/components/jams/JamFooter.tsx +6 -1
- package/src/components/jams/JamGuestForm.tsx +6 -1
- package/src/components/jams/JamObserverShell.tsx +4 -1
- package/src/components/jams/JamRoomIndicators.test.tsx +143 -0
- package/src/components/jams/JamRoomIndicators.tsx +15 -8
- package/src/components/jams/JamToolbarButton.tsx +7 -1
- package/src/components/post/feed/events/EventsCalendar.tsx +6 -2
- package/src/components/post/pieces/EventOccurrenceList.tsx +7 -2
- package/src/components/post/pieces/UnreadPostIndicator.tsx +4 -4
- package/src/components/post/post-form/ComposeAttachments.tsx +6 -2
- package/src/components/post/post-form/ResourceForm.tsx +16 -16
- package/src/components/post/post-form/VisibilitySheet.tsx +3 -1
- package/src/components/post/types/event/FullEvent.test.tsx +136 -15
- package/src/components/post/types/event/FullEvent.tsx +21 -5
- package/src/components/profile/ProfileSelector.test.tsx +115 -0
- package/src/components/profile/ProfileSelector.tsx +2 -2
- package/src/components/profile/ReportProfileOrPostModal.tsx +4 -1
- package/src/components/resources/CategoryPathPicker.tsx +117 -0
- package/src/components/resources/ResourceCard.test.tsx +12 -0
- package/src/components/resources/ResourceCard.tsx +8 -3
- package/src/components/resources/ResourceLibrary.test.tsx +334 -0
- package/src/components/resources/ResourceLibrary.tsx +5 -5
- package/src/components/resources/index.ts +2 -0
- package/src/pages/Welcome.tsx +4 -1
- package/src/theme/OpenPeepsThemeProvider.tsx +6 -5
- package/src/theme/color.ts +29 -0
- package/src/theme/useThemeOpacity.ts +30 -0
- package/src/theme/utils.ts +11 -22
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression tests for the folder-tree collision (issue #2148) and the
|
|
3
|
+
* RN CategoryPathPicker's web-parity semantics.
|
|
4
|
+
*
|
|
5
|
+
* A folder literally named `[TEST] A/B` and a folder `[TEST] A` containing
|
|
6
|
+
* a subfolder `B` used to produce the same `/`-joined identity key, so the
|
|
7
|
+
* two collapsed into one tree node (counts 2 instead of 1/1, shared
|
|
8
|
+
* expand/selection). The picker test locks in the maintainer's ask: typing
|
|
9
|
+
* `A/B` into "Add a folder" must report a single segment, never split.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const React = require('react');
|
|
13
|
+
const ReactTestRenderer = require('react-test-renderer');
|
|
14
|
+
|
|
15
|
+
let mockTranslations: Record<string, string> = {};
|
|
16
|
+
let mockLibrary: Record<string, unknown> = {};
|
|
17
|
+
|
|
18
|
+
jest.mock('react-native', () => ({
|
|
19
|
+
View: (props: { children?: unknown }) =>
|
|
20
|
+
React.createElement('View', null, props.children),
|
|
21
|
+
Pressable: (props: {
|
|
22
|
+
children?: unknown;
|
|
23
|
+
onPress?: () => void;
|
|
24
|
+
accessibilityLabel?: string;
|
|
25
|
+
accessibilityRole?: string;
|
|
26
|
+
}) =>
|
|
27
|
+
React.createElement(
|
|
28
|
+
'Pressable',
|
|
29
|
+
{
|
|
30
|
+
onPress: props.onPress,
|
|
31
|
+
accessibilityLabel: props.accessibilityLabel,
|
|
32
|
+
accessibilityRole: props.accessibilityRole,
|
|
33
|
+
},
|
|
34
|
+
props.children
|
|
35
|
+
),
|
|
36
|
+
ScrollView: (props: { children?: unknown }) =>
|
|
37
|
+
React.createElement('ScrollView', null, props.children),
|
|
38
|
+
TextInput: (props: {
|
|
39
|
+
value?: string;
|
|
40
|
+
onChangeText?: (text: string) => void;
|
|
41
|
+
placeholder?: string;
|
|
42
|
+
}) =>
|
|
43
|
+
React.createElement('TextInput', {
|
|
44
|
+
value: props.value,
|
|
45
|
+
onChangeText: props.onChangeText,
|
|
46
|
+
placeholder: props.placeholder,
|
|
47
|
+
}),
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
jest.mock('react-i18next', () => ({
|
|
51
|
+
useTranslation: () => ({
|
|
52
|
+
t: (key: string, opts?: { defaultValue?: string }) =>
|
|
53
|
+
mockTranslations[key] ?? opts?.defaultValue ?? key,
|
|
54
|
+
}),
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
jest.mock('@react-navigation/native', () => ({
|
|
58
|
+
useNavigation: () => ({ navigate: jest.fn() }),
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
jest.mock('@openpeepshq/common/lib', () => ({
|
|
62
|
+
// Mirrored from platform/common/src/lib/resources.ts — the built
|
|
63
|
+
// package ships ESM which Jest's node transform doesn't cover.
|
|
64
|
+
categoryPathKey: (path: readonly string[]): string => JSON.stringify(path),
|
|
65
|
+
RESOURCE_KINDS: ['video', 'file', 'gallery', 'audio', 'link'],
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
jest.mock('@openpeepshq/react', () => ({
|
|
69
|
+
useResourceLibrary: () => mockLibrary,
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
jest.mock('../ui/input', () => ({
|
|
73
|
+
Input: (props: Record<string, unknown>) =>
|
|
74
|
+
React.createElement('TextInput', props),
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
jest.mock('../ui/loading-spinner', () => ({
|
|
78
|
+
LoadingSpinner: () => null,
|
|
79
|
+
}));
|
|
80
|
+
|
|
81
|
+
jest.mock('../ui/themed-text', () => ({
|
|
82
|
+
ThemedText: (props: { children?: unknown }) =>
|
|
83
|
+
React.createElement('ThemedText', null, props.children),
|
|
84
|
+
}));
|
|
85
|
+
|
|
86
|
+
jest.mock('../icons', () => ({
|
|
87
|
+
LayoutGridIcon: () => null,
|
|
88
|
+
LayoutListIcon: () => null,
|
|
89
|
+
BookmarkIcon: () => null,
|
|
90
|
+
ChevronDownIcon: () => null,
|
|
91
|
+
ChevronRightIcon: () => null,
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
jest.mock('./ResourceCard', () => ({
|
|
95
|
+
ResourceCard: () => null,
|
|
96
|
+
}));
|
|
97
|
+
|
|
98
|
+
import type { CategoryPathPickerProps } from './CategoryPathPicker';
|
|
99
|
+
import { CategoryPathPicker } from './CategoryPathPicker';
|
|
100
|
+
import type { ResourceLibraryProps } from './ResourceLibrary';
|
|
101
|
+
import { ResourceLibrary } from './ResourceLibrary';
|
|
102
|
+
|
|
103
|
+
const slashPost = {
|
|
104
|
+
id: 'p-slash',
|
|
105
|
+
type: 'resource',
|
|
106
|
+
data: {
|
|
107
|
+
type: 'resource',
|
|
108
|
+
title: 'Slash folder',
|
|
109
|
+
categoryPath: ['[TEST] A/B'],
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const nestedPost = {
|
|
114
|
+
id: 'p-nested',
|
|
115
|
+
type: 'resource',
|
|
116
|
+
data: {
|
|
117
|
+
type: 'resource',
|
|
118
|
+
title: 'Nested folder',
|
|
119
|
+
categoryPath: ['[TEST] A', 'B'],
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const libraryFor = (posts: unknown[]) => ({
|
|
124
|
+
posts,
|
|
125
|
+
tree: [
|
|
126
|
+
{
|
|
127
|
+
name: '[TEST] A/B',
|
|
128
|
+
path: ['[TEST] A/B'],
|
|
129
|
+
children: [],
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: '[TEST] A',
|
|
133
|
+
path: ['[TEST] A'],
|
|
134
|
+
children: [{ name: 'B', path: ['[TEST] A', 'B'], children: [] }],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
tags: [],
|
|
138
|
+
pathPrefix: [] as string[],
|
|
139
|
+
hierarchy: true,
|
|
140
|
+
setPathPrefix: jest.fn(),
|
|
141
|
+
setHierarchy: jest.fn(),
|
|
142
|
+
query: { isLoading: false },
|
|
143
|
+
search: '',
|
|
144
|
+
setSearch: jest.fn(),
|
|
145
|
+
kind: 'all',
|
|
146
|
+
setKind: jest.fn(),
|
|
147
|
+
view: 'list',
|
|
148
|
+
setView: jest.fn(),
|
|
149
|
+
saved: false,
|
|
150
|
+
setSaved: jest.fn(),
|
|
151
|
+
selectedTags: [] as string[],
|
|
152
|
+
toggleTag: jest.fn(),
|
|
153
|
+
tagsOpen: false,
|
|
154
|
+
setTagsOpen: jest.fn(),
|
|
155
|
+
activeFilterCount: 0,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const render = (element: React.ReactElement) => {
|
|
159
|
+
let tree: ReturnType<typeof ReactTestRenderer.create>;
|
|
160
|
+
ReactTestRenderer.act(() => {
|
|
161
|
+
tree = ReactTestRenderer.create(element);
|
|
162
|
+
});
|
|
163
|
+
return tree!;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const pressables = (tree: ReturnType<typeof ReactTestRenderer.create>) =>
|
|
167
|
+
tree.root.findAllByType('Pressable');
|
|
168
|
+
|
|
169
|
+
const pressLabel = (
|
|
170
|
+
tree: ReturnType<typeof ReactTestRenderer.create>,
|
|
171
|
+
label: string
|
|
172
|
+
) => {
|
|
173
|
+
const targets = pressables(tree).filter(
|
|
174
|
+
(node: { props: { accessibilityLabel?: string } }) =>
|
|
175
|
+
node.props.accessibilityLabel === label
|
|
176
|
+
);
|
|
177
|
+
ReactTestRenderer.act(() => {
|
|
178
|
+
targets.forEach((node: { props: { onPress?: () => void } }) =>
|
|
179
|
+
node.props.onPress?.()
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const textOf = (node: unknown): string => {
|
|
185
|
+
const props = (node as { props?: { children?: unknown } }).props;
|
|
186
|
+
if (!props) return '';
|
|
187
|
+
return React.Children.toArray(props.children ?? [])
|
|
188
|
+
.map((child) =>
|
|
189
|
+
typeof child === 'string'
|
|
190
|
+
? child
|
|
191
|
+
: textOf(child as unknown as { props?: { children?: unknown } })
|
|
192
|
+
)
|
|
193
|
+
.join('');
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
describe('ResourceLibrary folder tree collision (issue #2148)', () => {
|
|
197
|
+
beforeEach(() => {
|
|
198
|
+
mockTranslations = {};
|
|
199
|
+
mockLibrary = libraryFor([slashPost, nestedPost]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('renders two separate top-level nodes with counts of 1 each', () => {
|
|
203
|
+
const tree = render(
|
|
204
|
+
React.createElement(ResourceLibrary, {
|
|
205
|
+
groupId: 'g1',
|
|
206
|
+
} as ResourceLibraryProps)
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
const labels = pressables(tree).map(
|
|
210
|
+
(node: { props: { accessibilityLabel?: string } }) =>
|
|
211
|
+
node.props.accessibilityLabel
|
|
212
|
+
);
|
|
213
|
+
expect(labels).toContain('Hide folder tree');
|
|
214
|
+
expect(labels).toContain('Expand folder');
|
|
215
|
+
|
|
216
|
+
// The tree collapses behind an "All folders" root; expand it so the
|
|
217
|
+
// two top-level nodes render.
|
|
218
|
+
pressLabel(tree, 'Expand folder');
|
|
219
|
+
|
|
220
|
+
const texts = tree.root
|
|
221
|
+
.findAll((node: { props?: { children?: unknown } }) =>
|
|
222
|
+
Array.isArray(node.props?.children)
|
|
223
|
+
)
|
|
224
|
+
.map(textOf);
|
|
225
|
+
const flat = texts.join('|');
|
|
226
|
+
// The name renders as one leaf ("[TEST] A/B") and the count as another
|
|
227
|
+
// ("(1)"), separated by nested text nodes, so assert them independently.
|
|
228
|
+
expect(flat).toContain('[TEST] A/B');
|
|
229
|
+
expect(flat).toContain('[TEST] A');
|
|
230
|
+
const counts = flat.match(/\(\d+\)/g) ?? [];
|
|
231
|
+
expect(counts).not.toContain('(2)');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('selects the slash-named folder without highlighting the nested one', () => {
|
|
235
|
+
const tree = render(
|
|
236
|
+
React.createElement(ResourceLibrary, {
|
|
237
|
+
groupId: 'g1',
|
|
238
|
+
} as ResourceLibraryProps)
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
// The tree collapses behind an "All folders" root; expand it so the
|
|
242
|
+
// slash-named top-level node renders.
|
|
243
|
+
pressLabel(tree, 'Expand folder');
|
|
244
|
+
|
|
245
|
+
// Selecting is a pathPrefix change; the slash-named folder's path must
|
|
246
|
+
// be passed through as a single segment.
|
|
247
|
+
const slashNode = pressables(tree).find(
|
|
248
|
+
(node: { props: { children?: unknown } }) =>
|
|
249
|
+
textOf(node).startsWith('[TEST] A/B')
|
|
250
|
+
);
|
|
251
|
+
expect(slashNode).toBeTruthy();
|
|
252
|
+
ReactTestRenderer.act(() => {
|
|
253
|
+
slashNode!.props.onPress();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const setPathPrefix = mockLibrary.setPathPrefix as jest.Mock;
|
|
257
|
+
expect(setPathPrefix).toHaveBeenCalledWith(['[TEST] A/B']);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe('CategoryPathPicker RN parity', () => {
|
|
262
|
+
beforeEach(() => {
|
|
263
|
+
mockTranslations = {};
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('reports a single segment for a slash-containing folder name (never splits)', () => {
|
|
267
|
+
const onChange = jest.fn();
|
|
268
|
+
const tree = render(
|
|
269
|
+
React.createElement(CategoryPathPicker, {
|
|
270
|
+
path: [],
|
|
271
|
+
tree: [{ name: 'A', path: ['A'], children: [] }],
|
|
272
|
+
onChange,
|
|
273
|
+
} as unknown as CategoryPathPickerProps)
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
const input = tree.root.findAllByType('TextInput')[0];
|
|
277
|
+
ReactTestRenderer.act(() => {
|
|
278
|
+
input.props.onChangeText('A/B');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const add = tree.root
|
|
282
|
+
.findAllByType('Pressable')
|
|
283
|
+
.find(
|
|
284
|
+
(node: { props: { accessibilityLabel?: string } }) =>
|
|
285
|
+
node.props.accessibilityLabel === 'Add'
|
|
286
|
+
);
|
|
287
|
+
expect(add).toBeTruthy();
|
|
288
|
+
ReactTestRenderer.act(() => {
|
|
289
|
+
add!.props.onPress();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
expect(onChange).toHaveBeenCalledWith(['A/B']);
|
|
293
|
+
expect(onChange).not.toHaveBeenCalledWith(['A', 'B']);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it('ignores blank input and selects an existing folder from the tree', () => {
|
|
297
|
+
const onChange = jest.fn();
|
|
298
|
+
const tree = render(
|
|
299
|
+
React.createElement(CategoryPathPicker, {
|
|
300
|
+
path: [],
|
|
301
|
+
tree: [
|
|
302
|
+
{
|
|
303
|
+
name: '[TEST] A',
|
|
304
|
+
path: ['[TEST] A'],
|
|
305
|
+
children: [{ name: 'B', path: ['[TEST] A', 'B'], children: [] }],
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
onChange,
|
|
309
|
+
} as unknown as CategoryPathPickerProps)
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
// Blank Add must not fire.
|
|
313
|
+
const add = tree.root
|
|
314
|
+
.findAllByType('Pressable')
|
|
315
|
+
.find(
|
|
316
|
+
(node: { props: { accessibilityLabel?: string } }) =>
|
|
317
|
+
node.props.accessibilityLabel === 'Add'
|
|
318
|
+
);
|
|
319
|
+
ReactTestRenderer.act(() => {
|
|
320
|
+
add!.props.onPress();
|
|
321
|
+
});
|
|
322
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
323
|
+
|
|
324
|
+
// Tapping the nested folder selects its full path.
|
|
325
|
+
const nested = pressables(tree).find(
|
|
326
|
+
(node: { props: { children?: unknown } }) => textOf(node) === 'B'
|
|
327
|
+
);
|
|
328
|
+
expect(nested).toBeTruthy();
|
|
329
|
+
ReactTestRenderer.act(() => {
|
|
330
|
+
nested!.props.onPress();
|
|
331
|
+
});
|
|
332
|
+
expect(onChange).toHaveBeenCalledWith(['[TEST] A', 'B']);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
|
|
|
4
4
|
import { useNavigation } from '@react-navigation/native';
|
|
5
5
|
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
|
6
6
|
import type { ResourceKind } from '@openpeepshq/common/types';
|
|
7
|
-
import { RESOURCE_KINDS } from '@openpeepshq/common/lib';
|
|
7
|
+
import { RESOURCE_KINDS, categoryPathKey } from '@openpeepshq/common/lib';
|
|
8
8
|
import type { CategoryNode } from '@openpeepshq/common/lib';
|
|
9
9
|
import { useResourceLibrary } from '@openpeepshq/react';
|
|
10
10
|
import { MainStackParamList } from '../navigation/types/index';
|
|
@@ -45,10 +45,10 @@ const Tree = ({
|
|
|
45
45
|
}) => (
|
|
46
46
|
<View className="gap-y-1">
|
|
47
47
|
{nodes.map((node) => {
|
|
48
|
-
const nodeKey = node.path
|
|
48
|
+
const nodeKey = categoryPathKey(node.path);
|
|
49
49
|
const nodeCount = counts.get(nodeKey) ?? 0;
|
|
50
50
|
const isExpanded = expanded.has(nodeKey);
|
|
51
|
-
const isSelected = selected
|
|
51
|
+
const isSelected = categoryPathKey(selected) === nodeKey;
|
|
52
52
|
const hasChildren = node.children.length > 0;
|
|
53
53
|
return (
|
|
54
54
|
<View key={nodeKey}>
|
|
@@ -133,11 +133,11 @@ export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
|
|
|
133
133
|
if (!path?.length) continue;
|
|
134
134
|
rootCount++;
|
|
135
135
|
for (let i = 1; i <= path.length; i++) {
|
|
136
|
-
const key = path.slice(0, i)
|
|
136
|
+
const key = categoryPathKey(path.slice(0, i));
|
|
137
137
|
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
|
-
counts.set(
|
|
140
|
+
counts.set(categoryPathKey([]), rootCount);
|
|
141
141
|
return counts;
|
|
142
142
|
}, [library.posts]);
|
|
143
143
|
|
|
@@ -2,3 +2,5 @@ export { ResourceLibrary } from './ResourceLibrary';
|
|
|
2
2
|
export type { ResourceLibraryProps } from './ResourceLibrary';
|
|
3
3
|
export { ResourceCard } from './ResourceCard';
|
|
4
4
|
export type { ResourceCardProps } from './ResourceCard';
|
|
5
|
+
export { CategoryPathPicker } from './CategoryPathPicker';
|
|
6
|
+
export type { CategoryPathPickerProps } from './CategoryPathPicker';
|
package/src/pages/Welcome.tsx
CHANGED
|
@@ -26,6 +26,7 @@ import Toast from 'react-native-toast-message';
|
|
|
26
26
|
import { useTranslation } from 'react-i18next';
|
|
27
27
|
|
|
28
28
|
import { OpenpeepsMarkdown } from '../components/markdown/index';
|
|
29
|
+
import { useThemeOpacity } from '../theme/useThemeOpacity';
|
|
29
30
|
type AccountSetupChecklistItem = {
|
|
30
31
|
Icon: LucideIcon;
|
|
31
32
|
key: string;
|
|
@@ -158,6 +159,7 @@ export const ChecklistItem = ({
|
|
|
158
159
|
}: AccountSetupChecklistItem) => {
|
|
159
160
|
const navigation =
|
|
160
161
|
useNavigation<NativeStackNavigationProp<MainStackParamList>>();
|
|
162
|
+
const { background } = useThemeOpacity();
|
|
161
163
|
|
|
162
164
|
/* eslint-disable @typescript-eslint/no-shadow */
|
|
163
165
|
const { currentProfile } = useOpenpeeps();
|
|
@@ -188,7 +190,8 @@ export const ChecklistItem = ({
|
|
|
188
190
|
});
|
|
189
191
|
}
|
|
190
192
|
}}
|
|
191
|
-
|
|
193
|
+
style={background('secondary', 0.1)}
|
|
194
|
+
className="flex px-1 flex-row justify-between items-center w-full py-4 rounded-md mb-2"
|
|
192
195
|
>
|
|
193
196
|
<ThemedView className=" flex p-4 items-center justify-center rounded-full bg-background">
|
|
194
197
|
<Icon size={20} className="text-foreground p-3" />
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import React, { createContext, useContext, useEffect, useMemo } from 'react';
|
|
2
2
|
import { View, useColorScheme as useRNColorScheme } from 'react-native';
|
|
3
|
-
import { colorScheme } from 'nativewind';
|
|
4
3
|
import { useOpenpeeps } from '@openpeepshq/react';
|
|
5
4
|
import { OpenPeepsTheme } from './types';
|
|
6
5
|
import { defaultTheme } from './defaults';
|
|
@@ -62,11 +61,13 @@ export const OpenPeepsThemeProvider = ({
|
|
|
62
61
|
);
|
|
63
62
|
|
|
64
63
|
useEffect(() => {
|
|
64
|
+
// Lazy-import nativewind so the provider module does not crash Jest (node
|
|
65
|
+
// env has no Appearance module). The actual set() call runs only when the
|
|
66
|
+
// component mounts in a real runtime.
|
|
67
|
+
const { colorScheme } = require('nativewind');
|
|
65
68
|
// nativewind maps colorScheme.set('system') to Appearance.setColorScheme(null),
|
|
66
|
-
// which crashes on Android RN 0.85
|
|
67
|
-
//
|
|
68
|
-
// 'system' preference (it derives from the live systemColorScheme above),
|
|
69
|
-
// so always drive nativewind with an explicit value.
|
|
69
|
+
// which crashes on Android RN 0.85. theme.isDark already resolves the active
|
|
70
|
+
// scheme, so always drive nativewind with an explicit value.
|
|
70
71
|
colorScheme.set(theme.isDark ? 'dark' : 'light');
|
|
71
72
|
}, [theme.isDark]);
|
|
72
73
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure color math shared by the app and its regression tests.
|
|
3
|
+
*
|
|
4
|
+
* Kept `nativewind`-free on purpose: `theme/utils.ts` imports `vars` from
|
|
5
|
+
* `nativewind`, which cannot load in the node jest environment. Splitting the
|
|
6
|
+
* color helpers into this dependency-free module lets the tests import and
|
|
7
|
+
* exercise the *real* `hexWithOpacity` instead of a hand-rolled copy (a copy
|
|
8
|
+
* would miss e.g. the `##` -> `#` normalization below).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Convert a hex color (#rrggbb) to an "R G B" channel string. */
|
|
12
|
+
export const hexToRgbChannels = (hex: string): string => {
|
|
13
|
+
const sanitized = hex.replaceAll('##', '#');
|
|
14
|
+
const m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitized);
|
|
15
|
+
if (!m) return hex;
|
|
16
|
+
const [, r, g, b] = m;
|
|
17
|
+
if (r === undefined || g === undefined || b === undefined) return hex;
|
|
18
|
+
return `${parseInt(r, 16)} ${parseInt(g, 16)} ${parseInt(b, 16)}`;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Convert a hex color to an `rgba(r, g, b, a)` string. */
|
|
22
|
+
export const hexWithOpacity = (hex: string, opacity: number): string => {
|
|
23
|
+
const sanitized = hex.replaceAll('##', '#');
|
|
24
|
+
const m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitized);
|
|
25
|
+
if (!m) return hex;
|
|
26
|
+
const [, r, g, b] = m;
|
|
27
|
+
if (r === undefined || g === undefined || b === undefined) return hex;
|
|
28
|
+
return `rgba(${parseInt(r, 16)}, ${parseInt(g, 16)}, ${parseInt(b, 16)}, ${opacity})`;
|
|
29
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useOpenPeepsTheme } from './OpenPeepsThemeProvider';
|
|
2
|
+
import { hexWithOpacity } from './color';
|
|
3
|
+
import type { ColorName } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Inline-style opacity helper for theme colors.
|
|
7
|
+
*
|
|
8
|
+
* NativeWind v4 cannot apply a `/N` opacity modifier to a CSS-variable color
|
|
9
|
+
* (`--primary` and friends), so a class like `bg-primary/10` resolves to no
|
|
10
|
+
* color at all. Every such call site must build the rgba value inline from
|
|
11
|
+
* the hex theme color instead — this hook is the single place that does it,
|
|
12
|
+
* so the workaround (and any future fix) lives in one spot.
|
|
13
|
+
*/
|
|
14
|
+
export const useThemeOpacity = () => {
|
|
15
|
+
const { colors } = useOpenPeepsTheme();
|
|
16
|
+
return {
|
|
17
|
+
/** `background` style for a token at the given opacity (0..1). */
|
|
18
|
+
background: (name: ColorName, opacity: number) => ({
|
|
19
|
+
backgroundColor: hexWithOpacity(colors[name], opacity),
|
|
20
|
+
}),
|
|
21
|
+
/** `borderColor` style for a token at the given opacity (0..1). */
|
|
22
|
+
border: (name: ColorName, opacity: number) => ({
|
|
23
|
+
borderColor: hexWithOpacity(colors[name], opacity),
|
|
24
|
+
}),
|
|
25
|
+
/** `color` style for a token at the given opacity (0..1). */
|
|
26
|
+
text: (name: ColorName, opacity: number) => ({
|
|
27
|
+
color: hexWithOpacity(colors[name], opacity),
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
};
|
package/src/theme/utils.ts
CHANGED
|
@@ -1,34 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { hexToRgbChannels, hexWithOpacity } from './color';
|
|
2
2
|
import { mapColors } from './defaults';
|
|
3
3
|
import type { OpenPeepsTheme } from './types';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const hexToRgbChannels = (hex: string): string => {
|
|
9
|
-
const sanitized = hex.replaceAll('##', '#');
|
|
10
|
-
const m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitized);
|
|
11
|
-
if (!m) return hex;
|
|
12
|
-
const [, r, g, b] = m;
|
|
13
|
-
if (r === undefined || g === undefined || b === undefined) return hex;
|
|
14
|
-
return `${parseInt(r, 16)} ${parseInt(g, 16)} ${parseInt(b, 16)}`;
|
|
15
|
-
};
|
|
5
|
+
// The pure color math lives in `./color` (nativewind-free) so tests can
|
|
6
|
+
// import the real implementation. Re-exported here for existing importers.
|
|
7
|
+
export { hexToRgbChannels, hexWithOpacity };
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (r === undefined || g === undefined || b === undefined) return hex;
|
|
24
|
-
return `rgba(${parseInt(r, 16)}, ${parseInt(g, 16)}, ${parseInt(b, 16)}, ${opacity})`;
|
|
25
|
-
};
|
|
9
|
+
// `vars` is required lazily: nativewind's import chain crashes at import
|
|
10
|
+
// time in a plain node jest env (no Appearance module), and this module is
|
|
11
|
+
// reached through the provider's import graph. getThemeVars only runs in a
|
|
12
|
+
// real runtime where the provider renders.
|
|
13
|
+
const getVars = (): (typeof import('nativewind'))['vars'] =>
|
|
14
|
+
require('nativewind').vars;
|
|
26
15
|
|
|
27
16
|
export const getThemeVars = (
|
|
28
17
|
colors: OpenPeepsTheme['colors'],
|
|
29
18
|
radii?: { buttonRadius?: string; radius?: string }
|
|
30
19
|
) =>
|
|
31
|
-
|
|
20
|
+
getVars()({
|
|
32
21
|
...Object.fromEntries(
|
|
33
22
|
Object.entries(colors).map(([key, value]) => [`--${key}`, value])
|
|
34
23
|
),
|