This episode is for members only

Sign up to access "Build a Forum with Inertia and Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
35. Dealing with SVG icons

Episodes

0%
Your progress
  • Total: 6h 54m
  • Played: 0m
  • Remaining: 6h 54m
Join or sign in to track your progress
01. Introduction and demo
6m 59s
0%
02. Getting set up
10m 36s
0%
03. Modifying registration for usernames
7m 15s
0%
04. Figuring out the forum layout
5m 57s
0%
05. Creating and listing topics
9m 15s
0%
06. Basic discussion listing
13m 33s
0%
07. Pinning discussions
4m 1s
0%
08. Tackling pagination in Inertia
8m 23s
0%
09. Customising pagination text in Laravel
52s
0%
10. Showing a discussion
6m 4s
0%
11. Setting up discussion posts
5m 53s
0%
12. Listing through discussion posts
5m 28s
0%
13. Adding more data to posts
8m 24s
0%
14. Adding pagination to posts
1m 35s
0%
15. Adding a post preview to discussions
4m 52s
0%
16. Adding the last reply to discussions
5m 54s
0%
17. Outputting discussion participants
8m 4s
0%
18. Limiting participants in the UI
5m 56s
0%
19. Ordering discussions by last post
4m 30s
0%
20. Handling deleted users
2m 31s
0%
21. Counting replies
8m 13s
0%
22. Building our first filter
8m 31s
0%
23. Highlighting current filters, and merging with pagination
5m 37s
0%
24. Adding auth specific filters
6m 40s
0%
25. Adding the topic filter
8m 18s
0%
26. Scaffolding the new discussion form
13m 29s
0%
27. Toggling the create discussion form
9m 2s
0%
28. Keeping form state
4m 59s
0%
29. Storing a new discussion
11m 29s
0%
30. Discussion validation and authorization
5m 1s
0%
31. Generating markdown for posts
8m 37s
0%
32. Toggling the markdown preview
7m 43s
0%
33. Fetching and displaying markdown
8m 6s
0%
34. Adding a markdown shortcut toolbar
5m 53s
0%
35. Dealing with SVG icons
7m 46s
0%
36. Creating the reply form
7m 48s
0%
37. Basic Inertia permission checking
6m 26s
0%
38. Creating replies to discussions
5m 37s
0%
39. Jumping to posts
11m 40s
0%
40. Automatically scrolling to posts
6m 18s
0%
41. Toggling post editing
7m 32s
0%
42. Editing posts
3m 36s
0%
43. Deleting posts
4m 21s
0%
44. Deleting discussions
6m 7s
0%
45. Setting up for best answers
7m 29s
0%
46. Toggling the best discussion answer
12m 14s
0%
47. Solved and unsolved filters
2m 23s
0%
48. Indexing discussions for search
8m 6s
0%
49. Searching discussions
12m 35s
0%
50. Debouncing search
2m 47s
0%
51. Adding mentionable functionality to forms
6m 32s
0%
52. Indexing users for mentioning
9m 9s
0%
53. Hooking up users for mentions
10m 10s
0%
54. Detecting and storing mentioned users
9m 54s
0%
55. Adding the mentioned filter
2m 26s
0%
56. Adding mentions to the markdown toolbar
1m 1s
0%
57. Adding mentions to the reply form
7m 21s
0%
58. Fixing up some unauthenticated state
1m 1s
0%
59. Fixing up post scrolling
1m 48s
0%
60. Reviewing SSR (Server-side rendering)
8m 20s
0%
61. Preventing parent posts from being deleted
2m 31s
0%
62. Improving solution marking
4m 9s
0%

Transcript

