<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Asfak Ahmed - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Asfak Ahmed - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 21 May 2026 16:10:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/asfakahmed/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Why Front-End Developers Should Understand UI/UX Design ]]>
                </title>
                <description>
                    <![CDATA[ When users interact with a website or application, the first thing they notice isn’t the code. Instead, it’s the design and experience. Smooth navigation, intuitive layouts, and visually appealing interfaces are what keep users engaged. Behind these ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/why-front-end-developers-should-understand-uiux-design/</link>
                <guid isPermaLink="false">68c4087abba5a7e638ae4cd6</guid>
                
                    <category>
                        <![CDATA[ Frontend Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ UI UX ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Asfak Ahmed ]]>
                </dc:creator>
                <pubDate>Fri, 12 Sep 2025 11:48:10 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1757677089930/07115f35-ba9e-452a-bf95-3a96de7a5d24.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When users interact with a website or application, the first thing they notice isn’t the code. Instead, it’s the design and experience. Smooth navigation, intuitive layouts, and visually appealing interfaces are what keep users engaged.</p>
<p>Behind these experiences, front-end developers play a pivotal role. They don’t just turn mockups into functioning code – they shape the bridge between design and functionality, making user experiences come to life.</p>
<p>To do this effectively, front-end developers need to understand UI (User Interface) and UX (User Experience) design.</p>
<ul>
<li><p><strong>UI</strong> focuses on the look and feel of the product, including colors, typography, buttons, layouts, and visual consistency.</p>
</li>
<li><p><strong>UX</strong> focuses on how users interact with the product, including ease of use, accessibility, responsiveness, and overall satisfaction.</p>
</li>
</ul>
<p>In today’s competitive digital landscape, knowing only code isn’t enough. Let’s explore why UI/UX knowledge is crucial for front-end developers and how it impacts both the developer’s career and the end-user experience.</p>
<h2 id="heading-table-of-contents"><strong>Table of Content</strong>s:</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-why-front-end-devs-should-understand-uxui">Why Front-End Devs Should Understand UX/UI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-key-uiux-principles-every-front-end-developer-should-know">Key UI/UX Principles Every Front-End Developer Should Know</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-uiux-knowledge-for-front-end-developers">Benefits of UI/UX Knowledge for Front-End Developers</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-developers-can-learn-uiux">How Developers Can Learn UI/UX</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-to-keep-in-mind-about-uxui">What to Keep in Mind about UX/UI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-front-end-devs-should-understand-uxui">Why Front-End Devs Should Understand UX/UI</h2>
<p>Front-end development is not just about writing clean code. It’s also about creating experiences. A developer might know every JavaScript framework and CSS trick, but if they don’t understand UI (User Interface) and UX (User Experience) principles, their work risks feeling clunky, confusing, or unappealing.</p>
<h3 id="heading-why-uiux-knowledge-matters">Why UI/UX Knowledge Matters</h3>
<ol>
<li><p><strong>Bridging the gap between design and development:</strong> Front-end developers act as the bridge between design mockups and functional applications. By understanding UI/UX principles, developers can implement designs more accurately, while also identifying potential improvements in usability or accessibility.</p>
</li>
<li><p><strong>Improving usability:</strong> UI/UX knowledge helps developers think beyond visuals. They consider user flow, navigation, responsiveness, and accessibility. For example, a button’s size, contrast, and placement affect how easily users interact with it.</p>
</li>
<li><p><strong>Creating consistent experiences:</strong> Without consistency, users get frustrated. Developers who understand design systems and UX patterns ensure that spacing, typography, colors, and interactions feel uniform across the app.</p>
</li>
<li><p><strong>Boosting collaboration with designers:</strong> Developers who know UI/UX can speak the same language as designers. This leads to smoother handoffs, fewer revisions, and faster delivery. Instead of just “coding what’s given,” they can proactively suggest alternatives when something won’t scale well.</p>
</li>
<li><p><strong>Improving user satisfaction and business goals:</strong> Ultimately, great UI/UX translates to happy users. An application that is visually appealing, easy to navigate, and responsive encourages engagement, reduces bounce rates, and drives conversions.</p>
</li>
</ol>
<h2 id="heading-key-uiux-principles-every-front-end-developer-should-know">Key UI/UX Principles Every Front-End Developer Should Know</h2>
<p>A website or application may be powered by complex logic in the background, but what users truly experience is the interface. This is where understanding UI/UX principles becomes essential. Let’s explore some of the core ideas every developer should master.</p>
<h3 id="heading-consistency-builds-trust">Consistency Builds Trust</h3>
<p>One of the most fundamental principles of good UI/UX is consistency. When layouts, typography, buttons, and navigation behave the same way across an application, users instantly feel more confident.</p>
<p>Imagine a scenario where a “Sign Up” button looks completely different on each page – one rounded, another outlined, another square. This inconsistency doesn’t just look unprofessional, it breaks trust and forces users to pause and think. A consistent interface, on the other hand, feels reliable and predictable.</p>
<p><strong>Example:</strong></p>
<p>The image below compares two sets of buttons to highlight the importance of design consistency. On the left, the "Consistent Buttons" example shows "Sign Up" and "Login" buttons that look identical in color, shape, and style, making the user interface appear clear and professional. On the right, the "Inconsistent Buttons" example displays the same buttons but with different colors, shapes, and text styles, which can make the interface confusing and visually unappealing.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757048948178/8d74c12b-f1ac-442a-81e5-906c5b0dce2c.png" alt="Consistency Builds Trust example image" class="image--center mx-auto" width="1363" height="180" loading="lazy"></p>
<h3 id="heading-guiding-users-with-visual-hierarchy">Guiding Users with Visual Hierarchy</h3>
<p>Users rarely read web pages word by word. Instead, they scan for what matters. That’s why visual hierarchy is so important. By adjusting size, contrast, color, and spacing, developers can subtly guide attention. Headlines should naturally stand out from body text, and primary actions like “Buy Now” or “Sign Up” should be visually stronger than secondary options.</p>
<p>A great example of this is a pricing page where the recommended plan is highlighted with a brighter color and a slightly larger card, drawing the eye instantly.</p>
<p><strong>Example:</strong></p>
<p>The image below shows three pricing plans Base, Pro, and Enterprise, with the Pro plan visually highlighted in the center. The design uses size, color contrast, and placement to guide users attention toward the Pro plan, illustrating the concept of visual hierarchy for easier decision-making.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757049059813/a2363890-44f6-4394-b307-db62fd3b38b0.png" alt="Guiding Users with Visual Hierarchy example image" class="image--center mx-auto" width="1130" height="644" loading="lazy"></p>
<h3 id="heading-accessibility-for-everyone">Accessibility for Everyone</h3>
<p>A beautiful interface is meaningless if it’s not usable by all. <a target="_blank" href="https://www.freecodecamp.org/news/why-accessibility-matters-in-ui-ux-design/">Accessibility</a> ensures that people with disabilities can interact with your site just as easily as others. This means paying attention to color contrast so text remains readable, using semantic HTML so screen readers can interpret content, and adding descriptive alt text to images.</p>
<p>Even small improvements – like ensuring forms have proper labels – can transform usability. A login form without labels may look clean, but it excludes users who rely on assistive technology.</p>
<p><strong>Example:</strong></p>
<p>The picture below compares two login forms to show the difference between poor and good form design. The "Bad Form" on the left lacks clear labels for its input fields and has a dull, hard-to-see login button, making it less user-friendly. The "Good Form" on the right uses visible labels for each field and a prominent, easy-to-find login button, which improves clarity and usability for users.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757049101402/de1a3424-d599-4243-b760-a9114b9d99f0.png" alt="Accessibility for Everyone example image" class="image--center mx-auto" width="1370" height="390" loading="lazy"></p>
<h3 id="heading-designing-for-all-screens">Designing for All Screens</h3>
<p>With most traffic now coming from mobile devices, responsive design is no longer optional. A layout that looks perfect on desktop but breaks on mobile will drive users away.</p>
<p>Mobile-first thinking encourages developers to start small and then expand layouts for larger screens. This ensures that buttons are touch-friendly, text remains readable, and navigation adapts naturally. A dashboard that stacks neatly into a single column on mobile but expands gracefully on desktop provides a consistent experience everywhere.</p>
<p><strong>Example:</strong></p>
<p>The image below shows how pricing plans are tailored for various screen sizes. On the left, the "Desktop Design" displays multiple plans side by side, allowing easy comparison. On the right, the "Mobile Design" stacks the plans vertically, making them easy to read and navigate on smaller screens.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757049871365/4a5b8bfb-5a83-415b-9ec2-55802abb9fb9.png" alt="Designing for All Screens example image" class="image--center mx-auto" width="1036" height="552" loading="lazy"></p>
<h3 id="heading-clear-feedback-and-interaction">Clear Feedback and Interaction</h3>
<p>Users should never be left guessing. Every interaction must provide feedback – whether that’s a hover animation on a button, a loading indicator when submitting a form, or a success/error message after an action.</p>
<p>Consider a “Submit” button: if it does nothing after being clicked, the user may think the site is broken. But if it shows a spinner followed by a success message, the experience feels reassuring and complete.</p>
<p><strong>Example:</strong></p>
<p>The gif below demonstrates clear user feedback during a button interaction. When the "Submit" button is clicked, a loading spinner appears to show that the action is being processed, followed by a success message once the process is complete. This ensures users know their action is registered and provides reassurance, preventing confusion or uncertainty.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757076411317/42e940e9-cf3d-4155-be43-94217f135468.gif" alt="Clear Feedback and Interaction example gif video" class="image--center mx-auto" width="1920" height="920" loading="lazy"></p>
<h3 id="heading-designing-with-the-user-in-mind">Designing with the User in Mind</h3>
<p>At the heart of UI/UX is empathy. A developer must always ask: “Is this intuitive for someone seeing it or using it for the first time?”</p>
<p>Placing the search bar at the top of a page makes sense because that’s where users expect it. Hiding it deep inside a menu might seem clever, but it often frustrates users. The best interfaces are designed around user expectations, not developer preferences.</p>
<p><strong>Example:</strong></p>
<p>The GIF image below demonstrates the impact of user-centered design in UI/UX. The "Good UX" example places the search bar at the top of the page, making it immediately visible and easy for users to find. In contrast, the "Bad UX" example hides the search bar inside a menu, which makes it harder to access and can frustrate users.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757076951279/47559d0f-7603-457f-be8f-f99797cf6ade.gif" alt="Designing with the User in Mind example gif video" class="image--center mx-auto" width="1920" height="904" loading="lazy"></p>
<h2 id="heading-benefits-of-uiux-knowledge-for-front-end-developers">Benefits of UI/UX Knowledge for Front-End Developers</h2>
<p>Front-end developers often see themselves as the builders, the ones who transform design files into functional web pages and applications. But those who also understand UI and UX principles unlock a significant advantage. Their work doesn’t just “look like the design” – it feels intuitive, efficient, and enjoyable for the end user.</p>
<h3 id="heading-bridging-the-gap-between-design-and-code">Bridging the Gap Between Design and Code</h3>
<p>Designers create mockups with a vision in mind, but many of those details get lost when moving into code. A developer with UI/UX knowledge understands why spacing is generous, why a button changes color on hover, or <em>why</em> a particular layout improves navigation.</p>
<p>Instead of copying pixels mechanically, they preserve the intent of the design. Without this understanding, designs may look “close enough” but feel awkward in practice.</p>
<p><strong>Example:</strong></p>
<p>The image below compares bad versus good UI design. The left side shows a messy header with mismatched buttons that have different shapes, sizes, and colors, demonstrating poor design consistency. The right side shows the same header redesigned properly with uniform blue buttons that are consistently sized and aligned, creating a much cleaner and more professional appearance.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757050797636/a9559e8b-ef9e-4aa9-9c79-08b1a986e0a2.png" alt="Bridging the Gap Between Design and Code example imge" class="image--center mx-auto" width="1895" height="331" loading="lazy"></p>
<h3 id="heading-building-for-usability-not-just-functionality">Building for Usability, Not Just Functionality</h3>
<p>Functionality answers the question: <em>Does this work?</em> Usability asks: <em>Does this feel natural to use?</em> Developers who understand UI/UX instinctively add helpful touches like loading indicators, clear error messages, and accessible labels.</p>
<p>Without this mindset, users encounter confusing flows, tiny buttons, hidden actions, or forms with no feedback. The product works, technically, but it doesn’t <em>work well.</em></p>
<p><strong>Example:</strong></p>
<p>The image below shows a login form comparison between poor and improved UX design. The left side demonstrates bad UX with empty input fields that have no placeholder text, a bland gray login button, and unclear help text that says "Forgot password? Not obvious." The right side shows the improved version with helpful placeholder text like "Enter your email" and "Enter your password," a prominent blue login button that's more visually appealing, and clearer, actionable help text that says "Forgot password? Click here to reset."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757050840166/34aacf33-3dec-446f-bada-22b8a9c14f2d.png" alt="Building for Usability, Not Just Functionality example image" class="image--center mx-auto" width="1903" height="493" loading="lazy"></p>
<h3 id="heading-faster-collaboration-and-fewer-iterations">Faster Collaboration and Fewer Iterations</h3>
<p>When developers understand design language, they can talk specifics, “The spacing here should follow the 8px grid,” instead of vague complaints like “This looks off.” This cuts down on revisions, keeps teams aligned, and builds trust between designers and developers.</p>
<p>Without this shared language, collaboration slows. Misinterpretations multiply, feedback loops drag on, and projects risk missing deadlines.</p>
<h3 id="heading-stronger-problem-solving-and-adaptability">Stronger Problem-Solving and Adaptability</h3>
<p>Projects rarely stick to the original plan. Edge cases and new requirements always emerge. Developers with UI/UX insight can make smart, user-centered choices on the fly, selecting layouts, typography scales, or navigation patterns that enhance the experience.</p>
<p>Without this knowledge, fixes tend to be purely technical, functional, but clunky, leaving users to struggle through awkward flows.</p>
<p><strong>Example:</strong></p>
<p>The image below demonstrates dashboard navigation design. The left side shows a "Cluttered Dashboard" with nine separate menu items scattered randomly (Users, Settings, Reports, Analytics, Notifications, Billing, Integrations, Logs, Support), making it overwhelming and difficult to navigate. The right side shows a "UX-Improved Dashboard" that organizes the same options into three logical categories: "User Management" (Users, Roles), "Settings" (General, Billing, Integrations), and "Analytics &amp; Support" (Reports, Analytics, Support). The improved version reduces cognitive load by grouping related functions, making the interface much easier to understand and navigate.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757050891751/50920984-c330-40b3-957b-6b56b18eaea0.png" alt="Enhancing Problem-Solving Skills example image" class="image--center mx-auto" width="1899" height="497" loading="lazy"></p>
<h3 id="heading-competitive-and-professional-advantage">Competitive and Professional Advantage</h3>
<p>In today’s job market, many developers can code a responsive layout. What makes someone stand out is their ability to think about both aesthetics and usability. Employers and clients value these hybrid professionals who bridge two traditionally separate worlds.</p>
<p>Without UI/UX awareness, developers risk blending into the crowd. Their work may be solid, but it lacks the polish and empathy that win user trust and give products a competitive edge.</p>
<h3 id="heading-missed-opportunities-for-user-trust">Missed Opportunities for User Trust</h3>
<p>Users form judgments about applications within seconds. If the interface looks clunky or confusing, it signals to them that the product may not be reliable. Developers who overlook UI/UX miss this psychological layer of trust. An intuitive, visually consistent interface not only helps users accomplish their tasks but also assures them that the product is worth relying on.</p>
<p><strong>Without UI/UX, developers often:</strong></p>
<ul>
<li><p>Push out features that confuse rather than guide.</p>
</li>
<li><p>Create inconsistent layouts that erode confidence.</p>
</li>
<li><p>Overwhelm users with unnecessary complexity.</p>
</li>
</ul>
<p><strong>Example:</strong></p>
<p>The image below illustrates how UI/UX design affects user trust. The left side shows "Missed Trust" with a poorly designed interface that has inconsistent styling - a basic input field, a small gray "Continue" button, and a confusing pink "Next Step?" button that creates uncertainty about what action to take. The right side shows "Built for Trust" with a clean, professional design featuring a well-styled input field with placeholder text and a prominent, clear "Continue" button that gives users confidence in their actions.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757152308435/754f5bf3-0fa0-4bf4-b725-634516fbe487.png" alt="Missed Opportunities for User Trust example image" class="image--center mx-auto" width="1054" height="354" loading="lazy"></p>
<h3 id="heading-inclusivity-and-accessibility">Inclusivity and Accessibility</h3>
<p>Accessibility is a core part of good UX. Developers with this awareness naturally consider screen reader support, keyboard navigation, and high-contrast modes. This ensures products welcome <em>all</em> users.</p>
<p>Without it, developers unintentionally exclude large groups of people and may even face compliance issues. The result isn’t just a less usable product, it fails to respect and include its audience.</p>
<h2 id="heading-how-developers-can-learn-uiux">How Developers Can Learn UI/UX</h2>
<p>For a long time, developers and designers were seen as working on two separate islands. Developers wrote code, designers crafted interfaces, and the two worlds met somewhere in the middle. But today, the line is much blurrier. Employers and teams increasingly expect developers to have at least a foundational understanding of UI/UX.</p>
<p>The good news is that UI/UX isn’t a mysterious art that only designers can master. Developers can learn it step by step, much like they learned programming languages or frameworks. It requires observation, practice, and empathy for the end user.</p>
<h3 id="heading-start-with-observation">Start with Observation</h3>
<p>One of the simplest ways to begin learning UI/UX is by observing products you already use every day. Pay attention to how you navigate your favorite apps or websites. Notice how buttons are placed, how forms are structured, or how feedback is given when you complete an action. Ask yourself: <em>Why does this feel easy to use? Why does this frustrate me?</em></p>
<p>Observation builds awareness. Once you start noticing design decisions in your daily digital life, you’ll begin to understand the invisible rules that shape user experiences.</p>
<h3 id="heading-understand-the-basics-of-ux-principles">Understand the Basics of UX Principles</h3>
<p>Before diving deep, you should familiarize yourself with some core UX principles. These aren’t abstract theories but practical guidelines for making products usable. To review them:</p>
<ul>
<li><p><strong>Consistency</strong>: Users expect familiar patterns. A back button should always go back, not do something unpredictable.</p>
</li>
<li><p><strong>Clarity</strong>: Every element should serve a purpose. If it confuses, it doesn’t belong.</p>
</li>
<li><p><strong>Feedback</strong>: Users need to know when an action has been registered, whether it’s a loading spinner, a success message, or an error prompt.</p>
</li>
<li><p><strong>Accessibility</strong>: Interfaces should be usable by everyone, including people with disabilities.</p>
</li>
</ul>
<p>Learning these basics gives you a strong foundation for evaluating and improving your work.</p>
<h3 id="heading-practice-through-side-projects">Practice Through Side Projects</h3>
<p>Theory alone won’t help – as a dev, you need to practice. A simple way to start is by taking small side projects and focusing not just on functionality but also on experience. Build a to-do app, for instance, but pay attention to how tasks are added, how the interface responds, and how information is displayed.</p>
<p>Treat each project as a learning ground for experimenting with usability, not just coding. You’ll begin to notice where users get stuck and how small tweaks can create big improvements.</p>
<h3 id="heading-learn-design-tools-but-dont-get-overwhelmed">Learn Design Tools, But Don’t Get Overwhelmed</h3>
<p>You don’t need to become a master of Figma or Sketch overnight, but learning the basics of design tools can help you bridge the gap between development and design. Being able to open a Figma file, understand layout grids, or inspect spacing gives you a significant advantage when collaborating with designers.</p>
<p>The goal isn’t to replace designers but to communicate better with them and reduce misinterpretations.</p>
<h3 id="heading-follow-real-world-uiux-examples">Follow Real-World UI/UX Examples</h3>
<p>Another powerful way to learn is by studying existing products. Look at how popular apps like Spotify, Airbnb, or Notion structure their interfaces. Read UX case studies where designers explain their decisions. These real-world examples give context to abstract principles and help developers see how theory translates into practice.</p>
<h3 id="heading-seek-feedback-from-designers-and-users">Seek Feedback from Designers and Users</h3>
<p>Learning UI/UX isn’t just about self-study. It’s also about listening. Share your projects with others, especially designers or actual users, and ask for feedback. What felt easy? What was confusing? As a developer, you may overlook usability issues because you’re too close to the code. External feedback helps you see the product with fresh eyes.</p>
<h3 id="heading-build-empathy-through-testing">Build Empathy Through Testing</h3>
<p>Nothing teaches UI/UX faster than watching someone use your product. Conduct informal usability tests with friends, colleagues, or even strangers. Observe where they hesitate, where they click instinctively, and where they get frustrated. This direct exposure helps developers understand that design isn’t just about what looks good, it’s about what feels natural in practice.</p>
<h3 id="heading-keep-learning-from-design-communities">Keep Learning from Design Communities</h3>
<p>UI/UX is a constantly evolving field. You can grow by engaging with design communities, reading blogs, or following design challenges. Platforms like Dribbble, Behance, and UX Collective provide inspiration and practical lessons. Over time, this exposure sharpens your design instincts and keeps you aligned with industry trends.</p>
<h3 id="heading-some-helpful-resources-to-learn-uiux">Some Helpful Resources to Learn UI/UX:</h3>
<p>📚 <strong>Books:</strong></p>
<ul>
<li><p><strong>Refactoring UI (Adam Wathan &amp; Steve Schoger)</strong>: <a target="_blank" href="https://refactoringui.com/book">https://refactoringui.com/book</a> (I’ve read this book, and the feedback is awesome)</p>
</li>
<li><p><strong>The Design of Everyday Things (Don Norman)</strong>: <a target="_blank" href="https://jnd.org/the-design-of-everyday-things/">https://jnd.org/the-design-of-everyday-things/</a></p>
</li>
<li><p><strong>Laws of UX (Jon Yablonski)</strong>: <a target="_blank" href="https://lawsofux.com/">https://lawsofux.com/</a></p>
</li>
</ul>
<p><strong>🌐 Blogs &amp; Guides:</strong></p>
<ul>
<li><p><strong>Smashing Magazine</strong>: <a target="_blank" href="https://www.smashingmagazine.com/category/uxdesign/">https://www.smashingmagazine.com/category/uxdesign/</a></p>
</li>
<li><p><strong>UX/UI Design Tutorial</strong>: <a target="_blank" href="https://www.freecodecamp.org/news/ui-ux-design-tutorial-from-zero-to-hero-with-wireframe-prototype-figma/">https://www.freecodecamp.org/news/ui-ux-design-tutorial-from-zero-to-hero-with-wireframe-prototype-figma/</a></p>
</li>
<li><p><strong>UX Collective</strong>: <a target="_blank" href="https://uxdesign.cc/">https://uxdesign.cc/</a></p>
</li>
<li><p><strong>Nielsen Norman Group (NNG)</strong>: <a target="_blank" href="https://www.nngroup.com/articles/">https://www.nngroup.com/articles/</a></p>
</li>
</ul>
<p>🎥 <strong>YouTube:</strong></p>
<ul>
<li><p><strong>DesignCourse (Gary Simon)</strong>: <a target="_blank" href="https://www.youtube.com/c/DesignCourse">https://www.youtube.com/c/DesignCourse</a></p>
</li>
<li><p><strong>Flux Academy</strong>: <a target="_blank" href="https://www.youtube.com/c/FluxWithRanSegall">https://www.youtube.com/c/FluxWithRanSegall</a></p>
</li>
<li><p><strong>Steve Schoger (Design Tips)</strong>: <a target="_blank" href="https://www.youtube.com/c/SteveSchoger">https://www.youtube.com/c/SteveSchoger</a></p>
</li>
<li><p><strong>Kevin Powell</strong>: <a target="_blank" href="https://www.youtube.com/kepowob">https://www.youtube.com/kepowob</a></p>
</li>
</ul>
<p><strong>🛠️ Tools &amp; Practice:</strong></p>
<ul>
<li><p><strong>Figma</strong>: <a target="_blank" href="https://www.figma.com/">https://www.figma.com/</a></p>
</li>
<li><p><strong>Dribbble</strong>: <a target="_blank" href="https://dribbble.com/">https://dribbble.com/</a></p>
</li>
<li><p><strong>Behance:</strong> <a target="_blank" href="https://www.behance.net/">https://www.behance.net/</a></p>
</li>
<li><p><strong>Mobbin</strong>: <a target="_blank" href="https://mobbin.com/">https://mobbin.com/</a></p>
</li>
</ul>
<h2 id="heading-what-to-keep-in-mind-about-uxui">What to Keep in Mind about UX/UI</h2>
<p>Some of these tips might feel counterintuitive to anyone who lives in code all day. Yet they explain why users behave the way they do, and why some products succeed where others fail. Here are some of the weird but true things about UI/UX that every developer should know.</p>
<h3 id="heading-users-rarely-read-they-scan">Users Rarely Read. They Scan….</h3>
<p>You may spend hours ensuring error messages, tooltips, or onboarding text are written clearly. But here’s the strange reality: most users don’t actually <em>read</em> them. They scan. Their eyes dart across headings, buttons, and visuals, often skipping over entire paragraphs.</p>
<p>This means your beautifully worded instructions may never be read. Instead, design needs to be built around <strong>visual cues and intuitive flows</strong> that guide users without relying on text.</p>
<p><strong>Example:</strong></p>
<p>The image contrasts verbose versus concise user instructions. The left side shows "Wordy Instructions" with a lengthy paragraph that overwhelms users with unnecessary details about clicking settings, navigating tabs, selecting preferences, and remembering to save changes. The right side demonstrates "Quick Setup" with a clean, numbered three-step process: "1. Open Settings, 2. Choose Preferences, 3. Click Save" followed by a clear "Save &amp; Continue" button. The improved version eliminates cognitive overload by presenting the same information in a scannable, actionable format that's much easier for users to follow and complete successfully.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757151919813/47bb5556-954d-43f0-ba10-98c9ebedcb7a.png" alt="Users Rarely Read. They Scan example image" class="image--center mx-auto" width="1041" height="326" loading="lazy"></p>
<h3 id="heading-a-buttons-size-can-outweigh-its-functionality">A Button’s Size Can Outweigh Its Functionality</h3>
<p>From a developer’s perspective, a button either triggers a function or it doesn’t. But in UX, the <strong>size, placement, and color</strong> of that button can matter more than the functionality behind it.</p>
<p>For example, two buttons may perform the same action, but if one is too small, hidden, or styled poorly, users might not notice it at all. The function exists in code, but in practice, it doesn’t exist for the user.</p>
<p><strong>Example:</strong></p>
<p>The image shows how button size and design affect usability. The left side has a small, gray "Save" button that's hard to notice and might be missed by users. The right side has the same "Save" button but larger, darker, and more prominent, making it much more likely that users will see and click it. Same function, but better design leads to better user interaction.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757151978289/c347439c-004a-4ca4-9a8c-8528ca48bc4c.png" alt="A Button’s Size Can Outweigh Its Functionality example image" class="image--center mx-auto" width="921" height="271" loading="lazy"></p>
<h3 id="heading-more-features-often-mean-less-usability">More Features Often Mean Less Usability</h3>
<p>It feels logical to think that giving users more options and features adds value. But weirdly, the opposite is often true. The more you add, the harder the product becomes to use.</p>
<p>This is called the <strong>paradox of choice</strong>: too many options create decision fatigue. Developers love shipping features, but good UX often means stripping things away until only the essentials remain.</p>
<h3 id="heading-ugly-designs-sometimes-work-better">“Ugly” Designs Sometimes Work Better</h3>
<p>Developers may assume a slick, modern UI always wins, but there are cases where “ugly” or “dated” designs perform better because they feel familiar and trustworthy. Think of Craigslist: it hasn’t changed much in decades, but people use it because it feels simple. This shows that usability can beat aesthetics when it comes to building trust.</p>
<p><strong>Example:</strong></p>
<p>The image illustrates that modern aesthetics don't always equal better usability. The left side shows a "Modern &amp; Slick" design with a sleek, dark "Explore" button that looks impressive and cutting-edge but might feel unfamiliar or untrustworthy to some users. The right side shows an "Old-School &amp; Familiar" design with a simple gray "Continue" button that appears outdated but feels trustworthy and familiar to users.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757152055106/b01d0abd-ef1a-42ac-9670-00601f27ef10.png" alt="“Ugly” Designs Sometimes Work Better example image" class="image--center mx-auto" width="1049" height="273" loading="lazy"></p>
<h3 id="heading-users-dont-think-like-developers">Users Don’t Think Like Developers</h3>
<p>This might be the weirdest truth of all: users don’t care about code efficiency, data structures, or the elegant architecture behind the app. They only care about what’s on their screen and whether it makes sense.</p>
<p>For example, a developer might proudly optimize a search algorithm, but if the search bar is hidden in a menu instead of being front and center, users will complain that “the app doesn’t have search.” The feature exists, but in their minds, it doesn’t.</p>
<p><strong>Example:</strong></p>
<p>The GIF image demonstrates the impact of user-centered design in UI/UX. The "Good UX" example places the search bar at the top of the page, making it immediately visible and easy for users to find. In contrast, the "Bad UX" example hides the search bar inside a menu, which makes it harder to access and can frustrate users.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757076951279/47559d0f-7603-457f-be8f-f99797cf6ade.gif" alt="Users Don’t Think Like Developers example gif video" class="image--center mx-auto" width="1920" height="904" loading="lazy"></p>
<h3 id="heading-small-delays-feel-bigger-than-they-are">Small Delays Feel Bigger Than They Are</h3>
<p>From a technical side, shaving 300 milliseconds off a request may not feel like a huge deal. But to a user, even slight delays feel magnified. A loading spinner that lingers for a second can feel like forever. UX research shows that anything beyond 2–3 seconds risks losing the user’s attention.</p>
<p>This means developers must think not only about raw performance but also about perceived performance using skeleton screens, micro-animations, or progress indicators to make waits feel shorter.</p>
<p><strong>Example:</strong></p>
<p>The image compares the loading experience design. The left side shows "Feels Slow" with just a basic spinner and "Loading..." text that makes even short delays feel endless to users. The right side shows "Feels Faster" using skeleton screens - gray placeholder blocks that mimic the content structure while loading, making the same delay feel much shorter because users can see that progress is happening and get a preview of what's coming.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757152098385/a764127e-7a34-4ac8-b04b-444f911b40b4.png" alt="Small Delays Feel Bigger Than They Are example image" class="image--center mx-auto" width="1045" height="338" loading="lazy"></p>
<h3 id="heading-accessibility-isnt-just-for-some-users">Accessibility Isn’t Just for “Some” Users</h3>
<p>Developers sometimes assume accessibility features (like screen reader support, keyboard navigation, or high-contrast modes) are niche concerns. But here’s the reality: everyone benefits.</p>
<p>Captions help people in noisy environments, large touch targets help on small screens, and good color contrast improves readability for everyone. Accessibility isn’t just about inclusivity – it’s about creating universally better experiences.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In modern web development, understanding UI/UX isn’t optional. It’s essential. Front-end developers aren’t just programmers – they are the architects of user experience. By learning design principles, developers improve collaboration, write smarter code, and create products that truly serve users.</p>
<p>Whether you’re just starting or already experienced, investing time in UI/UX knowledge will set you apart and open new opportunities. Explore design resources, collaborate closely with designers, and experiment with small design tasks in your projects.</p>
<p>At the end of the day, the best front-end developers don’t just write code, they create experiences users love.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write Better Names for Your Variables, Functions, and Classes – With Examples ]]>
                </title>
                <description>
                    <![CDATA[ Naming is one of the most important and challenging parts of writing clean, maintainable, and scalable code. A well-thought-out variable name, for example, can act as self-documenting code, saving time and effort in understanding the logic. But poorl... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-write-better-variable-names/</link>
                <guid isPermaLink="false">6750922cfe6f3f72caf2624a</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ variables ]]>
                    </category>
                
                    <category>
                        <![CDATA[ naming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Asfak Ahmed ]]>
                </dc:creator>
                <pubDate>Wed, 04 Dec 2024 17:32:28 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1733325693047/f3dff206-f0cf-47b0-9345-991b4d980d71.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Naming is one of the most important and challenging parts of writing clean, maintainable, and scalable code. A well-thought-out variable name, for example, can act as self-documenting code, saving time and effort in understanding the logic. But poorly chosen names, on the other hand, can lead to confusion and bugs.</p>
<p>This article will serve as a comprehensive guide on how to come up with meaningful names for class names, variables, and functions with examples and best practices.</p>
<h2 id="heading-why-does-naming-matter"><strong>Why Does Naming Matter?</strong></h2>
<ul>
<li><p><strong>Readability:</strong> Good names make your code intuitive and reduce the learning curve for others.</p>
</li>
<li><p><strong>Maintainability:</strong> It is easier to refactor or debug well-named code.</p>
</li>
<li><p><strong>Collaboration:</strong> Clear names improve team communication and productivity.</p>
</li>
<li><p><strong>Scalability:</strong> Meaningful names help keep large projects manageable.</p>
</li>
</ul>
<h2 id="heading-different-naming-convention-styles">Different Naming Convention Styles</h2>
<p>Different naming convention styles are crucial in improving code readability and maintainability across various programming languages.</p>
<p>Styles like camelCase, PascalCase, snake_case, and kebab-case are tailored to specific contexts and practices.</p>
<p><strong>camelCase</strong> is widely used for variables and functions, while <strong>PascalCase</strong> is preferred for classes. <strong>snake_case</strong> is a favorite in Python for its clarity, and <strong>kebab-case</strong> dominates CSS for HTML element styling.</p>
<p>Each style ensures consistency, making code intuitive for teams and future developers. Here’s a quick summary table of some popular naming conventions along with their use cases and examples:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Style</strong></td><td><strong>Example</strong></td><td><strong>Common Usage</strong></td></tr>
</thead>
<tbody>
<tr>
<td>camelCase</td><td><code>userName</code></td><td>Variables, functions, object properties</td></tr>
<tr>
<td>PascalCase</td><td><code>UserName</code></td><td>Classes, components, constructors</td></tr>
<tr>
<td>kebab-case</td><td><code>primary-button</code></td><td>CSS classes, HTML IDs, file names</td></tr>
<tr>
<td>snake_case</td><td><code>user_name</code></td><td>Variables, function names in Python</td></tr>
<tr>
<td>SCREAMING_SNAKE_CASE</td><td><code>MAX_CONNECTIONS</code></td><td>Constants</td></tr>
<tr>
<td>dot.case</td><td><code>config.file.path</code></td><td>Configurations, keys</td></tr>
<tr>
<td>Train-Case</td><td><code>Primary-Button</code></td><td>Titles rarely used</td></tr>
<tr>
<td>Hungarian Notation</td><td><code>bIsActive</code></td><td>Legacy code</td></tr>
<tr>
<td>UPPERCASE with Spaces</td><td><code>USER ACCOUNT DETAILS</code></td><td>Rare, mostly for old-style documentation</td></tr>
<tr>
<td>Flatcase</td><td><code>username</code></td><td>Minimalist, filenames, identifiers</td></tr>
</tbody>
</table>
</div><h3 id="heading-how-to-choose-the-right-style"><strong>How to Choose the Right Style</strong></h3>
<ol>
<li><p><strong>Language-Specific:</strong> Follow the conventions of your programming language or framework. For example:</p>
<ul>
<li><p>JavaScript: <code>camelCase</code> for variables and functions, <code>PascalCase</code> for components.</p>
</li>
<li><p>Python: <code>snake_case</code> for variables and functions.</p>
</li>
<li><p>CSS/HTML: <code>kebab-case</code> for class names and IDs.</p>
</li>
</ul>
</li>
<li><p><strong>Team or Project Standards:</strong> Consistency is key. Use the agreed style for your team/project.</p>
</li>
<li><p><strong>Purpose-Specific:</strong> Use naming styles that best represent the entity being named (for example, constants in <code>SCREAMING_SNAKE_CASE</code>).</p>
</li>
</ol>
<h2 id="heading-general-naming-guidelines"><strong>General Naming Guidelines</strong></h2>
<p>Before diving into specific naming conventions for class names, variables, and functions, let’s explore some universal principles:</p>
<ol>
<li><p><strong>Be descriptive and concise:</strong> Names should convey the purpose or role of the variable/function/etc:</p>
<pre><code class="lang-javascript"> <span class="hljs-comment">// Bad</span>
 <span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;

 <span class="hljs-comment">// Good</span>
 <span class="hljs-keyword">let</span> maxUsersAllowed = <span class="hljs-number">10</span>;
