Skip to content

Commit

Permalink
custom lineHeight approach
Browse files Browse the repository at this point in the history
  • Loading branch information
tienifr committed Dec 25, 2023
1 parent 7054e8d commit f3c1933
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function CodeRenderer(props) {
boxModelStyle={boxModelStyle}
textStyle={{...textStyle, ...textStyleOverride}}
key={props.key}
fontSize={fontSize}
/>
);
}
Expand Down
35 changes: 13 additions & 22 deletions src/components/InlineCodeBlock/WrappedText.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, {Fragment} from 'react';
import {StyleProp, TextStyle, View, ViewStyle} from 'react-native';
import Text from '@components/Text';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

Expand All @@ -15,9 +13,6 @@ type WrappedTextProps = ChildrenProps & {
* Style for each individual word (token) in the text. Note that a token can also include whitespace characters between words.
*/
wordStyles?: StyleProp<ViewStyle>;

/** The size of the text */
fontSize?: number;
};

/**
Expand All @@ -40,12 +35,11 @@ function getTextMatrix(text: string): string[][] {
* Validates if the text contains any emoji
*/
function containsEmoji(text: string): boolean {
return CONST.REGEX.EMOJIS.test(text);
return CONST.REGEX.EMOJI.test(text);
}

function WrappedText({children, wordStyles, textStyles, fontSize = variables.fontSizeNormal}: WrappedTextProps) {
function WrappedText({children, wordStyles, textStyles}: WrappedTextProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();

if (typeof children !== 'string') {
return null;
Expand All @@ -58,21 +52,18 @@ function WrappedText({children, wordStyles, textStyles, fontSize = variables.fon
// eslint-disable-next-line react/no-array-index-key
key={`${rowText[0]}-${rowIndex}`}
>
{rowText.map((colText, colIndex) => {
const lineHeight = StyleUtils.getCodeLineHeight(containsEmoji(colText), fontSize);
return (
// Outer View is important to vertically center the Text
<View
// eslint-disable-next-line react/no-array-index-key
key={`${colText}-${colIndex}`}
style={styles.codeWordWrapper}
>
<View style={[wordStyles, colIndex === 0 && styles.codeFirstWordStyle, colIndex === rowText.length - 1 && styles.codeLastWordStyle]}>
<Text style={[textStyles, lineHeight ? {lineHeight} : {}]}>{colText}</Text>
</View>
{rowText.map((colText, colIndex) => (
// Outer View is important to vertically center the Text
<View
// eslint-disable-next-line react/no-array-index-key
key={`${colText}-${colIndex}`}
style={styles.codeWordWrapper}
>
<View style={[wordStyles, colIndex === 0 && styles.codeFirstWordStyle, colIndex === rowText.length - 1 && styles.codeLastWordStyle]}>
<Text style={[textStyles, !containsEmoji(colText) && styles.codePlainTextStyle]}>{colText}</Text>
</View>
);
})}
</View>
))}
</Fragment>
));
}
Expand Down
1 change: 0 additions & 1 deletion src/components/InlineCodeBlock/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function InlineCodeBlock<TComponent extends TText>({TDefaultRenderer, defaultRen
<WrappedText
textStyles={textStyle}
wordStyles={[boxModelStyle, styles.codeWordStyle]}
fontSize={fontSize}
>
{defaultRendererProps.tnode.data}
</WrappedText>
Expand Down
4 changes: 4 additions & 0 deletions src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,10 @@ const styles = (theme: ThemeColors) =>
paddingRight: 5,
},

codePlainTextStyle: {
...codeStyles.codePlainTextStyle,
},

fullScreenLoading: {
backgroundColor: theme.componentBG,
opacity: 0.8,
Expand Down
6 changes: 5 additions & 1 deletion src/styles/utils/codeStyles/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ const codeTextStyle: CodeTextStyles = {
lineHeight: 15,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle};
const codePlainTextStyle: CodeTextStyles = {
lineHeight: 14.5,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle, codePlainTextStyle};
6 changes: 5 additions & 1 deletion src/styles/utils/codeStyles/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ const codeTextStyle: CodeTextStyles = {
lineHeight: 18,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle};
const codePlainTextStyle: CodeTextStyles = {
lineHeight: 15,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle, codePlainTextStyle};
3 changes: 2 additions & 1 deletion src/styles/utils/codeStyles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import {CodeTextStyles, CodeWordStyles, CodeWordWrapperStyles} from './types';
const codeWordWrapper: CodeWordWrapperStyles = {};
const codeWordStyle: CodeWordStyles = {};
const codeTextStyle: CodeTextStyles = {};
export default {codeWordWrapper, codeWordStyle, codeTextStyle};
const codePlainTextStyle: CodeTextStyles = {};
export default {codeWordWrapper, codeWordStyle, codeTextStyle, codePlainTextStyle};
8 changes: 0 additions & 8 deletions src/styles/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,6 @@ function getCodeFontSize(isInsideH1: boolean) {
return isInsideH1 ? 15 : 13;
}

/**
* Returns the line height for the HTML code tag renderer.
*/
function getCodeLineHeight(isEmojiChunk: boolean, fontSize: number): number {
return isEmojiChunk ? 0 : fontSize;
}

/**
* Gives the width for Emoji picker Widget
*/
Expand Down Expand Up @@ -1058,7 +1051,6 @@ const staticStyleUtils = {
getEmojiReactionBubbleTextStyle,
getFontFamilyMonospace,
getCodeFontSize,
getCodeLineHeight,
getFontSizeStyle,
getLineHeightStyle,
getMenuItemTextContainerStyle,
Expand Down

0 comments on commit f3c1933

Please sign in to comment.