Header and navigation
The starter theme header from Sneferu with its menu blocks and header group, the complete code and the settings a merchant can change.
On this page
What you'll build
The store header: logo area, menu with sub-links, search, language switch and the cart button. The Result below is drawn by the real Sneferu renderer from the code on this page.
Requirements
- Node.js 20 or newer
- A theme made with
npx sneferu init. Every file below is part of it. - The preview running:
npx sneferu dev
Code
Concepts this teaches: Section groups, Menus, Blocks, Cart drawer, Language switch.
These files come from the starter theme that npx sneferu init creates. The example is sections/header.snf.
<header class="sf-header sf-scheme-{{ section.settings.scheme }} sf-sticky-{{ section.settings.sticky }}">
<hk-container>
<div class="sf-header-bar">
<a class="sf-logo" href="{{ url('/') }}">
{% if section.settings.logo %}
<img
class="sf-logo-img sf-logo-h-{{ section.settings.logo_height }}"
src="{{ section.settings.logo }}"
alt="{{ store.name }}"
width="160"
height="{{ section.settings.logo_height }}"
>
{% endif %}
{% if section.settings.logo_text %}
<span class="sf-logo-text">{{ section.settings.logo_text }}</span>
{% elsif section.settings.logo == '' %}
<span class="sf-logo-text">{{ store.name }}</span>
{% endif %}
</a>
<nav class="sf-nav" aria-label="{{ t('menu') }}">
{% if menu(section.settings.menu).items %}
{% for item in menu(section.settings.menu).items %}
<div class="sf-nav-item">
<a class="sf-nav-link" href="{{ item.url }}" aria-current="{{ item.active }}">{{ item.title }}</a>
{% if item.children %}
<div class="sf-subnav">
{% for sub in item.children %}
<a class="sf-subnav-link" href="{{ sub.url }}" aria-current="{{ sub.active }}">{{ sub.title }}</a>
{% for leaf in sub.children %}
<a
class="sf-subnav-link sf-subnav-deep"
href="{{ leaf.url }}"
aria-current="{{ leaf.active }}"
>{{ leaf.title }}</a>
{% endfor %}
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
{% else %}{% for block in section.blocks %}<hk-block of="{{ block }}" />{% endfor %}{% endif %}
</nav>
<div class="sf-header-actions">
{% if section.settings.show_search %}
<details class="sf-search-toggle">
<summary class="sf-icon-button" aria-label="{{ t('search') }}"><hk-icon name="search" /></summary>
<div class="sf-search-panel"><hk-search-form placeholder="{{ t('search_placeholder') }}" /></div>
</details>
{% endif %}
{% if section.settings.show_language %}<hk-locale-switch />{% endif %}
{% if section.settings.show_cart %}
<a
class="sf-icon-button sf-cart-link"
href="{{ url('/cart') }}"
data-hk-open="cart-drawer"
aria-label="{{ t('cart') }}"
>
<hk-icon name="shopping-cart" />
<hk-cart-count />
</a>
{% endif %}
</div>
</div>
</hk-container>
{% if section.settings.show_cart %}
<hk-cart-drawer open-on-add="{{ section.settings.drawer_on_add }}">
<hk-cart-items />
<hk-cart-summary />
</hk-cart-drawer>
{% endif %}
</header>
<hk-schema>
{
"name": {
"en": "Header",
"ar": "الهيدر"
},
"version": 1,
"templates": ["header"],
"blocks": {
"allowed": ["menu-link"],
"max": 8
},
"settings": {
"menu": {
"type": "menu",
"label": {
"en": "Menu (the links below show when none is picked or it has no links)",
"ar": "القائمة (الروابط اللي تحت بتظهر لو مفيش قائمة أو مفيهاش روابط)"
},
"default": ""
},
"logo": {
"type": "image",
"label": {
"en": "Logo image",
"ar": "صورة الشعار"
},
"default": ""
},
"logo_text": {
"type": "text",
"label": {
"en": "Logo text (store name when empty)",
"ar": "نص الشعار (اسم المتجر لو فاضي)"
},
"default": "",
"localized": true,
"maxLength": 40
},
"logo_height": {
"type": "select",
"label": {
"en": "Logo height (px)",
"ar": "ارتفاع الشعار (بكسل)"
},
"options": ["24", "32", "40", "48", "64"],
"default": "40"
},
"scheme": {
"type": "select",
"label": {
"en": "Colour scheme",
"ar": "ألوان الهيدر"
},
"options": ["light", "primary", "accent"],
"default": "light"
},
"sticky": {
"type": "boolean",
"label": {
"en": "Stick to the top",
"ar": "تثبيت في أعلى الصفحة"
},
"default": false
},
"show_search": {
"type": "boolean",
"label": {
"en": "Show search",
"ar": "إظهار البحث"
},
"default": true
},
"show_language": {
"type": "boolean",
"label": {
"en": "Show language switch",
"ar": "إظهار تبديل اللغة"
},
"default": true
},
"show_cart": {
"type": "boolean",
"label": {
"en": "Show cart",
"ar": "إظهار السلة"
},
"default": true
},
"drawer_on_add": {
"type": "boolean",
"label": {
"en": "Open the cart when an item is added",
"ar": "فتح السلة عند الإضافة"
},
"default": true,
"visible_if": {
"setting": "show_cart",
"equals": true
}
}
}
}
</hk-schema><div class="sf-nav-item">
<a class="sf-nav-link" href="{{ url(block.settings.url) }}">{{ block.settings.label }}</a>
{% if block.blocks %}
<div class="sf-subnav">
{% for sub in block.blocks %}
<a class="sf-subnav-link" href="{{ url(sub.settings.url) }}">{{ sub.settings.label }}</a>
{% endfor %}
</div>
{% endif %}
</div>
<hk-schema>
{
"name": {
"en": "Menu link",
"ar": "رابط في القائمة"
},
"version": 1,
"blocks": {
"allowed": ["menu-sub-link"],
"max": 8
},
"settings": {
"label": {
"type": "text",
"label": {
"en": "Label",
"ar": "النص"
},
"default": {
"ar": "رابط",
"en": "Link"
},
"localized": true,
"maxLength": 40
},
"url": {
"type": "link",
"label": {
"en": "Link",
"ar": "الرابط"
},
"default": "/"
}
}
}
</hk-schema><a class="sf-subnav-link" href="{{ url(block.settings.url) }}">{{ block.settings.label }}</a>
<hk-schema>
{
"name": {
"en": "Dropdown link",
"ar": "رابط فرعي"
},
"version": 1,
"settings": {
"label": {
"type": "text",
"label": {
"en": "Label",
"ar": "النص"
},
"default": {
"ar": "رابط",
"en": "Link"
},
"localized": true,
"maxLength": 40
},
"url": {
"type": "link",
"label": {
"en": "Link",
"ar": "الرابط"
},
"default": "/"
}
}
}
</hk-schema>{
"sections": [
{
"id": "header",
"type": "header",
"settings": {
"menu": "main",
"show_search": true,
"show_language": true,
"show_cart": true
},
"blocks": [
{
"id": "nav-home",
"type": "menu-link",
"settings": { "label": { "ar": "الرئيسية", "en": "Home" }, "url": "/" }
},
{
"id": "nav-products",
"type": "menu-link",
"settings": { "label": { "ar": "المنتجات", "en": "Products" }, "url": "/products" }
},
{
"id": "nav-about",
"type": "menu-link",
"settings": { "label": { "ar": "من نحن", "en": "About" }, "url": "/pages/about" }
}
]
}
]
}Download: sections/header.snf, blocks/menu-link.snf, blocks/menu-sub-link.snf, groups/header.json in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/header.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
menu | Menu (the links below show when none is picked or it has no links) | menu | ||
logo | Logo image | image | ||
logo_text | Logo text (store name when empty) | text | set per language | |
logo_height | Logo height (px) | select | 40 | one of 24, 32, 40, 48, 64 |
scheme | Colour scheme | select | light | one of light, primary, accent |
sticky | Stick to the top | boolean | false | |
show_search | Show search | boolean | true | |
show_language | Show language switch | boolean | true | |
show_cart | Show cart | boolean | true | |
drawer_on_add | Open the cart when an item is added | boolean | true |
Text keys the code reads with t(). Change the English text in locales/en.json and the Arabic text under the same key in locales/ar.json.
| Key | English text |
|---|---|
menu | Menu |
search | Search |
search_placeholder | What are you looking for? |
How it works
- This code uses the tags
hk-container,hk-block,hk-icon,hk-search-form,hk-locale-switch,hk-cart-count,hk-cart-drawer,hk-cart-items,hk-cart-summary. hk-containerkeeps the header content at the page width, andhk-icondraws the search and cart icons.- The menu comes from
menu(section.settings.menu), or fromhk-blockmenu links when no menu is chosen (see Lookups). hk-search-formandhk-locale-switchare built-in controls: no script is needed for search or for switching language.hk-cart-countshows the live number of items, andhk-cart-drawerwithhk-cart-itemsandhk-cart-summaryis the slide-out cart.groups/header.jsonis a section group: it places the header on every page with its default menu links.
