Skip to main content

Beautiful Web Design With Semantic UI 2.2 and NodeJS Server-side Rendering


It’s worth pointing out that Semantic UI provides richer UI elements and robust features than Bootstrap 3/4. Semantic UI does contain more elements without the needs of 3rd party libraries. For examples:
  • dropdown has built-in animation & search function.
  • Step, Rating, Statistic are not existed in Bootstrap 3/4.
  • Semantic UI comes with more than 20 themes.
  • The CSS class names are more friendly to human words.
(Semantic UI is available for download if you haven’t tried it yet, and want to.)

This is a steps by steps guild show you how to create a basic Semantic UI theme, customize it, and setup the NodeJS server-side rendering development environment.

Prerequisite

This project leverages below technologies:

Install NodeJS

You can download & install the NodeJS from the official site or using NVM to install NodeJS. Moreover, NVM offers to switch different NodeJS version anytime. I’m using the version of NodeJS & NPM is:
$ node -v && npm -v
v8.9.0
5.5.1

Project Setup

Create the server App

Create a new directory for our project and initialize it using NPM. Type below commands in Terminal:
$ mkdir [your_project_name] && cs [your_project_name]
$ npm init -y
$ mkdir public
$ mkdir public/js
$ mkdir public/css
$ mkdir public/images
$ mkdir views

Specify server startup file

Modify the below properties inside the package.json file:
...
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
...

Install the ExpressJS

ExpressJS is a lightweight framework for NodeJS. To install it, type below command in the project root directory:
$ npm install --save express

Install the Task runner

Gulp is a task runner for build processes. It is similar to a batch file but provides much more powerful features. In the project root directory, run below commands to install Gulp & its helpers:
$ npm install -g gulp-cli
$ npm install --save-dev gulp gulp-chug gulp-csso gulp-less gulp-pug run-sequence

Install Semantic UI

To install Semantic UI into our project, type below command in the project root:
$ npm install semantic-ui --save
After answer few questions from Semantic UI with default values, then build the Semantic UI:
$ cd semantic
$ gulp build
$ cd ..
After the semantic-ui build, the built files are stored in ./dist/. That means the initial setup is done. Let’s get started for site customization.

Create the home page

I’ve translated a PUG (HTML template engine) file views/index.pug. This is the home page of this project. The PUG file contains almost everything of Semantic Ui elements in PUG syntax. For example:

As you can see, PUG syntax is not only the shorthand of HTML. But also type fewer and cleaner. Furthermore, it is more human words like.

You can see the full file of views/index.pug here.

Although we can view the PUG file using PUG view engine provided by ExpressJS, I chose pre-rendered approach in this project. It is mentioned in below Gulp build processes.

Create the server program

In the project root directory, create a simple NodeJS server program app.js:
const express = require('express')
const app = express()
app.use(express.static('public'))
app.listen(3000, () => console.log('The app listening on port 3000!'))

Create Gulp task file

In the project root directory, create a file gulpfile.js. The content is:

Now build our project. In project root, type:
$ gulp
After about 30 sec on my computer, the built files are generated in directory /public. Let's run the server:
$ npm start
Open the browser and browse http://localhost:3000. The home page should look like:

You’ll see many Semantic UI elements in there. That is the default theme of Semantic UI. But you’ll notice that no action when clicking some elements e.g. menu, dropdowns, etc. We need to create Javascript codes to initialize the Semantic UI. See the following section...

Create our own stylesheet and javascript

Semantic UI requires jQuery to initialize some elements. So I created a views/javascripts/demo.js javascript:

In addition, we need to add some extra styles for this demo. Create a stylesheet in views/stylesheets/demo.less as below content:

Note that the above LESS file is used by this project only.

Create our own theme for Semantic UI using LESS

