A Laravel Project Start to Finish: Part 2 - UI Design & Front-end Scaffolding

Jan 6th, 2020

Welcome to part 2 of a start to finish project using Laravel! In this section I'm going to cover how I begin to design my project and turn that into code. Let's get started!

In my projects, I use Adobe XD to create the page layouts, decide on a color scheme, and create assets in the the form of SVGs and images. XD is free by the way, so if you are worried about it being expensive, there's no need!

You can see my current progress below.

Imgur

Finding a Color Palette

You may be wondering how I landed on my color palette. There's a great tool for generating color palettes called coolors that I use. I'll just run through palettes until I see one that is appealing for my project. Then I'll lock in the colors that fit and keep regenerating the colors I don't like until I have all of the colors I want. Here is my color palette for DEVcelerate.

Generating SVGs and Images

I didn't even know that it was possible to create web assets in XD until I was watching tutorials on a great YouTube channel called DesignCourse. He uses XD for a lot of his design examples, and I have learned a lot from him, so check him out.

Let's look at the splash image to figure out how it was created.

Imgur

I'll also provide the colors so that you can follow along with the same design if you'd like.

  • Gradient: #304D6D to #13293D
  • Bottom Border: #82A0BC

The first step is we want to create a box that will serve as the building block for our final image. Essentially, we are using a combination of shapes to merge into a single shape that we need. We can use the menu to select the rectangle tool.

Imgur

Then we can draw the box and select our gradient color.

Imgur

Next, we want to make the bottom side rounded, so we want to draw a circle that overlaps our rectangle so that if they were combined we would have our desired shape. Once we feel comfortable we can hit the "Add" button with our two shapes selected to form them into a single object.

Imgur

If you did it the same way as my image above you are left with the circle protruding from either side. Just like we can add shapes together to create a different shape, we can also subtract them from each other. So we are going to create two rectangles and cover the sides that we want to trim off. Then we can select all of our shapes and press the "Subtract" button.

Imgur

The final part of our design process is duplicating our new shape with Ctrl + D, and shifting it down as much as you want to create the bottom border. Then we want to send our duplicated object to the back (Shift + Ctrl + [).

Imgur

Tada! Our finished asset!

Imgur

Lastly, we can group our objects together using Ctrl + G and, in the menu on the left, right clicking it and choosing "Export Selected".

Imgur

Set the desired settings and export it! I used PNG and "Exported for" was set to "web".

This same exact process can be used for SVGs, you can simply choose a different format when exporting. I used an image in this case because I wanted to set the background-image property of an HTML element.

Setting Up Tailwindcss

In this project I'll be writing my CSS using Tailwindcss. At the risk of sounding like a total shill, I love it. It changes the way you handle the styling of your pages completely. Tailwind is a utility CSS framework, which means that you use classes on your elements to represent CSS properties. So rather than styling a button by selecting it then applying the styles by writing CSS, you instead have classes already representing those properties and you add those classes to your button.

1<!-- Before -->
2<style>
3.my-button {
4 color: white;
5 background-color: blue;
6 border-radius: 0.25rem;
7 padding: 0.5rem 1.25rem;
8}
9</style>
10 
11<div>
12 <button class="my-button">This is my button</button>
13</div>
1<!-- With Tailwindcss -->
2<div>
3 <button class="text-white text-blue-500 rounded-sm py-2 px-5">This is my button</button>
4</div>

The documentation is very clear, so if you are interested go check it out for yourself. Personally, since using Tailwind, it's the only thing I want to use for my projects anymore. It prevents the normal CSS bloat that naturally occurs when creating a project and keeps your CSS files small.

Let's add it to the project.

  1. Run our initial $ npm install command to get everything working
  2. Pull in the packages we will need to use tailwind. $ npm install tailwindcss laravel-mix-tailwind
  3. Add tailwind to /webpack.mix.js
1// webpack.mix.js
2 
3const mix = require('laravel-mix');
4require('laravel-mix-tailwind'); // require our installed package
5 
6 
7mix.js('resources/js/app.js', 'public/js')
8 .sass('resources/sass/app.scss', 'public/css')
9 .tailwind(); // add this so we can handle the tailwind files
  1. Rename or create your tailwind config file at the project root as /tailwind.js
  2. Use /tailwind.js config to extend the styling. Here I've added my color palette and added some extra height/width properties that I will use
1module.exports = {
2 theme: {
3 extend: {
4 colors: {
5 darkest: '#13293D',
6 darker: '#304D6D',
7 dark: '#82A0BC',
8 primary: {
9 default: '#63ADF2',
10 hover: '#689AC8',
11 },
12 light: '#ECEEF1',
13 logo: '#B7D5F0',
14 mainred: '#DB5461',
15 },
16 fontSize: {
17 '7xl': '5rem',
18 },
19 width: {
20 '1/10': '10%',
21 '2/10': '20%',
22 '3/10': '30%',
23 '4/10': '40%',
24 '5/10': '50%',
25 '6/10': '60%',
26 '7/10': '70%',
27 '8/10': '80%',
28 '9/10': '90%',
29 },
30 height: {
31 'half': '50%',
32 'half-screen': '50vh',
33 }
34 },
35 },
36 variants: {},
37 }
  1. We can get rid of the bootstrap stuff that is in our /resources/sass/app.scss file and replace it completely with our tailwind imports
1// app.scss
2@tailwind base;
3 
4@tailwind components;
5 
6@tailwind utilities;
  1. Under /resources/js/ I emptied out most of the junk from bootstrap.js except for the two lines below. The only thing left in app.js is the line requiring bootstrap.js.
1// bootstrap.js
2window.axios = require('axios');
3 
4window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

At this point we should be able to run $ npm run dev to do a build of our resources and we shouldn't have any errors. If you have any content on the page it should be completely stripped of formatting (assuming the view page doesn't have inline styling like the default /resources/views/welcome.blade.php page does).

