Logo

dev-resources.site

for different kinds of informations.

Hyva : How to create Read More Read Less Button

Published at
12/18/2024
Categories
ecommerce
hyva
magneto
magentodevelopment
Author
evrig_solutions_
Author
16 person written this
evrig_solutions_
open
Hyva : How to create Read More Read Less Button

In this tutorial, we’ll walk you through how to easily create a “Read More Read Less” button in your Hyva Magento2 theme using Tailwind CSS and Alpine JS. This functionality enhances user experience by allowing users to toggle content visibility, making your page cleaner and more interactive.

Step-1. You just copy and paste the below code into your PHTML File, CMS block, or CMS Page.

You have two options for implementing the “Read More Read Less” button: with or without specifying height. Choose the approach that best fits your requirements.

Option: 1 WITHOUT height

<div x-data="{ isCollapsed: false, maxLength: 20, originalContent: '', content: '' }"
   x-init="originalContent = $el.firstElementChild.textContent.trim(); content = originalContent.slice(0, maxLength)"
>
   <span x-text="isCollapsed ? originalContent : content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed</span>
   <button
       @click="isCollapsed = !isCollapsed"
       x-show="originalContent.length > maxLength"
       x-text="isCollapsed ? 'Show less' : 'Show more'"
       class="cursor-pointer uppercase font-bold"
   >
   </button>
</div>
Enter fullscreen mode Exit fullscreen mode

Option: 2 WITH height

<div x-data="{ expanded: false }">
   <div class="relative" x-bind:class="{'max-h-48 overflow-hidden': !expanded}" x-ref="container" x-bind:style="expanded ? 'max-height: ' + ($refs.container.offsetHeight + 20) + 'px; padding-bottom: 40px;' : ''">
       <p class="pb-10" x-bind:class="{'pb-0': expanded}">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
       <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
       <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
       <div class="text-primary-500 text-center cursor-pointer uppercase font-bold absolute left-0 right-0 z-20 mx-auto bottom-0" @click="expanded = !expanded">
           <span x-text="expanded ? 'Read Less' : 'Read More'"></span>
       </div>
   </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Step-2. Run the below commands to see your changes
Once you’ve added the code, you need to run the following commands to see the changes in your Magento2 theme.

1.Clear the cache:

bin/magento cache:clean
Enter fullscreen mode Exit fullscreen mode

2.Build your Tailwind CSS:

npm run build-prod
Enter fullscreen mode Exit fullscreen mode

These commands ensure that the changes take effect on your site.

Advantages to adding the Read More Read Less Button:

  • Achieve this without adding any external JS/CSS.
  • Minimal use of Tailwind classes and Alpine.
  • Customizable: You can change the design to meet your requirements.
  • Responsive: This code adapts its layout to different screen sizes, ensuring your code looks great on all devices.
  • Customers can’t scroll more if they don’t want to read the full content

We hope this tutorial makes it easy for you to integrate a “Read More Read Less” button into your Hyva Magento 2 theme. It’s a simple yet effective way to enhance content visibility and improve the user experience on your site.

At Evrig, we specialize in creating high-quality Magento stores that are both functional and user-friendly. If you’re implementing custom features like the “Read More Read Less” button or need any other Magento development assistance, our team of experts is ready to support you every step of the way.

Get in touch with us today to discuss how we can help you elevate your e-commerce website with custom Magento solutions, tailored design and optimal performance.

ecommerce Article's
30 articles in total
Favicon
Hire Shopify Ecommerce Development Company | Aron Web Solutions
Favicon
Maximize eCommerce Growth with Customer Retention
Favicon
Boost Your eCommerce Success with the Best Magento 2 Extensions
Favicon
Hiding WooCommerce Cart when empty by Enqueueing JavaScript
Favicon
The Hidden Challenges of Cross-Border E-Commerce and How to Overcome Them
Favicon
Frishay.com: Redefining Online Shopping with Unbeatable Offers
Favicon
Streamline Your Multi-Brand eCommerce Operations with Hyvä and Magento 2
Favicon
Custom Ecommerce Solutions in India | Aron Web Solutions
Favicon
Boost Your eCommerce Success with the Best Magento 2 Extensions
Favicon
Custom Ecommerce Solutions for Small Business | Aron Web Solutions
Favicon
ecommerce
Favicon
Enhance Your Spurtcommerce Platform with Premium Extensions
Favicon
Key Features to Consider when Building a Robust e-commerce Platform
Favicon
Dynamic and responsive: A Shopify eCommerce website
Favicon
What is a Repeat Customer and How can you increase them?
Favicon
Role of Sustainable Practices in Indian E-Commerce Web Development in the Last Two Years
Favicon
Usability Testing : Enhancing User Experience for an E-commerce Website
Favicon
Future-Proofing Your Business in the B2B eCommerce World
Favicon
Woxro: Your ideal partner in India for Ecommerce development
Favicon
Hyva : How to create Read More Read Less Button
Favicon
Hiring Ecommerce App Developers: Skills, Roles & Things to Consider
Favicon
Zermatt.dev: Style it your Way with SASS or TailwindCSS
Favicon
Yotpo's Seamless Integration with Google Merchant
Favicon
Grab’s CEO Anthony Tan shares his top advice for those looking to become entrepreneurs
Favicon
Custom eCommerce Development Services by Rankshell
Favicon
5 Inspiring Loyalty Programs That Boost Customer Retention
Favicon
Building an Open Source Seller Center in Public
Favicon
Ecommerce Trends To Watch Out In 2025
Favicon
Understanding Laravel Cashier's Core Traits: A Deep Dive
Favicon
Best Programming Languages to Build an E-Commerce Website

Featured ones: