nativescript
via PatrickJS/awesome-cursorrules
NativeScript best practices for cross-platform mobile development with TypeScript, Vue, Svelte, and React.
What is nativescript?
Provides conventions and patterns for building NativeScript mobile applications, covering code organization, platform-specific handling, UI styling with TailwindCSS, and performance optimization. Use this rule when developing cross-platform mobile apps with NativeScript to ensure consistent structure, proper native integration, and efficient rendering.
- Organize modular components and services with platform-specific file patterns (.ios.ts, .android.ts)
- Apply naming conventions for platform-specific code and descriptive component/style names
- Configure TailwindCSS with platform and dark mode variants (ios:, android:, .ns-dark)
- Use GridLayout and StackLayout for responsive, performant layouts with proper nesting strategies
- Manage native class extensions with @NativeClass(), ObjCProtocols declarations, and delegate retention for iOS
- Implement conditional platform code using __ANDROID__ and __APPLE__ with tree-shaking support
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
NativeScript Best Practices
Code Style and Structure
- Organize code using modular components and services for maintainability.
- Use platform-specific files (
.ios.ts,.android.ts) when code exceeds 20 platform-specific lines. - When creating custom native code, use a folder structure like
custom-native/index.ios.ts,custom-native/index.android.ts,custom-native/common.ts,custom-native/index.d.tsto keep platform-specific code organized and easy to import with single import elsewhere, replacingcustom-nativewith the name of the custom code.
Naming Conventions
- Prefix platform-specific variables with
iosorandroid(e.g.,iosButtonStyle). - Name custom components and styles descriptively (
primaryButtonStyle,userProfileView).
Usage
- Use
@NativeClass()when extending native classes when needed - For iOS, when extending native classes, always use
static ObjCProtocols = [AnyUIKitDelegate];to declare custom delegates if a delegate is required or used. - For iOS, always retain custom delegate instances to prevent garbage collection. For example,
let delegate = MyCustomDelegate.new() as MyCustomDelegate, and ensure it is retained in the class scope. - Favor
__ANDROID__and__APPLE__for conditional platform code with tree-shaking. - Track and clean up all timers (
setTimeout,setInterval) to avoid memory leaks.
UI and Styling
- Always TailwindCSS as the CSS Framework using
"@nativescript/tailwind": "^2.1.0"for consistent styling paired with"tailwindcss": "~3.4.0". - Add ios: and android: style variants for platform-specific styling, addVariant('android', '.ns-android &'), addVariant('ios', '.ns-ios &');
- darkMode: ['class', '.ns-dark']
- Leverage
GridLayoutorStackLayoutfor flexible, responsive layouts. Place more emphasis on proper GridLayout usage for complex layouts but use StackLayout for simpler, linear arrangements. - Use
visibility: 'hidden'for elements that should not affect layout when hidden.
Performance Optimization
- Try to avoid deeply nesting layout containers but instead use
GridLayoutwisely to setup complex layouts. - Avoid direct manipulation of the visual tree during runtime to minimize rendering overhead.
- Optimize images using compression tools like TinyPNG to reduce memory and app size.
- Clean the project (
ns clean) after modifying files inApp_Resourcesorpackage.json.
Key Conventions
- Reuse components and styles to avoid duplication.
- Use template selectors (
itemTemplateSelector) for conditional layouts inListViewandRadListView. - Minimize heavy computations in UI bindings or methods.
- Only if using plain xml bindings, use
ObservableorObservableArrayproperties to reflect state changes efficiently. - When using Angular, React, Solid, Svelte or Vue, always leverage their respective state management, lifecycle hooks, rendering optimizations and reactive bindings for optimal performance.
Related rules
Senior full-stack TypeScript, React, Node.js guidance with clean architecture, testing, and WHY-oriented reasoning.
Quantitative factor research skills for designing, evaluating, and mining alpha factors in equities markets.
Android development with Jetpack Compose, clean architecture, and Material Design 3.
Angular development with Novo Elements UI library using standalone components.
Expert Angular 18 + TypeScript development with Jest, emphasizing clean code and performance.
Manage Kubernetes clusters, add-ons, stacks, and credentials via the Ankra CLI platform.