Chakra UI Crash Course #7 – Card Component

In this Chakra UI lesson we’ll use the Card Component to make a list of tasks on the dashboard page.

⭐⭐ Watch the whole course now (without ads) on Net Ninja Pro:

🐱‍💻 Access the course files on GitHub:
Course files –

🐱‍💻 React Tutorial:
On Net Ninja Pro –
On YouTube –
🐱‍💻 React Router in Depth Tutorial:
On Net Ninja Pro –
On YouTube –

🐱‍💻 CSS Flexbox Tutorial:
On YouTube –

🐱‍💻 CSS Grid Tutorial:
On Net Ninja Pro –
On YouTube –

🐱‍💻 Chakra docs –
🐱‍💻 VS Code –

All right there my friends so now we Have the basic layout up and running Next I want to take a look at the card Components so pretty much every UI Library has their own variation of a Card component and chakra UI is no Exception to that rule so here on the Docs you can see we have this component And if you scroll down you're going to See all the different types of content We can place inside a card so we can Have card headers we can have images Inside a card a card body card photos Etc now on our site I want to change the Grid of squares on the dashboard that we Have to be a grid of tasks where each Task is going to be inside its own card Component so to do this I'll be fetching A little bit of data from a Json file That I've already set up inside a data Folder now this Json file is on the Course file so you don't have to type it All out yourself you can just grab it From GitHub remember the link to that is Down below so if we take a look at that File we can see that we have a bunch of Json objects where each object is a task And each task has a unique ID a title a Description an author and an image URL Now that image URL is a path to an image Inside the public folder and it Represents the image of whatever author Created that task the idea being that Eventually we're going to add little

Avatars to each card as well But the first step is to fetch this data And to do that we'll be using Json Server to spin up a Dev server and Generate an endpoint for this Json Resource or this task resource rather Inside the Json file So in order to do this you'll need Json Server installed which you can do by Going to a terminal and typing npm Install hyphen J so it's installed Globally on your computer and then Json Hyphen server so once you've pressed Enter to install that we can then use Json server to watch that Json file by Typing Json hyphen server then hyphen W To watch a file and then we need a path To the file which is dot forward slash Data to go into the data folder and then Forward slash db.json which is the file Name and then press enter and we should Get back an end point at which we can Reach the tasks data or the tasks Resource inside the terminal so now we Can use the endpoint to fetch the data For the dashboard component so the way We're going to fetch that data inside This component is by using a loader now A loader function is something new to One of the later versions of react Router Dom you don't need to use this You could use a use effect hook inside This component and fetch it inside that I'm going to use a loader and if you

Want to learn more about loaders and how They work with react router Dom Definitely check out my react router In-depth course the link to that is down Below anyway to create a loader we just Need to export a function from this Component which is where we're fetching The data from so I'm going to export a Const called tasks Loader we set that equal to an async Function and inside that function all we Need to do is fetch that resource so we Say const response is equal to a weight Fetch and then we can paste in the end Point which was localhost 3000 forward Slash tasks remember we have to have Json server running to grab those tasks Okay So we await that and we have the Response and all we need to do is return The response.json and when we do that we Can access that data inside this Component and the way we do it is by Using a hook from react router Dom so we Can say const tasks is equal to use Loader data I'm going to click on that To Auto Import it from react router Dom And that grabs us the data from this Particular thing right here now at the Minute that won't work because we've not Registered the loader for this component Yes we have that function and yes we Export it but we need to register a Loader with this component and to do

That we have to go to the app component Where we have our routes and it's for The index route the dashboard all we Need to do is tack on a loader prop Right here and specify what function Should be used for the loader for this Component now that function was this one That we exported tasks loader so we just Type in tasks Loader click on that so it Imports it From that particular page or that Particular file And then we can save it so now when we Visit this route then react is going to Lock at this particular loader function It's going to fire that function and It's going to fetch the data and since We return that data right here that's What we get back when we use this hook Use load of data so now we have the Tasks and we can use those tasks inside This component now what I'm going to do Is get rid of all of these boxes right Here because we want to Output now each Task not just a box Now before we start playing around with Cards let me first of all get rid of This padding right here we don't really Need that anymore and I'm going to Change this Min width to be 300 pixels Instead so they're a little bit wider Each card So each card is essentially an item in This simple grid now we need to map

Through the tasks so let's say tasks And Double Ampersand and then tasks Dot map now the reason I do this is to Check we have tasks before we try to map Through them so for each one we fire a Function and we get access to that task And we can return some template for each Task now all I'm going to do for now is Just return a div for each one and Output a task title So remember the title was a property on Each task this thing right here and We're going to use the ID as the key for The div So task Dot ID so fingers crossed this should Work I'm going to save that and come to The browser and now you can see we're Cycling through the different tasks that We Fetch and they're being output in the Grid now it looks terrible but we are Going to create a card component now Instead of this little div right here so Let's delete that and instead use the Card components So then let's say card and you want to Click on this to Auto Import it up here Now remember we need a key prop on Whatever the parent element is inside This function that we're returning so Let's say key is equal to the task Dot ID so we can pass this key prop Through to these components that chakra

Provides us Okay so after that I also want to apply A few styles to each card and in fact we Won't do that just now we'll output the Card and see the default Styles then We'll start to customize it so there's Different things we can have inside a Card right we can have a card header a Card body we can also have a card footer So let's do each one of those let's do a Card header first I'm going to click on This to import it and inside the card Header we might have I don't know Something like a title we'll come back To it in a second we're also going to Have a card body so card body Click on that to import it and inside The card body we might have a little bit Of text and then also we'll have a card Footer these all components Click on that to import it and inside The card footer we might have a couple Of buttons or a like icon or something Like that Okay so let me just do Any old text in here we'll just do a Text component and say card header And then I'm going to copy that And I will paste it down here in the Body And then I will paste it again inside The footer just so we can see what these Look like if we go to the browser okay So we can see these cards and you might

