Svelte Tutorial for Beginners #25 - Poll Details Component

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago
Skills: React50%

Key Takeaways

Creates a poll details component using Svelte for outputting poll data

Full Transcript

okay then gang so we have this polished component right here which is cycling through the poles and it's just outputting the question property on each pole at the minute so it looks something like this over here if we have two poles now obviously we want to output a better template for each pole where we show the question the possible answers and the number of votes etc so what I'm going to do is actually create a new component for a pole detail and what we'll do is pass that particular pole into that component and the reason I'm doing this is to make it more modular and also less messy in this file because if we combine the two it's going to be a lot of code so I'll go to components and I'm going to create a new file called pole details dots felts and then inside here we're going to have a script and we're going to also have a template I'll start that off by saying div with a class of pole and I'll come back to that in a second and then final it we need a style tag as well like so okay so now let's pass in a pole into this so first of all let's say exports because we're going to pass it in as their prop and then let pole and to begin with we don't need to give it a value and inside the pol list we need to import that at the top that component so I'm gonna say import and then it's Paul details from and it's in the same directory so dot forward slash and Paul details dots felt okay so now we can render that down here instead of this right here the poll question so inside that div we'll get rid of this and instead we'll output Paul details like so and we need to pass through a particular hull into this poll details component so that every time we get a new poll in this cycle this each loop we're passing it into the poll details component then we can receive it right here okay so let's pass in that prop so Paul like so okay so over here we can now access Paul and we can output it in template so what's the first thing we want inside these poll details well probably the question so let's do an h3 and then just output the poll dots question first of all and then beneath the h3 we will do a div and that's going to have a class of answer so this is going to be one of the answers and each answer is going to live in a div with the class of answer so we can style it correctly later and at the top we're going to have a percent bar so div dot percent and it's also going to have a class of percents - a now this percent bar is going to be used to show the percentage of votes that this answer currently house and we'll see that in more detail later on we're just going to place it there for now as is now down here I want to do another spawn tag and inside here we're going to have the poll answer so Paul answer a so that's one of the options and then over here we're going to output as well in parentheses how many volts it currently has so I'm going to say Paul dots votes a so that's the number of votes it currently has now it's probably going to be zero to begin with for most polls because when we create one it starts off at zero but for our original poll which we have in Aptos felt our dummy one it's going to be nine and votes B is fifteen so now we have this for one of the answers let's do it for the second answer going to copy all of that and paste it down here and I'm gonna change this to percents B and change this to votes B and this to answer B like so okay so now we have all of this information we have the question we also have one of the answers that's basically an option that a user can eventually click to vote on this and the current number of votes for that option so if I save it then come over here we're going to see at the minute we just have this one poll because it refreshed but now we have the title we also have answer a and answer B and how many votes those two have so if I create a new one just a load of junk for now then we're gonna see that one right here as well or rather this one right here and they start off with votes each but now without putting all of that information now what I'd also like to do is output the total number of volts so for this one right here it will be 9 plus 15 that'll be 24 now to do that I am going to use a reactive value so let me do a comment up here saying reactive values and then we'll create a reactive value called total votes and that's going to be equal to Paul votes a plus Paul votes B right so that's the total number of votes and then we can output that underneath the poll question so I'll do a paragraph tag and then I'll say total votes is going to be equal to total volts the reactive value okay so let's make sure that works over here we can see now total votes is 24 if we add a new one it should be 0 because 0 plus 0 is 0 which we get awesome okay so now we need to style this a little bit because it doesn't look great at the minute so what I'm actually going to do is just copy over some styles from my github repo because this is not a CSS course and I'm going to paste them in here but I am going to walk you through them so the h3 which is the poll question up here that just has a margin of 0 top and bottom it strips that away auto left and right the color is a mid gray color then we have the paragraph tag which is the total votes the margin top is 6 pixels font size 14 pixels so we make it a little smaller the color is a lightish gray and the margin bottom is 30 pixels so there's going to be a bit of a margin between this and the answers then now speaking of the answers we have that selector right here for each of these answer divs and we have a background of a really light gray so each answer is going to have this background of a light gray we'll see that in a second a curse of pointer which is the little hand and I want that because in the future a user is going to click on one of the answers to vote for that answer then we have a margin of 10 pixels up and down so that the answers have a bit of separation from each other and auto left and right position is relative and that's because this thing right here the percent bar we're going to position that absolutely relative to the answer later on so that's why we give this a position of relative then when we hover over the answer the opacity of that goes to naught point six that's just a little hover effect and we'll see that in a second the span which is this thing right here where we actually output the answer and the number of votes we display that as inline-block so we can give it some padding ten pixels up and down 20 pixels left and right so let's take a look at what that looks like and that looks a bit better so let me just add a new one as well so we can see these side-by-side and we can see now we have these two polls so in the future when we click on one of these it's going to cast a vote for that thing that you click on and then the number should go up and this percent bar up here on each of the answers will adjust because say this one's 50% rate of votes then the percent bar is going to go all the way to the middle and that might be a red block and down here it would also be 50% so that would be a green block to the middle Eve over here a hundred percent of the votes were for this one then the red block would go all the way across the green one wouldn't really even start okay so that's what those percent blocks are going to be for and we'll work out those later on but for now I think what I'd like to do is move on and maybe create a custom card component so that each of these polls can sit inside a card so the card is like a little container that brings the content out it gives it a bit of depth and we'll take a look at that next

Original Description

Hey gang, in this Svelte tutorial we'll create our poll details component, to output the votes, name etc of each poll 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/svelte-tutorial/tree/lesson-2 🐱‍💻 🐱‍💻 Other Related Free Courses: + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 🐱‍💻 Svelte Docs https://svelte.dev/tutorial/basics 🐱‍💻 🐱‍💻 The Net Ninja Community Boards: https://community.thenetninja.co.uk/
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

Related Reads

Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →