Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix differences in inline code block on native vs web #58010

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import {View} from 'react-native';
import {splitBoxModelStyle} from 'react-native-render-html';
import type {CustomRendererProps, TPhrasing, TText} from 'react-native-render-html';
import * as HTMLEngineUtils from '@components/HTMLEngineProvider/htmlEngineUtils';
Expand Down Expand Up @@ -31,13 +32,15 @@ function CodeRenderer({TDefaultRenderer, key, style, ...defaultRendererProps}: C
};

return (
<InlineCodeBlock
defaultRendererProps={{...defaultRendererProps, style: {}}}
TDefaultRenderer={TDefaultRenderer}
boxModelStyle={boxModelStyle}
textStyle={{...textStyle, ...textStyleOverride}}
key={key}
/>
<View style={{marginBottom: 4}}>
<InlineCodeBlock
defaultRendererProps={{...defaultRendererProps, style: {marginBottom: 4}}}
TDefaultRenderer={TDefaultRenderer}
boxModelStyle={[boxModelStyle]}
textStyle={{...textStyle, ...textStyleOverride}}
key={key}
/>
</View>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { splitBoxModelStyle } from 'react-native-render-html';
import type { CustomRendererProps, TPhrasing, TText } from 'react-native-render-html';
import * as HTMLEngineUtils from '@components/HTMLEngineProvider/htmlEngineUtils';
import InlineCodeBlock from '@components/InlineCodeBlock';
import useStyleUtils from '@hooks/useStyleUtils';
import FontUtils from '@styles/utils/FontUtils';

type CodeRendererProps = CustomRendererProps<TText | TPhrasing> & {
/** Key of the element */
key?: string;
};

function CodeRenderer({ TDefaultRenderer, key, style, ...defaultRendererProps }: CodeRendererProps) {
const StyleUtils = useStyleUtils();
// We split wrapper and inner styles
// "boxModelStyle" corresponds to border, margin, padding and backgroundColor
const { boxModelStyle, otherStyle: textStyle } = splitBoxModelStyle(style ?? {});

/** Get the default fontFamily variant */
const font = FontUtils.fontFamily.platform.MONOSPACE.fontFamily;

// Determine the font size for the code based on whether it's inside an H1 element.
const isInsideH1 = HTMLEngineUtils.isChildOfH1(defaultRendererProps.tnode);

const fontSize = StyleUtils.getCodeFontSize(isInsideH1);

const textStyleOverride = {
fontSize,
fontFamily: font,
};

return (
<InlineCodeBlock
defaultRendererProps={{ ...defaultRendererProps, style: { marginBottom: 4 } }}
TDefaultRenderer={TDefaultRenderer}
boxModelStyle={[boxModelStyle]}
textStyle={{ ...textStyle, ...textStyleOverride }}
key={key}
/>
);
}

CodeRenderer.displayName = 'CodeRenderer';

export default CodeRenderer;
5 changes: 4 additions & 1 deletion src/components/InlineCodeBlock/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import {View} from 'react-native';
import type {TDefaultRendererProps} from 'react-native-render-html';
import Text from '@components/Text';
import type InlineCodeBlockProps from './types';
Expand Down Expand Up @@ -27,7 +28,9 @@ function InlineCodeBlock<TComponent extends TTextOrTPhrasing>({TDefaultRenderer,
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
<Text style={[boxModelStyle, textStyle]}>{data}</Text>
<View style={[boxModelStyle]}>
<Text style={[textStyle]}>{data}</Text>
</View>
</TDefaultRenderer>
);
}
Expand Down
Loading