Okay, we are ready to begin working on our actual views! To have your project automatically watch for changes in your assets and quickly rebuild it, you can use the command $ npm run watch-poll on a terminal tab.

Adding Our Landing Page View

Now that the design is complete in XD and I've got tailwind and sass setup, I'm ready to create my landing page view and get to work.

I added my route using the simple callback function for now in /routes/web.php

1// routes/web.php
2Route::get('/', function () {
3 return view('index');
4});

This will now return the view that I'm creating next: /resources/views/index.blade.php. First I'll be creating my master layout page that the content of my index page will be inserted into.

1<!-- /resources/views/layouts/master.blade.php -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <meta name="viewport" content="width=device-width, initial-scale=1">
8 <meta name="csrf-token" content="{{ csrf_token() }}">
9 
10 <title>{{ config('app.name', 'DEVcelerate') }}</title>
11 
12 <script src="{{ asset('js/app.js') }}" defer></script>
13 
14 <link rel="dns-prefetch" href="//fonts.gstatic.com">
15 <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,700,900&display=swap" rel="stylesheet">
16 
17 <link href="{{ asset('css/app.css') }}" rel="stylesheet">
18</head>
19<body class="bg-light text-darker">
20 
21 @yield('main')
22 
23</body>
24</html>

As you can see above, we are pulling in our processed resources in the head tag {{ asset('js/app.js') }} and {{ asset('css/app.css') }}. Then inside the body we are @yielding a section we are naming "main". Now on the pages that will use this layout, we can set the section to "main" and it will be inserted at that spot.

Let's check out the skeleton of our index page.

1@extends('layouts.master')
2@section('main')
3 
4<main class="min-h-screen w-full">
5 
6 <div
7 class="background-image absolute inset-0"
8 style="
9 background-image: url('{{ Storage::url('public/images/SplashBackground.png') }}'); background-repeat: no-repeat;
10 z-index: -50;
11 "
12 ></div>
13 
14 <section class="container mx-auto flex flex-col justify-center md:justify-between px-2" style="height: 60vh; margin-bottom: 15vh;">
15 <!-- ... -->
16 </section>
17 
18 <section class="container mx-auto px-2 py-24">
19 <!-- ... -->
20 </section>
21 
22 <section class="bg-white py-24">
23 <!-- ... -->
24 </section>
25 
26 <footer class="bg-darker text-light text-sm">
27 <!-- ... -->
28 </footer>
29 
30</main>
31 
32@endsection

As you can see we are extending our master layout and using tailwind to develop our layout. You'll notice I'm also using {{ Storage::url('public/images/SplashBackground.png') }}' to use the asset that I made in XD!

Using Created Assets

Using the assets are pretty simple. Move the saved files into your project under /storage/app/public/* where you can subdivide public into whatever you want. I used images for my image assets. Then, if you want to use the Storage facade to get the assets, you have to run an artisan command $ php artisan storage:link, which will create a symbolic link allowing access to those files.

Thanks for reading part 2--if you are trying to follow my steps and are running into issues feel free to contact me through the methods I provide at the top.

 

Did you enjoy this article? Did you hate it? Email me with your constructive criticisms at cwinter.web@gmail.com! The more feedback I get, the better job I can do writing helpful articles--and that is important to me. Thanks for reading!