00:00
I've gone ahead and pulled in a bunch of SVG icons into this project, which you can find on the source code over on GitHub.
00:07
These are from Iconic, so definitely check this out if you want to add any more icons or if you generally need them for your projects. So what we're going to do here is we're basically going to build out an SVG component that we can pass in the name of any of these icons
00:22
we have stored in this SVG directory, and we can just have them rendered on the page And then we can additionally pass any styles to them so we can change the colors, the width, height, whatever we need to do. So make sure you have these within the SVG directory of resources
00:37
if you're following along. And we're going to go ahead and build out this SVG component, which we're just going to put inside of each of these. And then we'll go ahead and change up the X that we have currently defined out
00:49
as times, I think, on the fixed form wrapper. Just check what we did here. And we might have done that on the create discussion form. So let's have a look for that.
00:58
Yeah, so we did that just here. So let's focus on our Markdown toolbar first of all. Now, we need a way to load in our SVG icons, and we're going to do this with Vite SVG Loader.
01:10
There are a ton of ways that you can use this down to the very simple just importing them. But I find this quite messy because if we have lots of icons on our pages or within particular components,
01:21
we have to do a huge amount of imports here to pull every single one in. So we're going to go ahead and create a component which allows us to do this very, very easily just by importing a single component. So we're going to start by installing the Vite SVG Loader.
01:38
This needs to be registered as a plugin over on our Vite config file where all of our other plugins are imported and used. So we're going to go ahead and import the SVG Loader from Vite SVG Loader.
01:52
Let's just try that again. And there we go. And then all we need to do is just pass this in to here. That's it. Invoke here.
01:59
And we're pretty much done. You can pass some options through to here. For example, if you didn't want SV Go to be used to change around your icons, this basically just tidies up icons in various ways.
02:12
We can set that to false. I tend to do this first of all, and then go back and change the icons if we need to remove anything. This tends to get rid of the view box of each icons,
02:21
which means we can't style them up nicely. But we'll set that to false now and review that a little bit later. OK, so the next thing we want to do is go ahead and create this SVG component out. We'll do this in the main directory here because we could use this
02:37
anywhere in our app. We're going to create this out as SVG.view. So how is this going to work? Well, let's create out our script section just here.
02:46
What we effectively want to do is accept a prop into this SVG component with the name of the icon that we want to display. So let's go ahead and create out our props in here with define props. And let's go ahead and accept a name into here.
03:02
We'll give a little bit more detail here just because we want this to be required. Like so, because it actually needs this to be able to render properly. So now that we've got this SVG component, let's head over to our Markdown toolbar here
03:18
and let's go ahead and see how we want to use this. So let's create our script in here. Go ahead and import SVG from SVG.view. And it's going to look a little bit like this.
03:29
We're going to say SVG name and we're going to give the name of the icon. So in our case, we have called this if we just go down to where we're storing these icon and bold. So it would just be icon bold.
03:42
And then this component will do everything it needs to render this component out for us. So let's go back over to this component and see how we can do this. So we want to go down here and create out a variable called icon,
03:57
and we want to dynamically or asynchronously pull in a component. So remember, the package that we're using allows us to import now SVGs as components, but we can't really do import and then icon bold from and then a thing we need to manually import this.
04:15
So let's create that variable, which we're going to use inside of our template to actually render the icon. And let's use define async component from view. So let's pull this in to our
04:28
component here to find async component from view. And then to do this, all we do is just call define async component and then into this we tell this what to do. How to pull in this component.
04:41
In our case, we're going to go ahead and manually import a component within here. Let's just get rid of these brackets because we do not need that. And in here, we're just going to give the path name to the icon. Now, this depends on your directory structure.
04:55
So for us, we are in components, so we need to go back, back again, back again into resources, into SVG. And then we need X dot and then SVG. So X is just going to be replaced out with the prop name that we're passing in.
05:15
So basically for icon hyphen bold, this is going to put an icon hyphen bold SVG from this SVG directory. Now, if we were to just dump this out on the page, so let's output the icon that we've just defined in that variable there on the page.
05:31
Let's just see what happens when we try to see this. So this literally just gives us an object. What we actually need to do is create out a component component in view and actually tell this what component is.
05:45
This is just a way to dynamically render our components via a variable name, something that you have inside of you. We just use this is prop and we just pass whatever component it is that we are importing into our template.
06:00
So now if we come over, there we go. We have a bold icon for our bold markdown toolbar, which is great. So we've done all the hard work. We can just duplicate down that SVG component call
06:13
and just have this work as normal now. But we want to go ahead and style each of these up as well. So let's go ahead and just add a couple of styles to this icon just here. So let's just set a height and a width on this for now.
06:26
There we go. And I think that looks great. We can click this now, type something, click bold, and it's done for us. So let's really just copy this down for each of these
06:38
and replace out each of the icon names. No need to go ahead and import these individually now. It's just going to work. What was that one code?
06:46
Let's just add that in. And we'll do the same for link as well. Like so. Great. So let's come over and there we go.
06:54
We have a nice markdown toolbar that we can use to do anything inside of here. Now, as part of this, I also included a close icon as well, which is just an X, but it's an icon, so it's going to look a little bit better so we can do exactly the same thing over on our create discussion form
07:11
with this just here. So we're going to go ahead and use that SVG component. So let's pull that in and we're going to set the name of this to icon close. Again, we can set a height and a width to this.
07:22
So that's a height and a width of five. And we'll just make sure we close that off properly and we go ahead and import this down here. So let's import.
07:35
SVG and see if that will also complete for us. Yeah, there we go. Great. So let's come over and there we go. We have a really nice close icon now that works and looks a little bit better.
62 episodes6 hrs 54 mins

Overview

Ready to build a forum with Inertia and Laravel?

Why a forum? A forum touches a whole load of concepts that you'll use throughout your development career – particularly on the client-side, where we'll be doing most of the heavy lifting.

So, let's build a clean, modern forum with features like markdown support, code highlighting, advanced filtering, user mentions, full-text search, the ability to mark best answers, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.