</code></pre>
</li>
<li><p><strong>Avoid cryptic abbreviations</strong> that might be hard for other devs to understand (or even your future self):</p>
<pre><code class="lang-javascript"> <span class="hljs-comment">// Bad</span>
 <span class="hljs-keyword">let</span> usrNm = <span class="hljs-string">"John"</span>;

 <span class="hljs-comment">// Good</span>
 <span class="hljs-keyword">let</span> userName = <span class="hljs-string">"John"</span>;
</code></pre>
</li>
<li><p><strong>Use consistent naming conventions:</strong> Choose a naming style (camelCase, PascalCase, kebab-case, snake_case) and stick with it throughout your project.</p>
</li>
<li><p><strong>Avoid reserved keywords or confusing names:</strong></p>
<pre><code class="lang-javascript"> <span class="hljs-comment">// Bad</span>
 <span class="hljs-keyword">let</span> <span class="hljs-keyword">let</span> = <span class="hljs-number">5</span>;

 <span class="hljs-comment">// Good</span>
 <span class="hljs-keyword">let</span> variableName = <span class="hljs-number">5</span>;
</code></pre>
</li>
</ol>
<p>Alright, now that we’ve covered the basis, lets dig deeper into some helpful naming conventions.</p>
<h2 id="heading-how-to-create-good-class-names"><strong>How to Create Good Class Names</strong></h2>
<p>Class names define the visual or structural behavior of elements in your application. Writing clear class names ensures your HTML and CSS are easy to understand and maintain.</p>
<h3 id="heading-1-use-descriptive-names"><strong>1. Use Descriptive Names</strong></h3>
<p>Class names should describe the purpose of the element, not its appearance.</p>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- Bad --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"red-button"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-comment">&lt;!-- Good --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"primary-button"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-2-follow-the-bem-block-element-modifier-methodology"><strong>2. Follow the BEM (Block-Element-Modifier) Methodology</strong></h3>
<p>BEM is a popular convention for writing scalable and maintainable CSS. It separates components into:</p>
<ul>
<li><p><strong>Block:</strong> Represents the component (for example, <code>card</code>).</p>
</li>
<li><p><strong>Element:</strong> Represents child elements of the block (for example, <code>card__title</code>).</p>
</li>
<li><p><strong>Modifier:</strong> Represents variations of the block or element (for example, <code>card__title--highlighted</code>).</p>
</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__title card__title--highlighted"</span>&gt;</span>Welcome<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card__description"</span>&gt;</span>This is a card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-3-use-kebab-case"><strong>3. Use kebab-case</strong></h3>
<p>CSS class names are traditionally written in kebab-case for better readability.</p>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- Bad --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"primaryButton"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-comment">&lt;!-- Good --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"primary-button"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-how-to-create-good-variable-names"><strong>How to Create Good Variable Names</strong></h2>
<p>Variables hold data and should have meaningful names that describe what they represent.</p>
<h3 id="heading-1-use-nouns-for-variables"><strong>1. Use Nouns for Variables</strong></h3>
<p>Variables are typically nouns because they represent entities or data.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Bad</span>
<span class="hljs-keyword">let</span> a = <span class="hljs-string">"John"</span>;

<span class="hljs-comment">// Good</span>
<span class="hljs-keyword">let</span> userName = <span class="hljs-string">"John"</span>;
</code></pre>
<h3 id="heading-2-use-prefixes-to-add-context"><strong>2. Use Prefixes to Add Context</strong></h3>
<p>Adding prefixes helps clarify the type or purpose of a variable:</p>
<ul>
<li><p><strong>Boolean:</strong> <code>is</code>, <code>has</code>, <code>can</code></p>
</li>
<li><p><strong>Numbers:</strong> <code>max</code>, <code>min</code>, <code>total</code></p>
</li>
<li><p><strong>Arrays:</strong> Use plural forms (for example, <code>users</code>, <code>items</code>).</p>
</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> isUserLoggedIn = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">const</span> maxUploadLimit = <span class="hljs-number">5</span>; <span class="hljs-comment">// MB</span>
<span class="hljs-keyword">const</span> usersList = [<span class="hljs-string">"John"</span>, <span class="hljs-string">"Jane"</span>];
</code></pre>
<h3 id="heading-3-avoid-generic-names"><strong>3. Avoid Generic Names</strong></h3>
<p>Avoid names like <code>data</code>, <code>value</code>, or <code>item</code> unless they’re necessary.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Bad</span>
<span class="hljs-keyword">let</span> data = <span class="hljs-number">42</span>;

<span class="hljs-comment">// Good</span>
<span class="hljs-keyword">let</span> userAge = <span class="hljs-number">42</span>;
</code></pre>
<h2 id="heading-how-to-create-good-function-names"><strong>How to Create Good Function Names</strong></h2>
<p>Functions perform actions, so their names should reflect the operation or process they execute.</p>
<h3 id="heading-1-use-verbs-for-functions"><strong>1. Use Verbs for Functions</strong></h3>
<p>Functions are action-oriented, so their names should begin with a verb:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Bad</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">userData</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// ...</span>
}

<span class="hljs-comment">// Good</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchUserData</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<h3 id="heading-2-be-specific-about-functionality"><strong>2. Be Specific About Functionality</strong></h3>
<p>Function names should indicate what they do.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Bad</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// ...</span>
}

<span class="hljs-comment">// Good</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleFormSubmit</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<h3 id="heading-3-use-prefixes-for-intent"><strong>3. Use Prefixes for Intent</strong></h3>
<ul>
<li><p>For event handlers: <code>handle</code>, <code>on</code></p>
</li>
<li><p>For utilities: <code>calculate</code>, <code>convert</code>, <code>format</code></p>
</li>
<li><p>For fetch operations: <code>fetch</code>, <code>get</code>, <code>load</code></p>
</li>
<li><p>For setters and getters: <code>set</code>, <code>get</code></p>
</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleButtonClick</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Button clicked!"</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateDiscount</span>(<span class="hljs-params">price, discountPercentage</span>) </span>{
  <span class="hljs-keyword">return</span> price * (discountPercentage / <span class="hljs-number">100</span>);
}
</code></pre>
<h2 id="heading-how-to-know-if-a-name-is-good-for-a-variable-function-or-class">How to Know if a Na<strong>me is Good for a Variable, Function, or Class</strong></h2>
<p>To understand if a name is good for a variable, function, or class, evaluating it using several key principles is important. Here’s a guide to help you decide whether a name is appropriate and meaningful in your programming context:</p>
<h3 id="heading-1-does-it-represent-the-purpose">1. <strong>Does It Represent the Purpose?</strong></h3>
<p><strong>Purpose-driven names</strong> are the most important characteristic of good naming. A name should immediately tell you what the variable, function, or class represents or does without needing to read additional comments or documentation.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask yourself: "When I read this name, can I immediately understand its purpose?"</p>
<p><strong>Example:</strong></p>
<ul>
<li><code>userAge</code> is better than <code>a</code> because <code>userAge</code> tells you what the variable represents, whereas <code>a</code> is too ambiguous.</li>
</ul>
<h3 id="heading-2-is-it-specific-enough">2. <strong>Is It Specific Enough?</strong></h3>
<p>The name should be <strong>specific enough</strong> to reflect the exact role of the entity in your code. Overly generic names like <code>data</code> or <code>temp</code> can be confusing because they don’t provide enough context.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Is this name specific to what this variable, function, or class represents in my application?"</p>
<p><strong>Example:</strong></p>
<ul>
<li><code>calculateTaxAmount()</code> is better than <code>calculate()</code> because it’s clear what the function is calculating.</li>
</ul>
<h3 id="heading-3-does-it-follow-a-consistent-naming-convention">3. <strong>Does It Follow a Consistent Naming Convention?</strong></h3>
<p><strong>Consistency</strong> in naming conventions is vital. When all team members follow the same conventions, the code is easier to understand and navigate.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Is this name consistent with the naming conventions used in the rest of the project?" Follow project guidelines such as:</p>
<ul>
<li><p><code>camelCase</code> for variables and functions (e.g., <code>userAge</code>)</p>
</li>
<li><p><code>PascalCase</code> for classes (e.g., <code>UserProfile</code>)</p>
</li>
<li><p><code>UPPERCASE_SNAKE_CASE</code> for constants (e.g., <code>MAX_USERS</code>)</p>
</li>
</ul>
<p><strong>Example:</strong></p>
<ul>
<li>If your team follows <code>camelCase</code>, <code>userData</code> is better than <code>UserData</code>.</li>
</ul>
<h3 id="heading-4-does-it-avoid-ambiguity">4. <strong>Does it Avoid Ambiguity?</strong></h3>
<p>A good name <strong>eliminates ambiguity</strong>. It should not be open to multiple interpretations. If it can mean different things in different contexts, it will lead to confusion.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Could someone unfamiliar with the codebase misinterpret what this name refers to?"</p>
<p><strong>Example:</strong></p>
<ul>
<li>Instead of naming a boolean <code>isValid</code>, use <code>isUserLoggedIn</code> or <code>isEmailVerified</code> to make it clearer what is being checked.</li>
</ul>
<h3 id="heading-5-is-it-easy-to-read-and-pronounce">5. <strong>Is It Easy to Read and Pronounce?</strong></h3>
<p>While not strictly necessary, <strong>ease of reading and pronunciation</strong> can improve the overall readability and maintainability of your code.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Is this name easy to read aloud, and can I understand it at a glance?"</p>
<p>Avoid long names, and use common abbreviations only when they are widely accepted.</p>
<p><strong>Example:</strong></p>
<ul>
<li><code>maxRetries</code> is better than <code>maximumNumberOfAttemptsToReconnect</code>.</li>
</ul>
<h3 id="heading-6-does-it-avoid-redundancy">6. <strong>Does It Avoid Redundancy?</strong></h3>
<p><strong>Avoid redundancy</strong> in names. Don’t repeat information that is already implied or described by the context.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Am I repeating information that is already clear from the surrounding context?"</p>
<p><strong>Example:</strong></p>
<ul>
<li>If you have a class named <code>User</code>, naming a method <code>userGetData()</code> is redundant. Instead, use <code>getData()</code>.</li>
</ul>
<h3 id="heading-7-is-it-self-documenting">7. <strong>Is It Self-Documenting?</strong></h3>
<p>The best names <strong>document themselves</strong>. Good names reduce the need for additional comments or explanations.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Does this name fully describe the variable, function, or class without requiring a comment to explain what it does?"</p>
<p><strong>Example:</strong></p>
<ul>
<li><code>calculateTotalPrice</code> is self-explanatory, so there’s no need for an additional comment like “This function calculates the total price after discount.”</li>
</ul>
<h3 id="heading-8-is-it-contextual-and-relevant-to-the-domain">8. <strong>Is It Contextual and Relevant to the Domain?</strong></h3>
<p>The name should fit within the context of your project and its domain. For example, naming conventions for a web application may differ from those for a mobile app or a machine learning model.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Is this name aligned with the domain and context of my project?"</p>
<p>If you’re working in a specific domain (for example, finance, health, gaming), use domain-specific terms that are easily recognizable.</p>
<p><strong>Example:</strong></p>
<ul>
<li>In a gaming app, <code>healthPoints</code> is more appropriate than <code>hp</code>, as it reflects its meaning.</li>
</ul>
<h3 id="heading-9-is-it-future-proof">9. <strong>Is It Future-Proof?</strong></h3>
<p>Think about how your code will evolve. Names should be flexible enough to accommodate future changes without requiring refactoring.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Will this name still make sense if the functionality changes or the project grows?"</p>
<p><strong>Example:</strong></p>
<ul>
<li><code>userInfo</code> could become outdated if the data structure changes. It’s better to use <code>userProfile</code> if you expect more fields to be added.</li>
</ul>
<h3 id="heading-10-does-it-avoid-magic-numbers-and-hard-coded-values">10. <strong>Does It Avoid Magic Numbers and Hard-Coded Values?</strong></h3>
<p><strong>Magic numbers</strong> (numbers with unclear meaning) should be avoided in favor of named constants.</p>
<p><strong>How to Assess:</strong></p>
<p>Ask: "Does this name represent a meaningful constant, or is it just a raw number?"</p>
<p><strong>Example:</strong></p>
<ul>
<li>Instead of using <code>1000</code>, use a constant like <code>MAX_FILE_SIZE</code> to explain the meaning behind the number.</li>
</ul>
<h2 id="heading-practical-examples"><strong>Practical Examples</strong></h2>
<h3 id="heading-css-example"><strong>CSS Example</strong></h3>
<p>The following CSS example demonstrates how to apply <strong>BEM (Block-Element-Modifier)</strong> naming conventions to maintain a structured and scalable class hierarchy in your stylesheet:</p>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- HTML --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar__list"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar__item navbar__item--active"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar__item"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar__item"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-comment">/* CSS */</span>
<span class="hljs-selector-class">.navbar</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#333</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}

