Skip to content
Merged
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
5 changes: 4 additions & 1 deletion example/src/Examples/ButtonExample.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Image, StyleSheet, View } from 'react-native';

import { Button, List } from 'react-native-paper';
import { Button, List, Text } from 'react-native-paper';

import { useExampleTheme } from '..';
import ScreenWrapper from '../ScreenWrapper';
Expand Down Expand Up @@ -283,6 +283,9 @@ const ButtonExample = () => {
>
Custom Font
</Button>
<Button mode="outlined" onPress={() => {}} style={styles.button}>
<Text variant="titleLarge">Custom text</Text>
</Button>
</View>

<View style={styles.row}>
Expand Down
16 changes: 15 additions & 1 deletion example/src/Examples/ChipExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import * as React from 'react';
import { Image, StyleSheet, View } from 'react-native';

import color from 'color';
import { Chip, List, MD2Colors, MD3Colors, Snackbar } from 'react-native-paper';
import {
Chip,
List,
MD2Colors,
MD3Colors,
Snackbar,
Text,
} from 'react-native-paper';

import { useExampleTheme } from '..';
import ScreenWrapper from '../ScreenWrapper';
Expand Down Expand Up @@ -310,6 +317,13 @@ const ChipExample = () => {
>
With custom close icon
</Chip>
<Chip
onPress={() => {}}
style={styles.chip}
textStyle={styles.tiny}
>
<Text variant="titleLarge">With custom text</Text>
</Chip>
</View>
<Chip mode="outlined" onPress={() => {}} style={styles.fullWidthChip}>
Full width chip
Expand Down
41 changes: 38 additions & 3 deletions src/components/Typography/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,43 @@ const Text = (
}));

if (theme.isV3 && variant) {
const font = theme.fonts[variant];
let font = theme.fonts[variant];
let textStyle = [font, style];

if (
rest.children &&
typeof rest.children === 'object' &&
'props' in rest.children
) {
const { props } = rest.children;

// Context: Some components have the built-in `Text` component with a predefined variant,
// that also accepts `children` as a `React.Node`. This can result in a situation,
// where another `Text` component is rendered within the built-in `Text` component.
// By doing that, we assume that user doesn't want to consume pre-defined font properties.
// Case one: Nested `Text` has different `variant` that specified in parent. For example:
// <Chip>
// <Text variant="displayMedium">Nested</Text>
// </Chip>
// Solution: To address the following scenario, the code below overrides the `variant`
// specified in a parent in favor of children's variant:
if (props.variant) {
font = theme.fonts[props.variant as VariantProp<typeof props.variant>];
textStyle = [style, font];
}

// Case two: Nested `Text` has specified `styles` which intefere
// with font properties, from the parent's `variant`. For example:
// <Chip>
// <Text style={{fontSize: 30}}>Nested</Text>
// </Chip>
// Solution: To address the following scenario, the code below overrides the
// parent's style with children's style:
if (!props.variant) {
textStyle = [style, props.style];
}
}

if (typeof font !== 'object') {
throw new Error(
`Variant ${variant} was not provided properly. Valid variants are ${Object.keys(
Expand All @@ -109,10 +145,9 @@ const Text = (
<NativeText
ref={root}
style={[
font,
styles.text,
{ writingDirection, color: theme.colors.onSurface },
style,
textStyle,
]}
{...rest}
/>
Expand Down
22 changes: 12 additions & 10 deletions src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,6 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A
numberOfLines={1}
style={
Array [
Object {
"fontFamily": "System",
"fontSize": 22,
"fontWeight": "400",
"letterSpacing": 0,
"lineHeight": 28,
},
Object {
"textAlign": "left",
},
Expand All @@ -653,15 +646,24 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A
},
Array [
Object {
"color": "rgba(28, 27, 31, 1)",
"fontFamily": "System",
"fontSize": 22,
"fontWeight": "400",
"letterSpacing": 0,
"lineHeight": 28,
},
false,
undefined,
Array [
Object {
"color": "rgba(28, 27, 31, 1)",
"fontFamily": "System",
"fontSize": 22,
"fontWeight": "400",
"letterSpacing": 0,
"lineHeight": 28,
},
false,
undefined,
],
],
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ exports[`can render leading checkbox control 1`] = `
maxFontSizeMultiplier={1.5}
style={
Array [
Object {
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
Object {
"textAlign": "left",
},
Expand All @@ -143,15 +136,24 @@ exports[`can render leading checkbox control 1`] = `
},
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "right",
},
undefined,
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "right",
},
undefined,
],
],
]
}
Expand Down Expand Up @@ -212,13 +214,6 @@ exports[`can render the Android checkbox on different platforms 1`] = `
maxFontSizeMultiplier={1.5}
style={
Array [
Object {
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
Object {
"textAlign": "left",
},
Expand All @@ -228,15 +223,24 @@ exports[`can render the Android checkbox on different platforms 1`] = `
},
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
],
],
]
}
Expand Down Expand Up @@ -411,13 +415,6 @@ exports[`can render the iOS checkbox on different platforms 1`] = `
maxFontSizeMultiplier={1.5}
style={
Array [
Object {
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
Object {
"textAlign": "left",
},
Expand All @@ -427,15 +424,24 @@ exports[`can render the iOS checkbox on different platforms 1`] = `
},
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
],
],
]
}
Expand Down Expand Up @@ -574,13 +580,6 @@ exports[`renders unchecked 1`] = `
maxFontSizeMultiplier={1.5}
style={
Array [
Object {
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
Object {
"textAlign": "left",
},
Expand All @@ -590,15 +589,24 @@ exports[`renders unchecked 1`] = `
},
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
"fontFamily": "System",
"fontSize": 16,
"fontWeight": "400",
"letterSpacing": 0.15,
"lineHeight": 24,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
Array [
Object {
"flexGrow": 1,
"flexShrink": 1,
},
false,
Object {
"color": "rgba(28, 27, 31, 1)",
"textAlign": "left",
},
undefined,
],
],
]
}
Expand Down
Loading