HTML first
Writing shared HTML features
A shared feature is an Angular component in packages/shared whose template works in a browser and under MasonKit.
A real example
packages/shared/src/lib/home/home-feature.component.ts:
@Component({
selector: 'app-home-feature',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './home-feature.component.html',
})
export class HomeFeature {
readonly features = FEATURES;
}Its data comes from packages/shared/src/lib/content.ts, which is plain TypeScript with no platform imports. The package index re-exports both, so either app can import { HomeFeature } from '@nativescript-unified/shared'.
Rules
- Use plain HTML elements (
div,p,h1toh6,ul,li,a,span,img) and Spartan helm components. - Import nothing from
@analogjs/*or@nativescript/*inpackages/shared. Those belong to an app. - Style with the token classes (
bg-background,text-foreground,bg-card,text-muted-foreground). The tokens come frompackages/ui/styles/tokens.css. - Put loose text that sits beside elements inside
@foror@ifin a<span>. MasonKit places dynamically inserted text nodes after their element siblings. - Prefer Tailwind classes for lengths. In a stylesheet, remember that
pxis a CSS pixel on MasonKit elements but a device pixel on core NativeScript views such asLabelorActionBar. The example stylesheets keep lengths out of CSS for that reason and hold only colours, gradients and weights. - Prefer
OnPushand signals. Both apps run zoneless.
Control flow works
This demo uses @for over plain markup, the same way the shared feature does:
Feature 1
Rendered from a loop with plain elements only.
Feature 2
Rendered from a loop with plain elements only.
Feature 3
Rendered from a loop with plain elements only.
<div class="flex flex-col gap-2">
@for (n of [1, 2, 3]; track n) {
<div class="flex flex-col gap-1 p-3 rounded-lg bg-card text-card-foreground border">
<h2 class="text-base font-semibold">Feature {{ n }}</h2>
<p class="text-sm text-muted-foreground">Rendered from a loop with plain elements only.</p>
</div>
}
</div>Using it in each app
On web, import the component in a page and use its selector. On native, do the same inside a page that provides the ActionBar and <scroll>. See Web vs native.
Larger examples
packages/shared/src/lib/examples holds thirteen full screens built the same way: pricing, checkout, profile, analytics, settings, events, ratings, sign in, music player, article, two chart screens and a sprint board that opens each kind of overlay. Each is one component plus one stylesheet that sets its own palette (see Scoped palettes). The home page of this site renders them live, and the native app opens them from its UI tab with Code and Styles tabs beside the preview.
Tailwind on native
apps/native runs tailwindcss through @nativescript/tailwind. Its app.css imports Tailwind and the shared tokens, and scans packages/shared/src for class names, so the same classes style native. Two cases differ from the web. Opacity-modified token colours such as bg-destructive/10 keep the plain light token colour, and helm's own rules can override a utility you put on a helm host. See Known native limitations.