#17 Bottom Navigation
Saturday, February 9, 2019
One of the most defining features of a mobile device is its relativaly small viewport. A popular way of dealing with this when it comes navigation is to place our anchor tags within a collapsable menu that is animated in and out with the press of a button. I have used and continue to use the hamburger icon for this very purpose. If you do any amount of searching you can find plenty of negativity around its use. One possible alternative, which we will implement here, is a bottom navigation menu that is displayed only on smaller form factor devices.
Parts
- Part 29: Offset Pager Urls
- Part 28: Offset Pager Start
- Part 27: Mock Context Builder
- Part 26: Mock Repository
- Part 25: Mock Async
- Part 24: Picture Tag Helper
- Part 23: Img DPR Tag Helper
- Part 22: Img Responsive Tag Helper
- Part 21: Img Optimized Display
- Part 20: Img Optimization
- Part 19: Img Lazy Loading
- Part 18: Img Responsive
- Part 17: Bottom Nav
- Part 16: Main Nav Cookie
- Part 15: Main Nav Mobile
- Part 14: Main Nav Search
- Part 13: Main Nav Auth
- Part 12: Main Nav Anchors
- Part 11: Main Nav Logo
- Part 10: Search Results
- Part 9: Search Manager
- Part 8: Search Start
- Part 7: Seeding the Database
- Part 6: Domain Database
- Part 5: Emailing Exceptions
- Part 4: Mailkit
- Part 3: View Renderer
- Part 2: Upgrade to 2.1
- Part 1: Quick Start
Starting with the markup
- WebUi
- Pages
- Shared
- _BottomNav.cshtml
- Shared
- Pages
We will start by specifying the starting markup for our bottom navigation (a).
_BottomNav.cshtml
<nav class="bottom-navigation">
<ul>
<li>
<a href="/">
<i class="fas fa-home fa-2x"></i>
<p>Home</p>
</a>
</li>
<li>
<a href="/applications/page-1">
<i class="fas fa-cloud fa-2x"></i>
<p>Apps</p>
</a>
</li>
<li>
<a href="/articles/page-1">
<i class="fas fa-newspaper fa-2x"></i>
<p>Articles</p>
</a>
</li>
<li>
<a href="/articles/series/page-1">
<i class="fas fa-book-open fa-2x"></i>
<p>Series</p>
</a>
</li>
</ul>
</nav>
Adding the bottom navigation to our pages
- WebUi
- Pages
- Shared
- _Layout.cshtml
- Shared
- Pages
Just like our main navigation menu we will want to include the bottom navigation bar on each pages which we can easily accomplish by adding it to our layout page (b).
_Layout.cshtml
...
<html>
...
<body>
...
<partial name="_BottomNav"/>
...
</body>
</html>
We need some icons
- WebUi
- Source
- main.page.ts
- Source
As we can see in the markup our bottom navigation is going to make use of a few different icons. As we have done before we are going to getting this icons from a font awesome package that we have previously installed (c).
main.page.ts
...
import { faBookOpen } from "@fortawesome/free-solid-svg-icons/faBookOpen";
import { faCloud } from "@fortawesome/free-solid-svg-icons/faCloud";
import { faHome } from "@fortawesome/free-solid-svg-icons/faHome";
import { faNewspaper } from "@fortawesome/free-solid-svg-icons/faNewspaper";
library.add(
...
faBookOpen,
faCloud,
...
faHome,
faNewspaper,
...
);
...
Time to make it look good
- WebUi
- Source
- components
- bottom-nav
- bottom-nav.component.scss
- bottom-nav
- components
- Source
Now that we have the markup and all of the icons that we are using in place it is time for us to do some styling (d).
bottom-nav.component.scss
@import "../../sass/_non-rendering.scss";
$dark-gray: #333 !default;
$action-color: #0984e3 !default;
$navigation-anchor-color: lighten($dark-gray, 20%) !default;
$navigation-background-color: white !default;
$navigation-border-color: $navigation-anchor-color !default;
$mobile-break-width: 1024px !default;
.bottom-navigation {
@include position(fixed, null 0 0 0);
background-color: $navigation-background-color;
border-color: $navigation-border-color;
border-style: solid;
@include border-width(1px 0 0 0);
z-index: 2000;
@include media-gt($mobile-break-width) {
display: none;
}
ul {
display: flex;
flex-direction: row;
max-width: 500px;
@include margin(null auto);
}
li {
flex: 1;
}
p {
margin: 0;
}
a {
display: block;
text-align: center;
text-decoration: none;
@include padding(0.625em 0);
color: $navigation-anchor-color;
}
}
Adding the css to our pages
- WebUi
- Source
- main.page.scss
- Source
To add our styles to each of our pages we just need to import our sass file into our main sass file (e). At the same time we also need to add some bottom padding.
main.page.scss
...
@import "./components/bottom-nav/bottom-nav.component.scss";
body {
.body-content {
@include padding(em(56.33px) null null null);
@include media-lt-or-eq(1024px) {
@include padding(null null em(79px) null);
}
}
}
Deciding which anchor is active
- WebUi
- Pages
- Shared
- _BottomNav.cshtml
- Shared
- Pages
The last thing we would like to do is to have the ability to visual show which, if any, of our bottom navigation routes is active. To do this we will apply a css class based on the current route (f).
_BottomNav.cshtml
@{
const string activeClass = "active";
var homeCssClass = "";
var applicationsCssClass = "";
var articlesCssClass = "";
var seriesCssClass = "";
if (Context.Request.Path.HasValue) {
var path = Context.Request.Path.Value;
if (path == "/")
{
homeCssClass = activeClass;
}
else if (path.Contains("applications/page"))
{
applicationsCssClass = activeClass;
}
else if (path.Contains("articles/page"))
{
articlesCssClass = activeClass;
}
else if (path.Contains("series/page"))
{
seriesCssClass = activeClass;
}
}
}
<nav class="bottom-navigation">
<ul>
<li>
<a class="@homeCssClass" ...>
...
</a>
</li>
<li>
<a class="@applicationsCssClass" ...>
...
</a>
</li>
<li>
<a class="@articlesCssClass" ...>
...
</a>
</li>
<li>
<a class="@seriesCssClass" ...>
...
</a>
</li>
</ul>
</nav>
Styling the active anchor
- WebUi
- Source
- components
- bottom-nav
- bottom-nav.component.scss
- bottom-nav
- components
- Source
Last but not least we just need to add a little styling to our active anchor (g).
bottom-nav.component.scss
...
.bottom-navigation {
...
a {
...
&.active {
background-color: darken($navigation-anchor-color, 10%);
color: $navigation-background-color;
}
}
}