Tuesday, 5 May 2020

Build Your Own Blog - Page Layout and Flexbox

<< Creating the BlogArchive Component                                  Table of Contents

In this part of the 'Build Your Own Blog' series, we want our web page to be suitable for various screen sizes, such as mobile and laptop.

(If you're new here, I recommend visiting the Table of Contents or here for the previous post)

Flexbox is a Responsive Web Design CSS technology that allows us to dynamically alter our web page to suit the dimnesions of the screen it is being displayed in.

By the end of this post, our blog site will be capable of supporting the following screen types:

Laptop



Mobile



Notice that the Blog Archive component is being pushed to beneath the Blog Post.


1. Open app.component.sass and add the following:
























The container class is our main container class that belongs to the parent div. It adds a margin to the whole page which is proportionate to the width of the screen. This will centre our web page by bringing its components together rather than being spread out. It's easier on the eyes.

You'll notice that the blog-body class uses display: flex. This will enable Flexbox on this div and all its child items.

justify-content: space-between will spread the components across the full width of it's container whilst adding an even amount of space between them.

flex-direction: row will force our components to appear in a row instead of being in a column.

flex-wrap: wrap is the magick for pushing the BlogArchive component beneath the BlogPost component if it can no longer fit within the width dimensions of the container.


flex will dynamically size the component proportionate to other components. blog-post has flex:3 and blog-archive has flex:1 - this means BlogArchive will be 3x bigger than the BlogPost component.

flex-grow will determine how quickly the component will grow/shrink into the remaining space of the screen as the screen size changes.

2.  Open app.component.html and add the following:



The final piece in the puzzle! We use all three components that we've created in this series and we place them within their own divs and corresponding classes to apply the final layout of the page.

3. Run ng serve
Congratulation! If you've made it this far, you should now have a basic blog web site that is responsive.

In the following posts, we will dive into creating our API layer using .Net Core Web API and AWS DynamoDB for accessing our blog data.


<< Creating the BlogArchive Component                                  Table of Contents

Build Your Own Blog - Create the BlogArchive Component and Angular Material Tree

<< Create the BlogPost Component                           Table of Contents                                Page Layout and Flexbox  >>

In this part of the 'Build Your Own Blog' series, we will create a component for the blog archive. This is responsible for navigating historical blogs. It will look like below:


(If you're new here, I recommend visiting the Table of Contents)

The Archive will have the following features:
  • Expandable tree view of historical blogs.
  • Tree will contain three level of nodes - year, month and blog post.
  • If a node contains a sub-nodes it will include the sub-node count next to the nodes name.
To achieve this, we will be using Angular Materials' Tree component to create the expandable tree layout.

I found my first time using the Tree component difficult. I heavily relied upon the following links to get it working. I recommend you also looking at them, if you have trouble with my description.

A useful video and description by Jameson Saunders on the Angular Material Tree:
https://www.youtube.com/watch?v=BALaj39rbZE&t=301s

A similar description and use of the Angular Material Tree by Vag from media.com:
https://medium.com/@vag/angular-material-tree-26adccadc63b


Let's go!

1. Add a new component called 'blogarchive' by running:

 ng generate component blogarchive

2. Install Angular Material by opening Visual Studio Code's Terminal and running:

ng add @angular/material

This will install Angular Material, the Component Dev Kit and Angular Animations.

3. Open blogarchive.component.ts and add the following:



Notice that we are hardcoding the tree data. We will come back to this in later posts and hook this up to a backend API for querying real blog post data.

FileNode represents a single node in our tree. The name is the label that will appear in the archive tree and children are it's sub-nodes.

NestedTreeControl represents the tree control itself. We pass it a parameter which contains an observable of node.children, this is so that as we navigate the tree, we can dynamically fetch a nodes children.

MatTreeNestedDataSource represents the JSON representation of our tree data. It contains the parent/child hierarchy.

Using rxjs, we create an instance of BehaviorSubject and setup a subscription that updates our data source (nestedDataSource.data) as our data changes (dataChange.next).

hasNestedChild will be used in our html for knowing if a child has any nested children.

getChildrenCount will be used for identifying the number of children a node has.


4. Open blogarchive.component.sass and add the following:

Angular Materials' Tree component has simple styling which isn't suitable for our blog.

The default font-colour is white so we override mat-tree-node and set the colour to red.
The default height of a node is also too large, so we reduce it by setting the height to 20px.
The default background colour of the component is black, so we set it to white by setting archive-tree.background-color 

We also override mat-icon-button.width to reduce the size of the arrow icon.

We also setup classes for the border of our component. We use box-shadow to give our component a soft shadow and border-radius to curve the corners.

5. Open blogarchive.component.hml and apply the following:


Notice the mat-tree element that contains two sub-elements (mat-tree-node and mat-nested-tree-node). The mat-tree dataSource is set to the nestedDataSource instance we setup in the constructor earlier; this contains our tree data. We also set the treeControl to the nestedTreeControl instance.

mat-tree-node represents a node that has no children. The mat-nested-tree-node represents a node that does have children. 

mat-tree-node and mat-nested-tree-node elements are used interchangeably depending on the state of hasNestedChild. 
mat-nested-tree-node
will conditionally set the arrow icon to right or down depending on whether nestedTreeControl.isExpanded or not.


Congratulations! We now have an Archive component for navigating historical blog posts.

<< Create the BlogPost Component                           Table of Contents                                Page Layout and Flexbox  >>

Build Your Own Blog - Create the BlogPost Component

<< Create Your First Component                        Table of Contents                   Create the BlogArchive Component >>  

We will now create a component containing the content of a blog. It will look like below:



(If you're new here, I recommend visiting the Table of Contents)

Each blog post will contain:
  • Date
  • Title
  • Content as HTML capable of supporting images, code snippets and text
  • Posted by and at what time.
  • A border - green-ish with a drop shadow.
Let's get started!

1. Add a new component called 'blogpost' by running:

 ng generate component blogpost

2. Open blogpost.component.sass and add the following classes:








































The blog-post class defines the border of the component. We set the height:auto so that the components height will be defined based on it's content. However, we don't allow it to be smaller than 150px.

word-wrap: break-word
forces the text in the component to move to a new line once it reaches the edge of the components width.

3. Create a new folder called 'types' in src > app > types






















4. Create a TypeScript file called BlogPost and add the following:













This class is responsible for transferring all the data from to the template (DOM). Think of this class as a Data Transfer Object (DTO) or a binding between the TypeScript Controller (blogpost.component.ts) and the View/Template.

5. Open blogpost.component.ts and add the following:



Notice that we create an instance of the BlogPost class, injecting in test data we wish to be displayed in the component. Later, we will modify this class to connect it to our backend API for fetching real blog data.

6. Open blogpost.component.html and add the following html, using the classes we've just defined:



The BlogPost component is now complete.

In my next post, we will create the BlogArchive component.


<< Create Your First Component                        Table of Contents                   Create the BlogArchive Component >>   













Build Your Own Blog - Create Your First Component and Component Driven Design

<< Your First Angular Project                                  Table of Contents                            Create the BlogPost Component >>

In this post we are going to build our first component using Angular. If you haven't already, please read the previous post to set up the prerequisites required.

(If you're new here, I recommend visiting the Table of Contents)

I will show you how to use the Angular CLI (ng command) to add a new component, download and apply a font and apply simple styling to our component and home page.

We will call our header component 'blogheader'. For now, it will contain only the title of the page "kite .net", more will be added later.



When choosing colours for your web page, it is advisable to pick colours form the same scheme. The reason is that we want the colours to compliment each other so that they appear aesthetically pleasing to our user. Read more about this here: colour schemes

Component Driven Design

The idea is that we arrange our website into components. Think of a component as a group of html, css and javascript designed for a specific purpose. In our case, we have Header, Post and Archive components.

These components will be modular that are re-usable and can be initialised multiple times on a website. This will reduce code duplication and complexity. Everything we need for a component is contained only within that component alone.

A component consists of the following parts:
  • Selector - The name of the element to instantiate the component)
  • Template (Html) - The layout of a component. i.e. How the data will be displayed.
  • Style (CSS/SASS) - The styling required for this component
  • TypeScript - The logic behind the initialisation of the component. i.e. Fetching the data required for the template.
Click here to read more about: Component Driven Development

If you're familiar with the Model-View-Controller (MVC) design pattern, it is very similar.

BlogHeader Component

1. Add a new component by running ng generate component blogheader


You will now see the following added to the project.



2. Download this font (zip) from Google Fonts: https://fonts.google.com/specimen/Allerta+Stencil



3. Create a new folder assets > fonts

4. Unzip the font to the fonts folder:

 

3. Open app > styles.sass and add the following:



This will allow us to refer to our font in our css as 'stencil'

4. Open blogheader.component.sass and add the following:









This will define our header class using our 'stencil' font plus other font attributes.

5. Open blogheader.component.html and add the following:




Applying the header class will apply the font, size and colour.


6. Open app.component.sass and add the following class:







The container class will be used for creating a central page layout across all components.

10. Finally, run your application locally using ng serve and see the results.


Well done! We've created our first component using Angular!

<< Your First Angular Project                                  Table of Contents                            Create the BlogPost Component >>

Build Your Own Blog - Your First Angular Project

<< Introduction                                                  Table of Contents                             Create Your First Component >>  

This post will explain everything you need to set up your first Angular project, the first step to creating our own blog website.

By the end of this post you will have a development environment ready for Angular development.

(If you're new here, I recommend visiting the Table of Contents)

Prerequisites:


Visual Studio Code Extensions:

I recommend downloading the following extensions in Visual Studio Code:
  • Sass 1.8.3 - For SASS syntax highlighting and AutoComplete

Install Angular

1. Open Visual Studio Code, and open the Terminal. View -> Terminal
2. Run npm install -g @angular/cli 
3. Run npm install --save-dev @angular-devkit/build-angular

Running these commands will install the Angular CLI and the devkits required to build Angular applications.

This gives us the ability to use ng commands that allow us to initialise an Angular project, add/remove components, add Angular Material to our project, etc


Initialising an Angular Project

1. Run the ng new command and follow the instructions to initialise a new angular project. The name of our blog web project will be 'redkiteblog'. 



After the project is initialised you will see a project structure as follows:



That's it! We now have the bare bones of an Angular project. 

2. Run ng serve



Once it has successfully built, I can access the project at http://localhost:4200/



Obviously, we don't want our blog to look anything like this, so next we'll be deleting all of this and creating our own blog.

<< Introduction                                                  Table of Contents                             Create Your First Component >>   

Build Your Own Blog - Introduction

                                                                                                      Your First Angular Project >> 


This blog series is for sharing my journey in creating a blog site from scratch. I have had a few attempts at doing this, but left semi-complete due to other project/life commitments.

However! I am more determined than ever to build a blog using new and exciting technologies, so that I can finally migrate my blog onto the cloud!

The narrative of the blog will be aimed at a newbie audience who want to learn how to build sites like this from scratch. I hope this will help you.

I'd love to hear your feedback and questions. And if you want me to explain something further, then please contact me using the comments below.

Table Of Contents


Viewing a Blog
The following sections will guide you through creating a blog site that allows you to view blog posts using Angular.

Introduction
Your First Angular Project
Create Your First Component and Component Driven Design
Create the BlogPost Component
Create the BlogArchive Component and Angular Material Tree
Page Layout and Flexbox

COMING SOON...

Blog API
.Net Core API
AWS Lambda
DynamoDB

Writing/Editing a Blog
wysiwyg
Authentication using AWS Cognito

Hosting
AWS Amplify

Project Goal

As with any good Agile project, I don't want to over-design the intricacies of a blog upfront. At this time, I merely want it to resemble this current blog on blogger.com. My inspiration and creativity will change as time progresses and thus will do the requirements.

I want the home page to look like this:



Therefore, the initial requirements will be to have the following features:

  • Home page
    • Header (containing "kite .net" and 'Login' link)
    • Blog content - this will contain the content of a blog post.
    • Blog Archive - This will show a tree view of all blogs I've posted broken down by year and month.
  • Login screen
    • The ability to login as an Administrator user, for writing/editing blogs.
  • Writing/Editing blogs
    • Authenticated user can edit any post.
    • Authenticated user will see 'Write post' link in header.
Possible future features:
  • Comments section
    • Allow the ability for readers to post comments.

 I will use the following technologies:
  • Angular 9+
  • SASS
  • Flexbox
  • .Net Core Web API
  • DynamoDB
  • AWS Lambda
  • AWS Amplify
  • AWS Cognito
  • Visual Studio Code
  • Visual Studio Community 2019

             Your First Angular Project >>