Skip to content

Commit c9c253e

Browse files
committed
fix: adjust lineHeight of iOS TextInput
1 parent 4f12feb commit c9c253e

File tree

6 files changed

+48
-33
lines changed

6 files changed

+48
-33
lines changed

packages/uikit-react-native-foundation/src/components/TextInput/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const TextInput = React.forwardRef<RNTextInput, Props>(function TextInput(
1515
const variantStyle = colors['ui']['input'][variant];
1616
const inputStyle = editable ? variantStyle.active : variantStyle.disabled;
1717
const underlineStyle = variant === 'underline' && { borderBottomWidth: 2, borderBottomColor: inputStyle.highlight };
18+
const fontStyle = {
19+
...typography.body3,
20+
lineHeight: typography.body3.fontSize ? typography.body3.fontSize * 1.2 : undefined,
21+
};
1822

1923
return (
2024
<RNTextInput
@@ -23,7 +27,7 @@ const TextInput = React.forwardRef<RNTextInput, Props>(function TextInput(
2327
selectionColor={inputStyle.highlight}
2428
placeholderTextColor={inputStyle.placeholder}
2529
style={[
26-
typography.body3,
30+
fontStyle,
2731
styles.input,
2832
{ color: inputStyle.text, backgroundColor: inputStyle.background },
2933
underlineStyle,
@@ -38,8 +42,11 @@ const TextInput = React.forwardRef<RNTextInput, Props>(function TextInput(
3842

3943
const styles = createStyleSheet({
4044
input: {
41-
paddingVertical: 8,
42-
paddingHorizontal: 16,
45+
includeFontPadding: false,
46+
paddingTop: 8,
47+
paddingBottom: 8,
48+
paddingLeft: 16,
49+
paddingRight: 16,
4350
},
4451
});
4552

packages/uikit-react-native-foundation/src/styles/createStyleSheet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ const preProcessor: Partial<StylePreprocessor> = {
1818
'height': SCALE_FACTOR_WITH_STR,
1919
'width': SCALE_FACTOR_WITH_STR,
2020
'padding': SCALE_FACTOR_WITH_STR,
21+
'paddingVertical': SCALE_FACTOR_WITH_STR,
22+
'paddingHorizontal': SCALE_FACTOR_WITH_STR,
2123
'paddingTop': SCALE_FACTOR_WITH_STR,
2224
'paddingBottom': SCALE_FACTOR_WITH_STR,
2325
'paddingLeft': SCALE_FACTOR_WITH_STR,
2426
'paddingRight': SCALE_FACTOR_WITH_STR,
2527
'margin': SCALE_FACTOR_WITH_STR,
28+
'marginVertical': SCALE_FACTOR_WITH_STR,
29+
'marginHorizontal': SCALE_FACTOR_WITH_STR,
2630
'marginTop': SCALE_FACTOR_WITH_STR,
2731
'marginBottom': SCALE_FACTOR_WITH_STR,
2832
'marginLeft': SCALE_FACTOR_WITH_STR,

packages/uikit-react-native/src/components/ChannelInput/EditInput.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React, { forwardRef } from 'react';
2-
import {
3-
NativeSyntheticEvent,
4-
Platform,
5-
TextInput as RNTextInput,
6-
TextInputSelectionChangeEventData,
7-
View,
8-
} from 'react-native';
2+
import { NativeSyntheticEvent, TextInput as RNTextInput, TextInputSelectionChangeEventData, View } from 'react-native';
93

104
import { MentionType } from '@sendbird/chat/message';
115
import { Button, TextInput, createStyleSheet, useToast } from '@sendbird/uikit-react-native-foundation';
@@ -27,6 +21,7 @@ interface EditInputProps extends ChannelInputProps {
2721

2822
const EditInput = forwardRef<RNTextInput, EditInputProps>(function EditInput(
2923
{
24+
style,
3025
text,
3126
onChangeText,
3227
messageToEdit,
@@ -81,7 +76,7 @@ const EditInput = forwardRef<RNTextInput, EditInputProps>(function EditInput(
8176
editable={!inputDisabled}
8277
autoFocus={autoFocus}
8378
onChangeText={onChangeText}
84-
style={styles.input}
79+
style={style}
8580
placeholder={STRINGS.LABELS.CHANNEL_INPUT_PLACEHOLDER_ACTIVE}
8681
onSelectionChange={onSelectionChange}
8782
>
@@ -112,13 +107,6 @@ const styles = createStyleSheet({
112107
flexDirection: 'column',
113108
alignItems: 'center',
114109
},
115-
input: {
116-
flex: 1,
117-
marginRight: 4,
118-
minHeight: 36,
119-
maxHeight: 36 * Platform.select({ ios: 2.5, default: 2 }),
120-
borderRadius: 20,
121-
},
122110
inputWrapper: {
123111
flexDirection: 'row',
124112
},

packages/uikit-react-native/src/components/ChannelInput/SendInput.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { forwardRef } from 'react';
22
import {
33
NativeSyntheticEvent,
4-
Platform,
54
TextInput as RNTextInput,
65
TextInputSelectionChangeEventData,
76
TouchableOpacity,
@@ -38,6 +37,7 @@ interface SendInputProps extends ChannelInputProps {
3837

3938
const SendInput = forwardRef<RNTextInput, SendInputProps>(function SendInput(
4039
{
40+
style,
4141
VoiceMessageInput,
4242
MessageToReplyPreview,
4343
AttachmentsButton,
@@ -173,7 +173,7 @@ const SendInput = forwardRef<RNTextInput, SendInputProps>(function SendInput(
173173
onSelectionChange={onSelectionChange}
174174
editable={!inputDisabled}
175175
onChangeText={onChangeText}
176-
style={styles.input}
176+
style={style}
177177
placeholder={getPlaceholder()}
178178
>
179179
{mentionManager.textToMentionedComponents(
@@ -284,13 +284,6 @@ const styles = createStyleSheet({
284284
alignItems: 'center',
285285
flexDirection: 'row',
286286
},
287-
input: {
288-
flex: 1,
289-
marginRight: 4,
290-
minHeight: 36,
291-
maxHeight: 36 * Platform.select({ ios: 2.5, default: 2 }),
292-
borderRadius: 20,
293-
},
294287
sendIcon: {
295288
marginLeft: 4,
296289
padding: 4,

packages/uikit-react-native/src/components/ChannelInput/index.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useEffect, useState } from 'react';
2-
import { KeyboardAvoidingView, Platform, TextInput, View } from 'react-native';
1+
import React, { useEffect, useMemo, useState } from 'react';
2+
import { KeyboardAvoidingView, Platform, StyleProp, StyleSheet, TextInput, TextStyle, View } from 'react-native';
33
import { useSafeAreaInsets } from 'react-native-safe-area-context';
44

55
import { createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
@@ -39,6 +39,9 @@ export type SuggestedMentionListProps = {
3939
};
4040

4141
export type ChannelInputProps = {
42+
// style
43+
style?: StyleProp<TextStyle>;
44+
4245
// default
4346
channel: SendbirdBaseChannel;
4447
shouldRenderInput: boolean;
@@ -85,7 +88,7 @@ const ChannelInput = (props: ChannelInputProps) => {
8588
const { channel, keyboardAvoidOffset, messageToEdit, setMessageToEdit } = props;
8689

8790
const { top, left, right, bottom } = useSafeAreaInsets();
88-
const { colors } = useUIKitTheme();
91+
const { colors, typography } = useUIKitTheme();
8992
const { sbOptions, mentionManager } = useSendbirdChat();
9093

9194
const { selection, onSelectionChange, textInputRef, text, onChangeText, mentionedUsers } = useMentionTextInput({
@@ -98,11 +101,18 @@ const ChannelInput = (props: ChannelInputProps) => {
98101

99102
const mentionAvailable =
100103
sbOptions.uikit.groupChannel.channel.enableMention && channel.isGroupChannel() && !channel.isBroadcast;
101-
102104
const inputKeyToRemount = GET_INPUT_KEY(mentionAvailable ? mentionedUsers.length === 0 : false);
103105

104106
const [inputHeight, setInputHeight] = useState(styles.inputDefault.height);
105107

108+
const fontStyle = useMemo(() => {
109+
if (!typography.body3.fontSize) return typography.body3;
110+
// NOTE: iOS does not support textAlignVertical, so we should adjust lineHeight to center the text in multiline TextInput.
111+
return { ...typography.body3, lineHeight: typography.body3.fontSize * 1.275, textAlignVertical: 'center' };
112+
}, [typography.body3.fontSize]);
113+
114+
const textInputStyle = StyleSheet.flatten([styles.input, fontStyle, props.style]);
115+
106116
useTypingTrigger(text, channel);
107117
useTextClearOnDisabled(onChangeText, props.inputDisabled);
108118
useAutoFocusOnEditMode(textInputRef, messageToEdit);
@@ -138,6 +148,7 @@ const ChannelInput = (props: ChannelInputProps) => {
138148
VoiceMessageInput={props.VoiceMessageInput ?? VoiceMessageInput}
139149
AttachmentsButton={props.AttachmentsButton ?? AttachmentsButton}
140150
MessageToReplyPreview={props.MessageToReplyPreview ?? MessageToReplyPreview}
151+
style={textInputStyle}
141152
/>
142153
)}
143154
{inputMode === 'edit' && messageToEdit && (
@@ -152,6 +163,7 @@ const ChannelInput = (props: ChannelInputProps) => {
152163
mentionedUsers={mentionedUsers}
153164
messageToEdit={messageToEdit}
154165
setMessageToEdit={setMessageToEdit}
166+
style={textInputStyle}
155167
/>
156168
)}
157169
</View>
@@ -211,6 +223,17 @@ const styles = createStyleSheet({
211223
inputDefault: {
212224
height: 56,
213225
},
226+
input: {
227+
flex: 1,
228+
marginRight: 4,
229+
borderRadius: 20,
230+
paddingTop: 8,
231+
paddingBottom: 8,
232+
minHeight: 36,
233+
// Android - padding area is hidden
234+
// iOS - padding area is visible
235+
maxHeight: Platform.select({ ios: 36 * 2 + 16, android: 36 * 2 }),
236+
},
214237
});
215238

216239
export default React.memo(ChannelInput);

sample/src/screens/SignInScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ const styles = StyleSheet.create({
9797
width: '100%',
9898
borderRadius: 4,
9999
marginBottom: 32,
100-
paddingHorizontal: 16,
101-
paddingVertical: 16,
100+
paddingTop: 16,
101+
paddingBottom: 16,
102102
},
103103
});
104104

0 commit comments

Comments
 (0)