react-native-richify 1.0.5 → 1.0.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.
Files changed (41) hide show
  1. package/README.md +328 -134
  2. package/lib/commonjs/components/RichTextInput.js +0 -4
  3. package/lib/commonjs/components/RichTextInput.js.map +1 -1
  4. package/lib/commonjs/components/Toolbar.js +1 -0
  5. package/lib/commonjs/components/Toolbar.js.map +1 -1
  6. package/lib/commonjs/components/ToolbarButton.js +37 -9
  7. package/lib/commonjs/components/ToolbarButton.js.map +1 -1
  8. package/lib/commonjs/constants/defaultStyles.js +59 -34
  9. package/lib/commonjs/constants/defaultStyles.js.map +1 -1
  10. package/lib/commonjs/hooks/useFormatting.js +9 -5
  11. package/lib/commonjs/hooks/useFormatting.js.map +1 -1
  12. package/lib/commonjs/utils/formatter.js.map +1 -1
  13. package/lib/module/components/RichTextInput.js +0 -4
  14. package/lib/module/components/RichTextInput.js.map +1 -1
  15. package/lib/module/components/Toolbar.js +1 -0
  16. package/lib/module/components/Toolbar.js.map +1 -1
  17. package/lib/module/components/ToolbarButton.js +38 -10
  18. package/lib/module/components/ToolbarButton.js.map +1 -1
  19. package/lib/module/constants/defaultStyles.js +58 -34
  20. package/lib/module/constants/defaultStyles.js.map +1 -1
  21. package/lib/module/hooks/useFormatting.js +9 -5
  22. package/lib/module/hooks/useFormatting.js.map +1 -1
  23. package/lib/module/utils/formatter.js.map +1 -1
  24. package/lib/typescript/src/components/RichTextInput.d.ts.map +1 -1
  25. package/lib/typescript/src/components/Toolbar.d.ts.map +1 -1
  26. package/lib/typescript/src/components/ToolbarButton.d.ts.map +1 -1
  27. package/lib/typescript/src/constants/defaultStyles.d.ts.map +1 -1
  28. package/lib/typescript/src/hooks/useFormatting.d.ts.map +1 -1
  29. package/lib/typescript/src/types/index.d.ts +11 -5
  30. package/lib/typescript/src/types/index.d.ts.map +1 -1
  31. package/lib/typescript/src/utils/formatter.d.ts +1 -1
  32. package/lib/typescript/src/utils/formatter.d.ts.map +1 -1
  33. package/package.json +10 -4
  34. package/src/components/RichTextInput.tsx +0 -4
  35. package/src/components/Toolbar.tsx +1 -0
  36. package/src/components/ToolbarButton.tsx +68 -16
  37. package/src/constants/defaultStyles.ts +47 -19
  38. package/src/hooks/useFormatting.ts +30 -5
  39. package/src/types/index.d.ts +13 -7
  40. package/src/types/index.ts +11 -5
  41. package/src/utils/formatter.ts +1 -1
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { TouchableOpacity, Text, StyleSheet } from 'react-native';
2
+ import { TouchableOpacity, Text, StyleSheet, View } from 'react-native';
3
3
  import type { ToolbarButtonProps } from '../types';
4
4
  import { DEFAULT_THEME } from '../constants/defaultStyles';
5
5
 
@@ -8,13 +8,23 @@ import { DEFAULT_THEME } from '../constants/defaultStyles';
8
8
  * Supports custom rendering via the `renderButton` prop.
9
9
  */
