Hero section
The starter theme hero section from Sneferu, with its complete code, the settings a merchant can change and how each tag works.
On this page
What you'll build
A big heading, a line of text and a button over a coloured band, with an optional image. 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: Sections, Settings, Blocks, Locale fallback, Snippets.
These files come from the starter theme that npx sneferu init creates. The example is sections/hero.snf.
<hk-section
background="{{ section.settings.background }}"
padding-y="{{ section.settings.padding_y }}"
mobile-padding-y="48"
>
<div class="sf-hero sf-align-{{ section.settings.align }}">
<div class="sf-hero-text">
{% for block in section.blocks %}<hk-block of="{{ block }}" />{% endfor %}
<hk-heading
text="{{ section.settings.title | default: t('hero_title') }}"
level="h1"
size="52"
mobile-size="32"
color="{{ section.settings.text_color }}"
/>
<hk-text
text="{{ section.settings.subtitle | default: t('hero_text') }}"
size="20"
color="{{ section.settings.text_color }}"
/>
{% render 'button', label: section.settings.button_label, href: section.settings.button_link, style: section.settings.button_style %}
</div>
{% if section.settings.image %}
<div class="sf-hero-media">
<hk-image
src="{{ section.settings.image }}"
alt="{{ section.settings.image_alt }}"
loading="eager"
aspect-ratio="4/5"
sizes="(min-width: 1024px) 40vw, 100vw"
/>
</div>
{% endif %}
</div>
</hk-section>
<hk-schema>
{
"name": {
"en": "Hero",
"ar": "البانر الرئيسي"
},
"version": 1,
"blocks": {
"allowed": ["chip"],
"max": 4
},
"settings": {
"title": {
"type": "text",
"label": {
"en": "Heading",
"ar": "العنوان"
},
"default": "",
"localized": true,
"maxLength": 80
},
"subtitle": {
"type": "textarea",
"label": {
"en": "Text",
"ar": "النص"
},
"default": "",
"localized": true,
"maxLength": 200
},
"button_label": {
"type": "text",
"label": {
"en": "Button",
"ar": "الزر"
},
"default": {
"ar": "تسوق دلوقتي",
"en": "Shop now"
},
"localized": true,
"maxLength": 30
},
"button_link": {
"type": "link",
"label": {
"en": "Button link",
"ar": "رابط الزر"
},
"default": "/products"
},
"button_style": {
"type": "select",
"label": {
"en": "Button style",
"ar": "شكل الزر"
},
"options": ["solid", "outline", "ghost"],
"default": "solid"
},
"image": {
"type": "image",
"label": {
"en": "Image",
"ar": "الصورة"
},
"default": ""
},
"image_alt": {
"type": "text",
"label": {
"en": "Image description",
"ar": "وصف الصورة"
},
"default": "",
"localized": true,
"maxLength": 120
},
"align": {
"type": "select",
"label": {
"en": "Text alignment",
"ar": "محاذاة النص"
},
"options": ["start", "center"],
"default": "start"
},
"background": {
"type": "color",
"label": {
"en": "Background",
"ar": "الخلفية"
},
"default": "theme:secondary"
},
"text_color": {
"type": "color",
"label": {
"en": "Text colour",
"ar": "لون النص"
},
"default": "theme:background"
},
"padding_y": {
"type": "range",
"label": {
"en": "Vertical padding",
"ar": "المسافة الرأسية"
},
"min": 0,
"max": 200,
"step": 8,
"default": 88
}
}
}
</hk-schema>{% if label %}<hk-button label="{{ label }}" href="{{ href }}" variant="{{ style }}" />{% endif %}Download: sections/hero.snf, snippets/button.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/hero.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
title | Heading | text | set per language | |
subtitle | Text | textarea | set per language | |
button_label | Button | text | {"ar":"تسوق دلوقتي","en":"Shop now"} | set per language |
button_link | Button link | link | /products | |
button_style | Button style | select | solid | one of solid, outline, ghost |
image | Image | image | ||
image_alt | Image description | text | set per language | |
align | Text alignment | select | start | one of start, center |
background | Background | color | theme:secondary | |
text_color | Text colour | color | theme:background | |
padding_y | Vertical padding | range | 88 | 0 to 200 in steps of 8 |
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 |
|---|---|
hero_title | Everything you need, in one place |
hero_text | Pick what you like and check out in a minute. |
How it works
- This code uses the tags
hk-section,hk-block,hk-heading,hk-text,hk-image,hk-button. hk-sectionis the band: itsbackgroundandpadding-ycome from the section settings, andmobile-padding-ytightens it on a phone.hk-headingandhk-textprint the title and subtitle.default: t('hero_title')falls back to the language file when the merchant leaves the setting empty (see Filters).hk-blockprints each block the merchant adds, here thechipbadges the schema allows (see Blocks).{% render 'button' %}calls thebuttonsnippet shown below the hero, so the button looks the same everywhere.hk-imageonly appears when an image is set: the{% if section.settings.image %}guard keeps the layout clean without one.