<span class="hljs-selector-class">.navbar__list</span> {
  <span class="hljs-attribute">list-style</span>: none;
}

<span class="hljs-selector-class">.navbar__item</span> {
  <span class="hljs-attribute">display</span>: inline-block;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}

<span class="hljs-selector-class">.navbar__item--active</span> {
  <span class="hljs-attribute">color</span>: orange;
}
</code></pre>
<p>Here’s what’s going on in this code:</p>
<ul>
<li><p><strong>BEM Naming</strong>: <code>navbar</code> is the <strong>Block</strong>, representing the main navigation component.</p>
</li>
<li><p><code>navbar__list</code> is the <strong>Element</strong>, a child of the block, representing the list of navigation items.</p>
</li>
<li><p><code>navbar__item</code> is another <strong>Element</strong> representing individual list items.</p>
</li>
<li><p><code>navbar__item--active</code> is a <strong>Modifier</strong>, used to highlight the active menu item.<br>  This approach makes it easy to understand relationships and roles within the HTML and CSS, supporting modular and reusable styles.</p>
</li>
</ul>
<h3 id="heading-javascript-example"><strong>JavaScript Example</strong></h3>
<p>This JavaScript example shows how to use meaningful and consistent naming conventions for variables and functions to make the code self-explanatory:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Variables</span>
<span class="hljs-keyword">let</span> isUserLoggedIn = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">const</span> maxAllowedItems = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Functions</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchUserDetails</span>(<span class="hljs-params">userId</span>) </span>{
  <span class="hljs-comment">// Fetch user data from the API</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateTotalPrice</span>(<span class="hljs-params">cartItems</span>) </span>{
  <span class="hljs-keyword">return</span> cartItems.reduce(<span class="hljs-function">(<span class="hljs-params">total, item</span>) =&gt;</span> total + item.price, <span class="hljs-number">0</span>);
}
</code></pre>
<p>Here’s what’s going on in the code:</p>
<ul>
<li><p><strong>Variables</strong>:</p>
<ul>
<li><p><code>isUserLoggedIn</code>: A boolean variable named to clearly indicate its purpose. Prefixing with <code>is</code> helps identify it as a boolean.</p>
</li>
<li><p><code>maxAllowedItems</code>: A constant with an uppercase <code>max</code> prefix shows it's a limit, making its intent clear.</p>
</li>
</ul>
</li>
<li><p><strong>Functions</strong>:</p>
<ul>
<li><p><code>fetchUserDetails(userId)</code>: The name reflects the purpose of the function, that is retrieving user details. The parameter <code>userId</code> is descriptive and avoids ambiguity.</p>
</li>
<li><p><code>calculateTotalPrice(cartItems)</code>: The function name explicitly states the action performed. The <code>cartItems</code> parameter is contextually relevant to the e-commerce domain.</p>
</li>
</ul>
</li>
</ul>
<p><strong>Why It’s Good</strong>: These conventions ensure the code is readable and intuitive, reducing the cognitive load for other developers working on the same project.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Meaningful naming is both an important convention and an art form that significantly impacts your code's readability and maintainability.</p>
<p>Try to follow these basic principles:</p>
<ul>
<li><p>Use descriptive, concise names.</p>
</li>
<li><p>Adhere to consistent conventions like BEM for class names and camelCase for variables and functions.</p>
</li>
<li><p>Use prefixes to add context and clarity.</p>
</li>
</ul>
<p>These and the other tips we’ve discussed here will make your code a joy to work with, whether you revisit it months later or collaborate with a team. Start applying these tips today, and watch your code quality soar.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Improve Your Front-End Development Workflow with the ZenUI Library ]]>
                </title>
                <description>
                    <![CDATA[ Hello everyone! In this guide, you’ll learn about the powerful ZenUI Library. It’s a comprehensive, free collection of UI components and templates designed to enhance your development workflow and elevate your projects. Whether you’re a developer loo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/improve-front-end-development-workflow-with-zenui-library/</link>
                <guid isPermaLink="false">66fd9575df00ebc41166c6af</guid>
                
                    <category>
                        <![CDATA[ zenui library ]]>
                    </category>
                
                    <category>
                        <![CDATA[ zenui ]]>
                    </category>
                
                    <category>
                        <![CDATA[ UI library ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Asfak Ahmed ]]>
                </dc:creator>
                <pubDate>Wed, 02 Oct 2024 18:48:21 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1727705676968/5e8a0836-8f8b-4ce3-a2fb-d98b2bd1eabe.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Hello everyone! In this guide, you’ll learn about the powerful <a target="_blank" href="https://zenui.net"><strong>ZenUI Library</strong></a>. It’s a comprehensive, free collection of UI components and templates designed to enhance your development workflow and elevate your projects.</p>
<p>Whether you’re a developer looking to fast-track your UI design or a business that needs sleek, modern templates, ZenUI Library has everything you need to build beautiful websites with minimal effort.</p>
<h3 id="heading-table-of-contents"><strong>Table of Contents:</strong></h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-why-use-zenui">Why Use ZenUI</a>?</p>
</li>
<li><p><a class="post-section-overview" href="#heading-zenui-features">ZenUI Features</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-tailwind-css-integration-and-framework-compatibility">Tailwind CSS Integration and Framework Compatibility</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-start-using-zenui">How to Start Using ZenUI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-customize-zenui-components">How to Customize ZenUI Components</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-zenui-website-templates">How to Use ZenUI Website Templates</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-extract-color-from-an-image">How to Extract Color from an Image</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-customize-any-icon-in-the-zenui-library">How to Customize Any Icon in the ZenUI Library</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-use-zenui">Why Use ZenUI?</h2>
<p>ZenUI was born out of the need for clean, customizable, and reusable components that you can easily integrate into various projects.</p>
<p>Unlike other libraries, ZenUI focuses on providing a <strong>tailored</strong> collection of components, ensuring your projects are unique and highly functional.</p>
<p>You can visit their website to learn more: <a target="_blank" href="https://zenui.net">https://zenui.net</a>.</p>
<h3 id="heading-key-zenui-benefits">Key ZenUI Benefits:</h3>
<ul>
<li><p><strong>Completely Free</strong>: No hidden costs. ZenUI Library is and will remain free for all users.</p>
</li>
<li><p><strong>Modern UI Components</strong>: Pre-designed, fully responsive components based on modern web standards.</p>
</li>
<li><p><strong>Customizable</strong>: You can easily modify the components to match your project’s branding and style.</p>
</li>
<li><p><strong>Developer-Friendly</strong>: Built with developers in mind, ZenUI components are well-documented, easy to integrate, and come with clean, semantic code.</p>
</li>
<li><p><strong>Customizable Icons</strong>: Access a vast library of icons that you can customize and download as <strong>PNG</strong> or <strong>SVG</strong>, or simply copy the icon’s code to use directly in your project. Personalize colors, sizes, and styles effortlessly to suit your needs.</p>
</li>
<li><p><strong>Color Palette Generator</strong>: Design stunning color schemes with the built-in <strong>color palette generator</strong>. You can upload an image and extract its dominant colors, then generate and customize a full-color palette based on your choices. Whether for UI elements or branding, this tool ensures your design’s color harmony.</p>
</li>
</ul>
<h2 id="heading-zenui-features">ZenUI Features</h2>
<p>ZenUI has a bunch of features designed to streamline your UI development. Let's dive into each feature and explore how it can benefit you.</p>
<h3 id="heading-1-pre-built-ui-components"><strong>1. Pre-Built UI Components</strong></h3>
<p>ZenUI provides many pre-built UI components to streamline your development process. Here’s the full list of what you get:</p>
<ul>
<li><p><code>Buttons</code></p>
</li>
<li><p><code>Navigation Bars</code></p>
</li>
<li><p><code>Cards</code>, <code>Modals and Popups</code></p>
</li>
<li><p><code>Forms and Inputs</code></p>
</li>
<li><p><code>Accordions</code></p>
</li>
<li><p><code>Dropdowns and Menus</code></p>
</li>
<li><p><code>Progress Bars and Loaders</code></p>
</li>
<li><p><code>Tabs</code></p>
</li>
<li><p><code>Badges and Tooltips</code></p>
</li>
<li><p><code>Toast Notifications</code></p>
</li>
<li><p><code>Responsive Grids and Layouts</code></p>
</li>
<li><p><code>Pagination</code></p>
</li>
<li><p><code>Avatars</code></p>
</li>
<li><p><code>Breadcrumbs</code></p>
</li>
<li><p><code>Checkboxes</code></p>
</li>
<li><p><code>Radio Buttons</code></p>
</li>
<li><p><code>Alerts</code></p>
</li>
<li><p><code>Footers</code></p>
</li>
<li><p><code>Headers</code></p>
</li>
<li><p><code>Sidebars</code></p>
</li>
<li><p><code>Search Bars</code></p>
</li>
<li><p><code>Timelines</code></p>
</li>
<li><p><code>Tag Chips</code></p>
</li>
<li><p><code>Spinners</code></p>
</li>
<li><p><code>Skeleton Loaders</code></p>
</li>
<li><p><code>Image Galleries</code></p>
</li>
<li><p><code>Pricing Tables</code></p>
</li>
<li><p><code>Hero Sections</code></p>
</li>
</ul>
<p>With this extensive collection, ZenUI equips you to build fast, responsive, and visually appealing user interfaces efficiently.</p>
<h3 id="heading-2-image-color-extractor"><strong>2. Image Color Extractor</strong></h3>
<p>For designers who need precise color inspiration, ZenUI offers an Image Color Extractor tool. It allows you to:</p>
<ul>
<li><p><strong>Extract Colors:</strong> Upload an image, and the tool will automatically detect and generate a palette of its dominant colors.</p>
</li>
<li><p><strong>Color Picker:</strong> Manually select any spot on the image to fine-tune your palette.</p>
</li>
</ul>
<p>This feature helps streamline your design process by providing accurate, vibrant color combinations directly from your images, ensuring your projects have cohesive and impactful color schemes.</p>
<h3 id="heading-3-template-gallery"><strong>3. Template Gallery</strong></h3>
<p>For developers and designers looking for a jumpstart, ZenUI includes <strong>template collections</strong> like:</p>
<ul>
<li><p><strong>Business Websites</strong>: Build multi-page websites for businesses with a few clicks.</p>
</li>
<li><p><strong>Landing Pages</strong>: Clean and modern landing pages optimized for conversions.</p>
</li>
</ul>
<p>Each template is designed with UX best practices in mind, ensuring a seamless user experience across devices.</p>
<h3 id="heading-4-essential-ui-blocks"><strong>4. Essential UI Blocks</strong></h3>
<p>With <strong>ZenUI Blocks</strong>, you don’t have to build from scratch. These blocks are pre-made sections designed to help you construct entire web pages quickly and efficiently. Here’s the full list of blocks you can use:</p>
<ul>
<li><p><code>Hero Sections</code></p>
</li>
<li><p><code>Responsive Navbar</code></p>
</li>
<li><p><code>Pricing Sections</code></p>
</li>
<li><p><code>Responsive Footer</code></p>
</li>
<li><p><code>Newsletter Form</code></p>
</li>
<li><p><code>Contact Form</code></p>
</li>
<li><p><code>Responsive Search Bar</code></p>
</li>
<li><p><code>Responsive Sidebar</code></p>
</li>
<li><p><code>Call-to-Action Sections</code></p>
</li>
<li><p><code>404 Error Pages</code></p>
</li>
<li><p><code>Empty Page</code></p>
</li>
</ul>
<p>All blocks are fully customizable with minimal effort, allowing you to build websites faster while ensuring a polished, professional look.</p>
<h3 id="heading-5-svg-icons-collection"><strong>5. SVG Icons Collection</strong></h3>
<p>ZenUI 2.0 comes with a set of beautifully designed, scalable <strong>SVG icons</strong>. These icons can be used in any project without compromising page load speed or resolution quality. Just grab the icons, and use them wherever you need, from buttons to navigation menus.</p>
<h2 id="heading-tailwind-css-integration-and-framework-compatibility">Tailwind CSS Integration and Framework Compatibility</h2>
<p><strong>ZenUI</strong> is built with <strong>Tailwind CSS</strong>, a utility-first CSS framework that empowers you to customize your designs with minimal code. By leveraging Tailwind's utility classes, you can easily adjust the look and feel of any component without needing to write custom CSS.</p>
<h3 id="heading-compatibility">Compatibility</h3>
<p>ZenUI is designed to be versatile and compatible with various frameworks, making it easy to integrate into your projects. You can seamlessly use ZenUI with:</p>
<ul>
<li><p><strong>React</strong>: Perfect for building interactive UIs and managing state efficiently.</p>
</li>
<li><p><strong>Next.js</strong>: Ideal for server-rendered applications and static site generation.</p>
</li>
</ul>
<p>With ZenUI, you can create visually stunning, responsive web applications quickly and efficiently, regardless of your tech stack.</p>
<h2 id="heading-how-to-start-using-zenui">How to Start Using ZenUI</h2>
<p>One of the key advantages of ZenUI is that you don’t need to install anything to start using it. ZenUI provides all the necessary <strong>HTML structure</strong> and <strong>Tailwind CSS classes</strong> directly in its documentation, allowing you to copy and paste the code into your projects without any additional setup.</p>
<p>If you're working with frameworks like <strong>React</strong> or <strong>Next.js</strong>, all you need is a project that has Tailwind CSS integrated. You can simply copy the component or block code and paste it directly into your JSX, and you’re ready to go. There’s no need to install external dependencies or packages.</p>
<p>ZenUI makes it easy to kickstart your project without any friction—just start a project with React or Next.js, integrate Tailwind CSS, and start building with ZenUI’s pre-built components and blocks.</p>
<p>Here’s a step-by-step guide on how to get started using the ZenUI Library:</p>
<h3 id="heading-1-visit-the-zenui-library-website">1. Visit the ZenUI Library Website</h3>
<p>Head over to the <a target="_blank" href="https://zenuilibrary.com">ZenUI Library website</a> where you can browse through a wide variety of UI components, templates, essential blocks, and SVG icons. ZenUI Library offers everything from simple buttons to more complex layouts.</p>
<h3 id="heading-2-browse-and-select-a-component">2. Browse and Select a Component</h3>
<p>Once on the website, explore the different sections for components like:</p>
<ul>
<li><p>Buttons</p>
</li>
<li><p>Forms</p>
</li>
<li><p>Modals</p>
</li>
<li><p>Navigation Bars</p>
</li>
<li><p>Cards</p>
</li>
<li><p>Alert, and more.</p>
</li>
</ul>
<p>Each component is showcased with a live preview, making it easier to find exactly what you need for your project.</p>
<h3 id="heading-3-copy-the-component-code">3. Copy the Component Code</h3>
<p>Every component listed in the ZenUI Library has an associated code section.</p>
<p><strong>You don’t need to install any dependencies –</strong> simply <strong>copy the JSX Code</strong> for the selected component.</p>
<h3 id="heading-4-integrate-the-code-into-your-project">4. Integrate the Code into Your Project</h3>
<p>After selecting a component from the ZenUI Library website and copying the code, integrating it into your project is incredibly simple:</p>
<p><strong>Paste the Code</strong>:</p>
<ul>
<li><p><strong>React or Next.js Project</strong>: Open your desired component file (for example, <code>App.js</code> any other component file) and <strong>paste</strong> the copied code directly into the file where you want to paste it.</p>
</li>
<li><p><strong>Tailwind CSS Project</strong>: If you’re using plain HTML with Tailwind CSS, paste just the HTML code from ZenUI Library into your HTML file.</p>
</li>
</ul>
<p><strong>Done!</strong> You’re all set. You can now see the component on your project without needing to install any dependencies or extra packages.</p>
<p>ZenUI Library components are designed to work out of the box, so after pasting the code, you will immediately see the component functioning on your project.</p>
<h3 id="heading-5-customize-components">5. <strong>Customize Components</strong></h3>
<p>All components and templates in the ZenUI Library are <strong>highly customizable</strong>. You can change colors, sizes, and layouts using Tailwind or modify the HTML structure to fit your unique needs. Whether you are building a light or dark theme, ZenUI offers complete flexibility.</p>
<p>I’ll walk you through customizing a card component step by step below.</p>
<h3 id="heading-6-start-building-with-zenui-components">6. <strong>Start Building with ZenUI Components</strong></h3>
<p>Now that the components are integrated, you can focus on building the rest of your application. The pre-built, responsive, and reusable ZenUI components will speed up your development process without sacrificing design quality.</p>
<p>By following these steps, you can start using ZenUI Library right away in your project. Whether you’re building with React, Next.js, or plain HTML with Tailwind CSS, ZenUI Library offers a simple and effective solution for UI development without the hassle of managing additional dependencies.</p>
<h2 id="heading-how-to-customize-zenui-components">How to Customize ZenUI Components</h2>
<p>Here's a detailed breakdown of how you can customize a <strong>card component</strong> in the ZenUI Library using Tailwind CSS. This will cover changing colors, adjusting sizes, and modifying the layout to fit your project's look and feel.</p>
<h3 id="heading-original-card-component"><strong>Original Card Component</strong></h3>
<p>This is a basic card component using Tailwind CSS:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-white shadow-lg rounded-lg p-6"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Image"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-full h-48 object-cover rounded-t-lg"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-xl font-bold mt-4"</span>&gt;</span>Card Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-gray-600 mt-2"</span>&gt;</span>This is a customizable card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mt-4 bg-blue-500 text-white px-4 py-2 rounded"</span>&gt;</span>Learn More<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-1-customize-colors"><strong>1. Customize Colors</strong></h3>
<p>You can easily change the background colors, text colors, and button colors by modifying the Tailwind classes.</p>
<h4 id="heading-changing-background-and-text-colors-for-dark-mode"><strong>Changing Background and Text Colors for Dark Mode</strong></h4>
<p>To make the card compatible with dark mode, you'll want to adjust the background and text colors. Here’s the code you can use to do that:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-white dark:bg-gray-800 shadow-lg rounded-lg p-6"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Image"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-full h-48 object-cover rounded-t-lg"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-xl font-bold mt-4 text-gray-900 dark:text-white"</span>&gt;</span>Card Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-gray-600 dark:text-gray-400 mt-2"</span>&gt;</span>This is a customizable card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mt-4 bg-blue-500 dark:bg-yellow-500 text-white dark:text-gray-900 px-4 py-2 rounded"</span>&gt;</span>Learn More<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Code breakdown:</strong></p>
<ul>
<li><p><code>dark:bg-gray-800</code>: Changes the background color to dark gray when dark mode is active.</p>
</li>
<li><p><code>dark:text-white</code>: Sets the text color to white in dark mode for the title.</p>
</li>
<li><p><code>dark:text-gray-400</code>: Lightens the paragraph text in dark mode for better readability.</p>
</li>
<li><p><code>dark:bg-yellow-500</code>: Changes the button background to yellow in dark mode.</p>
</li>
<li><p><code>dark:text-gray-900</code>: Changes the button text color to dark gray in dark mode</p>
</li>
</ul>
<h3 id="heading-2-configure-tailwindconfigjs-to-enable-dark-mode"><strong>2. Configure</strong> <code>tailwind.config.js</code> <strong>to Enable Dark Mode</strong></h3>
<p>To enable <strong>dark mode</strong> in your Tailwind CSS project, you need to configure two main things:</p>
<ol>
<li><p><strong>Set up the</strong> <code>tailwind.config.js</code> file to enable dark mode.</p>
</li>
<li><p><strong>Adjust your HTML to toggle dark mode on and off.</strong></p>
</li>
</ol>
<p>Let’s walk through the process step-by-step.</p>
<p>In the <code>tailwind.config.js</code> file, add the <code>darkMode</code> option. Tailwind CSS supports two strategies for dark mode:</p>
<ul>
<li><p><code>'media'</code>: Uses the user's system preference (light or dark mode).</p>
</li>
<li><p><code>'class'</code>: Uses a <code>dark</code> class that you can toggle manually (gives you more control).</p>
</li>
</ul>
<p><strong>Example Configuration Using the</strong> <code>'class'</code> Strategy:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">darkMode</span>: <span class="hljs-string">'class'</span>, <span class="hljs-comment">// Enable class-based dark mode</span>
  <span class="hljs-comment">// Other configurations...</span>
  <span class="hljs-attr">theme</span>: {
    <span class="hljs-attr">extend</span>: {
      <span class="hljs-comment">// Customizations...</span>
    },
  },
  <span class="hljs-attr">variants</span>: {
    <span class="hljs-attr">extend</span>: {
      <span class="hljs-comment">// Variants...</span>
    },
  },
  <span class="hljs-attr">plugins</span>: [],
};
</code></pre>
<h4 id="heading-set-up-your-html-to-toggle-dark-mode"><strong>Set Up Your HTML to Toggle Dark Mode</strong></h4>
<ol>
<li><strong>For Class-Based Dark Mode (</strong><code>darkMode: 'class'</code>): To control dark mode manually, you need to add or remove the <code>dark</code> class on your root HTML element.</li>
</ol>
<p><strong>Add the</strong> <code>dark</code> Class to Enable Dark Mode:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"dark"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- Head content --&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- Your content --&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<ol start="2">
<li><strong>Toggle Dark Mode with JavaScript:</strong> You can also add a button to toggle dark mode dynamically:</li>
</ol>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"toggleDarkMode()"</span>&gt;</span>Toggle Dark Mode<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggleDarkMode</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">document</span>.documentElement.classList.toggle(<span class="hljs-string">'dark'</span>);
  }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p><strong>Summary</strong></p>
