Styling
tip
You are kindly advised to read โCSS Processing before continuing.
Inline Styles#
Inline styles set via the HTML โstyle attribute are processed by the โ@native-html/css-processor library. You don't need to wonder if a CSS property will break your app: the CSS processor acts as a compatibility layer between React Native styles and CSS properties. This library gives you leverage on inline CSS processing via multiple props:
Disable inline styles processing altogether.
Whitelist the camel-cased CSS properties that you wish to be included.
Blacklist the camel-cased CSS properties that you wish to be excluded.
Let's try it out:
- JSX Source
- HTML Snippet
- TRT Snapshot
Props#
The โRenderHTML component has four props to customize elements styles:
โ
baseStyleThe styles for the root component. Inheritable styles will be inherited by all children.
โ
idsStylesTarget elements with the HTML โ
idattribute.Target elements with the HTML โ
classattribute;โ
tagsStylesTarget elements by tag name.
Each of these props is a record mapping identifiers with a โMixedStyleDeclaration.
note
There is not (yet) ways to provide CSS selectors to target elements. Because it would require a full-featured CSS parser to build an AST, this feature might be supported in the future with an extension.
Mixed Style Records#
Introduction#
A mixed style declaration is an object similar to โStyleSheet's create method argument values. However, it supports a blend of React Native ViewStyle, TextStyle and camel-cased CSS properties. See โCSS Processing for a complete reference.
In the above snippet, mixed-style declarations are the objects defined as values of the tagsStyles record. A few important comments:
Line 2. The โ
<body>will always be present and can be confidently targeted, event when missing in the HTML snippet.Line 3. This is the default property, with the exception of โ
<pre>and a few other tags.Line 4. Thanks to CSS inheritence, all textual children of โ
<body>will default to this property.Line 7. The โTRE implements CSS specificity. A rule targetting the โ
<a>element will override rules defined for its ancestors (in this case, โ<body>).
warning
You should never provide styles from React Native โStyleSheet. These will not work.
Example#
- JSX Source
- HTML Snippet
- TRT Snapshot