Not see it very well on the screen but You should see a subtle Shadow all the Way around it soft Corners so that is The card component and we have a card Header we have the body and the footer They all say header but three different Places in the card right so now we Actually want to replace these things With actual content So then let me get rid of all those and I'm going to start with the card body So I just want to Output the description We have a description property right Here so let me do a text component and Inside that we want to Output the card Not description Now it's going to be black text oops not Card it should be task Description cannot type Okay so this is black text right here I Want to make it light gray so we can Either do that using the text component Or the card body so let's do the card Body we'll say color is equal to Gray And then dot 500 so it's like a medium Gray Save that and that looks a little nicer And that's all I want for the card body Next I'm going to do the card header now This is a little bit more complex Because what I want eventually is to Have a little avatar on the left and Then on the right I want to have a Heading and also the auth under the

Heading so to do this we're going to use The flex component so we can see the Avatar on the left and then the other Stuff on the right in the heading so Let's do a flex component and import That And inside here we want a box And this is going to be for the Avatar Later on now I'm going to give this a Width of 50 pixels and also a height of 50 pixels and this is just a placeholder For now for the Avatar since we've not Covered that component we'll replace it Later on with the avatar for now let's Just do some text inside here with AV Which stands for Avatar Okay so that's going to sit on the left On the right I want another box And that box inside it is going to have The heading the title so let's do a Heading component and we'll say the task Title is going to be output there as Style props I want to say in fact we'll Just say as and set that equal to an H3 Because the default one is an H2 and Then also I want to set the size of this To be small so this is a built-in Size for chakra it's just small text Okay that will do and then below that We'll have a text component and the text Component is going to be for the author So we'll say bye and then in curly Braces task Dot author all right so now oops I

Should just be task Now in the header we're going to have This on the left and then this on the Right so let's save it and preview Okay we get an error heading is not Defined of course it's not because we Need to import it so let's do that Heading like so Save it and now that should work all Right looking okay so we have the title The author and then later on this is Going to be a little Avatar as well plus We have the description in the card body All right cool so now adjust the card Footer and what I'd like to do is two Buttons and they're going to be some Kind of maybe view icon and also an edit Icon so let's go back over here and come To the card footer And inside here we're going to do a Horizontal stack remember this allows us A little bit like Flex over here to put Two things or three things next to each Other so we don't need all the Flexibility of this Flex component and In fact what I'm going to do is just Apply a bit of a gap to that just so There's a bit more space if I save that Okay hdac is not defined really need to Keep on top of this Save it Now we can see a little bit more of a Gap between those two anyway let's get Back to the H stack that's a little bit

Like the flex component so it's going to Sit different items next to each other Inside here and the two items I want are Going to be button components so click On that to import it so the first one Right here and I'm going to say watch as If we're watching that particular task a Bit like you can watch a GitHub repo and Then we'll do another button down here And this is going to say comment so Leave a comment now I also want to apply An icon to each button if I just save it Right now we can see we have those two Buttons However it would be nice if there was an Icon on the left of each one of those so To do that we can use a prop called Left Icon and we can set that equal to some Kind of Icon so let me do that for both Of these buttons And remember we covered icons in an Earlier lecture all we need to do is Import whatever icons we want to use now When we use a button we have to Output The icon as a component whereas before In the sidebar when we used a list icon We didn't okay But that's because this is a component And we're actually using a left icon Prop and we're passing in a component Okay so we're going to use the view icon For this click on this to import it make Sure it does up here yeah And

Let me close this Same down here but this time we're going To have the edit Icon Like so All right save that and if we take a Look we should have those two icons cool Now you can also pass a variant prop Into different components and buttons Are one of those components so I could Say right here Variance And set that equal to a variant of the Button now one of those variants is Ghost so let me save it just using that One as a go so we can see the difference And you can see how that doesn't have a Background but when we hover over it Does and that's the kind of button I'd Like for both of these so let's also add That variant to this one So variant is equal to ghost like so Save that and preview and that's looking Pretty nice all right cool so there's One more thing I'd like to do and that's Just to add a divider between the body And the footer and we can do that using A divider Components so divider like so Make sure it Imports at the top and then Down here you can just close it I'm also Going to add a border color to this just To color it and the color is going to be Gray

200 so a pretty light gray save it and Preview and that looks a lot better to Me awesome So there we go my friends we have now These different card components looking Pretty nice later on we'll finish them By adding these avatars over here I think for now what I'd like to do is Just go back to the card component Itself and just add a few Styles so let Me go to the card component over here I'm going to apply a border hyphen top Which is colorized and also in fact Let's set the oops not hyphen top it Should become a case let's set the width Of the border to be 8 pixels And we'll set the Border color And that's going to be purple 400 and the background was set to be White because at the minute I think it's Gray yeah And that should be white just so they Stand out a little bit save that all Right so they look a little nicer Awesome Okay so I would actually like them to Sit a little further down from this nav Bar right here so what we could do is we Could find the nav bar itself And we could add a margin bottom so Let's do that margin bottom and set that Equal to 40 pixels see what that looks Like Save it and preview and that looks a bit

Better awesome okay then my friends so There is the card component there's Loads of different things you can do With it just check out the documentation You can see we can add all these Different kind of things So uh yeah in the next lesson what we're Going to do is we're going to take a Look at the tabs component and we're Going to add that onto the profile page

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *