@openpeepshq/rn-components 0.20.0 → 0.22.0

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.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Contract for `markdownTheme` (issue #2074): the optional `baseTextStyle` is
3
+ * merged over every text-bearing style (`body`, `paragraph`, `list`,
4
+ * `listItem`) and nothing is altered when it is not provided. This is the one
5
+ * function every markdown surface's text metrics flow through, so a silent
6
+ * regression in the merge must be caught here directly (the component tests
7
+ * mock `../markdown` and never exercise it).
8
+ */
9
+
10
+ jest.mock('react-native', () => ({
11
+ StyleSheet: {
12
+ flatten: (s: unknown) => (Array.isArray(s) ? Object.assign({}, ...s) : s),
13
+ },
14
+ }));
15
+
16
+ import { markdownTheme } from './theme';
17
+ import { mapColors } from '../../theme/defaults';
18
+
19
+ // Real light-theme defaults (nativewind-free), so the assertions check the
20
+ // hex values the app actually ships — a test with an invented color would
21
+ // keep passing if the real default drifted.
22
+ const colors = mapColors(false);
23
+
24
+ const TEXT_STYLES = ['body', 'paragraph', 'list', 'listItem'] as const;
25
+
26
+ test('without baseTextStyle the base 16/24 foreground text styles survive', () => {
27
+ const theme = markdownTheme(colors);
28
+ for (const name of TEXT_STYLES) {
29
+ const style = theme[name] as Record<string, unknown>;
30
+ // The real light default foreground (theme/defaults.ts), not a test-only hex.
31
+ expect(style.color).toBe('#0a0a0b');
32
+ // `body` carries only the color; the block styles carry the 16/24 metrics.
33
+ if (name !== 'body') {
34
+ expect(style.fontSize).toBe(16);
35
+ expect(style.lineHeight).toBe(24);
36
+ }
37
+ }
38
+ });
39
+
40
+ test('baseTextStyle merges over every text-bearing style, keeping defaults', () => {
41
+ const theme = markdownTheme(colors, {
42
+ baseTextStyle: { fontSize: 14, lineHeight: 20, color: '#71717a' },
43
+ });
44
+ for (const name of TEXT_STYLES) {
45
+ const style = theme[name] as Record<string, unknown>;
46
+ expect(style).toMatchObject({
47
+ fontSize: 14,
48
+ lineHeight: 20,
49
+ color: '#71717a',
50
+ });
51
+ }
52
+ });
53
+
54
+ test('a partial override only replaces the keys it provides', () => {
55
+ const theme = markdownTheme(colors, {
56
+ baseTextStyle: { fontSize: 12 },
57
+ });
58
+ for (const name of TEXT_STYLES) {
59
+ const style = theme[name] as Record<string, unknown>;
60
+ expect(style.fontSize).toBe(12);
61
+ // lineHeight 24 and the real default foreground are untouched.
62
+ if (name !== 'body') {
63
+ expect(style.lineHeight).toBe(24);
64
+ }
65
+ expect(style.color).toBe(colors.foreground);
66
+ }
67
+ });
68
+
69
+ test('non-text-bearing styles are left untouched by baseTextStyle', () => {
70
+ const themed = markdownTheme(colors, {
71
+ baseTextStyle: { fontSize: 14, color: '#71717a' },
72
+ });
73
+ const plain = markdownTheme(colors);
74
+ expect(themed.link).toEqual(plain.link);
75
+ expect(themed.blockquote).toEqual(plain.blockquote);
76
+ expect(themed.code_inline).toEqual(plain.code_inline);
77
+ expect(themed.heading1).toEqual(plain.heading1);
78
+ });
@@ -1,12 +1,27 @@
1
- import { StyleSheet } from 'react-native';
1
+ import { StyleProp, TextStyle, StyleSheet } from 'react-native';
2
2
  import { OpenPeepsTheme } from '../../theme/types';
3
3
 
4
+ export interface MarkdownThemeOptions {
5
+ /**
6
+ * Text metrics override (e.g. `text-sm` → `{fontSize: 14, lineHeight: 20}`)
7
+ * merged over the theme's paragraph/list styles for compact contexts that
8
+ * must stay smaller than the base 16/24 markdown.
9
+ */
10
+ baseTextStyle?: StyleProp<TextStyle>;
11
+ }
12
+
13
+ const mergeTextStyles = (
14
+ base: TextStyle,
15
+ override?: StyleProp<TextStyle>
16
+ ): TextStyle => ({ ...base, ...(StyleSheet.flatten(override) as TextStyle) });
17
+
4
18
  export const markdownTheme = (
5
- colors: OpenPeepsTheme['colors']
19
+ colors: OpenPeepsTheme['colors'],
20
+ { baseTextStyle }: MarkdownThemeOptions = {}
6
21
  ): StyleSheet.NamedStyles<unknown> => {
7
22
  return {
8
23
  body: {
9
- color: colors.foreground,
24
+ ...mergeTextStyles({ color: colors.foreground }, baseTextStyle),
10
25
  },
11
26
  link: {
12
27
  color: colors.primary,
@@ -23,19 +38,22 @@ export const markdownTheme = (
23
38
  fontSize: 20,
24
39
  },
25
40
  paragraph: {
26
- color: colors.foreground,
27
- lineHeight: 24,
28
- fontSize: 16,
41
+ ...mergeTextStyles(
42
+ { color: colors.foreground, lineHeight: 24, fontSize: 16 },
43
+ baseTextStyle
44
+ ),
29
45
  },
30
46
  list: {
31
- color: colors.foreground,
32
- lineHeight: 24,
33
- fontSize: 16,
47
+ ...mergeTextStyles(
48
+ { color: colors.foreground, lineHeight: 24, fontSize: 16 },
49
+ baseTextStyle
50
+ ),
34
51
  },
35
52
  listItem: {
36
- color: colors.foreground,
37
- lineHeight: 24,
38
- fontSize: 16,
53
+ ...mergeTextStyles(
54
+ { color: colors.foreground, lineHeight: 24, fontSize: 16 },
55
+ baseTextStyle
56
+ ),
39
57
  },
40
58
  blockquote: {
41
59
  backgroundColor: colors.background,
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Layout contract for the RN `CompactReplyParent` strip (issue #2074): the
3
+ * author name/handle line renders first and can never be clipped. The strip
4
+ * keeps its `max-h-20` cap but aligns its row content to the start so any
5
+ * residual overflow clips at the bottom instead of the top. The note/question
6
+ * bodies stay clamped to one line via `maxLines={1}`. A visual-attachment
7
+ * thumbnail renders beside the pinned author line, and the hidden/deleted
8
+ * tombstone branches render the placeholder instead of the author line.
9
+ */
10
+
11
+ const React = require('react');
12
+ const ReactTestRenderer = require('react-test-renderer');
13
+
14
+ jest.mock('react-native', () => ({
15
+ Pressable: (props: {
16
+ children?: unknown;
17
+ className?: string;
18
+ onPress?: () => void;
19
+ accessibilityLabel?: string;
20
+ }) =>
21
+ React.createElement(
22
+ 'Pressable',
23
+ {
24
+ className: props.className,
25
+ onPress: props.onPress,
26
+ accessibilityLabel: props.accessibilityLabel,
27
+ },
28
+ props.children
29
+ ),
30
+ View: (props: { className?: string; children?: unknown }) =>
31
+ React.createElement('View', { className: props.className }, props.children),
32
+ }));
33
+
34
+ jest.mock('react-i18next', () => ({
35
+ useTranslation: () => ({
36
+ t: (key: string, opts?: Record<string, string | number>) =>
37
+ (key.startsWith('posts.compact.pollOptions')
38
+ ? `${opts?.count} options`
39
+ : key
40
+ ).replace(/\{\{(\w+)\}\}/g, (_m: string, name: string) =>
41
+ String(opts?.[name] ?? '')
42
+ ),
43
+ }),
44
+ }));
45
+
46
+ jest.mock('@react-navigation/native', () => ({
47
+ useNavigation: () => ({ navigate: jest.fn() }),
48
+ }));
49
+
50
+ jest.mock('@openpeepshq/common', () => ({
51
+ formatEventWhen: () => '',
52
+ isHiddenPost: (post: { hidden?: boolean }) => !!post.hidden,
53
+ }));
54
+
55
+ jest.mock('../profile/index', () => ({
56
+ ProfileAvatar: () => null,
57
+ }));
58
+
59
+ jest.mock('../icons/index', () => ({
60
+ CalendarIcon: () => null,
61
+ PaperclipIcon: () => null,
62
+ PlayIcon: () => null,
63
+ }));
64
+
65
+ jest.mock('../ui/themed-text', () => ({
66
+ ThemedText: (props: Record<string, unknown>) =>
67
+ React.createElement(
68
+ 'ThemedText',
69
+ { numberOfLines: props.numberOfLines },
70
+ props.children
71
+ ),
72
+ }));
73
+
74
+ jest.mock('../ui/themed-view', () => ({
75
+ ThemedView: (props: Record<string, unknown>) =>
76
+ React.createElement(
77
+ 'ThemedView',
78
+ { className: props.className },
79
+ props.children
80
+ ),
81
+ }));
82
+
83
+ jest.mock('../ui/svg-image', () => ({
84
+ SvgImage: (props: { uri?: string }) =>
85
+ React.createElement('SvgImage', { uri: props.uri }),
86
+ }));
87
+
88
+ jest.mock('./TruncatedMarkdown', () => ({
89
+ TruncatedMarkdown: (props: {
90
+ maxLines?: number;
91
+ maxChars?: number;
92
+ className?: string;
93
+ }) =>
94
+ React.createElement('Excerpt', {
95
+ maxLines: props.maxLines,
96
+ maxChars: props.maxChars,
97
+ className: props.className,
98
+ }),
99
+ }));
100
+
101
+ import type { ReactElement } from 'react';
102
+ import { CompactReplyParent } from './CompactReplyParent';
103
+
104
+ const makePost = (data: Record<string, unknown>) =>
105
+ ({
106
+ id: 'post-1',
107
+ type: 'note',
108
+ profile: {
109
+ id: 'bob',
110
+ displayName: 'A Very Long Display Name',
111
+ handle: 'some-long-handle-1234',
112
+ },
113
+ data: { type: 'note', content: 'hello' },
114
+ ...data,
115
+ }) as never;
116
+
117
+ const render = (element: ReactElement) => {
118
+ let tree!: ReactTestRenderer.ReactTestRenderer;
119
+ ReactTestRenderer.act(() => {
120
+ tree = ReactTestRenderer.create(element);
121
+ });
122
+ return tree;
123
+ };
124
+
125
+ /** Flattened pre-order of the test tree: node types and string children. */
126
+ const flatten = (
127
+ node:
128
+ | ReactTestRenderer.ReactTestRendererJSON
129
+ | ReactTestRenderer.ReactTestRendererJSON[]
130
+ | string
131
+ | null
132
+ ): string[] => {
133
+ if (node == null) return [];
134
+ if (typeof node === 'string') return [node];
135
+ const list = Array.isArray(node) ? node : [node];
136
+ return list.flatMap((n) =>
137
+ typeof n === 'string' ? [n] : [n.type as string, ...flatten(n.children)]
138
+ );
139
+ };
140
+
141
+ /** All string text of a rendered JSON subtree. */
142
+ const jsonText = (
143
+ node:
144
+ | ReactTestRenderer.ReactTestRendererJSON
145
+ | ReactTestRenderer.ReactTestRendererJSON[]
146
+ | string
147
+ | null
148
+ ): string => {
149
+ if (node == null) return '';
150
+ if (typeof node === 'string') return node;
151
+ const list = Array.isArray(node) ? node : [node];
152
+ return list
153
+ .map((n) => (typeof n === 'string' ? n : jsonText(n.children)))
154
+ .join('');
155
+ };
156
+
157
+ /** All JSON nodes of a rendered tree, pre-order. */
158
+ const allNodes = (
159
+ node:
160
+ | ReactTestRenderer.ReactTestRendererJSON
161
+ | ReactTestRenderer.ReactTestRendererJSON[]
162
+ | string
163
+ | null
164
+ ): ReactTestRenderer.ReactTestRendererJSON[] => {
165
+ if (node == null || typeof node === 'string') return [];
166
+ const list = Array.isArray(node) ? node : [node];
167
+ return list.flatMap((n) => [n, ...allNodes(n.children)]);
168
+ };
169
+
170
+ test('the strip container caps height, keeps overflow hidden and aligns content to the start', () => {
171
+ const tree = render(<CompactReplyParent post={makePost({})} />);
172
+ const pressable = tree.root.find((n) => n.type === 'Pressable');
173
+ const cls = pressable.props.className as string;
174
+ expect(cls).toContain('max-h-20');
175
+ expect(cls).toContain('overflow-hidden');
176
+ expect(cls).toContain('items-start');
177
+ expect(cls).not.toContain('items-center');
178
+ ReactTestRenderer.act(() => tree.unmount());
179
+ });
180
+
181
+ test('the author line renders before the one-line body excerpt', () => {
182
+ const tree = render(
183
+ <CompactReplyParent
184
+ post={makePost({
185
+ data: { type: 'note', content: 'a reply body' },
186
+ })}
187
+ />
188
+ );
189
+ const order = flatten(tree.toJSON());
190
+ const nameIdx = order.indexOf('A Very Long Display Name');
191
+ const bodyIdx = order.indexOf('Excerpt');
192
+ expect(nameIdx).toBeGreaterThanOrEqual(0);
193
+ expect(bodyIdx).toBeGreaterThanOrEqual(0);
194
+ expect(nameIdx).toBeLessThan(bodyIdx);
195
+ ReactTestRenderer.act(() => tree.unmount());
196
+ });
197
+
198
+ test('the author line is clamped to one line so it can never wrap and clip', () => {
199
+ const tree = render(
200
+ <CompactReplyParent
201
+ post={makePost({
202
+ data: { type: 'note', content: 'a reply body' },
203
+ })}
204
+ />
205
+ );
206
+ // The single-line header clamp: without it a long name+handle would wrap
207
+ // and the wrapped line is exactly what the strip cap would clip. The author
208
+ // row is the ThemedText carrying the display name (and handle), so assert
209
+ // that row is the single-line one.
210
+ const authorRow = allNodes(tree.toJSON()).find(
211
+ (n) =>
212
+ n.type === 'ThemedText' &&
213
+ n.props.numberOfLines === 1 &&
214
+ jsonText(n.children).includes('A Very Long Display Name')
215
+ );
216
+ expect(authorRow).toBeTruthy();
217
+ ReactTestRenderer.act(() => tree.unmount());
218
+ });
219
+
220
+ test('the note body excerpt is clamped to one line at the sm size', () => {
221
+ const tree = render(
222
+ <CompactReplyParent
223
+ post={makePost({
224
+ data: { type: 'note', content: 'a reply body' },
225
+ })}
226
+ />
227
+ );
228
+ const json = JSON.stringify(tree.toJSON());
229
+ expect(json).toContain('Excerpt');
230
+ // maxLines={1} must reach the excerpt (previously dropped on RN).
231
+ expect(json).toContain('"maxLines":1');
232
+ expect(json).toContain('"maxChars":80');
233
+ expect(json).toContain('text-sm text-muted-foreground');
234
+ ReactTestRenderer.act(() => tree.unmount());
235
+ });
236
+
237
+ test('a block-hidden reply tombstone renders the hidden placeholder, not the reply content', () => {
238
+ // A hidden post (toHiddenPost) reaches the strip the same way a deleted one
239
+ // does, but with `hidden: true` and no `deletedAt`. The web twin branches on
240
+ // `isHiddenPost` and renders `posts.hiddenMessage`; RN must mirror that
241
+ // instead of showing a bare name row with an empty body.
242
+ const tree = render(
243
+ <CompactReplyParent
244
+ post={makePost({
245
+ hidden: true,
246
+ data: { type: 'note', content: 'secret reply body' },
247
+ })}
248
+ />
249
+ );
250
+ const text = jsonText(tree.toJSON());
251
+ // The i18n mock returns keys verbatim, so the placeholder is the key itself.
252
+ expect(text).toContain('posts.hiddenMessage');
253
+ // None of the reply content renders: no body excerpt, no name row.
254
+ const json = JSON.stringify(tree.toJSON());
255
+ expect(json).not.toContain('Excerpt');
256
+ expect(text).not.toContain('A Very Long Display Name');
257
+ expect(text).not.toContain('secret reply body');
258
+ // The deleted placeholder must not render either (hidden is the first branch).
259
+ expect(text).not.toContain('posts.compact.deleted');
260
+ ReactTestRenderer.act(() => tree.unmount());
261
+ });
262
+
263
+ test('a deleted reply renders the deleted placeholder, not the hidden message', () => {
264
+ // Reverse of the hidden-tombstone test: with `hidden` unset the second
265
+ // branch must win, so a future swap of the two ternary arms (or of the
266
+ // `isHiddenPost`/`deletedAt` checks) renders reply content for deleted
267
+ // posts and this fails.
268
+ const tree = render(
269
+ <CompactReplyParent
270
+ post={makePost({
271
+ deletedAt: '2026-11-01T00:00:00.000Z',
272
+ data: { type: 'note', content: 'deleted reply body' },
273
+ })}
274
+ />
275
+ );
276
+ const text = jsonText(tree.toJSON());
277
+ // Keyed on the i18n mock's verbatim key echo — do not simplify the mock to
278
+ // a no-op, it would silently weaken these assertions.
279
+ expect(text).toContain('posts.compact.deleted');
280
+ // None of the reply content renders: no body excerpt, no name row.
281
+ const json = JSON.stringify(tree.toJSON());
282
+ expect(json).not.toContain('Excerpt');
283
+ expect(text).not.toContain('A Very Long Display Name');
284
+ expect(text).not.toContain('deleted reply body');
285
+ // The hidden placeholder must not leak into the deleted branch.
286
+ expect(text).not.toContain('posts.hiddenMessage');
287
+ ReactTestRenderer.act(() => tree.unmount());
288
+ });
289
+
290
+ test('a question post with an image attachment keeps the author line and the compact thumb', () => {
291
+ // The `question` arm of `compactThumb` is one of several (note/question/
292
+ // link/article) that feed the 64×64 thumbnail beside the pinned author line.
293
+ // Pinning it here keeps that branch↔thumbnail pairing from drifting the same
294
+ // way the author line did: a post that should carry a thumb rendering without
295
+ // one (or the thumb detached from its author line) fails this.
296
+ const tree = render(
297
+ <CompactReplyParent
298
+ post={makePost({
299
+ type: 'question',
300
+ data: {
301
+ type: 'question',
302
+ content: 'Which option wins?',
303
+ options: [{ id: 'a' }, { id: 'b' }],
304
+ attachments: [
305
+ {
306
+ type: 'image',
307
+ url: 'https://example.com/poll.png',
308
+ previewUrl: 'https://example.com/poll-preview.png',
309
+ meta: { mimetype: 'image/png' },
310
+ },
311
+ ],
312
+ },
313
+ })}
314
+ />
315
+ );
316
+ const order = flatten(tree.toJSON());
317
+ // The author line still renders for the question branch.
318
+ const nameIdx = order.indexOf('A Very Long Display Name');
319
+ expect(nameIdx).toBeGreaterThanOrEqual(0);
320
+ // Exactly one 64×64 (size-16) thumbnail container renders, and it points
321
+ // at the attachment's preview URL (not the full-size URL).
322
+ const thumbViews = allNodes(tree.toJSON()).filter(
323
+ (n) =>
324
+ n.type === 'View' &&
325
+ typeof n.props?.className === 'string' &&
326
+ n.props.className.includes('size-16')
327
+ );
328
+ expect(thumbViews).toHaveLength(1);
329
+ const svgImage = allNodes(tree.toJSON()).find((n) => n.type === 'SvgImage');
330
+ expect(svgImage).toBeTruthy();
331
+ expect(svgImage?.props.uri).toBe('https://example.com/poll-preview.png');
332
+ // The question body still renders its one-line excerpt + poll-option count.
333
+ const json = JSON.stringify(tree.toJSON());
334
+ expect(json).toContain('Excerpt');
335
+ expect(order).toContain('2 options');
336
+ ReactTestRenderer.act(() => tree.unmount());
337
+ });
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  formatEventWhen,
3
+ isHiddenPost,
3
4
  type Article,
4
5
  type Event,
5
6
  type MediaAttachmentData,
@@ -139,7 +140,7 @@ const CompactTypeBody = ({ post }: { post: CompactPost }) => {
139
140
  maxBlocks={1}
140
141
  maxChars={80}
141
142
  maxLines={1}
142
- className="text-sm text-muted-foreground"
143
+ className="break-words text-sm text-muted-foreground"
143
144
  />
144
145
  {docName ? (
145
146
  <View className="flex-row items-center gap-1">
@@ -165,7 +166,7 @@ const CompactTypeBody = ({ post }: { post: CompactPost }) => {
165
166
  maxBlocks={1}
166
167
  maxChars={80}
167
168
  maxLines={1}
168
- className="text-sm text-muted-foreground"
169
+ className="break-words text-sm text-muted-foreground"
169
170
  />
170
171
  {optionCount > 0 ? (
171
172
  <ThemedText
@@ -238,21 +239,36 @@ export const CompactReplyParent = ({
238
239
  const name = post.profile.displayName || `@${post.profile.handle}`;
239
240
  const thumb = post.deletedAt ? null : compactThumb(post);
240
241
 
242
+ // Intentionally divergent from the web twin: web keeps `items-center` and
243
+ // vertically centers its (truly line-clamped) content in an inner
244
+ // `min-h-0 flex-col justify-center overflow-hidden` column; RN cannot
245
+ // line-clamp rendered markdown, so it starts the row at the top and lets
246
+ // any residual overflow clip at the bottom — the author line can never be
247
+ // cut. See the approved plan for issue #2074.
241
248
  return (
242
249
  <Pressable
243
250
  accessibilityLabel={t('posts.compact.viewPost', {
244
251
  defaultValue: 'View post',
245
252
  })}
246
253
  onPress={() => navigation.navigate('Post', { id: post.id })}
247
- className={`bg-muted mx-2 mb-2 max-h-20 flex-row items-center gap-2 overflow-hidden rounded-lg px-2 py-1.5 ${className ?? ''}`}
254
+ className={`bg-muted mx-2 mb-2 max-h-20 flex-row items-start gap-2 overflow-hidden rounded-lg px-2 py-1.5 ${className ?? ''}`}
248
255
  >
249
256
  <ProfileAvatar profile={post.profile} className="size-8" />
250
- {post.deletedAt ? (
257
+ {isHiddenPost(post) ? (
251
258
  <ThemedText
252
259
  className="flex-1 text-sm text-muted-foreground"
253
260
  numberOfLines={2}
254
261
  >
255
- {t('posts.compact.deleted')}
262
+ {t('posts.hiddenMessage', { defaultValue: 'Hidden message' })}
263
+ </ThemedText>
264
+ ) : post.deletedAt ? (
265
+ <ThemedText
266
+ className="flex-1 text-sm text-muted-foreground"
267
+ numberOfLines={2}
268
+ >
269
+ {t('posts.compact.deleted', {
270
+ defaultValue: 'This post has been deleted.',
271
+ })}
256
272
  </ThemedText>
257
273
  ) : (
258
274
  <>