Tuesday, 5 May 2020

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  >>

No comments:

Post a Comment