Skip to content

Commit cf04bff

Browse files
committed
fix: use styleprop and stylesheet.flatten
1 parent db827a3 commit cf04bff

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/lib/CodeHighlighter.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
ScrollView,
66
type ViewStyle,
77
type TextStyle,
8+
type StyleProp,
9+
StyleSheet,
810
} from "react-native";
911
import SyntaxHighlighter, {
1012
type SyntaxHighlighterProps,
@@ -18,8 +20,8 @@ import {
1820

1921
export interface CodeHighlighterProps extends SyntaxHighlighterProps {
2022
hljsStyle: ReactStyle;
21-
containerStyle?: ViewStyle;
22-
textStyle?: TextStyle;
23+
containerStyle?: StyleProp<ViewStyle>;
24+
textStyle?: StyleProp<TextStyle>;
2325
}
2426

2527
export const CodeHighlighter: FunctionComponent<CodeHighlighterProps> = ({
@@ -37,18 +39,19 @@ export const CodeHighlighter: FunctionComponent<CodeHighlighterProps> = ({
3739
const getStylesForNode = (node: rendererNode): TextStyle[] => {
3840
const classes: string[] = node.properties?.className || [];
3941
return classes
40-
.map<TextStyle | undefined>((c: string) => stylesheet[c])
42+
.map((c: string) => stylesheet[c])
4143
.filter((c) => !!c) as TextStyle[];
4244
};
4345

4446
const renderNode = (nodes: rendererNode[], keyPrefix = "row") =>
4547
nodes.reduce<ReactNode[]>((acc, node, index) => {
4648
const keyPrefixWithIndex = `${keyPrefix}_${index}`;
4749
if (node.children) {
48-
const styles = [
49-
{ ...textStyle, color: stylesheet.hljs?.color },
50+
const styles = StyleSheet.flatten([
51+
textStyle,
52+
{ color: stylesheet.hljs?.color },
5053
getStylesForNode(node),
51-
];
54+
]);
5255
acc.push(
5356
<Text style={styles} key={keyPrefixWithIndex}>
5457
{renderNode(node.children, `${keyPrefixWithIndex}_child`)}
@@ -63,7 +66,7 @@ export const CodeHighlighter: FunctionComponent<CodeHighlighterProps> = ({
6366
return acc;
6467
}, []);
6568

66-
const nativeRenderer = (props: rendererProps) => {
69+
const renderer = (props: rendererProps) => {
6770
const { rows } = props;
6871
return (
6972
<ScrollView
@@ -78,7 +81,7 @@ export const CodeHighlighter: FunctionComponent<CodeHighlighterProps> = ({
7881
return (
7982
<SyntaxHighlighter
8083
{...rest}
81-
renderer={nativeRenderer}
84+
renderer={renderer}
8285
CodeTag={View}
8386
PreTag={View}
8487
style={{}}

0 commit comments

Comments
 (0)