10
10
  export const ToolbarButton: React.FC<ToolbarButtonProps> = React.memo(
11
- ({ label, active, onPress, theme, renderButton }) => {
11
+ ({ label, accessibilityLabel, active, onPress, theme, renderButton }) => {
12
12
  // Custom render
13
13
  if (renderButton) {
14
- return renderButton({ active, onPress, label });
14
+ return renderButton({ active, onPress, label, accessibilityLabel });
15
15
  }
16
16
 
17
17
  const resolvedTheme = theme ?? DEFAULT_THEME;
18
+ const baseTextColor =
19
+ resolvedTheme.toolbarButtonTextStyle?.color ??
20
+ DEFAULT_THEME.toolbarButtonTextStyle?.color;
21
+ const activeTextColor =
22
+ resolvedTheme.toolbarButtonActiveTextStyle?.color ??
23
+ DEFAULT_THEME.toolbarButtonActiveTextStyle?.color ??
24
+ baseTextColor;
25
+ const resolvedIconColor = active ? activeTextColor : baseTextColor;
26
+ const iconColor =
27
+ typeof resolvedIconColor === 'string' ? resolvedIconColor : undefined;
18
28
 
19
29
  const buttonStyle = [
20
30
  resolvedTheme.toolbarButtonStyle ?? DEFAULT_THEME.toolbarButtonStyle,
@@ -23,17 +33,11 @@ export const ToolbarButton: React.FC<ToolbarButtonProps> = React.memo(
23
33
  DEFAULT_THEME.toolbarButtonActiveStyle),
24
34
  ];
25
35
 
26
- const textStyle = [
27
- resolvedTheme.toolbarButtonTextStyle ??
28
- DEFAULT_THEME.toolbarButtonTextStyle,
29
- active &&
30
- (resolvedTheme.toolbarButtonActiveTextStyle ??
31
- DEFAULT_THEME.toolbarButtonActiveTextStyle),
32
- // Make italic button actually italic, bold button actually bold, etc.
33
- label === 'I' && styles.italicLabel,
34
- label === 'U' && styles.underlineLabel,
35
- label === 'S' && styles.strikethroughLabel,
36
- ];
36
+ const derivedAccessibilityLabel =
37
+ accessibilityLabel ??
38
+ (typeof label === 'string' || typeof label === 'number'
39
+ ? `Format ${label}`
40
+ : 'Toolbar action');
37
41
 
38
42
  return (
39
43
  <TouchableOpacity
@@ -41,10 +45,10 @@ export const ToolbarButton: React.FC<ToolbarButtonProps> = React.memo(
41
45
  onPress={onPress}
42
46
  activeOpacity={0.7}
43
47
  accessibilityRole="button"
44
- accessibilityLabel={`Format ${label}`}
48
+ accessibilityLabel={derivedAccessibilityLabel}
45
49
  accessibilityState={{ selected: active }}
46
50
  >
47
- <Text style={textStyle}>{label}</Text>
51
+ {renderToolbarContent(label, iconColor, active, resolvedTheme)}
48
52
  </TouchableOpacity>
49
53
  );
50
54
  },
@@ -53,6 +57,10 @@ export const ToolbarButton: React.FC<ToolbarButtonProps> = React.memo(
53
57
  ToolbarButton.displayName = 'ToolbarButton';
54
58
 
55
59
  const styles = StyleSheet.create({
60
+ contentWrapper: {
61
+ alignItems: 'center',
62
+ justifyContent: 'center',
63
+ },
56
64
  italicLabel: {
57
65
  fontStyle: 'italic',
58
66
  },
@@ -63,3 +71,47 @@ const styles = StyleSheet.create({
63
71
  textDecorationLine: 'line-through',
64
72
  },
65
73
  });
74
+
75
+ function renderToolbarContent(
76
+ label: ToolbarButtonProps['label'],
77
+ iconColor: string | undefined,
78
+ active: boolean,
79
+ theme: NonNullable<ToolbarButtonProps['theme']>,
80
+ ) {
81
+ if (typeof label === 'string' || typeof label === 'number') {
82
+ const textStyle = [
83
+ theme.toolbarButtonTextStyle ?? DEFAULT_THEME.toolbarButtonTextStyle,
84
+ active &&
85
+ (theme.toolbarButtonActiveTextStyle ??
86
+ DEFAULT_THEME.toolbarButtonActiveTextStyle),
87
+ label === 'I' && styles.italicLabel,
88
+ label === 'U' && styles.underlineLabel,
89
+ label === 'S' && styles.strikethroughLabel,
90
+ ];
91
+
92
+ return <Text style={textStyle}>{label}</Text>;
93
+ }
94
+
95
+ if (React.isValidElement(label)) {
96
+ return (
97
+ <View style={styles.contentWrapper}>
98
+ {React.cloneElement(
99
+ label as React.ReactElement<{
100
+ color?: string;
101
+ size?: number;
102
+ strokeWidth?: number;
103
+ }>,
104
+ {
105
+ color:
106
+ (label.props as { color?: string }).color ?? iconColor,
107
+ size: (label.props as { size?: number }).size ?? 18,
108
+ strokeWidth:
109
+ (label.props as { strokeWidth?: number }).strokeWidth ?? 2,
110
+ },
111
+ )}
112
+ </View>
113
+ );
114
+ }
115
+
116
+ return <View style={styles.contentWrapper}>{label}</View>;
117
+ }
@@ -1,3 +1,21 @@
1
+ import React from 'react';
2
+ import {
3
+ Bold,
4
+ Code,
5
+ Heading1,
6
+ Heading2,
7
+ Heading3,
8
+ ImagePlus,
9
+ Italic,
10
+ Link2,
11
+ List,
12
+ ListOrdered,
13
+ Strikethrough,
14
+ TextAlignCenter,
15
+ TextAlignEnd,
16
+ TextAlignStart,
17
+ Underline,
18
+ } from 'lucide-react-native';
1
19
  import type { RichTextTheme, FormatStyle, ToolbarItem } from '../types';
2
20
 
3
21
  /**
@@ -168,23 +186,33 @@ export const DEFAULT_THEME: RichTextTheme = {
168
186
  * Default toolbar items for the built-in toolbar.
169
187
  */
170
188
  export const DEFAULT_TOOLBAR_ITEMS: ToolbarItem[] = [
171
- { id: 'bold', label: 'B', format: 'bold' },
172
- { id: 'italic', label: 'I', format: 'italic' },
173
- { id: 'underline', label: 'U', format: 'underline' },
174
- { id: 'strikethrough', label: 'S', format: 'strikethrough' },
175
- { id: 'code', label: '<>', format: 'code' },
176
- { id: 'h1', label: 'H1', heading: 'h1' },
177
- { id: 'h2', label: 'H2', heading: 'h2' },
178
- { id: 'h3', label: 'H3', heading: 'h3' },
179
- { id: 'bullet', label: '\u2022', listType: 'bullet' },
180
- { id: 'ordered', label: '1.', listType: 'ordered' },
181
- { id: 'link', label: 'Link', actionType: 'link' },
182
- { id: 'image', label: 'Img', actionType: 'image' },
183
- { id: 'align-left', label: 'L', textAlign: 'left' },
184
- { id: 'align-center', label: 'C', textAlign: 'center' },
185
- { id: 'align-right', label: 'R', textAlign: 'right' },
186
- { id: 'format-markdown', label: 'MD', outputFormat: 'markdown' },
187
- { id: 'format-html', label: 'HTML', outputFormat: 'html' },
188
- { id: 'preview-literal', label: 'Raw', outputPreviewMode: 'literal' },
189
- { id: 'preview-rendered', label: 'View', outputPreviewMode: 'rendered' },
189
+ { id: 'bold', label: createToolbarIcon(Bold), format: 'bold', accessibilityLabel: 'Bold' },
190
+ { id: 'italic', label: createToolbarIcon(Italic), format: 'italic', accessibilityLabel: 'Italic' },
191
+ { id: 'underline', label: createToolbarIcon(Underline), format: 'underline', accessibilityLabel: 'Underline' },
192
+ { id: 'strikethrough', label: createToolbarIcon(Strikethrough), format: 'strikethrough', accessibilityLabel: 'Strikethrough' },
193
+ { id: 'code', label: createToolbarIcon(Code), format: 'code', accessibilityLabel: 'Code' },
194
+ { id: 'h1', label: createToolbarIcon(Heading1), heading: 'h1', accessibilityLabel: 'Heading 1' },
195
+ { id: 'h2', label: createToolbarIcon(Heading2), heading: 'h2', accessibilityLabel: 'Heading 2' },
196
+ { id: 'h3', label: createToolbarIcon(Heading3), heading: 'h3', accessibilityLabel: 'Heading 3' },
197
+ { id: 'bullet', label: createToolbarIcon(List), listType: 'bullet', accessibilityLabel: 'Bullet list' },
198
+ { id: 'ordered', label: createToolbarIcon(ListOrdered), listType: 'ordered', accessibilityLabel: 'Ordered list' },
199
+ { id: 'link', label: createToolbarIcon(Link2), actionType: 'link', accessibilityLabel: 'Link' },
200
+ { id: 'image', label: createToolbarIcon(ImagePlus), actionType: 'image', accessibilityLabel: 'Insert image' },
201
+ { id: 'align-left', label: createToolbarIcon(TextAlignStart), textAlign: 'left', accessibilityLabel: 'Align left' },
202
+ { id: 'align-center', label: createToolbarIcon(TextAlignCenter), textAlign: 'center', accessibilityLabel: 'Align center' },
203
+ { id: 'align-right', label: createToolbarIcon(TextAlignEnd), textAlign: 'right', accessibilityLabel: 'Align right' },
204
+ { id: 'format-markdown', label: 'MD', outputFormat: 'markdown', accessibilityLabel: 'Markdown output' },
205
+ { id: 'format-html', label: 'HTML', outputFormat: 'html', accessibilityLabel: 'HTML output' },
206
+ { id: 'preview-literal', label: 'Raw', outputPreviewMode: 'literal', accessibilityLabel: 'Raw output view' },
207
+ { id: 'preview-rendered', label: 'View', outputPreviewMode: 'rendered', accessibilityLabel: 'Rendered preview' },
190
208
  ];
209
+
210
+ function createToolbarIcon(
211
+ Icon: React.ComponentType<{
212
+ color?: string;
213
+ size?: number;
214
+ strokeWidth?: number;
215
+ }>,
216
+ ) {
217
+ return React.createElement(Icon);
218
+ }
@@ -112,15 +112,28 @@ export function useFormatting({
112
112
 
113
113
  const setListType = useCallback(
114
114
  (listType: ListType) => {
115
+ const currentListType =
116
+ selection.start === selection.end
117
+ ? getSelectionStyle(segments, selection).listType ??
118
+ activeStyles.listType
119
+ : getSelectionStyle(segments, selection).listType;
120
+ const nextListType: ListType =
121
+ currentListType === listType ? 'none' : listType;
122
+
115
123
  if (selection.start === selection.end) {
116
124
  onActiveStylesChange({
117
125
  ...activeStyles,
118
- listType: listType === 'none' ? undefined : listType,
119
- heading: listType === 'none' ? activeStyles.heading : undefined,
126
+ listType: nextListType === 'none' ? undefined : nextListType,
127
+ heading:
128
+ nextListType === 'none' ? activeStyles.heading : undefined,
120
129
  });
121
130
  }
122
131
 
123
- const newSegments = setListTypeOnLine(segments, selection, listType);
132
+ const newSegments = setListTypeOnLine(
133
+ segments,
134
+ selection,
135
+ nextListType,
136
+ );
124
137
  onSegmentsChange(newSegments);
125
138
  },
126
139
  [
@@ -134,14 +147,26 @@ export function useFormatting({
134
147
 
135
148
  const setTextAlign = useCallback(
136
149
  (textAlign: TextAlign) => {
150
+ const currentTextAlign =
151
+ selection.start === selection.end
152
+ ? getSelectionStyle(segments, selection).textAlign ??
153
+ activeStyles.textAlign
154
+ : getSelectionStyle(segments, selection).textAlign;
155
+ const nextTextAlign =
156
+ currentTextAlign === textAlign ? undefined : textAlign;
157
+
137
158
  if (selection.start === selection.end) {
138
159
  onActiveStylesChange({
139
160
  ...activeStyles,
140
- textAlign,
161
+ textAlign: nextTextAlign,
141
162
  });
142
163
  }
143
164
 
144
- const newSegments = setTextAlignOnLine(segments, selection, textAlign);
165
+ const newSegments = setTextAlignOnLine(
166
+ segments,
167
+ selection,
168
+ nextTextAlign,
169
+ );
145
170
  onSegmentsChange(newSegments);
146
171
  },
147
172
  [
@@ -1,3 +1,4 @@
1
+ import type { ReactElement, ReactNode } from 'react';
1
2
  import type { TextStyle, ViewStyle, TextInputProps, ColorValue } from 'react-native';
2
3
  /**
3
4
  * Supported inline formatting types.
@@ -183,8 +184,10 @@ export interface RichTextTheme {
183
184
  export interface ToolbarItem {
184
185
  /** Unique identifier. */
185
186
  id: string;
186
- /** Display label or icon text. */
187
- label: string;
187
+ /** Display content for the button: text, emoji, or a React element. */
188
+ label: ReactNode;
189
+ /** Accessibility label used when the button content is not plain text. */
190
+ accessibilityLabel?: string;
188
191
  /** The format type this button toggles (for inline formats). */
189
192
  format?: FormatType;
190
193
  /** The heading level this button sets. */
@@ -204,7 +207,7 @@ export interface ToolbarItem {
204
207
  /** Whether this item is currently active. */
205
208
  active?: boolean;
206
209
  /** Custom render function for the button. */
207
- renderButton?: (props: ToolbarButtonRenderProps) => React.ReactElement | null;
210
+ renderButton?: (props: ToolbarButtonRenderProps) => ReactElement | null;
208
211
  }
209
212
  /**
210
213
  * Props passed to a custom toolbar button renderer.
@@ -212,7 +215,8 @@ export interface ToolbarItem {
212
215
  export interface ToolbarButtonRenderProps {
213
216
  active: boolean;
214
217
  onPress: () => void;
215
- label: string;
218
+ label: ReactNode;
219
+ accessibilityLabel?: string;
216
220
  }
217
221
  /**
218
222
  * Props passed to a custom toolbar renderer.
@@ -264,8 +268,10 @@ export interface OverlayTextProps {
264
268
  * Props for the ToolbarButton component.
265
269
  */
266
270
  export interface ToolbarButtonProps {
267
- /** Button label text. */
268
- label: string;
271
+ /** Button label or icon content. */
272
+ label: ReactNode;
273
+ /** Accessibility label used for assistive technologies. */
274
+ accessibilityLabel?: string;
269
275
  /** Whether the button is currently active. */
270
276
  active: boolean;
271
277
  /** Press handler. */
@@ -302,7 +308,7 @@ export interface ToolbarProps {
302
308
  /** Called when the image button is pressed. */
303
309
  onRequestImage?: () => void;
304
310
  /** Custom render function for the entire toolbar. */
305
- renderToolbar?: (props: ToolbarRenderProps) => React.ReactElement | null;
311
+ renderToolbar?: (props: ToolbarRenderProps) => ReactElement | null;
306
312
  }
307
313
  /**
308
314
  * Props for the main RichTextInput component.
@@ -1,3 +1,4 @@
1
+ import type { ReactNode } from 'react';
1
2
  import type { TextStyle, ViewStyle, TextInputProps, ColorValue } from 'react-native';
2
3
 
3
4
  // ─── Format Types ────────────────────────────────────────────────────────────
@@ -224,8 +225,10 @@ export interface RichTextTheme {
224
225
  export interface ToolbarItem {
225
226
  /** Unique identifier. */
226
227
  id: string;
227
- /** Display label or icon text. */
228
- label: string;
228
+ /** Display content for the button: text, emoji, or a React element. */
229
+ label: ReactNode;
230
+ /** Accessibility label used when the button content is not plain text. */
231
+ accessibilityLabel?: string;
229
232
  /** The format type this button toggles (for inline formats). */
230
233
  format?: FormatType;
231
234
  /** The heading level this button sets. */
@@ -254,7 +257,8 @@ export interface ToolbarItem {
254
257
  export interface ToolbarButtonRenderProps {
255
258
  active: boolean;
256
259
  onPress: () => void;
257
- label: string;
260
+ label: ReactNode;
261
+ accessibilityLabel?: string;
258
262
  }
259
263
 
260
264
  /**
@@ -316,8 +320,10 @@ export interface OverlayTextProps {
316
320
  * Props for the ToolbarButton component.
317
321
  */
318
322
  export interface ToolbarButtonProps {
319
- /** Button label text. */
320
- label: string;
323
+ /** Button label or icon content. */
324
+ label: ReactNode;
325
+ /** Accessibility label used for assistive technologies. */
326
+ accessibilityLabel?: string;
321
327
  /** Whether the button is currently active. */
322
328
  active: boolean;
323
329
  /** Press handler. */
@@ -95,7 +95,7 @@ export function setListTypeOnLine(
95
95
  export function setTextAlignOnLine(
96
96
  segments: StyledSegment[],
97
97
  selection: SelectionRange,
98
- textAlign: TextAlign,
98
+ textAlign?: TextAlign,
99
99
  ): StyledSegment[] {
100
100
  return setLineStyleOnSelection(segments, selection, {
101
101
  textAlign,