- Tailwind CSS is a utility-first CSS framework for building custom user interfaces.
- Utility-first means that you can use the classes to style your components. You don't need to write CSS yourself.
- It is not opinionated. Tailwind CSS doesn't tell you how your site should look like. You can use it to build your own design.
- However, it is not a UI kit. It doesn't provide you with ready-made components. You can use it to build your own
components.
- There are UI kits built with Tailwind CSS, for example Tailwind UI
- Some not-component libraries are headlessui.dev and shadcn/ui. They can be used to build your own component library.
- Core concepts
-
Continue last exercise. Create a new branch 'tailwind' with git.
-
Download the full version of the MediaAPI. Put it in the same main folder as the rest of this course's projects.
-
Also check if the common types have been updated and need to be downloaded.
-
- Add plugins to
prettier.config.js
:
export default { ... plugins: ["prettier-plugin-tailwindcss"], };
- Or in json format to
.prettierrc
file if it's in use:"plugins": ["prettier-plugin-tailwindcss"]
- Add plugins to
-
Rename
index.css
toindex_old.css
. Create a new blankindex.css
and add the following the beginning of the file:@tailwind base; @tailwind components; @tailwind utilities;
- These are the default styles that Tailwind CSS provides.
-
Copy
:root
rule fromindex_old.css
toindex.css
to get the basic styles back to the app. -
Open
Layout.jsx
and add the same styles to theLayout
component as you had before, but use Tailwind CSS classes instead of CSS.- Start with
ul
andli
elements inNav
component. Use Tailwind CSS docs to find the right classes. The old styles are inindex_old.css
if you need to check them.
- Start with
-
Do you really need to add the same styles to all
<li>
components? Isn't that repeating yourself? Yes it is. And it is supposed to be like that.- You can however use pseudo classes like
*:
to add styles to direct children of E.g.ul
element.
- You can however use pseudo classes like
-
Go through
index_old.css
and make the app look like it did before (or better) with Tailwind CSS classes.
-
Continue in the same branch.
-
In this exercise we'll add two buttons to
MediaRow
component:Modify
andDelete
which are shown only when the user is logged in. At this point the buttons don't do anything but console.log something. -
Use
useUserContext()
to get theuser
from the context. -
Add the buttons to the
MediaRow
component. Use Tailwind CSS classes to style the buttons the same way as theShow
button/View
link is styled. -
Use conditional rendering to show the buttons only when the user is logged in and the user is the owner of the media. If user is admin, the buttons are shown always. If the user is not logged in, the buttons are not shown at all.
-
Add event listeners to the buttons. Something like this:
<button className="your tailwind classes here" onClick={() => console.log("modify/delete", item)} >
-
Test that the buttons are shown only when the user is logged in and that the event listeners work.
-
Add functions for deleting and modifying media items to
apiHooks.js
:deleteMedia
andmodifyMedia
. Use the same URL as in theMediaAPI
for deleting and modifying media items. -
For now, you can use
useNavigate
hook to refresh the page after deleting or modifying the media item.
- Continue in the same branch.
- Now we add likes to the media items. The user can like a media item only once.
- Create a new component
Likes.jsx
in thecomponents
folder. - Add a button to the
Likes
component. The button can have just text or a heart icon. You can decide. It also needs to show the number of likes. - Add new hook
useLike
toapiHooks.js
. The hook should have functionspostLike
,deleteLike
,getLikesByMediaId
andgetLikesByUser
. Use the same URL as in theMediaAPI
for liking media items. - Create states for
likes
anduserLikes
in theLikes
component. These are used to store all the likes of the media item and whether the user has liked the media item. - Use
useEffect
to get the likes of the media item when the component is mounted. - Add event listener to the button. If the user has liked the media item, the button should show that the user has liked the media item. If the user hasn't liked the media item, the button should show that the user can like the media item.
- Add a function to the button that posts a like to the media item if the user hasn't liked the media item. If the user has liked the media item, the function should delete the like.
- Add the
Like
component to theSingleView
component. The button should be active only when the user is logged in. The number of likes should be shown always.
- Run
npm build
ornpm run build
- Move build folder to your public_html
- Test your app:
http://users.metropolia.fi/~username/tailwind
- Modify README.md. Change the link in
Open [X](X) to view it in the browser.
to point to the above link. - git add, commit & push to remote repository
- Submit the link to correct branch of your repository to Oma