<ul>
<li><p><strong>Step 1:</strong> In your <code>tailwind.config.js</code> file set <code>darkMode</code> to <code>'class'</code> or <code>'media'</code>.</p>
<ul>
<li><p>Use <code>'class'</code> if you want to toggle dark mode manually.</p>
</li>
<li><p>Use <code>'media'</code> to automatically follow the user's system preference.</p>
</li>
</ul>
</li>
<li><p><strong>Step 2:</strong> If using <code>'class'</code> mode, add or remove the <code>dark</code> class on your <code>&lt;html&gt;</code> or <code>&lt;body&gt;</code> tag to enable or disable dark mode.</p>
</li>
<li><p><strong>Step 3:</strong> In your HTML, use the <code>dark:</code> prefix in Tailwind CSS classes to specify styles for dark mode.</p>
</li>
</ul>
<h3 id="heading-3-customize-sizes-and-spacing"><strong>3. Customize Sizes and Spacing</strong></h3>
<p>You can modify the size of the card and adjust the padding and margins to better fit your layout.</p>
<h4 id="heading-larger-card-with-more-padding"><strong>Larger Card with More Padding</strong></h4>
<p>If you want a larger card with more padding and a bigger image:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-white shadow-lg rounded-lg p-10"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Image"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-full h-64 object-cover rounded-t-lg"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-xl font-bold mt-8"</span>&gt;</span>Card Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-gray-600 mt-4"</span>&gt;</span>This is a customizable card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mt-8 bg-blue-500 text-white px-6 py-3 rounded-lg"</span>&gt;</span>Learn More<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Code breakdown:</strong></p>
<ul>
<li><p><code>p-10</code> increases the padding inside the card for a more spacious layout.</p>
</li>
<li><p><code>h-64</code> changes the image height to 64 units (16rem), making it taller.</p>
</li>
<li><p><code>mt-8</code> adds more space between elements, like the title and button.</p>
</li>
<li><p><code>px-6 py-3</code> increases the padding inside the button, making it larger.</p>
</li>
<li><p><code>rounded-lg</code> adds larger, more prominent rounded corners to the button.</p>
</li>
</ul>
<h3 id="heading-4-modify-the-layout"><strong>4. Modify the Layout</strong></h3>
<p>You can also adjust the card layout by making it more compact or aligning content differently.</p>
<h4 id="heading-compact-card-with-left-aligned-content"><strong>Compact Card with Left-Aligned Content</strong></h4>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-white shadow-lg rounded-lg p-4 flex items-start"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Image"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-24 h-24 object-cover rounded-lg mr-4"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-xl font-bold"</span>&gt;</span>Card Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-gray-600 mt-2"</span>&gt;</span>This is a customizable card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mt-4 bg-blue-500 text-white px-4 py-2 rounded"</span>&gt;</span>Learn More<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Code breakdown:</strong></p>
<ul>
<li><p><code>flex items-start</code> makes the card a flexible box layout with content aligned to the top.</p>
</li>
<li><p><code>w-24 h-24</code> resizes the image to a smaller, square format.</p>
</li>
<li><p><code>mr-4</code> adds a margin to the right of the image, separating it from the text content.</p>
</li>
<li><p>The rest of the content (title, text, and button) is contained in a <code>div</code> to keep it aligned next to the image.</p>
</li>
</ul>
<h3 id="heading-5-customize-it-with-responsive-design"><strong>5. Customize it with Responsive Design</strong></h3>
<p>Tailwind makes it easy to customize the card for different screen sizes.</p>
<h4 id="heading-responsive-card-for-mobile-and-desktop"><strong>Responsive Card for Mobile and Desktop</strong></h4>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-white shadow-lg rounded-lg p-6 md:p-10"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Image"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-full h-48 md:h-64 object-cover rounded-t-lg"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-xl font-bold mt-4 md:mt-8"</span>&gt;</span>Card Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-gray-600 mt-2 md:mt-4"</span>&gt;</span>This is a customizable card component.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mt-4 md:mt-8 bg-blue-500 text-white px-4 py-2 md:px-6 md:py-3 rounded-lg"</span>&gt;</span>Learn More<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><strong>Code breakdown:</strong></p>
<ul>
<li><p><code>md:p-10</code> increases the padding on larger screens (for example, tablets or desktops).</p>
</li>
<li><p><code>md:h-64</code> makes the image taller on larger screens, providing a better visual experience.</p>
</li>
<li><p><code>md:mt-8</code> adds more spacing between elements on larger screens to prevent a cramped look.</p>
</li>
<li><p><code>md:px-6 md:py-3</code> enlarges the button on larger screens, making it easier to interact with.</p>
</li>
</ul>
<p>As you’ve just seen, with Tailwind CSS, you can customize ZenUI Library components in various ways:</p>
<ol>
<li><p><strong>Colors:</strong> Adjust background, text, and button colors for light and dark themes.</p>
</li>
<li><p><strong>Sizes &amp; Spacing:</strong> Modify padding, margins, and dimensions to fit your layout.</p>
</li>
<li><p><strong>Layout:</strong> Use a flexbox or grid to alter how content is arranged within the card.</p>
</li>
<li><p><strong>Responsive Design:</strong> Tailor your components for different screen sizes using responsive utility classes.</p>
</li>
</ol>
<h2 id="heading-how-to-use-zenui-website-templates">How to Use ZenUI Website Templates</h2>
<p>Getting started with <strong>ZenUI’s Website Templates</strong> is quick and easy. I’ll now walk you through setting up your project using one of ZenUI’s pre-built templates.</p>
<h3 id="heading-step-1-choose-a-template">Step 1: Choose a Template</h3>
<p>Head over to the <strong>ZenUI Library website</strong> and navigate to the templates section. You’ll find a variety of website templates.</p>
<ul>
<li><p>Pick a template that fits your project needs.</p>
</li>
<li><p>Go to the template GitHub repository by clicking the <strong>Get Template</strong> button.</p>
</li>
</ul>
<h3 id="heading-step-2-clone-the-repository-on-your-desktop">Step 2: Clone the Repository on Your Desktop</h3>
<p>When you go to the GitHub repository by clicking the <strong>Get Template</strong> button, you will see a button called <strong>Code.</strong> Click on it you will see an HTTPS URL like the below picture. Copy the URL.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727202543195/e1c05f17-b12f-4ab3-9b9d-23cebd948349.png" alt="clone-zenui-template-from-github-repository" class="image--center mx-auto" width="712" height="572" loading="lazy"></p>
<p>After copying the HTTPS URL just open your computer <code>terminal</code> and type a command like the one below:</p>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> project-https-url-here
</code></pre>
<p>After processing the command with the correct HTTPS URL, you will get the template on your local computer. Now open the template project on a code editor of your choice.</p>
<p>After opening the project in your code editor, you need to do just 2 things:</p>
<ul>
<li><p>Install the npm packages using the command <code>npm install</code> (packages will be react-icons, react-router-dom, and so on)</p>
</li>
<li><p>Run the project on your <strong>localhost</strong> using the command <code>npm run dev</code></p>
</li>
</ul>
<h3 id="heading-step-3-customize-the-template">Step 3: Customize the Template</h3>
<p>You can <a target="_blank" href="https://youtu.be/wPopdyqpxHQ?si=p-12ygf5LiXCLAQ7">watch this video</a> on how to use the ZenUI Library template and how to customize it. Or you can play around with:</p>
<ul>
<li><p><strong>Modifying Content</strong>: Change text, images, or links in the HTML file.</p>
</li>
<li><p><strong>Changing Colors</strong>: Update Tailwind CSS utility classes (for example, <code>bg-blue-500</code> to <code>bg-red-500</code>) to apply your custom branding.</p>
</li>
<li><p><strong>Adding New Components</strong>: Copy and paste additional components or blocks from the <strong>ZenUI Library</strong> documentation and integrate them into the template.</p>
</li>
</ul>
<h2 id="heading-how-to-extract-color-from-an-image">How to Extract Color from an Image</h2>
<p>First, go to the <a target="_blank" href="https://zenui.net">ZenUI Library</a> website. Then click on the Opacity Palette button from the top navigation bar. You’ll see some of the palettes that are generated by default from ZenUI. Just click on the image icon button from the icon search bar on the right side, as you can see in the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727273411733/738020d4-bdbe-4a8e-98ce-63aa21576a7b.png" alt="upload-image" class="image--center mx-auto" width="1437" height="401" loading="lazy"></p>
<p>Then you need to select your image from your device. After selecting the image, press the open button like in the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727273571064/e90f47f7-dab9-458c-a26d-c6b20ba4199f.png" alt="select-image-from-device" class="image--center mx-auto" width="1781" height="1015" loading="lazy"></p>
<p>Now you will see a magical tool called “<strong>Extract Color From Uploaded Image</strong>” like the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727273665557/5faad30a-c3fc-4783-b59c-f6999b22d927.png" alt="zenui-color-picker-modal" class="image--center mx-auto" width="1590" height="839" loading="lazy"></p>
<p>Now it’s time to select your color from the image. When you move the cursor on the image, you will see an indicator that lets you see whatever color you’re hovering over right now, as you can see in the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727273940270/147b94a5-b95d-47b8-bb5e-437d0276c8a1.jpeg" alt="color-picker-real-time-indecator" class="image--center mx-auto" width="522" height="313" loading="lazy"></p>
<p>Below that, you will see your selected color in the “<strong>Selected Color</strong>” section. From there, you can copy your selected color <strong>Hex</strong> or <strong>RGB</strong> color code. Also, you can generate the selected color palette by clicking the “<strong>Generate Palette</strong>” button.</p>
<p>Also, you can generate a color palette by giving the color code. If you have a color code and you want to make the color palette, just paste the color code in the input field reference, as you can see in the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727274330786/019228a2-aae8-41e3-bb69-c2fe8a68f4d5.png" alt="color-palette-generator-input" class="image--center mx-auto" width="1360" height="396" loading="lazy"></p>
<p>After pasting your color code, just click on the “Generate” button and the color palette will be automatically generated.</p>
<p>After generating the color palette, you can copy the color code from the generated palette for any part – just click on the palette part. For this, you need to copy the color code. You will see a modal come up with your color code variant like “HEX”, “RGB”, or “HSL” like in the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727274619884/871ad483-a3f8-4d8e-9b9e-76ac7cbd0ee5.png" alt="copy-color-code" class="image--center mx-auto" width="1405" height="610" loading="lazy"></p>
<h2 id="heading-how-to-customize-any-icon-in-the-zenui-library">How to Customize Any Icon in the ZenUI Library</h2>
<p>Go to the <a target="_blank" href="https://zenui.net">ZenUI Library</a> website, and click on the icons button from the top navigation bar. This will bring you to the ZenUI icons homepage. Now choose an icon you want to use and click on it. You will see a drawer come out like in the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727274886810/a283107e-aa84-4537-b7f8-7e866b163422.png" alt="customize-zenui-icon" class="image--center mx-auto" width="599" height="775" loading="lazy"></p>
<p>Now you can change the icon <strong>Color</strong> and <strong>Size.</strong> After you’ve customized it, you can save the icon multi-variant like PNG, or SVG, or copy the icon SVG code.</p>
<h2 id="heading-join-the-zenui-community">Join the ZenUI Community</h2>
<p>ZenUI isn’t just about freely available components. It’s also about fostering a community of developers and designers. Join the <a target="_blank" href="https://web.facebook.com/share/g/wAbkC1XFB5Ssc121/"><strong>ZenUI community</strong></a> so you can share your work, learn from fellow developers, and get early access to new components and templates.</p>
<p>You can contribute to ZenUI by submitting templates, reporting bugs, or suggesting features. You can find the community on platforms like <a target="_blank" href="https://github.com/Asfak00/zenui-library">GitHub</a> and <a target="_blank" href="https://web.facebook.com/zenuilibrary"><strong>Facebook</strong></a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Whether building a personal portfolio, an e-commerce site, or a business platform, ZenUI can be your go-to resource for high-quality, customizable UI components and templates. With its simplicity, flexibility, and extensive range of options, ZenUI makes front-end development a breeze.</p>
<p>Feel free to explore the library at <a target="_blank" href="https://zenui.net">https://zenui.net</a>, and don’t hesitate to reach out if you need help or have feedback.</p>
<p><strong>Happy Coding!</strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use the JavaScript Selection API: Build a Rich Text Editor and Real-Time Element Detection ]]>
                </title>
                <description>
                    <![CDATA[ When you interact with web pages, a common task you’ll perform often is selecting text. Whether it's highlighting a section of a paragraph to copy, marking important parts of a document, or working with interactive features like note-taking or text e... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-the-javascript-selection-api-to-build-a-rich-text-editor/</link>
                <guid isPermaLink="false">66e85a7659fa541150c1c327</guid>
                
                    <category>
                        <![CDATA[ selection api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ element detection ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ #richtext ]]>
                    </category>
                
                    <category>
                        <![CDATA[  DOM In JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript event handling ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Asfak Ahmed ]]>
                </dc:creator>
                <pubDate>Mon, 16 Sep 2024 16:19:02 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1726225376443/ae5d57c8-e79e-4dc4-b218-c3a5e34f8293.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you interact with web pages, a common task you’ll perform often is selecting text. Whether it's highlighting a section of a paragraph to copy, marking important parts of a document, or working with interactive features like note-taking or text editing, text selections are part of our everyday browsing experience.</p>
<p>The JavaScript <strong>Selection API</strong> is what makes it possible to interact programmatically with these text selections.</p>
<p>In this tutorial, we'll dive deep into the Selection API, explore what it can do, and demonstrate how you can use it to create interactive, selection-based web applications.</p>
<h3 id="heading-in-this-article-we-will-cover">In this article, we will cover:</h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-the-selection-api">Explore the JavaScript Selection API, a powerful tool for interacting with and manipulating user-selected text on a web page.</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-documentexeccommand-function">Introduce document.execCommand(), an easy-to-use method for adding formatting to selected text, including bold, italic, and underline.</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-example-use-case-how-to-build-a-rich-text-editor-with-the-javascript-selection-api">Demonstrate how to build a simple rich text editor with basic formatting features using both the Selection API and execCommand().</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-get-the-clicked-element-and-its-real-time-position">Detect clicked elements and their positions in real time.</a></p>
</li>
</ul>
<h2 id="heading-what-is-the-selection-api">What is the Selection API?</h2>
<p>The <strong>Selection API</strong> is a web API provided by modern browsers that enables developers to work with user text selections on a web page. It allows you to:</p>
<ol>
<li><p>Get details about the currently selected text.</p>
</li>
<li><p>Modify or manipulate selections programmatically.</p>
</li>
<li><p>Detect when users make a selection.</p>
</li>
<li><p>Store, replace, or delete selections of text.</p>
</li>
</ol>
<p>This API is commonly used for rich text editors, copy/paste functionality, custom tooltips, highlighting, annotations, and more.</p>
<h2 id="heading-what-can-you-do-using-the-selection-api">What Can You Do Using the Selection API?</h2>
<p>The Selection API gives you the power to interact with user-selected text in a variety of ways. Some key capabilities include:</p>
<ol>
<li><p><strong>Get the currently selected text</strong>: Extract the highlighted text that the user selects in the document.</p>
</li>
<li><p><strong>Modify the selection</strong>: Programmatically set or modify a selection, either by setting new start and end points for the selection or collapsing it entirely.</p>
</li>
<li><p><strong>Remove the selection</strong>: Clear a selection once you're done using it.</p>
</li>
<li><p><strong>Extract position information</strong>: Know where the selection begins and ends, both within the document and on the screen (useful for custom tooltips or annotations).</p>
</li>
<li><p><strong>Apply custom styling</strong>: You can style the selected text, add highlights, or trigger events when a user makes a selection.</p>
</li>
</ol>
<h2 id="heading-key-components-of-the-selection-api">Key Components of the Selection API</h2>
<p>To effectively use the Selection API, it's important to understand some of its core concepts. Here are the key objects and methods:</p>
<h3 id="heading-1-selection-object"><strong>1.</strong> <code>Selection</code> <strong>Object</strong></h3>
<p>The <code>Selection</code> object represents the current selection of text on a web page. It’s accessible through the <code>window.getSelection()</code> method and is the core object you’ll interact with.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
<span class="hljs-built_in">console</span>.log(selection);
</code></pre>
<p>The <code>Selection</code> object provides multiple properties and methods to retrieve, modify, and manipulate user-selected text.</p>
<h3 id="heading-2-range-object"><strong>2.</strong> <code>Range</code> <strong>Object</strong></h3>
<p>The <code>Range</code> object represents a fragment of a document. It holds information about the start and end points of a selection and allows you to manipulate portions of the document.</p>
<p>For example, you can create a range to highlight or manipulate specific text nodes or retrieve the text content within a certain range.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
<span class="hljs-keyword">const</span> range = selection.getRangeAt(<span class="hljs-number">0</span>); <span class="hljs-comment">// Get the first range of the selection</span>
<span class="hljs-built_in">console</span>.log(range);
</code></pre>
<h2 id="heading-key-methods-and-properties-of-the-selection-api">Key Methods and Properties of the Selection API</h2>
<p>Here’s a breakdown of the most commonly used methods and properties of the <strong>Selection API</strong> and the <strong>Range API</strong>:</p>
<h3 id="heading-selection-api-methods"><strong>Selection API Methods:</strong></h3>
<h4 id="heading-1-windowgetselection">1. <code>window.getSelection()</code>:</h4>
<p>The <code>window.getSelection()</code> method is used to retrieve the current text selection on the webpage. It returns an <code>Selection</code> object, which represents the range of text selected by the user, or the caret's current position (if no text is selected).</p>
<p><strong>Details about</strong> <code>window.getSelection()</code><strong>:</strong></p>
<p>The <code>Selection</code> object contains details about the currently selected text (if any), including the start and end nodes, offsets, and methods for manipulating the selection.</p>
<p>If no text is selected, the <code>Selection</code> the object will reflect the caret's current position without any selected range.</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
<span class="hljs-built_in">console</span>.log(selection);  <span class="hljs-comment">// Logs the Selection object to the console</span>
</code></pre>
<p><strong>Example Usage:</strong></p>
<p>Check if any text is selected:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
<span class="hljs-keyword">if</span> (selection.rangeCount &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Text is selected'</span>);
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'No text selected'</span>);
}
</code></pre>
<p>This checks if any text is selected by verifying if <code>rangeCount</code> (the number of text ranges) is greater than zero.</p>
<h4 id="heading-2-selectionanchornode-and-selectionfocusnode">2. <code>Selection.anchorNode</code> <strong>and</strong> <code>Selection.focusNode</code><strong>:</strong></h4>
<p><code>anchorNode</code> represents the node where the user started the selection. It's the starting point of the selection, although visually it could be at the end depending on the user's drag direction.</p>
<p><code>focusNode</code> represents the node where the user ended the selection. It's the ending point of the selection, but again, this could visually appear as the start of the selection if the user selected backward.</p>
<p><strong>Important details:</strong></p>
<ul>
<li><p><strong>Selection direction</strong>: If the selection is made left-to-right (dragging forward), this <code>anchorNode</code> will be the earlier part of the selection. If the selection is made right-to-left (dragging backward), the <code>anchorNode</code> will be at the later part of the selection, but the focus will appear first.</p>
</li>
<li><p><strong>Node types</strong>: Both <code>anchorNode</code> and <code>focusNode</code> return DOM nodes. This means they can be text nodes, element nodes, or any other type of node within the document.</p>
</li>
<li><p><strong>Offsets</strong>: Along with these properties, <code>Selection.anchorOffset</code> and <code>Selection.focusOffset</code> give you the exact character offset within the nodes where the selection starts and ends.</p>
</li>
</ul>
<h4 id="heading-3-selectiontostring">3. <code>Selection.toString()</code>:</h4>
<p>To use the <code>Selection.toString()</code> method, you can simply call it on the current selection. This method returns the string value of the currently selected text in the document.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> selectedText = selection.toString();
<span class="hljs-built_in">console</span>.log(selectedText);  <span class="hljs-comment">// Output: The selected text</span>
</code></pre>
<p><strong>How it works:</strong></p>
<ol>
<li><p><code>window.getSelection()</code>: This retrieves the current <code>Selection</code> object representing the user-selected text.</p>
</li>
<li><p><code>.toString()</code>: This converts the selected range to a plain string of text.</p>
</li>
<li><p>The result is then printed to the console.</p>
</li>
</ol>
<h4 id="heading-4-selectionremoveallranges">4. <code>Selection.removeAllRanges()</code>:</h4>
<p>The <code>Selection.removeAllRanges()</code> method is used to clear or remove any current text selection on a webpage. When called, it deselects any text that the user may have selected, leaving the selection empty.</p>
<pre><code class="lang-typescript">selection.removeAllRanges();
</code></pre>
<p><strong>How it works:</strong></p>
<ul>
<li><p><code>window.getSelection()</code>: Retrieves the current <code>Selection</code> object.</p>
</li>
<li><p><code>.removeAllRanges()</code>: Clears the selection, effectively deselecting any highlighted text on the page.</p>
</li>
</ul>
<h4 id="heading-5-selectionaddrangerange">5. <code>Selection.addRange(range)</code>:</h4>
<p>The <code>Selection.addRange(range)</code> method is used to add a specific <code>Range</code> object to the current selection in the document. This allows you to programmatically select a range of text or elements.</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// Create a range object</span>
<span class="hljs-keyword">const</span> range = <span class="hljs-built_in">document</span>.createRange();

<span class="hljs-comment">// Select a specific element or part of the document</span>
<span class="hljs-keyword">const</span> element = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myElement'</span>);

<span class="hljs-comment">// Set the range to start at the beginning of the element and end at the end</span>
range.selectNodeContents(element);

<span class="hljs-comment">// Clear any existing selection and add the new range</span>
<span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
selection.removeAllRanges();  <span class="hljs-comment">// Remove existing selections</span>
selection.addRange(range);  <span class="hljs-comment">// Add the new range</span>
</code></pre>
<p><strong>How it works:</strong></p>
<ol>
<li><p><code>document.createRange()</code>: Creates a new <code>Range</code> object.</p>
</li>
<li><p><code>range.selectNodeContents()</code>: Sets the range to cover the contents of a specific DOM element (in this case, the element with ID <code>myElement</code>).</p>
</li>
<li><p><code>selection.removeAllRanges()</code>: Clears any previously selected text or elements.</p>
</li>
<li><p><code>selection.addRange(range)</code>: Adds the definition <code>Range</code> to the selection, making it the currently selected text or content.</p>
</li>
</ol>
<p><strong>Use Cases:</strong></p>
<ul>
<li><p><strong>Programmatically Select Text</strong>: If you want to highlight specific parts of the document programmatically, you can use this method to select them.</p>
</li>
<li><p><strong>Custom Selection Logic</strong>: In web applications that require specific text or element selections, such as editors or custom UI tools, this can be used to manage user selections.</p>
</li>
</ul>
<h3 id="heading-range-api-methods"><strong>Range API Methods:</strong></h3>
<h4 id="heading-1-rangesetstartnode-offset">1. <code>range.setStart(node, offset)</code>:</h4>
<p>The <code>Range.setStart(node, offset)</code> method is used to set the starting point of an <code>Range</code> object. You specify <code>node</code> where the range should start and the <code>offset</code> (position within the node) for the starting point.</p>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><code>node</code>: The DOM node where the range should start. It can be an element node or a text node.</p>
</li>
<li><p><code>offset</code>: The position within the node where the range starts. For element nodes, this is the index of child nodes. For text nodes, it's the character position within the text.</p>
</li>
</ul>
<pre><code class="lang-typescript"><span class="hljs-comment">// Create a range object</span>
<span class="hljs-keyword">const</span> range = <span class="hljs-built_in">document</span>.createRange();

<span class="hljs-comment">// Select the text node where the range should start</span>
<span class="hljs-keyword">const</span> textNode = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myElement'</span>).firstChild;

<span class="hljs-comment">// Set the start of the range at the 5th character in the text node</span>
range.setStart(textNode, <span class="hljs-number">5</span>);
</code></pre>
<p><strong>Code explanation:</strong></p>
<ol>
<li><p><code>document.createRange()</code>: Creates a new <code>Range</code> object.</p>
</li>
<li><p><code>textNode</code>: This refers to the first child of the element with ID <code>myElement</code>, which is expected to be a text node.</p>
</li>
<li><p><code>range.setStart(textNode, 5)</code>: Sets the start of the range at the 5th character in the text node.</p>
</li>
</ol>
<p><strong>Important notes:</strong></p>
<ul>
<li><p>In <strong>text nodes</strong>, the <code>offset</code> refers to the position of a character within the text. For example, <code>offset = 5</code> means the range starts after the 5th character.</p>
</li>
<li><p>In <strong>element nodes</strong>, the <code>offset</code> refers to the index of child nodes. For instance, <code>offset = 0</code> would start before the first child element, while <code>offset = 1</code> would start after the first child.</p>
</li>
</ul>
<p><strong>Example use case:</strong></p>
<p>You could use <code>setStart</code> in a scenario where you want to highlight or extract part of some text, starting at a specific point:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> range = <span class="hljs-built_in">document</span>.createRange();
<span class="hljs-keyword">const</span> textNode = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myText'</span>).firstChild;
range.setStart(textNode, <span class="hljs-number">3</span>); <span class="hljs-comment">// Start at the 4th character</span>
range.setEnd(textNode, <span class="hljs-number">8</span>);   <span class="hljs-comment">// End at the 9th character</span>

<span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
selection.removeAllRanges(); <span class="hljs-comment">// Clear previous selections</span>
selection.addRange(range);   <span class="hljs-comment">// Highlight the selected text</span>
</code></pre>
<p>In this example, it selects the text starting from the 4th character and ending at the 9th character, effectively highlighting that part of the text.</p>
<h4 id="heading-2-rangeclonecontents">2. <code>range.cloneContents()</code>:</h4>
<p>The <code>Range.cloneContents()</code> method is used to create a copy of the content within the specified range. It returns a <code>DocumentFragment</code> containing the nodes and content from the range but does not modify the original document.</p>
<p><strong>Key details:</strong></p>
<ul>
<li><p><strong>Returns</strong>: A <code>DocumentFragment</code> that contains the cloned nodes and elements within the range.</p>
</li>
<li><p><strong>Non-destructive</strong>: This method does not alter or remove the content from the original document – it simply creates a copy.</p>
</li>
</ul>
<p><strong>Example code:</strong></p>
<pre><code class="lang-typescript"><span class="hljs-comment">// Create a range object</span>
<span class="hljs-keyword">const</span> range = <span class="hljs-built_in">document</span>.createRange();

<span class="hljs-comment">// Select the content of a specific element</span>
<span class="hljs-keyword">const</span> element = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myElement'</span>);
range.selectNodeContents(element);

<span class="hljs-comment">// Clone the contents of the range</span>
<span class="hljs-keyword">const</span> clonedContent = range.cloneContents();

<span class="hljs-comment">// Append the cloned content somewhere in the document</span>
<span class="hljs-built_in">document</span>.body.appendChild(clonedContent);
</code></pre>
<p><strong>How it works:</strong></p>
<ol>
<li><p><code>document.createRange()</code>: Creates a new <code>Range</code> object.</p>
</li>
<li><p><code>range.selectNodeContents(element)</code>: Selects all the content within the specified element.</p>
</li>
<li><p><code>range.cloneContents()</code>: Creates a <code>DocumentFragment</code> that contains a copy of the selected contents.</p>
</li>
<li><p><code>document.body.appendChild(clonedContent)</code>: Appends the cloned content somewhere in the document (in this case, at the end of the body).</p>
</li>
</ol>
<p><strong>Use cases:</strong></p>
<ol>
<li><p><strong>Duplicating Content</strong>: Use this method when you need to create a copy of selected content without altering the original.</p>
</li>
<li><p><strong>Manipulating Copied Data</strong>: After cloning the contents, you can modify or process the cloned fragment (for example, for drag-and-drop features, custom tooltips, or saving a portion of content).</p>
</li>
</ol>
<p><strong>Example scenario:</strong></p>
<p>If you want to copy part of a webpage’s content and display it elsewhere:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> range = <span class="hljs-built_in">document</span>.createRange();
<span class="hljs-keyword">const</span> element = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'#textElement'</span>);
range.setStart(element.firstChild, <span class="hljs-number">0</span>);  <span class="hljs-comment">// Set start of range</span>
range.setEnd(element.firstChild, <span class="hljs-number">10</span>);   <span class="hljs-comment">// Set end of range (first 10 characters)</span>

<span class="hljs-comment">// Clone the content and append it to another element</span>
<span class="hljs-keyword">const</span> clonedContent = range.cloneContents();
<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'#targetElement'</span>).appendChild(clonedContent);
</code></pre>
<p>In this example, the first 10 characters <code>#textElement</code> are cloned and inserted into <code>#targetElement</code>. This does not affect the original content in <code>#textElement</code>.</p>
<h3 id="heading-use-cases-of-the-selection-api">Use Cases of the Selection API</h3>
<h4 id="heading-1-highlighting-text">1. Highlighting text</h4>
<p>Using the Selection API, you can highlight text dynamically based on user input. For example, you can wrap the selected text in a <code>&lt;mark&gt;</code> tag to highlight it.</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>This is some selectable text.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"highlightSelection()"</span>&gt;</span>Highlight<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">highlightSelection</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
    <span class="hljs-keyword">if</span> (selection.rangeCount &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">const</span> range = selection.getRangeAt(<span class="hljs-number">0</span>);
        <span class="hljs-keyword">const</span> highlight = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">'mark'</span>);
        range.surroundContents(highlight);
    }
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>This script allows users to highlight the selected text with a simple click.</p>
<h4 id="heading-2-copying-selected-text">2. Copying selected text</h4>
<p>You can easily extract and manipulate the selected text with the <code>Selection.toString()</code> method. Here’s an example of copying the selected text to the clipboard:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>Select any portion of this text and copy it to the clipboard.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"copySelection()"</span>&gt;</span>Copy<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">copySelection</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
    <span class="hljs-keyword">const</span> text = selection.toString();

    navigator.clipboard.writeText(text).then(<span class="hljs-function">() =&gt;</span> {
        alert(<span class="hljs-string">"Copied to clipboard: "</span> + text);
    });
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>This snippet allows users to copy any selected text and paste it elsewhere.</p>
<h4 id="heading-3-annotating-text">3. Annotating text</h4>
<p>You can combine the Selection API with custom annotations. You can display a floating tooltip or annotation box by detecting the selection’s position on the page.</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Select text to see the annotation box.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
<span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"mouseup"</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
    <span class="hljs-keyword">const</span> selectedText = selection.toString();

    <span class="hljs-keyword">if</span> (selectedText.length &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">const</span> range = selection.getRangeAt(<span class="hljs-number">0</span>);
        <span class="hljs-keyword">const</span> rect = range.getBoundingClientRect();

        <span class="hljs-keyword">const</span> annotationBox = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">"div"</span>);
        annotationBox.style.position = <span class="hljs-string">"absolute"</span>;
        annotationBox.style.left = <span class="hljs-string">`<span class="hljs-subst">${rect.left}</span>px`</span>;
        annotationBox.style.top = <span class="hljs-string">`<span class="hljs-subst">${rect.top - <span class="hljs-number">30</span>}</span>px`</span>;
        annotationBox.textContent = <span class="hljs-string">"Annotate this!"</span>;
        <span class="hljs-built_in">document</span>.body.appendChild(annotationBox);
    }
});
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>This script creates an annotation box just above the selected text.</p>
<h3 id="heading-advanced-features-of-the-selection-api">Advanced Features of the Selection API</h3>
<ol>
<li><p><strong>Multiple Ranges</strong>: Some browsers support multiple text selections on a single page, where you can select multiple text ranges and manipulate them simultaneously.</p>
</li>
<li><p><strong>Detecting Selection Changes</strong>: You can listen for <code>selectionchange</code> events on the document, allowing you to detect when the user changes their selection.</p>
<pre><code class="lang-typescript"> <span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"selectionchange"</span>, <span class="hljs-function">() =&gt;</span> {
     <span class="hljs-keyword">const</span> selection = <span class="hljs-built_in">window</span>.getSelection();
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Selection changed:"</span>, selection.toString());
 });
