Skip to content

Commit 7049256

Browse files
committed
Adding a component-specific CSS section
1 parent 0c8fb75 commit 7049256

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

images/chapter2/android/4.png

26.2 KB
Loading

images/chapter2/ios/4.png

21.2 KB
Loading

index.html

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,19 +428,57 @@ <h4 class="exercise-start">
428428
border-color: black;
429429
}
430430
</code></pre>
431+
<blockquote>
432+
<p><strong>NOTE</strong>: We’ll leave the <code>platform.android.css</code> file empty as we have no Android-specific changes to make yet.</p>
433+
</blockquote>
431434
<div class="exercise-end"></div>
432435

433436
<p>NativeScript supports CSS&#39;s <code>@import</code> statement for importing one CSS file into another. So this new line of code imports the CSS rules from <code>platform.css</code> into <code>app.css</code>. But, you might have noticed that Groceries does not have a file named <code>platform.css</code>—only <code>app/platform.android.css</code> and <code>app/platform.ios.css</code> exist. What&#39;s going on here?</p>
434437
<p><a id="platform-specific-files"></a>When you execute <code>tns run</code>, or <code>tns livesync</code>, the NativeScript CLI takes your code from the <code>app</code> folder and places it in the native projects located in the <code>platforms/ios</code> and <code>platforms/android</code> folders. Here the naming convention comes in: while moving files, the CLI intelligently selects <code>.android.*</code> and <code>.ios.*</code> files. To give a specific example, the CLI moves <code>platform.ios.css</code> into <code>platforms/ios</code> and renames it to <code>platform.css</code>; similarly, the CLI moves <code>platform.android.css</code> into <code>platforms/android</code>, and again renames it to <code>platform.css</code>. This convention provides a convenient way to branch your code to handle iOS and Android separately, and it&#39;s supported for any type of file in NativeScript—not just CSS files. You&#39;ll see a few more examples of this convention later in this guide.</p>
435-
<blockquote>
436-
<p><strong>Note</strong>: The <code>platform.android.css</code> file is currently empty as we have no Android-specific changes to make yet. We’ll make some Android-specific tweaks later in the guide.</p>
437-
</blockquote>
438-
<p>With these changes in place, you&#39;ll notice that the app has a bit more spacing, and also has a distinctly different look on iOS and Android:</p>
438+
<p>With these changes in place, you&#39;ll notice that the app has a bit more spacing, and also that the text fields have borders on iOS but that Android:</p>
439439
<p><img src="images/chapter2/ios/3.png" alt="login 3">
440440
<img src="images/chapter2/android/3.png" alt="login 3"></p>
441-
<p>Despite our changes, the app still doesn’t look great, and that’s because we’re going to apply another batch of styles at the component level.</p>
441+
<p>Despite our changes the app still looks pretty ugly, and that’s because we’re going to apply another batch of styles at the component level. Let’s look at how that works.</p>
442442
<h3 id="component-specific-css">Component-specific CSS</h3>
443-
<p>TODO: Write this!</p>
443+
<p>Much like on the web, sometimes in your NativeScript apps you want to write CSS rules that apply to your entire application, and sometimes you want to write CSS rules that apply to a specific portion of the interface. In the previous section you saw how to use NativeScript’s <code>app.css</code> file to write global rules, and in this section you’ll learn how to use a component’s <code>styleUrls</code> property to apply rules that are scoped to individual components.</p>
444+
<h4 class="exercise-start">
445+
<b>Exercise</b>: Add component-specific CSS
446+
</h4>
447+
448+
<p>Open your app’s <code>app/app.component.ts</code> file and add a <code>styleUrls</code> property such that that full <code>@Component</code> declaration now looks like this:</p>
449+
<pre><code class="lang-JavaScript">@Component({
450+
selector: &quot;my-app&quot;,
451+
template: `
452+
&lt;StackLayout&gt;
453+
&lt;TextField hint=&quot;Email Address&quot; keyboardType=&quot;email&quot;
454+
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/TextField&gt;
455+
&lt;TextField hint=&quot;Password&quot; secure=&quot;true&quot;&gt;&lt;/TextField&gt;
456+
457+
&lt;Button text=&quot;Sign in&quot;&gt;&lt;/Button&gt;
458+
&lt;Button text=&quot;Sign up for Groceries&quot;&gt;&lt;/Button&gt;
459+
&lt;/StackLayout&gt;
460+
`,
461+
styleUrls: [&quot;pages/login/login-common.css&quot;, &quot;pages/login/login.css&quot;]
462+
})
463+
</code></pre>
464+
<div class="exercise-end"></div>
465+
466+
<p>In Angular 2, the <code>stylesUrl</code> points at an array of stylesheets that should be used to style a component. In this case, you’re telling Angular to use two stylesheets, <code>login-common.css</code>, and <code>login.css</code>.</p>
467+
<p>Why two files? Much like you divided your global files into <code>app.css</code>, <code>platform.ios.css</code>, and <code>platform.android.css</code>, this structure gives you a similar ability to separate common login styling in <code>login-common.css</code>, iOS-specific login styling <code>login.ios.css</code>, and Android-specific login styling in <code>login.android.css</code>. Before we see what your app looks like now, there’s one small change you need to make. If you open <code>app/pages/login/login-common.css</code> you’ll see the final selector used is <code>#submit-button</code>. Much like using CSS on the web, in NativeScript you can both <code>id</code> and <code>class</code> attributes to target specific user interface components.</p>
468+
<p>Let’s see how it works by adding an <code>id</code> to your app’s “Sign In” button.</p>
469+
<h4 class="exercise-start">
470+
<b>Exercise</b>: Add an <code>id</code> attribute
471+
</h4>
472+
473+
<p>Open your app’s <code>app/app.component.ts</code> file, find <code>&lt;Button text=&quot;Sign in&quot;&gt;&lt;/Button&gt;</code> in your component’s <code>template</code>, and replace it with the code below:</p>
474+
<pre><code class="lang-XML">&lt;Button text=&quot;Sign in&quot; id=&quot;submit-button&quot;&gt;&lt;/Button&gt;
475+
</code></pre>
476+
<div class="exercise-end"></div>
477+
478+
<p>As you can see, in NativeScript you have a lot of options for how you can apply CSS rules. You can apply the rules globally for both platforms, for iOS specifically, or for Android specifically. And you can also apply rules at the component level. With this last <code>id</code> change in place your app is starting to look a little nicer:</p>
479+
<p><img src="images/chapter2/ios/4.png" alt="login 4">
480+
<img src="images/chapter2/android/4.png" alt="login 4"></p>
481+
<p>To continue polishing the visuals of this login screen, let’s look at how we can add an image of this app’s logo.</p>
444482
<h3 id="images">Images</h3>
445483
<p>In NativeScript you use the <code>&lt;Image&gt;</code> UI element and its <code>src</code> attribute to add images to your pages. The <code>src</code> attribute lets you specify your image in three ways. The first (and simplest) way is to point at the URL of an image:</p>
446484
<pre><code class="lang-XML">&lt;Image src=&quot;https://www.nativescript.org/images/default-source/landingpages/logo.png&quot;&gt;&lt;/Image&gt;
@@ -547,7 +585,7 @@ <h4 class="exercise-start">
547585
<h3 id="structuring-your-app">Structuring your app</h3>
548586
<p>There are many reasons to segment your application into modular units, and you can <a href="https://en.wikipedia.org/wiki/Modular_programming">read about the various benefits on Wikipedia</a>. However, keeping NativeScript apps modular has one unique benefit: the ability to share the code you write between Angular-2-built web apps, and Angular-2-build native apps.</p>
549587
<p>Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you <em>do</em> have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse for your native and web apps can be an invaluable time saver.</p>
550-
<p>To see how this works in action let’s create a few files.</p>
588+
<p>To see how this works in action let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
551589
<h4 class="exercise-start">
552590
<b>Exercise</b>: Add a model object
553591
</h4>

src/chapters/chapter2.md

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,24 +270,74 @@ TextField {
270270
}
271271
```
272272

273+
> **NOTE**: We’ll leave the `platform.android.css` file empty as we have no Android-specific changes to make yet.
274+
273275
<div class="exercise-end"></div>
274276

275277
NativeScript supports CSS's `@import` statement for importing one CSS file into another. So this new line of code imports the CSS rules from `platform.css` into `app.css`. But, you might have noticed that Groceries does not have a file named `platform.css`—only `app/platform.android.css` and `app/platform.ios.css` exist. What's going on here?
276278

277279
<a id="platform-specific-files"></a>When you execute `tns run`, or `tns livesync`, the NativeScript CLI takes your code from the `app` folder and places it in the native projects located in the `platforms/ios` and `platforms/android` folders. Here the naming convention comes in: while moving files, the CLI intelligently selects `.android.*` and `.ios.*` files. To give a specific example, the CLI moves `platform.ios.css` into `platforms/ios` and renames it to `platform.css`; similarly, the CLI moves `platform.android.css` into `platforms/android`, and again renames it to `platform.css`. This convention provides a convenient way to branch your code to handle iOS and Android separately, and it's supported for any type of file in NativeScript—not just CSS files. You'll see a few more examples of this convention later in this guide.
278280

279-
> **Note**: The `platform.android.css` file is currently empty as we have no Android-specific changes to make yet. We’ll make some Android-specific tweaks later in the guide.
280-
281-
With these changes in place, you'll notice that the app has a bit more spacing, and also has a distinctly different look on iOS and Android:
281+
With these changes in place, you'll notice that the app has a bit more spacing, and also that the text fields have borders on iOS but that Android:
282282

283283
![login 3](images/chapter2/ios/3.png)
284284
![login 3](images/chapter2/android/3.png)
285285

286-
Despite our changes, the app still doesn’t look great, and that’s because we’re going to apply another batch of styles at the component level.
286+
Despite our changes the app still looks pretty ugly, and that’s because we’re going to apply another batch of styles at the component level. Let’s look at how that works.
287287

288288
### Component-specific CSS
289289

290-
TODO: Write this!
290+
Much like on the web, sometimes in your NativeScript apps you want to write CSS rules that apply to your entire application, and sometimes you want to write CSS rules that apply to a specific portion of the interface. In the previous section you saw how to use NativeScript’s `app.css` file to write global rules, and in this section you’ll learn how to use a component’s `styleUrls` property to apply rules that are scoped to individual components.
291+
292+
<h4 class="exercise-start">
293+
<b>Exercise</b>: Add component-specific CSS
294+
</h4>
295+
296+
Open your app’s `app/app.component.ts` file and add a `styleUrls` property such that that full `@Component` declaration now looks like this:
297+
298+
``` JavaScript
299+
@Component({
300+
selector: "my-app",
301+
template: `
302+
<StackLayout>
303+
<TextField hint="Email Address" keyboardType="email"
304+
autocorrect="false" autocapitalizationType="none"></TextField>
305+
<TextField hint="Password" secure="true"></TextField>
306+
307+
<Button text="Sign in"></Button>
308+
<Button text="Sign up for Groceries"></Button>
309+
</StackLayout>
310+
`,
311+
styleUrls: ["pages/login/login-common.css", "pages/login/login.css"]
312+
})
313+
```
314+
315+
<div class="exercise-end"></div>
316+
317+
In Angular 2, the `stylesUrl` points at an array of stylesheets that should be used to style a component. In this case, you’re telling Angular to use two stylesheets, `login-common.css`, and `login.css`.
318+
319+
Why two files? Much like you divided your global files into `app.css`, `platform.ios.css`, and `platform.android.css`, this structure gives you a similar ability to separate common login styling in `login-common.css`, iOS-specific login styling `login.ios.css`, and Android-specific login styling in `login.android.css`. Before we see what your app looks like now, there’s one small change you need to make. If you open `app/pages/login/login-common.css` you’ll see the final selector used is `#submit-button`. Much like using CSS on the web, in NativeScript you can both `id` and `class` attributes to target specific user interface components.
320+
321+
Let’s see how it works by adding an `id` to your app’s “Sign In” button.
322+
323+
<h4 class="exercise-start">
324+
<b>Exercise</b>: Add an `id` attribute
325+
</h4>
326+
327+
Open your app’s `app/app.component.ts` file, find `<Button text="Sign in"></Button>` in your component’s `template`, and replace it with the code below:
328+
329+
``` XML
330+
<Button text="Sign in" id="submit-button"></Button>
331+
```
332+
333+
<div class="exercise-end"></div>
334+
335+
As you can see, in NativeScript you have a lot of options for how you can apply CSS rules. You can apply the rules globally for both platforms, for iOS specifically, or for Android specifically. And you can also apply rules at the component level. With this last `id` change in place your app is starting to look a little nicer:
336+
337+
![login 4](images/chapter2/ios/4.png)
338+
![login 4](images/chapter2/android/4.png)
339+
340+
To continue polishing the visuals of this login screen, let’s look at how we can add an image of this app’s logo.
291341

292342
### Images
293343

0 commit comments

Comments
 (0)