SvelteKit Crash Course Tutorial #6 - Layout Components

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

The video tutorial covers creating layout components in SvelteKit, a framework for building web applications, and demonstrates how to use them to wrap page components and apply global styles.

Full Transcript

all right then gang so in the last lesson we created this title component a reusable component we put that inside the lib folder and then we use that in both pages the homepage and the about page right here now this is fine but what if we had 10 20 30 40 or even more pages then what we'd have to do is we'd have to import the title component on each of those pages and we'd have to use that component as well now this is fine but it might get a bit tiresome and it's a little bit unnecessary to be honest and also if you changed where the title was on every page you'd have to change every individual page component that's a lot of work so instead what we can do to combat this is create what's known as a layout component and a layout component wraps our page components and they contain different elements or different parts of a template that would be common to every page now every page in this website could have a title and also a footer so those things would go inside the layout component and those things would then wrap every other page component that we have so in order to create a layout component we go to the routes folder right click new file and then it's underscore underscore and then it's called layout dot svelts so these double underscores they tell sveltkit look this is not a normal page component so you wouldn't go to forward slash layout in the address bar and hit enter to get a layout page instead it says okay double underscore twice first now i know that this is a special layout component so a layout component is pretty much the same as any other svelte component it contains a script a template and also some styles so what i'm going to do is create first of all a script just in case we need to do any logic in there or import later on and then down here we're going to do the template so i'll do a header first of all and in fact we're going to have the title component inside the header and we'll pass in a title prop we'll set it equal to ninja gaming guides like so now because we're using that title component we have to import it at the top over here so let's do that import title from lib or dollar sign lib rather forward slash title dot felt all right so that's the header and then we'll have a main tag now inside the main tag this is where i want basically all of the page content to go right we'll come back to that in a minute though now down here at the bottom of every page will be a footer and inside that we'll do a paragraph tag and i'll just say copyright 2022 ninja gaming guides like so all right so now this right here this is where i want the page content to go so if i go to forward slash i get the index component dropped in here if i go to forward slash about i get the about component dropped in here now the way we do that inside felt kit is by just using a special tag called slot not slow router or saluter slots okay that's not going to work let me just do this manually slots like so and when sveltkit sees this it looks for the page component that we're currently at the address for so if i was at forward slash about it would look for the about component and it places it right here where the slot tag is all right so then that's pretty much it but we do want some styles as well and i'm just going to paste those in at the bottom so we say we have a header up here and we display that as flex and then down here we say justify content center so everything should sit in the middle and then down here we say the main tag max width 960 pixels margin top and bottom 20 pixels also left and right that centralizes everything in the page to the middle and then the footer we say text align center awesome so that my friends is pretty much it for now let's see what it looks like in the browser okay then so in the browser we now see two titles we see the one at the top and this is coming from the layout component and also we see the footer coming from the layout components and this one here is because we also have the title component embedded into the individual pages so right now we're on the about page we see all the about content in the middle which is where that slot tag was so that's working at least but we do see this title component again we'll get rid of that in a second if we go to the home component again we see the layout is working we have the title at the top we have the main content in the middle where the slot would be and then at the bottom we have the footer awesome so we just need to get rid of the extra title components but also i want to show you how we can add in some global styles as well so then first things first let's go to these other page components and get rid of the import right here for the title and we can get rid of the title there as well then in the about page get rid of the import and get rid of the title component all right so now we shouldn't see those titles duplicated now the other thing i wanted to quickly show you is how we add some global styles in here because say for example i wanted to style the body tag how would i do that well i can't place a body selector in here because the body tag is not in this template the body tag is actually just here right so what i could do is i could add the styles directly here if i wanted to in this page inside the head or what i could do is i could create a global style sheet inside the source folder and then that would be imported just at the top right here and when it imports that style sheet it's just going to place those styles in the head of our document so let me do this let me go to the source folder i'm going to create a new folder called styles you don't have to call it styles you don't even have to put it in this folder i'm going to do it just for organization and then i'm going to create a new file called global.css and then inside this global css file we can place whatever css that we want now i'm just going to go to my repo and i'm just going to copy all these woohoo so let me just copy them and paste them over here dead simple we have an import at the top which is for a google font and the family is this thing right here now down here we say the body has this purple background color a margin of zero then we start the headings the anchor tags the paragraphs and the ally tags we give all of those this very light purple or pinky color the font family is this thing that we just imported up here so this one and then the font way is normal so some dead simple global styles right now in here we can import those and all we have to do is say import and then where are they coming from well currently we're inside the routes folder so we need to come out of there so i'll say dot dot forward slash to come out with the folder then into the styles folder and then we want the global.css file and that's all we need to do nothing else needs doing with this now and in fact i need to get rid of that body at the bottom so now since we've imported these this should all work and these global styles should take effect in the web page and voila there we go so now we can see this purple background and everything looks a bit better we've got this nice retro font going on i said nice it it's not really that nice but it does look retro for gaming if we go to the about page we can see all of the styles still take effect and also we can see now we don't get that duplicate title on both of these pages as well now by the way if we go to the guides page we're also getting this layout right here we have the title at the top and we have the footer at the bottom so this layout component that we created it's actually being used on every single page component even if those page components are nested in other folders like this one is right here okay now what if we wanted this guides page right here or any other component any page component inside the guides folder to maybe have a different layout well we can do that using reset layouts and we're going to talk about those in the next lesson

Original Description

🔥 Get access to this course on Net Ninja Pro: https://netninja.dev/p/sveltekit-tutorial 🔥 Get access to more premium courses on Net Ninja Pro: https://netninja.dev/courses 🔥 My Udemy Courses: http://www.thenetninja.co.uk/udemy/modern-javascript http://www.thenetninja.co.uk/udemy/react-and-firebase http://www.thenetninja.co.uk/udemy/vue-and-firebase http://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/sveltekit-tutorial 🐱‍💻 Svelte Crash Course: On Net Ninja Pro - https://netninja.dev/p/svelte-for-beginners-firebase On YouTube - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hlbrVO_2QFVqVPhlZmz7tO 🐱‍💻 SvelteKit docs - https://kit.svelte.dev/docs 🐱‍💻 VS Code - https://code.visualstudio.com/ 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video tutorial teaches how to create layout components in SvelteKit to wrap page components and apply global styles, making it easier to manage and maintain web applications.

Key Takeaways
  1. Create a new layout component in the routes folder
  2. Add a script, template, and styles to the layout component
  3. Use the slot tag to wrap page components
  4. Apply global styles using a CSS file
  5. Import the global styles into the layout component
💡 Using layout components in SvelteKit can help simplify the process of managing and maintaining web applications by providing a way to wrap page components and apply global styles.

Related Reads

📰
Why SnapDOM Beats html2canvas for DOM-to-Image Capture
Learn why SnapDOM outperforms html2canvas for DOM-to-image capture and how to use it in your frontend projects
Dev.to · Juan Martin
📰
I built 42 landing page templates as single HTML files (no npm, no build step)
Learn how to create simple landing page templates as single HTML files without relying on npm or build steps, and why this approach matters for efficient web development
Dev.to · Segcam spa
📰
Part 7B — Section 2 — React Event Handling Explained: Forms, Event Object & User Input.
Learn React event handling for forms and user input to improve your frontend skills
Medium · JavaScript
📰
Stop Using window.resize for Everything: A Practical Guide to the ResizeObserver API
Learn to replace window.resize with the ResizeObserver API for more efficient and effective resize event handling
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →