Tuesday, October 8, 2019

Building an Admin Dashboard Layout With CSS and a Touch of JavaScript

Building an Admin Dashboard Layout With CSS and a Touch of JavaScript
Final product image
What You'll Be Creating

In this new tutorial, we’ll create a responsive admin dashboard layout with CSS and a touch of JavaScript. To build it, we’ll borrow some ideas from the WordPress dashboard, such as its collapsible sidebar menu.

Throughout the tutorial we’ll face plenty of challenges, but ones which will give us good practice for enhancing our front-end skills.

Without further ado, let’s have a look at the final admin dashboard demo (hit the Collapse button at the foot of the sidebar to see the collapsible nav in action, and check out the full screen version to play with its responsiveness):

1. Begin With the Page Markup

To kick off the markup we’ll need we’ll start with an SVG, a header, and a section:

SVG Sprites

As you might imagine with any admin dashboard we’re going to need a bunch of icons. Thankfully, Envato Elements provides an ever-expanding collection of useful vector icons, so let’s take advantage of that library and download these Trade and Dashboard Icons.

Trade and Dashboard Icons
Trade and Dashboard Icons on Envato Elements

Rather than including them directly in the page via an img or svg tag, let’s go one step further and create an SVG sprite. To do this, we’ll wrap all the icons in an SVG container. The container should be hidden, so we’ll apply display: none to it. If we don’t hide it, a big empty area will appear at the top of the page.

Each icon will be placed inside a symbol element with a unique ID and a viewBox attribute which will depend on the icon size. We can then render the target icon whenever we need it by calling the use element (I’ll show you how in a minute). 

For now, let’s just become familiar with the markup needed for the SVG sprite:

And really, that’s all we need to create our inline SVG sprite.

Header

Moving on with our admin dashboard layout, let’s look at the page header. 

Within it, we’ll define a nav element which will serve as the wrapper for the following elements:

  • The logo
  • The Collapse button that will toggle the menu on mobile screens
  • The menu itself which will contain the menu links, two headings, and the collapse/expand button. It might be more semantically correct to have two individual menus and place the headings outside them, but you can approach things differently if you prefer.

Here’s how it’ll look like on wide screens (>767px):

The header layout

The header structure:

Notice two things in the code above:

  • How we use the use element to reference the target icons.
  • The ARIA attributes (aria-expanded, aria-label, aria-hidden) that we add to the toggle buttons. These attributes will help us make the component a bit more accessible. Later, we’ll discuss how their values will be updated based on the button’s state.

Section

The section will contain two nested sections.

Section #1

Inside the first section, we’ll place the search form and some info (name, avatar, and notifications) about the current logged in user.

Here’s its appearance on wide screens (>767px):

The layout of the first section

The section structure:

Again, notice that we add some ARIA attributes to the submit button.

Section #2

Inside the second section, just for enriching the demo with some dummy content, we’ll place a bunch of article placeholders. These might typically contain tabular data, charts, or feeds of some kind.

Here’s its appearance on wide screens (>767px):

The layout of the second section

The section structure:

2. Define Some Basic Styles

With the markup for our admin dashboard ready, we’ll forge on with the CSS. The first step, as always, is to specify some CSS variables and common reset styles:

Note: for simplicity I won’t walk through all the CSS rules in the tutorial. There are almost 400 lines of CSS here. If you want to, you can check them all by clicking the CSS tab of the demo project.

3. Define the Main Dashboard Styles

At this point, we’re ready to concentrate on the page styles.

Style the Header

The header will be a fixed position element. Its width will be 220px and its height equal to the viewport height. In case its contents exceed the viewport height, a vertical scrollbar will appear.

The nav element will behave as a flex container with a minimum height of 100%. Remember that its direct children are three: 

  1. the logo, 
  2. the mobile menu toggle button, 
  3. and the menu. 

The toggle button will be visible only on small screens (<768px).  Here are the styles we need:

Tip: In case you prefer an absolutely positioned header that covers the full page height, add the following styles:

Menu Styles

The menu will serve as a flex container, and we’ll give it flex: 1, so it’ll expand and cover the full parent height.

The last menu item will be given a margin-top: auto because it should be positioned at the very bottom of the menu. This behavior will be clearer when the header scrollbar doesn’t appear. To test it, try to remove some menu items, or check the demo in a tall screen.

The links and the button inside the menu will also act as flex containers and their contents (text and icons) should be vertically aligned.

The menu headings will be a bit smaller compared to the other menu elements. In addition, we’ll increase the spacing between their characters.

Here’s a part of the menu styles:

Page Content Styles

Remember that the .page-content section contains two sub-sections.

This section will be placed 220px away from the left side of the viewport. Plus, we’ll give it width: calc(100% - 220px). Note that its left property value is equal to the header width.

Its styles:

Search and User Styles

Also, remember that the .search-and-user section contains two elements: the search form and the .admin-profile.

To lay it out, we’ll use CSS Grid. The search form will cover the full available space and there will be a 50px gap between it and its sibling. Both siblings will be vertically aligned.

The submit button inside the form will be absolutely positioned. It will only contain a decorative icon, and we’ll therefore need an ARIA attribute to allow screenreaders to interpret it and thereby make it accessible.

The .admin-profile, which contains two elements, will behave as a flex container with vertically centered content. The badge (counter) element will be absolutely positioned inside its parent with horizontally and vertically centered content.

Here’s a part of the required styles for this section:

Grid Styles

To lay out the articles, we’ll take advantage of CSS grid. We’ll give all articles a fixed height of 300px. Apart from the first and last articles which will cover the full parent width, all the others will be part of a two-column layout.

The associated styles:

4. Toggle Header

Each time we click on the collapse/expand button, the header state will change. If it’s expanded, it will collapse (leaving just icon variants of the menu items), and vice versa. 

The expandcollapsed state of the header

Keep in mind that this functionality will be available only on screens greater than 767px. For smaller screens, our header will have a different layout, which we’ll get to shortly.

During the collapsed state of the header, the body receives the collapsed class. At that point, the following things happen:

  • The header shrinks. Its width changes from 220px to 40px.
  • In response to this, the .page-content section grows. Specifically, its width changes from width: calc(100% - 220px) to width: calc(100% - 40px). In addition, its left property value becomes 40px instead of 220px.
  • The logo, the menu headings, the menu links text, and the menu button text disappear.
  • The aria-expanded and aria-label attribute values of the toggle button are updated. Plus, its icon is rotated 180 degrees, so it looks like an expand icon.

Here’s the JavaScript code that implements this functionality:

And all the associated styles:

5. Show Tooltip on Admin Menu Items

Now let’s go one step further and add another new feature to the collapsible header.

As we discussed in the previous section, when the header becomes collapsed, the text of the menu links will disappear. This means, at that point, only the SVG icons will be visible. So, let’s display a tooltip that will give users a better understanding of what each link does.

To do so, each time a menu link (icon) is being hovered over, we’ll add the title attribute to it, with the value being its plain text. But again, that should happen only when the header is collapsed and the window width is at least 768px.

Tooltip

Here’s the corresponding JavaScript: 

6. Going Responsive

On screens up to 767px wide, our page will look like this:

The mobile layout

That’s a big difference from our sidebar arrangement, right? Let’s highlight the most important differences compared to the desktop version:

  • Both the header and .page-content have position: static and width: 100%.
  • The flex direction of the nav element changes from column to row.
  • The mobile menu toggle button becomes visible.
  • The menu is absolutely positioned right underneath the header and initially hidden. It will become visible each time we click on the toggle button. 
  • The collapse/expand button and the .greeting element are hidden.
  • The .search-and-user section is absolutely positioned and placed right next to the mobile menu toggle button. 

Below you can see a part of the responsive styles:


7. Toggle Mobile Menu

Each time we click on the toggle button, the menu state will change. If it’s expanded, it will collapse, and vice versa.

The opened state of the mobile menu

During the expanded state of the menu, the body receives the mob-menu-opened class. At that point, the following things happen:

  • The menu appears.
  • The aria-expanded and aria-label attribute values of the toggle button are updated. Plus, its icon is rotated 180 degrees, so it looks like an expand icon.

Here’s the required JavaScript code:

And the associated CSS:


Conclusion

That’s it folks! We successfully built a fully functional admin dashboard layout. You’ll be able to expand on this foundation to create all kinds of admin interfaces. Hopefully you enjoyed this journey as much as I did!

Just a last note. I’m certainly not an accessibility expert, yet I tried to make this component more accessible by adding some common ARIA attributes. During this process, I checked the WordPress and Codepen dashboards for reference. There might be additional ARIA attributes that could have been included in the code. For example, I excluded the aria-controls attribute which is responsible for identifying the related content, but that was because Aria-Controls is Poop.

If I missed anything, or you think that some things should have been done differently, let me know in the comments below.

As always, thanks for reading!

More Admin Dashboard UI Resources


No comments:

Post a Comment