
Laravel blade SVG component part 2: Tooltips with alpine.js
Hey there, fellow SVG enthusiasts! Remember when we embarked on that adventure of creating a dynamic SVG component with Laravel Blade? We dove deep, solving the problem of bloated SVG lists, and, lo and behold, we crafted a single, mighty SVG component to rule them all. If you missed it, take a peek at Part 1. 🚀
Alright, let's level up! Today, I’m back with part 2, and it’s time to sprinkle some magic dust on our SVGs. Let’s talk tooltips with Alpine.js. Because why should SVGs just be visual? Let’s make them informative! 🤓✨
Enhancing SVGs with Alpine Tooltip Integration
Integrating user-friendly elements into our designs ensures a seamless user experience. Enter Alpine Tooltip - a custom directive crafted for Alpine.js by Ryan Chandler. By integrating this directive with our SVG components, we'll effortlessly add interactive tooltips to our icons. This guide walks you through the integration process, showcasing how you can enhance your SVGs to be more interactive and informative. Elevate your SVG game and provide users with clear, context-rich visuals, making your site stand out both in terms of usability and SEO.
⼻1. Install Alpine-Tooltip
Install via NPM Begin by installing the package using npm:
npm install @ryangjchandler/alpine-tooltip
Register the Tooltip Directive
After the installation, we need to add the x-tooltip
directive to our Alpine.js project. To do this, register the plugin with Alpine in our resources/app.js
:
import Alpine from "alpinejs";
import Tooltip from "@ryangjchandler/alpine-tooltip"; //THIS IS NEW
Alpine.plugin(Tooltip); // THIS IS NEW ALSO
window.Alpine = Alpine;
window.Alpine.start();
Add the required CSS: For the tooltips to display correctly, we must include the appropriate CSS. Include the following stylesheet in our HTML document:
<link rel="stylesheet" href="https://unpkg.com/tippy.js@6/dist/tippy.css" />
⼻2. Update the current to use tooltips
The power of blade lies in its flexibility and extensibility. Let's take our SVG component to the next level by integrating tooltips. And the coolest of all is that we can tweak the component in such a way that it will not produce any bugs whatsoever on the already applied components in our project.
Before:
The SVG component was straightforward. It rendered SVG icons from the tabler-sprite.svg
file. However, there was no provision for tooltips.
<svg {{ $attributes }} width="24" height="24">
<use xlink:href="{{ asset('tabler-sprite.svg').'#tabler-'.$svg }}" />
</svg>
After:
Now, we've enhanced the SVG component. By integrating the x-tooltip
directive, we can easily display tooltips on SVG icons. The new component structure accepts two properties: title for the tooltip
content and svg
for the SVG icon reference.
@props([
'title' => '',
'svg' => '',
])
<svg {{ $attributes }} x-data x-tooltip.raw="{{ $title }}" width="24" height="24">
<use xlink:href="{{ asset('tabler-sprite.svg').'#tabler-'.$svg }}" />
</svg>
Now we can use this cool component like this:
<x-svg
svg="news"
title="Show Post"
class="text-indigo-400 transition cursor-pointer hover:text-indigo-600"
/>
And we are all set!
📚Documentation
With the alpine-tooltip directive now part of our SVG component, there's a whole new world of enhancements and tweaks at our fingertips. And this is just the beginning!
If you're curious about further amplifying the capabilities of this component, check out the alpine-tooltip directive's documentation. It offers insights into advanced configurations, modifications, magic functions, and more.
Get Better at Laravel!
Do you know what is the best way to learn Laravel? Laracasts of course! What else?
Why Every Dev is Raving About Laracasts:
🚀 All-Star Cast: Learn Laravel from the best! We're talking the creator of Laravel, Taylor Otwell, the genius behind Livewire, Caleb Porzio, and more Laravel legends.
🛠️ Full Toolkit: It’s not just Laravel! Dive deep into Livewire, Alpine.js, and the entire Laravel ecosystem and its intricacies.
🔄 Always Fresh: With direct insights from industry leaders, you’re getting content that’s not just up-to-date, but ahead of its time.
💡 Real-World Ready: Theory’s cool, but practical knowledge? Priceless. Jump from lesson to project seamlessly.
👩💻 Join the Elite: Step into a community that’s all about pushing boundaries and perfecting the craft.
Bottom line: Want to level up fast? Laracasts is your jam. Dive in using my referral link and join the coding revolution. 🔗 Laracasts
Conclusion
Thanks for joining me on this SVG component journey with Laravel and Alpine.js. I'm always on the lookout for cool updates and ways to make things better. Whenever I find something new and exciting, I'll be sure to share it here. So, stay tuned for more updates in this series. Keep coding and exploring!
🚀 Spread the Love & Support the Realm
Hey there, fellow Realmer! If this guide illuminated a new path in your Laravel journey, especially with our focus on SVGs and tooltips, your support would mean a lot. Every bit of magic helps
👑 Crown & Share: If you found value in this post, please give it a crown and share it with your fellow code enthusiasts. Spreading knowledge is what Sudorealm is all about! Fun fact the Author with the most crowns inside a realm will be crowned as the Realm King! 🤴
🛍 Affiliate Treasures Below: Dive into the depths below the post to uncover some affiliate products I've curated just for you. It's a great way to support the realm and discover some nerdy treasures.
🐦 Twitter/X Shoutout: Feeling extra grateful or have some cool feedback? Drop me a shoutout on Twitter – I'd love to hear from you!
☕️ Coffee Driven Development: Enjoyed the content? Help keep my coding sessions energized with a cuppa! BuyMeACoffee
Thanks for being a part of our realm. Every bit of support propels our community to new horizons. Until next time, keep exploring!
I am @d3ad R1nger, Creator of Sudorealm, thank you for reading.