Semantic UI default theme is stored in semantic/src/themes/default directory. Now we create our own theme directory, e.g. mytheme. And copy all files from default to mytheme recursively. In project root, type:
$ mkdir semantic/src/themes/mytheme
$ cp -R semantic/src/themes/default/* semantic/src/themes/mytheme/
The next step is to tell Semantic UI to use & build the new theme. Edit /semantic/src/theme.config file Global section, replace all ‘default’ values to ‘mytheme’:
...
/* Global */
@site       : 'mytheme';
@reset      : 'mytheme';
/* Elements */
@button     : 'mytheme';
@container  : 'mytheme';
...
Adjust the font size to bigger little bit. Edit the Semantic UI site file semantic/src/themes/mytheme/globals/site.variables Base Sizes section, as below:
/*-------------------
      Base Sizes
--------------------*/
/* This is the single variable that controls them all */
@emSize   : 18px; // Original 14px
/* The size of page text  */
@fontSize : 18px; // Original 14px
The next step is to change the color. Every website has its own brand color. We can create and assign our own brand color into the Semantic UI. To create a brand color, we can use COOLORS. Coolors does have a very nice interface. And it is very easy to create our own color scheme. Below is the screenshot when I create my brand color scheme:

When finish, copy & paste the colors into the Semantic UI site file (same file as above) semantic/src/themes/mytheme/globals/site.variables Brand Colors section, as below:
...
/*-------------------
    Brand Colors
--------------------*/
@primaryColor        : #5D7EA1;
@secondaryColor      : #8FB2B1;
@lightPrimaryColor   : #77A6B6;
@lightSecondaryColor : #B3D89C;
...
...
/*-------------------
        Page
--------------------*/
@pageBackground      : #F2FAE9;
...
At this time, we can run build processes again. Type gulp again and refresh the browser. The home page is changed to:

Conclusion

We’ve created a new basic Semantic UI theme. You can add more pages to your project. I would suggest that to configure the Gulp to watch the LESS files instead of rebuilding the whole Semantic UI every time.

All source codes of this project are stored in this Github repo

Comments

Popular posts from this blog

Create An Online Store Theme Used By MyCMS

MyCMS is an open-source Content Management System to generate static online shop website. You can use my hosting to input your products, or you can download the source codes and host it in your own server (running NodeJS). Please refer to my Github repo for the detailed installation instructions. This blog is a step-by-step tutorial that shows you how to create an online-shop theme. In this tutorial, it’s using my hosting to input the shop details and products. If you’re hosting the MyCMS by yourself, just change the domain name to yours will do. Introducing MyCMS Before making the theme, you’ll need to use MyCMS to configure the demo shop and input two demo products. MyCMS generates a static website via a theme. The generated static website is NO server program required. You can put the website files (HTML/CSS/JS) to any CDN, hosting. Shop Configuration You must prepare below settings Before using MyCMS: Setting Description Example Store name Your store name will be displayed in t

How I make a web-components based dynamic Javascript page at the top of Google Search

Introduction Everybody wants their website shown at the first position of Google search. SEO (Search Engine Optimization) is a big topic. I just helped my client's website shows the database records at the top search rankings (at least several Chinese generic keywords). See the three example questions all are listed at top ranking: Website background: My client's website  popa.qa  is a traditional Chinese Q&A site that lets members ask and answer questions. All answers and questions are storing in the database server. Step 1: Create The Project This blog illustrates the problems and the steps to fix that with project source codes. Below is the description of the basic project: NodeJS backend (server.js) Develop an API ( /get-database-records ) to simulate getting database records Web-components frontend (index.html) An example component IndexPage  make use of  LitElement to render database records To start the server type: npm start Then I check the webpage speed using th

Progressive Web App Wordpress Theme (Part 1/2)

After I read two articles: Progressive Web Apps Are The Next Big Thing and More Surprising Statistics About WordPress Usage . I decided to build a Wordpress theme which integrated Progressive Web Apps (PWA) to see how cool it is when two “Big Thing” mix together. TL;DR To avoid Too Long; Didn’t Read. I break the whole procedure into two blogs. In part 1, I will build a Progressive Web App in the local server. What PWA features to be built here? According to PWA wiki , the PWA will be progressive enhancements. The final webapp will be: Responsive design: I choose Bootstrap 4 because it’s mobile-first design. Contents retrieve from Wordpress CMS via REST API . It’s a natural way the App is developed in Javascript. Offline support: The PWA still can run and display the contents when no Internet connection. Installable on Android home screen: I w