</code></pre>
</li>
<li><p><strong>Working with Forms</strong>: Selections can be useful in forms, allowing you to auto-complete, copy, or validate the user’s input based on selected text.</p>
</li>
</ol>
<p>The <strong>JavaScript Selection API</strong> is a powerful tool for creating dynamic and interactive web applications. Whether you want to implement custom copy/paste functionality, enable annotations, or build advanced text editors, the Selection API provides the control and flexibility you need to handle user selections.</p>
<p>With its easy-to-use methods and properties, you can enhance user experiences and create intuitive, selection-based features.</p>
<h2 id="heading-example-use-case-how-to-build-a-rich-text-editor-with-the-javascript-selection-api">Example Use Case: How to Build a Rich Text Editor with the JavaScript Selection API</h2>
<p>The Selection API not only allows you to interact with text selections but also opens the door to more advanced text manipulation, like creating a <strong>rich text editor</strong>. A rich text editor (RTE) allows users to format selected text with features such as bold, italic, and underline.</p>
<p>In this section, we'll walk through how to build a basic rich text editor using the Selection API and provide an example with key formatting features.</p>
<h3 id="heading-how-does-the-selection-api-help-build-a-rich-text-editor">How Does the Selection API Help Build a Rich Text Editor?</h3>
<p>The Selection API lets you:</p>
<ul>
<li><p>Detect the text selected by the user.</p>
</li>
<li><p>Programmatically modify the selected content, for example, applying bold, italic, or underlined styling.</p>
</li>
<li><p>Allow users to make in-place edits with simple UI controls (like buttons or keyboard shortcuts).</p>
</li>
</ul>
<p>By using the <code>window.getSelection()</code> and Range API, you can manipulate text based on user actions (such as clicking a "Bold" button). You can then wrap the selected text in appropriate HTML tags (<code>&lt;b&gt;</code>, <code>&lt;i&gt;</code>, <code>&lt;u&gt;</code>) or apply inline styles.</p>
<h3 id="heading-basic-rich-text-editor-features">Basic Rich Text Editor Features</h3>
<p>For our example, we'll implement three core formatting features:</p>
<ol>
<li><p><strong>Bold</strong>: Make the selected text bold.</p>
</li>
<li><p><strong>Italic</strong>: Italicize the selected text.</p>
</li>
<li><p><strong>Underline</strong>: Underline the selected text.</p>
</li>
</ol>
<h4 id="heading-html-structure">HTML Structure</h4>
<p>Here’s a simple layout for the editor with buttons for Bold, Italic, and Underline:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Simple Rich Text Editor<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-id">#editor</span> {
      <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ccc</span>;
      <span class="hljs-attribute">min-height</span>: <span class="hljs-number">150px</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">10px</span>;
    }
    <span class="hljs-selector-class">.toolbar</span> {
      <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">5px</span>;
    }
    <span class="hljs-selector-class">.toolbar</span> <span class="hljs-selector-tag">button</span> {
      <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">5px</span>;
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"toolbar"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"formatText('bold')"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">b</span>&gt;</span>Bold<span class="hljs-tag">&lt;/<span class="hljs-name">b</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"formatText('italic')"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Italic<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"formatText('underline')"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">u</span>&gt;</span>Underline<span class="hljs-tag">&lt;/<span class="hljs-name">u</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-comment">&lt;!-- Contenteditable div for the editor --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"editor"</span> <span class="hljs-attr">contenteditable</span>=<span class="hljs-string">"true"</span>&gt;</span>
    Type your text here...
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"editor.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h4 id="heading-javascript-handling-formatting-with-the-selection-api">JavaScript: Handling Formatting with the Selection API</h4>
<p>Now that we have the basic structure, let's add JavaScript to handle text formatting. We’ll use the <strong>Selection API</strong> and <code>document.execCommand()</code>, a legacy method still supported by most browsers, to apply formatting.</p>
<p>Here’s the JavaScript to make the buttons functional:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// Function to format text based on the command</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">formatText</span>(<span class="hljs-params">command</span>) </span>{
  <span class="hljs-built_in">document</span>.execCommand(command, <span class="hljs-literal">false</span>, <span class="hljs-literal">null</span>);
}
</code></pre>
<p>The <code>execCommand</code> method allows you to perform commands on the content inside an element that has the <code>contenteditable</code> attribute. In our case, the commands will be <code>'bold'</code>, <code>'italic'</code>, and <code>'underline'</code>.</p>
<h3 id="heading-the-documentexeccommand-function">The <code>document.execCommand()</code> Function</h3>
<p>The <code>document.execCommand()</code> function is a legacy method provided by browsers that allows developers to perform various document editing operations directly on content within an <code>contenteditable</code> element. This method has been widely used to build rich text editors for web applications due to its simplicity and browser support.</p>
<p>Though it's still functional in most modern browsers, it's worth noting that <code>execCommand</code> has been deprecated and may not be supported in future versions of browsers. But it still provides a good starting point for basic rich text editors.</p>
<p>If you're looking for a long-term solution, newer APIs like the Selection API combined with Range API or third-party libraries (like Quill.js and Draft.js) are recommended for complex editing needs.</p>
<h3 id="heading-what-is-documentexeccommand">What is <code>document.execCommand()</code>?</h3>
<p>The <code>document.execCommand()</code> the method executes a specified command for manipulating or formatting text in a <strong>contenteditable</strong> element (such as a div, textarea, or input field). It can perform commands such as applying styles, modifying text alignment, creating links, and much more.</p>
<h4 id="heading-syntax-of-the-execcommand">Syntax of the <code>execCommand()</code>:</h4>
<pre><code class="lang-typescript"><span class="hljs-built_in">document</span>.execCommand(command, showUI, value);
</code></pre>
<ul>
<li><p><code>command</code>: A string that represents the command to execute (for example, <code>'bold'</code>, <code>'italic'</code>, <code>'underline'</code>, <code>'createLink'</code>).</p>
</li>
<li><p><code>showUI</code>: A boolean value indicating whether the default user interface for the command should be shown (almost always <code>false</code>, as browser UIs are often inconsistent).</p>
</li>
<li><p><code>value</code>: Optional. A string representing the value to pass for certain commands (for example, the URL for creating a link).</p>
</li>
</ul>
<h4 id="heading-return-value">Return Value</h4>
<p><code>execCommand()</code> returns <code>true</code> if the command is successfully executed or <code>false</code> otherwise.</p>
<h3 id="heading-how-to-enhance-the-rich-text-editor">How to Enhance the Rich Text Editor</h3>
<p>While the example above gives you a basic rich text editor, you can expand its features by adding more controls and handling other commands:</p>
<ul>
<li><p><strong>Text Color</strong>: Change the color of the selected text using <code>execCommand('foreColor', false, 'red')</code>.</p>
</li>
<li><p><strong>Text Alignment</strong>: Align text left, center, or right using commands like <code>execCommand('justifyCenter')</code>.</p>
</li>
<li><p><strong>Undo/Redo</strong>: Implement undo and redo functionality using <code>execCommand('undo')</code> and <code>execCommand('redo')</code>.</p>
</li>
<li><p><strong>Adding Links</strong>: Allow users to add links with <code>execCommand('createLink', false, '</code><a target="_blank" href="http://example.com"><code>http://example.com</code></a><code>')</code>.</p>
</li>
</ul>
<p>Using the Selection API combined with <code>document.execCommand()</code>, we’ve built a simple, yet functional rich text editor with bold, italic, and underline features. This basic editor can be further enhanced with additional features like font size, color, and alignment to create a full-fledged rich text editor for your web applications.</p>
<h3 id="heading-how-to-get-the-clicked-element-and-its-real-time-position"><strong>How to Get the Clicked Element and its Real-Time Position</strong></h3>
<p>The simplest way to detect the clicked element in a webpage is by using the <code>click</code> event listener in JavaScript.</p>
<p>Here's how you can do it:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> clickedElement = event.target;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'You clicked on:'</span>, clickedElement.tagName);
});
</code></pre>
<h4 id="heading-code-explanation">Code explanation:</h4>
<ul>
<li><p><code>document.addEventListener('click', ...)</code>: This attaches an event listener to the whole document.</p>
</li>
<li><p><a target="_blank" href="http://event.target"><code>event.target</code></a>: This property returns the specific element that was clicked.</p>
</li>
<li><p><code>clickedElement.tagName</code>: This provides the tag name of the clicked element (like <code>DIV</code>, <code>SPAN</code>, <code>BUTTON</code>, etc.).</p>
</li>
</ul>
<p>This will log the element’s tag name to the console when you click anywhere on the document.</p>
<h3 id="heading-how-to-get-the-elements-realtime-position"><strong>How to Get the Element's Realtime Position</strong></h3>
<p>Once you have the clicked element, you can find its position on the screen using JavaScript’s DOM API. Specifically, <code>getBoundingClientRect()</code> gives us the element's position relative to the viewport.</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> clickedElement = event.target;
    <span class="hljs-keyword">const</span> position = clickedElement.getBoundingClientRect();

    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Element: <span class="hljs-subst">${clickedElement.tagName}</span>`</span>);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Position: Top - <span class="hljs-subst">${position.top}</span>px, Left - <span class="hljs-subst">${position.left}</span>px`</span>);
});
</code></pre>
<h4 id="heading-code-explanation-1">Code explanation:</h4>
<ul>
<li><p><code>getBoundingClientRect()</code>: This method returns the size of an element and its position relative to the viewport. It gives you several useful properties:</p>
<ul>
<li><p><code>top</code>: Distance from the top of the viewport.</p>
</li>
<li><p><code>left</code>: Distance from the left of the viewport.</p>
</li>
<li><p><code>right</code>: Distance from the left edge to the right edge of the element.</p>
</li>
<li><p><code>bottom</code>: Distance from the top edge to the bottom edge of the element.</p>
</li>
</ul>
</li>
</ul>
<p>The <code>top</code> and <code>left</code> values are usually the most useful, as they tell you where the element is positioned.</p>
<h3 id="heading-full-example-with-code"><strong>Full Example with Code</strong></h3>
<p>Let’s put everything together. We’ll create an interactive example where clicking on any element displays its tag name and position in a tooltip-like fashion.</p>
<p>Here’s the complete code:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Detect Clicked Element and Position<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-tag">body</span> {
            <span class="hljs-attribute">font-family</span>: Arial, sans-serif;
        }
        <span class="hljs-selector-class">.tooltip</span> {
            <span class="hljs-attribute">position</span>: absolute;
            <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#333</span>;
            <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;
            <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span>;
            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
            <span class="hljs-attribute">font-size</span>: <span class="hljs-number">12px</span>;
            <span class="hljs-attribute">display</span>: none;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"tooltip"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tooltip"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Click on elements to see their tag and position<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph. Click on it!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Click me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width: 100px; height: 100px; background-color: lightblue;"</span>&gt;</span>Click this box<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-keyword">const</span> tooltip = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'tooltip'</span>);

    <span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> clickedElement = event.target;
        <span class="hljs-keyword">const</span> position = clickedElement.getBoundingClientRect();

        <span class="hljs-comment">// Get the tag name of the clicked element</span>
        <span class="hljs-keyword">const</span> elementTag = clickedElement.tagName;

        <span class="hljs-comment">// Get the element's current position</span>
        <span class="hljs-keyword">const</span> top = position.top + <span class="hljs-built_in">window</span>.scrollY; <span class="hljs-comment">// Account for page scroll</span>
        <span class="hljs-keyword">const</span> left = position.left + <span class="hljs-built_in">window</span>.scrollX;

        <span class="hljs-comment">// Display the tooltip near the clicked element</span>
        tooltip.innerHTML = <span class="hljs-string">`Tag: <span class="hljs-subst">${elementTag}</span>&lt;br&gt;Position: Top - <span class="hljs-subst">${<span class="hljs-built_in">Math</span>.round(top)}</span>px, Left - <span class="hljs-subst">${<span class="hljs-built_in">Math</span>.round(left)}</span>px`</span>;
        tooltip.style.display = <span class="hljs-string">'block'</span>;
        tooltip.style.top = <span class="hljs-string">`<span class="hljs-subst">${top + <span class="hljs-number">20</span>}</span>px`</span>; <span class="hljs-comment">// Offset to position below the element</span>
        tooltip.style.left = <span class="hljs-string">`<span class="hljs-subst">${left + <span class="hljs-number">20</span>}</span>px`</span>; <span class="hljs-comment">// Offset to position to the right of the element</span>
    });

    <span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'scroll'</span>, <span class="hljs-function">() =&gt;</span> {
        tooltip.style.display = <span class="hljs-string">'none'</span>; <span class="hljs-comment">// Hide the tooltip when the user scrolls</span>
    });
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-2">Code explanation:</h4>
<ol>
<li><p><strong>HTML structure</strong>:</p>
<ul>
<li><p>The <code>tooltip</code> div is initially hidden but will be shown dynamically when a user clicks on an element.</p>
</li>
<li><p>We’ve included some clickable elements (<code>h1</code>, <code>p</code>, <code>button</code>, <code>div</code>) for demonstration purposes.</p>
</li>
</ul>
</li>
<li><p><strong>JavaScript</strong>:</p>
<ul>
<li><p>When any element is clicked, we calculate its tag name and position using <a target="_blank" href="http://event.target"><code>event.target</code></a> and <code>getBoundingClientRect()</code>.</p>
</li>
<li><p>We update the <code>tooltip</code> content and move it dynamically based on the element’s position.</p>
</li>
<li><p><code>window.scrollY</code> and <code>window.scrollX</code> are added to account for any scrolling that has occurred on the page.</p>
</li>
</ul>
</li>
<li><p><strong>CSS</strong>:</p>
<ul>
<li><p>The tooltip is styled as a small box with a dark background and white text. It is initially hidden (<code>display: none</code>).</p>
</li>
<li><p>When an element is clicked, the tooltip is positioned near the clicked element by adjusting its <code>top</code> and <code>left</code> styles.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-live-example">Live Example:</h3>
<p>Click anywhere on the webpage, and you’ll see the tag name and position of the clicked element displayed in a tooltip. This method is beneficial for creating custom interactions, debugging, or handling complex layouts in modern web applications.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this tutorial, we explored how to use the <strong>JavaScript Selection API</strong> to interact with text the user selects and manipulate it programmatically. We also learned about the <code>document.execCommand()</code> function, which, despite being deprecated, allows us to apply basic text formatting like bold, italic, and underline to the selected content.</p>
<p>We demonstrated how to build a simple rich text editor with basic features using these tools. We also covered how to detect which HTML element was clicked by using the <code>click</code> event and accessing the <a target="_blank" href="http://event.target"><code>event.target</code></a> property.</p>
<p>These techniques form the foundation for creating dynamic, interactive web text editors.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Event Handling Works in Vue 3: A Guide for Developers ]]>
                </title>
                <description>
                    <![CDATA[ Event handling in Vue 3 allows developers to respond to user interactions like clicks, key presses, form submissions, and more. Vue provides simple and flexible ways to manage these interactions, enabling you to build dynamic and engaging application... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-event-handling-works-in-vue-3-guide-for-devs/</link>
                <guid isPermaLink="false">66e1d18aee859f8ea9224bdb</guid>
                
                    <category>
                        <![CDATA[ Vue.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Vue3 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ event handling ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Asfak Ahmed ]]>
                </dc:creator>
                <pubDate>Wed, 11 Sep 2024 17:21:14 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1725980520061/87728aa1-f3c5-451d-9f11-5163f527d029.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Event handling in Vue 3 allows developers to respond to user interactions like clicks, key presses, form submissions, and more.</p>
<p>Vue provides simple and flexible ways to manage these interactions, enabling you to build dynamic and engaging applications.</p>
<h3 id="heading-in-this-guide-well-cover">In this guide, we'll cover:</h3>
<ul>
<li><p>Basic event handling (for example, <code>click</code> events)</p>
</li>
<li><p>Event modifiers like <code>.prevent</code>, <code>.once</code>, and <code>.stop</code></p>
</li>
<li><p>Custom events between parent and child components</p>
</li>
<li><p>Handling events in forms</p>
</li>
<li><p>Keyboard events</p>
</li>
<li><p>The basics of <code>emit</code></p>
</li>
<li><p>The basics of <code>v-model</code></p>
</li>
</ul>
<p>By the end, you'll be able to handle a wide range of events and improve user interaction in your Vue applications.</p>
<h3 id="heading-basic-event-handling">Basic Event Handling</h3>
<p>Vue makes it easy to handle basic events like <code>click</code>, <code>input</code>, and <code>submit</code> directly in your template. You can use the <code>@</code> symbol (short for <code>v-on</code>) to listen for events on DOM elements.</p>
<h4 id="heading-example-handling-a-click-event">Example: Handling a Click Event</h4>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"handleClick"</span>&gt;</span>Click Me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> message = ref(<span class="hljs-string">'Hello, Vue 3!'</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleClick</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Button Clicked!'</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation">Code explanation:</h4>
<ul>
<li><p><code>@click="handleClick"</code>: The <code>@</code> symbol is shorthand for <code>v-on</code>. It listens for the <code>click</code> event and calls the <code>handleClick</code> method when the button is clicked.</p>
</li>
<li><p><code>message.value = 'Button Clicked!'</code>: In Vue 3's Composition API, <code>ref</code> creates reactive variables. When the button is clicked, the <code>message</code> reacts to variable updates, and the change is reflected in the DOM automatically.</p>
</li>
</ul>
<p>This simple mechanism of listening to events and binding methods is foundational to handling user interactions in Vue.</p>
<h3 id="heading-event-modifiers">Event Modifiers</h3>
<p>Vue event modifiers allow you to control how events are handled, preventing default behavior or stopping propagation, for example. Common event modifiers include <code>.prevent</code>, <code>.stop</code>, <code>.once</code>, <code>.capture</code>, and <code>.passive</code>.</p>
<h4 id="heading-1-the-prevent-modifier">1. The <code>.prevent</code> Modifier</h4>
<p>The <code>.prevent</code> modifier calls <code>event.preventDefault()</code>, preventing the default behavior of events like form submission.</p>
<h5 id="heading-example-using-prevent-to-handle-form-submission">Example: Using <code>.prevent</code> to Handle Form Submission</h5>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> @<span class="hljs-attr">submit.prevent</span>=<span class="hljs-string">"handleSubmit"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"inputValue"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ output }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> inputValue = ref(<span class="hljs-string">''</span>);
<span class="hljs-keyword">const</span> output = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleSubmit</span>(<span class="hljs-params"></span>) </span>{
  output.value = <span class="hljs-string">`Form submitted with value: <span class="hljs-subst">${inputValue.value}</span>`</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-1">Code explanation:</h4>
<ul>
<li><p><code>@submit.prevent</code>: Prevents the form from refreshing the page when submitted, allowing the <code>handleSubmit</code> function to process the form data instead.</p>
</li>
<li><p><code>v-model="inputValue"</code>: Two-way data binding between the form input and the <code>inputValue</code> reactive variable. It updates in real time as the user types.</p>
</li>
</ul>
<p><strong>When to use</strong> <code>.prevent</code><strong>:</strong> Use <code>.prevent</code> when handling forms or other elements where you want to prevent the default behavior, such as preventing links from navigating.</p>
<h4 id="heading-2-the-stop-modifier">2. The <code>.stop</code> Modifier</h4>
<p>The <code>.stop</code> modifier calls <code>event.stopPropagation()</code>, preventing the event from bubbling up to parent elements.</p>
<h5 id="heading-example-prevent-event-bubbling">Example: Prevent Event Bubbling</h5>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"handleDivClick"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click.stop</span>=<span class="hljs-string">"handleButtonClick"</span>&gt;</span>Click Me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> message = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleDivClick</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Div clicked!'</span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleButtonClick</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Button clicked!'</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-2">Code explanation:</h4>
<ul>
<li><code>.stop</code>: Clicking the button only triggers <code>handleButtonClick</code> and prevents the click from propagating to the parent <code>div</code>. Without <code>.stop</code>, clicking the button would also trigger <code>handleDivClick</code>.</li>
</ul>
<p><strong>When to use</strong> <code>.stop</code><strong>:</strong> Use it to prevent parent elements from reacting to child element events.</p>
<h4 id="heading-3-the-once-modifier">3. The <code>.once</code> Modifier</h4>
<p>The <code>.once</code> modifier ensures that the event listener is only called once.</p>
<h5 id="heading-example-handling-a-click-event-once">Example: Handling a Click Event Once</h5>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click.once</span>=<span class="hljs-string">"handleClickOnce"</span>&gt;</span>Click Me Once<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> message = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleClickOnce</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Button clicked once!'</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-3">Code explanation:</h4>
<ul>
<li><code>.once</code>: The <code>handleClickOnce</code> method is triggered the first time the button is clicked. Subsequent clicks do nothing because the event listener is removed after the first execution.</li>
</ul>
<p><strong>When to use</strong> <code>.once</code><strong>:</strong> Use it for actions that should only happen once, such as a one-time form submission.</p>
<h4 id="heading-4-the-capture-modifier">4. The <code>.capture</code> Modifier</h4>
<p>The <code>.capture</code> modifier makes the event handler trigger during the capture phase rather than the bubbling phase.</p>
<h5 id="heading-example-handling-an-event-in-the-capture-phase">Example: Handling an Event in the Capture Phase</h5>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> @<span class="hljs-attr">click.capture</span>=<span class="hljs-string">"handleClickCapture"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"handleClickButton"</span>&gt;</span>Click Me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> message = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleClickCapture</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Click event captured!'</span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleClickButton</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Button clicked!'</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-4">Code explanation:</h4>
<ul>
<li><code>.capture</code>: The click on the parent <code>div</code> is handled first, before the child button’s click event, because the <code>capture</code> phase happens before the bubbling phase.</li>
</ul>
<p><strong>When to use</strong> <code>.capture</code><strong>:</strong> Useful when you need to intercept an event before it reaches its target.</p>
<h3 id="heading-custom-events">Custom Events</h3>
<p>In Vue, child components can emit custom events to communicate with parent components. This pattern is commonly used to pass data or trigger methods in parent components.</p>
<h4 id="heading-example-emitting-and-handling-custom-events">Example: Emitting and Handling Custom Events</h4>
<p><code>ParentComponent.vue</code>:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> @<span class="hljs-attr">custom-event</span>=<span class="hljs-string">"handleCustomEvent"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ parentMessage }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
<span class="hljs-keyword">import</span> ChildComponent <span class="hljs-keyword">from</span> <span class="hljs-string">'./ChildComponent.vue'</span>;

<span class="hljs-keyword">const</span> parentMessage = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleCustomEvent</span>(<span class="hljs-params">payload</span>) </span>{
  parentMessage.value = <span class="hljs-string">`Received custom event with payload: <span class="hljs-subst">${payload}</span>`</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p><code>ChildComponent.vue</code>:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"emitCustomEvent"</span>&gt;</span>Emit Custom Event<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { defineEmits } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> emit = defineEmits();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">emitCustomEvent</span>(<span class="hljs-params"></span>) </span>{
  emit(<span class="hljs-string">'custom-event'</span>, <span class="hljs-string">'Hello from ChildComponent'</span>);
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-5">Code explanation:</h4>
<ul>
<li><p><code>defineEmits()</code>: This is used in the child component to define custom events. Here, the child emits a <code>custom-event</code> with a payload of <code>'Hello from ChildComponent'</code>. (<a target="_blank" href="https://asfakahmedsblog.hashnode.dev/understanding-vuejs-emit-a-complete-guide">you can learn more details of emit from here</a>)</p>
</li>
<li><p><strong>Event Handling in Parent</strong>: The parent component listens for <code>custom-event</code> and responds by updating its <code>parentMessage</code> with the event payload.</p>
</li>
</ul>
<p><strong>When to use custom events:</strong> Use them for communication between parent and child components, especially for passing data from child to parent.</p>
<h3 id="heading-event-handling-in-forms">Event Handling in Forms</h3>
<p>Vue’s <code>v-model</code> simplifies handling form inputs by creating two-way data binding between the form field and a data variable.</p>
<h4 id="heading-example-handling-input-and-form-submission">Example: Handling Input and Form Submission</h4>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> @<span class="hljs-attr">submit.prevent</span>=<span class="hljs-string">"handleSubmit"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"formData.name"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Name"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"formData.email"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Email"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ formOutput }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> formData = ref({ <span class="hljs-attr">name</span>: <span class="hljs-string">''</span>, <span class="hljs-attr">email</span>: <span class="hljs-string">''</span> });
<span class="hljs-keyword">const</span> formOutput = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleSubmit</span>(<span class="hljs-params"></span>) </span>{
  formOutput.value = <span class="hljs-string">`Submitted Name: <span class="hljs-subst">${formData.value.name}</span>, Email: <span class="hljs-subst">${formData.value.email}</span>`</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-6">Code explanation:</h4>
<ul>
<li><p><code>v-model="</code><a target="_blank" href="http://formData.name"><code>formData.name</code></a><code>"</code>: This binds the input field directly to the <a target="_blank" href="http://formData.name"><code>formData.name</code></a> variable, allowing automatic updates as the user types. (<a target="_blank" href="https://asfakahmedsblog.hashnode.dev/understanding-vuejs-v-model-a-complete-guide">you can learn more details of v-model from here</a>)</p>
</li>
<li><p>The <code>handleSubmit</code> method processes the form data and displays it in the paragraph below the form.</p>
</li>
</ul>
<h3 id="heading-keyboard-events">Keyboard Events</h3>
<p>Vue also makes it easy to handle keyboard events such as <code>keydown</code>, <code>keyup</code>, and <code>keypress</code>.</p>
<h4 id="heading-example-handling-keyboard-events">Example: Handling Keyboard Events</h4>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">"handleEnterKey"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Press Enter"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">setup</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;

<span class="hljs-keyword">const</span> message = ref(<span class="hljs-string">''</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleEnterKey</span>(<span class="hljs-params"></span>) </span>{
  message.value = <span class="hljs-string">'Enter key pressed!'</span>;
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h4 id="heading-code-explanation-7">Code explanation:</h4>
<ul>
<li><code>@keydown.enter</code>: Listens for the <code>enter</code> keypress and triggers the <code>handleEnterKey</code> function when pressed. This is useful for form submissions or other actions that should be triggered by a specific key press.</li>
</ul>
<h3 id="heading-wrapping-up">Wrapping Up</h3>
<p>Event handling in Vue 3 is pretty straightforward and flexible. From basic click events to custom events and form handling, Vue's event system allows you to create interactive, dynamic applications.</p>
<p>By using event modifiers and custom events, you can fine-tune how events are handled in your app. Understanding these techniques will enable you to create responsive, user-friendly interfaces.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
