ink icon-with-text" role="tab" aria-controls="TabPanel-{{ block.id }}-{{ forloopindex…
- User
ink icon-with-text" role="tab" aria-controls="TabPanel-{{ block.id }}-{{ forloop_index }}" data-index="{{ forloop_index }}"{% if forloop_first %} disabled{% endif %}> {{- tab_heading -}} {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- else -%} {{- tab_heading -}} {%- endif -%} {%- endif -%} {%- endfor -%} {%- if block.settings.button_label != blank -%} <a class="mega-menu__link link icon-with-text flex items-center justify-between w-full h-full"{% if block.settings.button_link != blank %} href="{{ block.settings.button_link }}"{% if block.settings.button_external %} target="blank" rel="noopener"{% endif %}{% else %} role="link" aria-disabled="true"{% endif %}> {{ block.settings.button_label | escape }} {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} {%- endif -%} {%- liquid assign forloop_first = true assign forloop_index = 0 -%} {%- for i in (1..5) -%} {%- assign tab_target_link = 'tab_target_link' | append: i -%} {%- if block.settings[tab_target_link] != blank and block.settings[tab_target_link].type == 'collection_link' -%} {%- liquid assign promo_image = 'promo_image_' | append: i assign tab_target = 'tab_target_' | append: i assign tab_target_heading = block.settings[tab_target] | default: block.settings[tab_target_link].object.title assign tab_products = 'tab_products_' | append: i assign products = block.settings[tab_products] | default: block.settings[tab_target_link].object.products -%} <div id="TabPanel-{{ block.id }}-{{ forloop_index }}" class="mega-menu__panel{% if block.settings[promo_image] == blank %} without-promo{% endif %} flex w-full" role="tabpanel"{% unless forloop_first %} hidden{% endunless %}> {%- if block.settings.heading_2 != blank -%} {{ block.settings.heading_2 | escape }} {%- endif -%} {{- 'collections.subcollections.all_products' | t: collection: tab_target_heading -}} ({{ block.settings[tab_target_link].object.all_products_count }}) {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} {%- for product in products limit: 4 -%} {%- render 'product-card', product: product, product_id: product.id, section_id: section.id, block_id: block.id, lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, show_icon_list: false -%} {%- else -%} {%- for i in (1..4) -%} {%- capture placeholder -%}product-{% cycle '1', '2', '3', '4' %}{%- endcapture -%} {%- render 'product-card-placeholder', lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, placeholder: placeholder -%} {%- endfor -%} {%- endfor -%} {%- if block.settings[promo_image] != blank -%} {%- liquid assign promo_image_ratio = 'promo_image_ratio_' | append: i assign promo_heading = 'promo_heading_' | append: i assign promo_subtext = 'promo_subtext_' | append: i assign promo_overlay_content = 'promo_overlay_content_' | append: i assign promo_link = 'promo_link_' | append: i assign promo_tag = 'promo_tag_' | append: i assign promo_color_text = 'promo_color_text_' | append: i assign promo_color_overlay = 'promo_color_overlay_' | append: i assign promo_overlay_opacity = 'promo_overlay_opacity_' | append: i -%} <a class="media-card__link flex flex-col w-full h-full relative"{% if block.settings[promo_link] != blank %} href="{{ block.settings[promo_link] }}"{% else %} role="link" aria-disabled="true"{% endif %} aria-label="{{ block.settings[promo_heading] | default: block.settings[promo_subtext] | escape }}" style="--color-foreground: {{ block.settings[promo_color_text].rgb }}; --color-overlay: {{ block.settings[promo_color_overlay].rgb }}; --overlay-opacity: {{ block.settings[promo_overlay_opacity] | divided_by: 100.0 }};" > {%- if block.settings[promo_tag] != blank -%} {{ block.settings[promo_tag] | escape }} {%- endif -%} {{- block.settings[promo_image] | image_url: width: block.settings[promo_image].width | image_tag: loading: 'lazy', widths: '180,360,540,720,900,1080', is: 'lazy-image' -}} {%- if block.settings[promo_heading] != blank or block.settings[promo_subtext] != blank -%} {%- if block.settings[promo_heading] != blank -%} {{ block.settings[promo_heading] | escape }} {%- endif -%} {%- if block.settings[promo_subtext] != blank -%} {{ block.settings[promo_subtext] }} {%- endif -%} {%- render 'icon', icon: 'arrow-right', size: 'xs', class: 'transform shrink-0 hidden xl:block' -%} {%- endif -%} {%- endif -%} {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- endif -%} {%- endfor -%} {%- endunless -%}
Assistant
To add an alphabet and 0-9 number filtering feature to your mega menu, you'll need to:
-
Modify the Liquid:
- Add a new section for the filter buttons (0-9, A-Z, All).
- Add a
data-first-charattribute to each tab navigation item (buttonora) so JavaScript can identify its starting character. - Wrap the existing tab navigation items in a container to make them easier to target with JavaScript.
-
Add JavaScript:
- Listen for clicks on the filter buttons.
- When a filter button is clicked, iterate through the tab navigation items and show/hide them based on their
data-first-charattribute.
-
Add CSS:
- A simple class to hide elements.
- Basic styling for the filter buttons.
Here's how you can modify your code:
{%- if block.settings.menu != blank -%} <div class="mega-menu__content flex-1 max-w-lg mx-auto overflow-hidden"> {%- unless block.settings.menu.empty? -%} <ul class="flex flex-col gap-8"> <li class="mega-menu__item mega-menu__item--nav opacity-0"> <!-- NEW: Filter Buttons (0-9, A-Z, All) --> <div class="mega-menu__filter-bar flex flex-wrap gap-2 mb-4"> <button type="button" class="filter-button button button--small" data-filter="all">All</button> {%- for i in (0..9) -%} <button type="button" class="filter-button button button--small" data-filter="{{ i }}">{{ i }}</button> {%- endfor -%} {%- for i in (97..122) -%} {# ASCII a-z #} {%- assign char = i | char -%} <button type="button" class="filter-button button button--small" data-filter="{{ char | upcase }}">{{ char | upcase }}</button> {%- endfor -%} </div> <!-- END NEW: Filter Buttons --> <tabs-element class="mega-menu__nav tabs-element text-sm text-opacity leading-none uppercase"> <!-- NEW: Wrapper for navigation items --> <div class="mega-menu__nav-items-wrapper grid gap-3"> {%- liquid assign forloop_first = true assign forloop_index = 0 -%} {%- for i in (1..5) -%} {%- assign tab_target = 'tab_target_' | append: i -%} {%- assign tab_target_link = 'tab_target_link_' | append: i -%} {%- assign tab_heading = block.settings[tab_target] -%} {%- if block.settings[tab_target_link] != blank and block.settings[tab_target_link].type == 'collection_link' -%} {%- if tab_heading == blank -%} {%- assign tab_heading = block.settings[tab_target_link].object.title -%} {%- endif -%} {% comment %} Add data-first-char attribute for filtering {% endcomment %} {%- assign first_char = tab_heading | strip_html | truncatewords: 1, '' | slice: 0 | upcase -%} {%- if block.settings.use_tabs -%} <button type="button" class="mega-menu__nav-item link icon-with-text" role="tab" aria-controls="TabPanel-{{ block.id }}-{{ forloop_index }}" data-index="{{ forloop_index }}" {% if forloop_first %} disabled{% endif %} data-first-char="{{ first_char }}" {# NEW #} > {{- tab_heading -}} </button> {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- else -%} <a href="{{ block.settings[tab_target_link].url }}" class="mega-menu__nav-item flex gap-2 items-center opacity-0 link icon-with-text" data-first-char="{{ first_char }}" {# NEW #} > {{- tab_heading -}} </a> {%- endif -%} {%- endif -%} {%- endfor -%} </div> <!-- END NEW: Wrapper --> </tabs-element> {%- if block.settings.button_label != blank -%} <div class="mega-menu__footer relative"> <a class="mega-menu__link link icon-with-text flex items-center justify-between w-full h-full"{% if block.settings.button_link != blank %} href="{{ block.settings.button_link }}"{% if block.settings.button_external %} target="_blank" rel="noopener"{% endif %}{% else %} role="link" aria-disabled="true"{% endif %}> <span class="heading link-text text-base-xl tracking-tight">{{ block.settings.button_label | escape }}</span> {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} </a> </div> {%- endif -%} </li> <li class="mega-menu__item mega-menu__item--panels opacity-0 w-full"> {%- liquid assign forloop_first = true assign forloop_index = 0 -%} {%- for i in (1..5) -%} {%- assign tab_target_link = 'tab_target_link_' | append: i -%} {%- if block.settings[tab_target_link] != blank and block.settings[tab_target_link].type == 'collection_link' -%} {%- liquid assign promo_image = 'promo_image_' | append: i assign tab_target = 'tab_target_' | append: i assign tab_target_heading = block.settings[tab_target] | default: block.settings[tab_target_link].object.title assign tab_products = 'tab_products_' | append: i assign products = block.settings[tab_products] | default: block.settings[tab_target_link].object.products -%} <div id="TabPanel-{{ block.id }}-{{ forloop_index }}" class="mega-menu__panel{% if block.settings[promo_image] == blank %} without-promo{% endif %} flex w-full" role="tabpanel"{% unless forloop_first %} hidden{% endunless %}> <div class="grid gap-8"> <div class="flex items-center justify-between gap-4"> {%- if block.settings.heading_2 != blank -%} <p class="text-sm text-opacity leading-none uppercase">{{ block.settings.heading_2 | escape }}</p> {%- endif -%} <a class="mega-menu__link link icon-with-text" href="{{ block.settings[tab_target_link].object.url }}"> <span class="heading link-text text-base-xl tracking-tight flex items-center gap-1"> {{- 'collections.subcollections.all_products' | t: collection: tab_target_heading -}} <span class="text-base font-medium tracking-tight">({{ block.settings[tab_target_link].object.all_products_count }})</span> </span> {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} </a> </div> <motion-list class="product-grid card-grid card-grid--4 mobile:card-grid--1 grid" initialized> {%- for product in products limit: 4 -%} {%- render 'product-card', product: product, product_id: product.id, section_id: section.id, block_id: block.id, lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, show_icon_list: false -%} {%- else -%} {%- for i in (1..4) -%} {%- capture placeholder -%}product-{% cycle '1', '2', '3', '4' %}{%- endcapture -%} {%- render 'product-card-placeholder', lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, placeholder: placeholder -%} {%- endfor -%} {%- endfor -%} </motion-list> </div> {%- if block.settings[promo_image] != blank -%} {%- liquid assign promo_image_ratio = 'promo_image_ratio_' | append: i assign promo_heading = 'promo_heading_' | append: i assign promo_subtext = 'promo_subtext_' | append: i assign promo_overlay_content = 'promo_overlay_content_' | append: i assign promo_link = 'promo_link_' | append: i assign promo_tag = 'promo_tag_' | append: i assign promo_color_text = 'promo_color_text_' | append: i assign promo_color_overlay = 'promo_color_overlay_' | append: i assign promo_overlay_opacity = 'promo_overlay_opacity_' | append: i -%} <div class="media-card media-card--{{ settings.card_style }}{% if block.settings[promo_overlay_content] %} media-card--overlap{% endif %}"> <a class="media-card__link flex flex-col w-full h-full relative"{% if block.settings[promo_link] != blank %} href="{{ block.settings[promo_link] }}"{% else %} role="link" aria-disabled="true"{% endif %} aria-label="{{ block.settings[promo_heading] | default: block.settings[promo_subtext] | escape }}" style="--color-foreground: {{ block.settings[promo_color_text].rgb }}; --color-overlay: {{ block.settings[promo_color_overlay].rgb }}; --overlay-opacity: {{ block.settings[promo_overlay_opacity] | divided_by: 100.0 }};" > {%- if block.settings[promo_tag] != blank -%} <div class="badges z-2 absolute grid gap-3 pointer-events-none"> <span class="badge badge--onsale flex items-center gap-1d5 font-medium leading-none rounded-full">{{ block.settings[promo_tag] | escape }}</span> </div> {%- endif -%} <div class="media media--{{ block.settings[promo_image_ratio] }} relative overflow-hidden"> {{- block.settings[promo_image] | image_url: width: block.settings[promo_image].width | image_tag: loading: 'lazy', widths: '180,360,540,720,900,1080', is: 'lazy-image' -}} </div> {%- if block.settings[promo_heading] != blank or block.settings[promo_subtext] != blank -%} <div class="media-card__content flex justify-between items-center gap-4 w-full"> <div class="media-card__text opacity-0 shrink-1 grid gap-0d5"> {%- if block.settings[promo_heading] != blank -%} <p> <span class="heading reversed-link text-xl-3xl tracking-tighter leading-tight">{{ block.settings[promo_heading] | escape }}</span> </p> {%- endif -%} {%- if block.settings[promo_subtext] != blank -%} <p class="leading-none text-xs">{{ block.settings[promo_subtext] }}</p> {%- endif -%} </div> {%- render 'icon', icon: 'arrow-right', size: 'xs', class: 'transform shrink-0 hidden xl:block' -%} </div> {%- endif -%} </a> </div> {%- endif -%} </div> {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- endif -%} {%- endfor -%} </li> </ul> {%- endunless -%} </div> {%- endif -%} <style> /* Basic styling for filter buttons */ .mega-menu__filter-bar .filter-button { background-color: var(--color-background); color: var(--color-foreground); border: 1px solid var(--color-foreground); padding: 0.5rem 0.8rem; border-radius: 4px; cursor: pointer; font-size: 0.875rem; /* text-sm */ line-height: 1; } .mega-menu__filter-bar .filter-button.active { background-color: var(--color-primary); /* Or a highlight color */ color: var(--color-primary-text); border-color: var(--color-primary); } /* Class to hide elements */ .is-hidden { display: none !important; } </style> <script> document.addEventListener('DOMContentLoaded', () => { const filterButtons = document.querySelectorAll('.mega-menu__filter-bar .filter-button'); const navItemsWrapper = document.querySelector('.mega-menu__nav-items-wrapper'); const navItems = navItemsWrapper ? navItemsWrapper.querySelectorAll('.mega-menu__nav-item') : []; if (!filterButtons.length || !navItems.length) { return; // Exit if elements not found } // Function to apply filter const applyFilter = (filterChar) => { navItems.forEach(item => { const itemFirstChar = item.dataset.firstChar; if (filterChar === 'all' || itemFirstChar === filterChar) { item.classList.remove('is-hidden'); } else { item.classList.add('is-hidden'); } }); }; // Add click listeners to filter buttons filterButtons.forEach(button => { button.addEventListener('click', () => { // Remove active class from all buttons filterButtons.forEach(btn => btn.classList.remove('active')); // Add active class to clicked button button.classList.add('active'); const filterValue = button.dataset.filter; applyFilter(filterValue); }); }); // Optionally, activate the "All" filter by default on load const allButton = document.querySelector('.mega-menu__filter-bar .filter-button[data-filter="all"]'); if (allButton) { allButton.click(); // Simulate a click to show all initially } }); </script>Explanation of Changes:
-
Liquid Modifications:
- Filter Bar HTML: A new
divwith classmega-menu__filter-baris added before thetabs-element. Inside it, "All" and loops for 0-9 and A-Z createbuttonelements. Each button has adata-filterattribute corresponding to its character. data-first-charAttribute:- Inside your existing
for i in (1..5)loop, for eachbuttonorawith the classmega-menu__nav-item, a newdata-first-char="{{ first_char }}"attribute is added. first_charis derived fromtab_headingby:strip_html: Removes any HTML tags that might be in the heading.truncatewords: 1, '': Keeps only the first word (and removes the ellipsis thattruncatewordsmight add). This is a robust way to get the very first part of the string.slice: 0: Takes the very first character of that string.upcase: Converts it to uppercase for consistent comparison (since our filter buttons are also uppercase).
- Inside your existing
mega-menu__nav-items-wrapper: The entire block of tab navigation items (buttons andatags) is wrapped in adivwith this new class. This makes it easier for JavaScript to select all the items for filtering.
- Filter Bar HTML: A new
-
CSS:
is-hidden: A simple utility class to hide elements usingdisplay: none !important;.filter-buttonstyling: Provides basic button styles and anactivestate for visual feedback.
-
JavaScript (
<script>tag):- Event Listener: The script runs when the DOM is fully loaded.
- Element Selection: It selects all filter buttons (
.filter-button) and all mega menu navigation items (.mega-menu__nav-item). applyFilter(filterChar)Function:- This function takes the
filterChar(e.g., 'A', '0', 'all') as an argument. - It iterates through each
navItem. - It reads the
data-first-charattribute from thenavItem. - If
filterCharis 'all', it removes theis-hiddenclass (shows the item). - Otherwise, it compares
itemFirstCharwithfilterChar. If they match, it shows the item; otherwise, it hides it.
- This function takes the
- Click Handlers:
- It attaches a
clickevent listener to eachfilter-button. - When a button is clicked, it first removes the
activeclass from all buttons and then adds it to the clicked one. - It then calls
applyFilter()with thedata-filtervalue of the clicked button.
- It attaches a
- Initial State:
allButton.click()simulates a click on the "All" button when the page loads, ensuring all items are visible by default and the "All" button is highlighted.
How to Implement:
- Locate your mega menu Liquid snippet: This code snippet typically lives in a
sectionsfile (e.g.,header.liquidif the mega menu is part of the header) or asnippetsfile (e.g.,mega-menu.liquid) that is included in yourheader.liquid. - Replace the relevant part of your existing code with the modified code provided above.
- Ensure your theme's base CSS/JS: This solution assumes your theme uses CSS variables (
--color-background, etc.) and that thebuttonclass has some base styling. Adjust the CSS to match your theme's existing design system. The JavaScript should ideally be integrated into your theme's main JS bundle, but for quick testing, placing it directly in the Liquid file within<script>tags works.
-
- User
code is not working
Assistant
Okay, "not working" can mean a few things:
- Buttons don't appear/filter bar isn't there.
- Buttons appear, but clicking them does nothing.
- Clicking buttons hides/shows items, but they remain invisible/don't display correctly.
- JavaScript errors in the console.
Let's address the most common issues and provide a refined solution.
Common Problems and Fixes:
-
opacity-0onmega-menu__nav-itemlinks: Your original<a>tag hadopacity-0which makes it visually hidden. While theis-hiddenclass usesdisplay: none, ifopacity-0is never removed by your theme's animation script, the links will always be invisible even when "shown" by the filter.- Fix: Remove
opacity-0from the<a>tag in Liquid. Let theis-hiddenclass manage visibility.
- Fix: Remove
-
tabs-elementcustom element: This is the most complex part. Custom elements (liketabs-element) often have their own internal logic for managing child visibility,role="tab",aria-controls, and thehiddenattribute on panels. Ourdisplay: nonemight conflict or be overridden by its internal workings.- Approach: We'll assume for now that
display: none !important;should work. If it doesn't, you might need to investigate yourtabs-element's source code or consider a different approach if its behavior is too restrictive. - Also,
disabledattribute: The{% if forloop_first %} disabled{% endif %}on the first tab button. When you filter, if this disabled tab becomes visible, it will still be disabled. This might be a UX issue. We'll leave it for now as it's typically controlled by thetabs-elementand doesn't directly prevent filtering visibility.
- Approach: We'll assume for now that
-
Handling non-alphanumeric/empty first characters: What if a menu item starts with a symbol, a space, or is empty? The
first_charlogic needs to be more robust.- Fix: Add a check for
blankor non-alphanumeric characters and assign them to an 'OTHER' category. Add a corresponding '#' filter button.
- Fix: Add a check for
Revised Liquid Code:
{%- if block.settings.menu != blank -%} <div class="mega-menu__content flex-1 max-w-lg mx-auto overflow-hidden"> {%- unless block.settings.menu.empty? -%} <ul class="flex flex-col gap-8"> <li class="mega-menu__item mega-menu__item--nav opacity-0"> {# This opacity-0 on the parent li is likely for a global fade-in, handled by your theme's JS/CSS. We are focusing on the visibility of individual nav items within it. #} <!-- NEW: Filter Buttons (0-9, A-Z, # for Other) --> <div class="mega-menu__filter-bar flex flex-wrap gap-2 mb-4"> <button type="button" class="filter-button button button--small" data-filter="all">All</button> {%- for i in (0..9) -%} <button type="button" class="filter-button button button--small" data-filter="{{ i }}">{{ i }}</button> {%- endfor -%} {%- for i in (97..122) -%} {# ASCII a-z #} {%- assign char = i | char -%} <button type="button" class="filter-button button button--small" data-filter="{{ char | upcase }}">{{ char | upcase }}</button> {%- endfor -%} <button type="button" class="filter-button button button--small" data-filter="OTHER">#</button> {# For items starting with numbers/symbols #} </div> <!-- END NEW: Filter Buttons --> <tabs-element class="mega-menu__nav tabs-element text-sm text-opacity leading-none uppercase"> <!-- NEW: Wrapper for navigation items - Essential for JavaScript selection --> <div class="mega-menu__nav-items-wrapper grid gap-3"> {%- liquid assign forloop_first = true assign forloop_index = 0 -%} {%- for i in (1..5) -%} {%- assign tab_target = 'tab_target_' | append: i -%} {%- assign tab_target_link = 'tab_target_link_' | append: i -%} {%- assign tab_heading = block.settings[tab_target] -%} {%- if block.settings[tab_target_link] != blank and block.settings[tab_target_link].type == 'collection_link' -%} {%- if tab_heading == blank -%} {%- assign tab_heading = block.settings[tab_target_link].object.title -%} {%- endif -%} {% comment %} Determine the first character for filtering {% endcomment %} {%- assign temp_char = tab_heading | strip_html | slice: 0 | upcase -%} {%- assign first_char = temp_char -%} {% comment %} Assign to 'OTHER' if not an alphanumeric character {% endcomment %} {%- if first_char == blank or first_char contains ' ' -%} {# Check for blank or leading space #} {%- assign first_char = 'OTHER' -%} {%- elsif first_char contains '0' or first_char contains '1' or first_char contains '2' or first_char contains '3' or first_char contains '4' or first_char contains '5' or first_char contains '6' or first_char contains '7' or first_char contains '8' or first_char contains '9' -%} {# It's a number, first_char is already correct #} {%- elsif first_char contains 'A' or first_char contains 'B' or first_char contains 'C' or first_char contains 'D' or first_char contains 'E' or first_char contains 'F' or first_char contains 'G' or first_char contains 'H' or first_char contains 'I' or first_char contains 'J' or first_char contains 'K' or first_char contains 'L' or first_char contains 'M' or first_char contains 'N' or first_char contains 'O' or first_char contains 'P' or first_char contains 'Q' or first_char contains 'R' or first_char contains 'S' or first_char contains 'T' or first_char contains 'U' or first_char contains 'V' or first_char contains 'W' or first_char contains 'X' or first_char contains 'Y' or first_char contains 'Z' -%} {# It's a letter, first_char is already correct #} {%- else -%} {# It's a symbol or something else, assign to 'OTHER' #} {%- assign first_char = 'OTHER' -%} {%- endif -%} {%- if block.settings.use_tabs -%} <button type="button" class="mega-menu__nav-item link icon-with-text" role="tab" aria-controls="TabPanel-{{ block.id }}-{{ forloop_index }}" data-index="{{ forloop_index }}" {% if forloop_first %} disabled{% endif %} data-first-char="{{ first_char }}" > {{- tab_heading -}} </button> {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- else -%} <a href="{{ block.settings[tab_target_link].url }}" class="mega-menu__nav-item flex gap-2 items-center link icon-with-text" {# REMOVED opacity-0 FROM HERE #} data-first-char="{{ first_char }}" > {{- tab_heading -}} </a> {%- endif -%} {%- endif -%} {%- endfor -%} </div> <!-- END NEW: Wrapper --> </tabs-element> {%- if block.settings.button_label != blank -%} <div class="mega-menu__footer relative"> <a class="mega-menu__link link icon-with-text flex items-center justify-between w-full h-full"{% if block.settings.button_link != blank %} href="{{ block.settings.button_link }}"{% if block.settings.button_external %} target="_blank" rel="noopener"{% endif %}{% else %} role="link" aria-disabled="true"{% endif %}> <span class="heading link-text text-base-xl tracking-tight">{{ block.settings.button_label | escape }}</span> {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} </a> </div> {%- endif -%} </li> <li class="mega-menu__item mega-menu__item--panels opacity-0 w-full"> {# This opacity-0 is for the parent li of the panels, handled by theme JS #} {%- liquid assign forloop_first = true assign forloop_index = 0 -%} {%- for i in (1..5) -%} {%- assign tab_target_link = 'tab_target_link_' | append: i -%} {%- if block.settings[tab_target_link] != blank and block.settings[tab_target_link].type == 'collection_link' -%} {%- liquid assign promo_image = 'promo_image_' | append: i assign tab_target = 'tab_target_' | append: i assign tab_target_heading = block.settings[tab_target] | default: block.settings[tab_target_link].object.title assign tab_products = 'tab_products_' | append: i assign products = block.settings[tab_products] | default: block.settings[tab_target_link].object.products -%} <div id="TabPanel-{{ block.id }}-{{ forloop_index }}" class="mega-menu__panel{% if block.settings[promo_image] == blank %} without-promo{% endif %} flex w-full" role="tabpanel"{% unless forloop_first %} hidden{% endunless %}> <div class="grid gap-8"> <div class="flex items-center justify-between gap-4"> {%- if block.settings.heading_2 != blank -%} <p class="text-sm text-opacity leading-none uppercase">{{ block.settings.heading_2 | escape }}</p> {%- endif -%} <a class="mega-menu__link link icon-with-text" href="{{ block.settings[tab_target_link].object.url }}"> <span class="heading link-text text-base-xl tracking-tight flex items-center gap-1"> {{- 'collections.subcollections.all_products' | t: collection: tab_target_heading -}} <span class="text-base font-medium tracking-tight">({{ block.settings[tab_target_link].object.all_products_count }})</span> </span> {%- render 'icon', icon: 'arrow-right', size: 'sm', class: 'transform shrink-0' -%} </a> </div> <motion-list class="product-grid card-grid card-grid--4 mobile:card-grid--1 grid" initialized> {%- for product in products limit: 4 -%} {%- render 'product-card', product: product, product_id: product.id, section_id: section.id, block_id: block.id, lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, show_icon_list: false -%} {%- else -%} {%- for i in (1..4) -%} {%- capture placeholder -%}product-{% cycle '1', '2', '3', '4' %}{%- endcapture -%} {%- render 'product-card-placeholder', lazy_load: false, image_ratio: settings.product_image_ratio, image_fill: settings.product_image_fill, show_secondary_image: settings.product_secondary_image, show_vendor: settings.product_vendor, show_rating: settings.product_rating, show_color_swatches: settings.product_color_swatches, show_countdown: settings.product_countdown, show_quick_add: settings.product_quick_add, show_quick_view: settings.product_quick_view, show_save_amount: settings.product_save_amount, save_type: settings.product_save_type, placeholder: placeholder -%} {%- endfor -%} {%- endfor -%} </motion-list> </div> {%- if block.settings[promo_image] != blank -%} {%- liquid assign promo_image_ratio = 'promo_image_ratio_' | append: i assign promo_heading = 'promo_heading_' | append: i assign promo_subtext = 'promo_subtext_' | append: i assign promo_overlay_content = 'promo_overlay_content_' | append: i assign promo_link = 'promo_link_' | append: i assign promo_tag = 'promo_tag_' | append: i assign promo_color_text = 'promo_color_text_' | append: i assign promo_color_overlay = 'promo_color_overlay_' | append: i assign promo_overlay_opacity = 'promo_overlay_opacity_' | append: i -%} <div class="media-card media-card--{{ settings.card_style }}{% if block.settings[promo_overlay_content] %} media-card--overlap{% endif %}"> <a class="media-card__link flex flex-col w-full h-full relative"{% if block.settings[promo_link] != blank %} href="{{ block.settings[promo_link] }}"{% else %} role="link" aria-disabled="true"{% endif %} aria-label="{{ block.settings[promo_heading] | default: block.settings[promo_subtext] | escape }}" style="--color-foreground: {{ block.settings[promo_color_text].rgb }}; --color-overlay: {{ block.settings[promo_color_overlay].rgb }}; --overlay-opacity: {{ block.settings[promo_overlay_opacity] | divided_by: 100.0 }};" > {%- if block.settings[promo_tag] != blank -%} <div class="badges z-2 absolute grid gap-3 pointer-events-none"> <span class="badge badge--onsale flex items-center gap-1d5 font-medium leading-none rounded-full">{{ block.settings[promo_tag] | escape }}</span> </div> {%- endif -%} <div class="media media--{{ block.settings[promo_image_ratio] }} relative overflow-hidden"> {{- block.settings[promo_image] | image_url: width: block.settings[promo_image].width | image_tag: loading: 'lazy', widths: '180,360,540,720,900,1080', is: 'lazy-image' -}} </div> {%- if block.settings[promo_heading] != blank or block.settings[promo_subtext] != blank -%} <div class="media-card__content flex justify-between items-center gap-4 w-full"> <div class="media-card__text opacity-0 shrink-1 grid gap-0d5"> {%- if block.settings[promo_heading] != blank -%} <p> <span class="heading reversed-link text-xl-3xl tracking-tighter leading-tight">{{ block.settings[promo_heading] | escape }}</span> </p> {%- endif -%} {%- if block.settings[promo_subtext] != blank -%} <p class="leading-none text-xs">{{ block.settings[promo_subtext] }}</p> {%- endif -%} </div> {%- render 'icon', icon: 'arrow-right', size: 'xs', class: 'transform shrink-0 hidden xl:block' -%} </div> {%- endif -%} </a> </div> {%- endif -%} </div> {%- liquid assign forloop_first = false assign forloop_index = forloop_index | plus: 1 -%} {%- endif -%} {%- endfor -%} </li> </ul> {%- endunless -%} </div> {%- endif -%}Revised CSS:
<style> /* Basic styling for filter buttons */ .mega-menu__filter-bar .filter-button { background-color: var(--color-background); color: var(--color-foreground); border: 1px solid var(--color-foreground); padding: 0.5rem 0.8rem; border-radius: 4px; cursor: pointer; font-size: 0.875rem; /* text-sm */ line-height: 1; min-width: 2.5rem; /* Give them a consistent minimum width */ text-align: center; transition: all 0.2s ease-in-out; /* Smooth transition for active state */ } .mega-menu__filter-bar .filter-button.active { background-color: var(--color-primary); /* Or a highlight color from your theme */ color: var(--color-primary-text); /* Text color for the active button */ border-color: var(--color-primary); } /* Class to hide elements */ .is-hidden { display: none !important; /* Use !important to override any other styles */ } </style>Revised JavaScript:
<script> document.addEventListener('DOMContentLoaded', () => { const filterButtons = document.querySelectorAll('.mega-menu__filter-bar .filter-button'); const navItemsWrapper = document.querySelector('.mega-menu__nav-items-wrapper'); const navItems = navItemsWrapper ? navItemsWrapper.querySelectorAll('.mega-menu__nav-item') : []; // console.log('Filter Buttons Found:', filterButtons.length); // console.log('Nav Items Found:', navItems.length); if (!filterButtons.length || !navItems.length) { // console.warn('Mega menu filter elements not found. Filtering will not work.'); return; // Exit if elements not found } // Function to apply filter const applyFilter = (filterChar) => { // console.log('Applying filter for:', filterChar); let firstVisibleItem = null; navItems.forEach(item => { const itemFirstChar = item.dataset.firstChar; // console.log(` Item: ${item.textContent.trim()}, First Char: ${itemFirstChar}`); if (filterChar === 'all' || itemFirstChar === filterChar) { item.classList.remove('is-hidden'); // Keep track of the first visible item after filtering if (!firstVisibleItem) { firstVisibleItem = item; } } else { item.classList.add('is-hidden'); } }); // After filtering, if 'use_tabs' is true, we need to ensure an active tab is visible. // This part requires interaction with your 'tabs-element' custom logic. // A simple approach is to click the first visible item to activate its tab. // This assumes your 'tabs-element' listens for clicks on its children. if (firstVisibleItem && firstVisibleItem.tagName === 'BUTTON' && !firstVisibleItem.disabled) { // If the currently active tab is hidden, activate the first visible one. // This might be specific to how your 'tabs-element' works. // You might need to trigger a custom event or directly call a method on 'tabs-element' // if clicking the button doesn't automatically switch the tab panel. firstVisibleItem.click(); } else if (!firstVisibleItem && filterChar !== 'all') { // If no items are visible for the current filter, perhaps show a message or default to 'All'. // For now, we'll just not activate any specific tab. } else if (firstVisibleItem && firstVisibleItem.tagName === 'A') { // If it's a direct link (not a tab), no need to activate a panel. } }; // Add click listeners to filter buttons filterButtons.forEach(button => { button.addEventListener('click', () => { // Remove active class from all buttons filterButtons.forEach(btn => btn.classList.remove('active')); // Add active class to clicked button button.classList.add('active'); const filterValue = button.dataset.filter; applyFilter(filterValue); }); }); // Optionally, activate the "All" filter by default on load const allButton = document.querySelector('.mega-menu__filter-bar .filter-button[data-filter="all"]'); if (allButton) { allButton.click(); // Simulate a click to show all initially and set 'All' as active } }); </script>How to Test and Debug:
- Clear Caches: Ensure your browser and Shopify theme caches are cleared after making changes.
- Inspect Element: Right-click on a menu item in your browser and choose "Inspect" (or "Inspect Element").
- Check
data-first-char: Verify that yourmega-menu__nav-itemelements (bothbuttonanda) have thedata-first-charattribute set correctly (e.g.,data-first-char="A"ordata-first-char="5"ordata-first-char="OTHER"). - Check
is-hiddenclass: When you click a filter button, observe if theis-hiddenclass is being added/removed from themega-menu__nav-itemelements as expected. - Check Computed Styles: With an element selected in the inspector, go to the "Computed" tab (or "Styles" tab) and search for
display. Make suredisplay: noneis applied whenis-hiddenis present.
- Check
- Console Errors: Open your browser's developer console (F12 or Cmd+Option+J). Look for any JavaScript errors. These are critical for debugging.
tabs-elementbehavior: If the filtering seems to work (elements getis-hiddenclass), but the actual tab panels aren't switching correctly, then thetabs-elementcustom logic is the problem. This script only hides/shows the buttons/links, not the content panels. Ifblock.settings.use_tabsis true, thetabs-elementcomponent is responsible for managing the content panels. ThefirstVisibleItem.click()attempts to trigger that, but it might not be sufficient if yourtabs-elementis very opinionated.
By following these steps, you should be able to pinpoint why the filtering isn't working as expected. The most common issues are the
opacity-0preventing visibility ordata-first-charbeing incorrectly populated.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.