You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working on view markup with NativeScript, a collection of elements you interact with are registered for you like [GridLayout](https://docs.nativescript.org/ui/grid-layout), [Button](https://docs.nativescript.org/ui/button), [Label](https://docs.nativescript.org/ui/label), etc. These are just commonly used elements.
8
+
When working with view markup in NativeScript, you interact with a set of pre-registered elements, such as [`GridLayout`](https://docs.nativescript.org/ui/grid-layout), [`Button`](https://docs.nativescript.org/ui/button), and [`Label`](https://docs.nativescript.org/ui/label). These are commonly referred to as **"view components."**
9
9
10
-
You can create new custom native elements or extend existing ones to enhance behavior for all sorts of cases.
10
+
You can also create custom native elements or extend existing ones to add specialized functionality to your application.
11
11
12
-
Let's first look at how you would register new elements across all flavors and then we'll discuss how to build one, starting with a simple example of a custom View class.
12
+
This guide demonstrates how to:
13
+
14
+
- Register custom view elements across all NativeScript flavors.
15
+
- Implement a simple custom view component, starting from a basic class definition:
13
16
14
17
```ts
15
18
import { View } from'@nativescript/core'
16
19
17
20
exportclassCheckboxextendsView {
18
-
//impl
21
+
//implementation details
19
22
}
20
23
```
21
24
@@ -138,109 +141,303 @@ You can now use it like anything else:
138
141
</Tab>
139
142
</Tabs>
140
143
141
-
## Creating New Views
144
+
## Creating Custom View Components
142
145
143
-
Creating a new view element for use across anything in NativeScript (Angular, React, Solid, Svelte, TypeScript by itself, Vue, etc) is exactly the same.
146
+
The process for creating new NativeScript view components is consistent across Angular, React, Solid, Svelte, TypeScript, and Vue.
144
147
145
-
### Custom View Anatomy
148
+
### Anatomy of a Custom View
146
149
147
-
There's only 2 **fundamental** required aspects to any custom NativeScript view component (with 2 _optional_ additions):
150
+
Every custom NativeScript view must have:
148
151
149
-
1. Extend any NativeScript View.
150
-
2. (**required**) `createNativeView`: Construct and return any platformnative view.
- (**required**) A class extending any NativeScript View.
153
+
- (**required**) `createNativeView`: Construct and return a platform-native view._Note: It's only required if you want to override what's being returned from the extended class._
154
+
- (_optional_) `initNativeView`: Perform initialization after creation.
155
+
- (_optional_) `disposeNativeView`: Cleanup resources when removed.
153
156
154
-
Let's look at those within the context of an example:
157
+
Example:
155
158
156
159
```ts
157
-
// 1. (required) Extend any NativeScript View
160
+
import { View } from'@nativescript/core'
161
+
158
162
exportclassCustomViewextendsView {
159
-
// 2. (required) Construct and return any platform native view
160
163
createNativeView() {
161
-
// return instance of UIView or android.view.View;
164
+
// return UIView or android.view.View instance
162
165
}
163
166
164
-
// 3. (optional) initialize anything
165
-
initNativeView() {}
167
+
initNativeView() {
168
+
// initialization code
169
+
}
166
170
167
-
// 4. (optional) cleanup anything
168
-
disposeNativeView() {}
171
+
disposeNativeView() {
172
+
// cleanup code
173
+
}
169
174
}
170
175
```
171
176
172
-
Let's explain each point respectively.
177
+
#### Extending Existing Views
173
178
174
-
1. (required) extend any NativeScript View: **any** NativeScript view, for example these are all valid:
179
+
You can extend **any**existing NativeScript view, for example:
4.`disposeNativeView`: Destroy and cleanup anything if needed.
211
-
212
-
You will find many examples of this pattern throughout @nativescript/core, for example:
205
+
#### Initialization and Cleanup
213
206
214
-
-[Button implementation for iOS](https://github.com/NativeScript/NativeScript/blob/96af6fa83e586a1c443c8b179701450d803e12aa/packages/core/ui/button/index.ios.ts#L21)
215
-
-[Button implementation for Android](https://github.com/NativeScript/NativeScript/blob/96af6fa83e586a1c443c8b179701450d803e12aa/packages/core/ui/button/index.android.ts#L72)
207
+
-`initNativeView`: Perform setup after the view has been created.
208
+
-`disposeNativeView`: Free resources as needed.
216
209
217
-
All NativeScript plugins follow this exact same pattern as well, for example:
210
+
Explore more examples within NativeScript core and plugins:
218
211
219
-
-[@nativescript/flutter for iOS](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.ios.ts#L40)
220
-
-[@nativescript/flutter for Android](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.android.ts#L80)
-[Custom Flutter view example (iOS)](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.ios.ts#L40)
215
+
-[Custom Flutter view example (Android)](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.android.ts#L80)
221
216
222
-
You can also learn a lot about this with additional details such as using Cocoapods, Gradle and more in [this blog post series](https://blog.nativescript.org/create-a-custom-view-plugin-marquee-label/).
217
+
For additional details on advanced topics like using Cocoapods or Gradle, refer to [this blog series](https://blog.nativescript.org/create-a-custom-view-plugin-marquee-label/).
223
218
224
-
### Implementing within a Project
219
+
##Project Structure for Custom Views
225
220
226
-
Implementing custom native elements at any moment within a project starts with creating a folder for your new element followed by the implementation. A common folder organization is as follows:
221
+
Create a dedicated folder for your component, such as:
227
222
228
223
```bash
229
224
./checkbox
230
225
common.ts
231
226
index.android.ts
232
-
index.d.ts
233
227
index.ios.ts
228
+
index.d.ts
234
229
```
235
230
236
-
This represents a new encapsulated custom native `<Checkbox />` element sharing common code via `common.ts` with each platform implementation represented via it's suffix. The `index.d.ts` is a convenience allowing one to simply import via the folder to register the element anywhere, for example:
231
+
This structure encapsulates platform-specific implementations and shared logic, simplifying imports and registration:
237
232
238
233
```ts
239
234
import { Checkbox } from'./checkbox'
235
+
```
236
+
237
+
## Real-World Example: Custom Checkbox
238
+
239
+
Let's create a `<Checkbox>` component that behaves consistently on iOS and Android like this:
240
+
241
+
<iframestyle="width: 100%; min-height: 200px; aspect-ratio: 16 / 9;"src="https://www.youtube.com/embed/08uip43irOM"title="Creating custom elements with NativeScript"frameborder="0"allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"allowfullscreen></iframe>
242
+
243
+
See the full working example on StackBlitz: https://stackblitz.com/edit/nativescript-create-custom-elements-checkbox?file=src%2Fapp%2Fcheckbox%2Fcommon.ts
240
244
241
-
// register custom element using examples above for the flavor you're using
245
+
In NativeScript, creating custom UI components can be straightforward and powerful. In this guide, you'll learn how to build a simple, reusable Checkbox component using only built-in modules from `@nativescript/core`. We'll leverage a combination of `GridLayout`, `Label`, and Material Design icons.
246
+
247
+
### Step-by-Step Guide
248
+
249
+
#### 1. Extend `GridLayout`
250
+
251
+
The base structure of our Checkbox will utilize `GridLayout`, which serves as a container for the checkbox icon:
252
+
253
+
```typescript
254
+
import { GridLayout } from'@nativescript/core'
255
+
256
+
exportclassCheckboxCommonextendsGridLayout {}
257
+
```
258
+
259
+
#### 2. Initialize Checkbox with a Font Icon
260
+
261
+
Next, we'll add a Label to serve as our checkbox icon, using Material Design icons for the visual representation.
262
+
263
+
```typescript
264
+
import { GridLayout, Label, Color } from'@nativescript/core'
Creating custom components in NativeScript allows you to:
434
+
435
+
- Build complex components from simple elements
436
+
- Utilize powerful styling and animations
437
+
- Easily manage and expose customizable properties
438
+
439
+
Whether creating fully native views or hybrid custom components, NativeScript provides flexibility and efficiency to meet your application's needs.
440
+
244
441
## Customize Existing Elements?
245
442
246
-
Rather than create a new element from scratch, you may want to customize existing view elements. Learn about how to [extend any element for custom beavhior here](/guide/customizing-view-elements).
443
+
Rather than create new elements from scratch, you may want to just adjust existing view elements. Learn about how to [extend any element for custom behavior here](/guide/customizing-view-elements).
0 commit comments