Skip to content

Commit 90b8a3d

Browse files
committed
feat: new bypassAnonymousTPhrasingNodes prop
This prop allows to opt-out from a new behavior introduced during 6.0.0-beta stage where TPhrasing nodes with one child would be bypassed. fix #514
1 parent 52a857a commit 90b8a3d

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

packages/render-html/src/RenderHTMLConfigProvider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const childrenRendererContext = {
2323
export type RenderHTMLConfigPropTypes = Record<keyof RenderHTMLConfig, any>;
2424

2525
export const renderHTMLConfigPropTypes: RenderHTMLConfigPropTypes = {
26+
bypassAnonymousTPhrasingNodes: PropTypes.bool,
2627
defaultTextProps: PropTypes.object,
2728
defaultViewProps: PropTypes.object,
2829
enableExperimentalMarginCollapsing: PropTypes.bool,

packages/render-html/src/TPhrasingRenderer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export default function TPhrasingRenderer(
3535
const TNodeChildrenRenderer = useTNodeChildrenRenderer();
3636
// When a TPhrasing node is anonymous and has only one child, its
3737
// rendering amounts to rendering its only child.
38-
if (props.tnode.tagName == null && props.tnode.children.length <= 1) {
38+
if (
39+
props.sharedProps.bypassAnonymousTPhrasingNodes &&
40+
props.tnode.tagName == null &&
41+
props.tnode.children.length <= 1
42+
) {
3943
return React.createElement(TNodeChildrenRenderer, {
4044
tnode: props.tnode
4145
});

packages/render-html/src/context/defaultSharedProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function WebViewPlaceholder() {
1313
}
1414

1515
const defaultSharedProps: Required<RenderHTMLSharedProps> = {
16+
bypassAnonymousTPhrasingNodes: true,
1617
debug: false,
1718
defaultTextProps: {
1819
selectable: false,

packages/render-html/src/shared-types.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,57 @@ export interface RenderHTMLSharedProps {
245245
* @defaultValue A `TouchableNativeFeedback` based component on Android, `TouchableHighlight` based component on other platforms.
246246
*/
247247
GenericPressable?: ComponentType<GenericPressableProps>;
248+
248249
/**
249250
* The WebView component used by plugins (iframe, table)...
250251
* See {@link https://github.com/native-html/plugins | @native-html/plugins}.
251252
*
252253
* @defaultValue `() => null`
253254
*/
254255
WebView?: ComponentType<any>;
256+
257+
/**
258+
* When `true` (default), anonymous {@link TPhrasing} nodes parents of a
259+
* lonely {@link TText} node are not translated as React Native `Text`
260+
* elements. Instead, their child is directly rendered, e.g. with no `Text`
261+
* wrapper.
262+
*
263+
* @example **With `true`:**
264+
*
265+
* ```xml
266+
* <TPhrasing>
267+
* <TText>Hello</TText>
268+
* </TPhrasing>
269+
* ```
270+
*
271+
* is translated to
272+
*
273+
* ```xml
274+
* <Text>Hello</Text>
275+
* ```
276+
*
277+
* **With `false`:**
278+
*
279+
* ```xml
280+
* <TPhrasing>
281+
* <TText>Hello</TText>
282+
* </TPhrasing>
283+
* ```
284+
*
285+
* is translated to
286+
*
287+
* ```xml
288+
* <Text><Text>Hello</Text></Text>
289+
* ```
290+
*
291+
* @warning Unless strictly necessary, this should be left to `true` because
292+
* some styles don't apply to nested React Native `Text` elements
293+
* (`borderRadius`, `padding`...).
294+
*
295+
* @defaultValue true
296+
*/
297+
bypassAnonymousTPhrasingNodes?: boolean;
298+
255299
/**
256300
* A function which takes contentWidth and tagName as arguments and returns a
257301
* new width. Can return Infinity to denote unconstrained widths.
@@ -268,6 +312,7 @@ export interface RenderHTMLSharedProps {
268312
* @defaultValue `(c) => c`
269313
*/
270314
computeEmbeddedMaxWidth?: (contentWidth: number, tagName: string) => number;
315+
271316
/**
272317
* Provide support for list style types which are not supported by this
273318
* library.
@@ -290,29 +335,34 @@ export interface RenderHTMLSharedProps {
290335
* ```
291336
*/
292337
customListStyleSpecs?: Record<string, ListStyleSpec>;
338+
293339
/**
294340
* Log to the console a snapshot of the rendered {@link TDocument} after each
295341
* transient render tree invalidation.
296342
*
297343
* @defaultValue `false`
298344
*/
299345
debug?: boolean;
346+
300347
/**
301348
* Default props for Text elements in the render tree.
302349
*
303350
* @remarks "style" will be merged into the tnode own styles.
304351
*/
305352
defaultTextProps?: TextProps;
353+
306354
/**
307355
* Default props for View elements in the render tree.
308356
*
309357
* @remarks "style" will be merged into the tnode own styles.
310358
*/
311359
defaultViewProps?: ViewProps;
360+
312361
/**
313362
* Default props for WebView elements in the render tree used by plugins.
314363
*/
315364
defaultWebViewProps?: any;
365+
316366
/**
317367
* Enable or disable margin collapsing CSS behavior (experimental!).
318368
* See {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing | MDN docs}.

0 commit comments

Comments
 (0)