Bootstrap 5 Smile Rating Toggle Switch
An animated two-state rating switch where a smiley knob changes its mouth as it slides between Bad and Fun, usable by click, tap or keyboard.
<section class="wdw-stage">
<div class="container" style="max-width: 760px;">
<div class="wdw-card overflow-hidden">
<div class="p-4 p-md-5 text-center">
<span class="wdw-eyebrow">Quick feedback</span>
<h1 class="h2 wdw-heading mt-1 mb-1">How was your experience?</h1>
<p class="wdw-muted mb-0">Slide the switch, or tap a side, to tell us.</p>
</div>
<div class="wdw-aurora smile-panel px-3 px-md-5 py-5">
<form id="smileForm" class="d-flex align-items-center justify-content-center gap-3 gap-md-4" onsubmit="return false">
<button type="button" class="smile-label active" id="smileBad" data-smile="bad" tabindex="-1">Bad</button>
<button type="button" role="switch" aria-checked="false" aria-label="Experience rating" aria-describedby="smileStatus" class="smile-track" id="smileSwitch">
<span class="smile-knob">
<svg viewBox="0 0 40 40" aria-hidden="true">
<circle cx="14" cy="16" r="2.4" fill="currentColor"/>
<circle cx="26" cy="16" r="2.4" fill="currentColor"/>
<path class="smile-mouth" d="M13 27 Q20 27 27 27" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round"/>
</svg>
</span>
</button>
<button type="button" class="smile-label" id="smileFun" data-smile="fun" tabindex="-1">Fun</button>
</form>
</div>
<div class="p-4 d-flex justify-content-center">
<p class="smile-chip mb-0" id="smileStatus" role="status" aria-live="polite"><span class="smile-emoji" aria-hidden="true">😕</span> <span id="smileText">Currently: Bad. Sorry to hear that.</span></p>
</div>
</div>
</div>
</section>
/* Web Designer Wall library style — shared base for every Bootstrap 5 snippet.
Indigo accent, Plus Jakarta Sans, soft cards, pill buttons. Works in light and dark. */
@import url("https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap");
:root,
[data-bs-theme="light"] {
--wdw-font: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--bs-body-font-family: var(--wdw-font);
--bs-primary: #4f46e5;
--bs-primary-rgb: 79, 70, 229;
--bs-primary-text-emphasis: #3730a3;
--bs-primary-bg-subtle: #eef2ff;
--bs-primary-border-subtle: #c7d2fe;
--bs-link-color: #4f46e5;
--bs-link-color-rgb: 79, 70, 229;
--bs-link-hover-color: #4338ca;
--bs-link-hover-color-rgb: 67, 56, 202;
--bs-focus-ring-color: rgba(79, 70, 229, 0.25);
--bs-border-radius: 0.75rem;
--bs-border-radius-lg: 1rem;
--bs-border-radius-xl: 1.25rem;
--wdw-radius-card: 1.25rem;
--wdw-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 16px 40px -16px rgba(16, 24, 40, 0.22);
--wdw-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px -4px rgba(16, 24, 40, 0.12);
--wdw-stage: #f5f6fb;
}
[data-bs-theme="dark"] {
--bs-primary: #818cf8;
--bs-primary-rgb: 129, 140, 248;
--bs-primary-text-emphasis: #c7d2fe;
--bs-primary-bg-subtle: rgba(129, 140, 248, 0.14);
--bs-primary-border-subtle: rgba(129, 140, 248, 0.35);
--bs-link-color: #a5b4fc;
--bs-link-color-rgb: 165, 180, 252;
--bs-link-hover-color: #c7d2fe;
--bs-link-hover-color-rgb: 199, 210, 254;
--bs-focus-ring-color: rgba(129, 140, 248, 0.35);
--wdw-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
--wdw-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px -4px rgba(0, 0, 0, 0.5);
--wdw-stage: #0f1117;
}
body { font-family: var(--wdw-font); -webkit-font-smoothing: antialiased; }
/* Bootstrap compiles its blue into these components; point them at the accent. */
.btn-primary {
--bs-btn-bg: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca;
--bs-btn-active-bg: #3730a3; --bs-btn-active-border-color: #3730a3;
--bs-btn-disabled-bg: var(--bs-primary); --bs-btn-disabled-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
[data-bs-theme="dark"] .btn-primary { --bs-btn-color: #0f1117; --bs-btn-hover-bg: #a5b4fc; --bs-btn-hover-border-color: #a5b4fc; --bs-btn-hover-color: #0f1117; --bs-btn-active-color: #0f1117; }
.btn-outline-primary {
--bs-btn-color: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: var(--bs-primary); --bs-btn-hover-border-color: var(--bs-primary);
--bs-btn-active-bg: var(--bs-primary); --bs-btn-active-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
.btn { --bs-btn-font-weight: 600; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .5rem; }
.btn-sm { min-height: 36px; }
.form-control, .form-select { min-height: 44px; border-radius: .75rem; }
.form-control:focus, .form-select:focus, .form-check-input:focus { border-color: rgba(var(--bs-primary-rgb), .6); box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.form-check-input:checked { background-color: var(--bs-primary); border-color: var(--bs-primary); }
.nav-pills { --bs-nav-pills-link-active-bg: var(--bs-primary); }
.nav { --bs-nav-link-color: var(--bs-secondary-color); --bs-nav-link-hover-color: var(--bs-emphasis-color); }
.pagination { --bs-pagination-active-bg: var(--bs-primary); --bs-pagination-active-border-color: var(--bs-primary); --bs-pagination-color: var(--bs-body-color); --bs-pagination-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.progress, .progress-stacked { --bs-progress-bar-bg: var(--bs-primary); }
.list-group { --bs-list-group-active-bg: var(--bs-primary); --bs-list-group-active-border-color: var(--bs-primary); }
.dropdown-menu { --bs-dropdown-link-active-bg: var(--bs-primary); border-radius: .875rem; box-shadow: var(--wdw-shadow-sm); }
.accordion { --bs-accordion-active-bg: var(--bs-primary-bg-subtle); --bs-accordion-active-color: var(--bs-primary-text-emphasis); --bs-accordion-btn-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.badge { font-weight: 600; }
/* Library building blocks — use these instead of re-inventing them per snippet. */
.wdw-stage { min-height: 100vh; display: flex; align-items: center; background: var(--wdw-stage); padding-block: 3rem; }
.wdw-card { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: var(--wdw-radius-card); box-shadow: var(--wdw-shadow); }
.wdw-card-sm { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: 1rem; box-shadow: var(--wdw-shadow-sm); }
.wdw-aurora { position: relative; color: #fff; background: radial-gradient(60% 55% at 100% 0, #818cf8 0, transparent 70%), radial-gradient(65% 60% at 0 100%, #ec4899 0, transparent 65%), #1e1b4b; }
.wdw-eyebrow { display: inline-block; font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--bs-primary); }
.wdw-heading { font-weight: 800; letter-spacing: -.02em; color: var(--bs-emphasis-color); }
.wdw-muted { color: var(--bs-secondary-color); }
.wdw-icon { width: 2.75rem; height: 2.75rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: .875rem; background: var(--bs-primary-bg-subtle); color: var(--bs-primary); }
/* Component */
/* Custom switch: a smiley knob needs an animated SVG face, which .form-switch cannot express; it keeps role=switch + aria-checked semantics. */
.smile-track { --w: 12rem; position: relative; width: var(--w); height: 6rem; flex: none; border-radius: 99px; border: 0; padding: 0; background: rgba(255,255,255,.18); box-shadow: inset 0 0 0 2px rgba(255,255,255,.45); transition: background-color .3s, box-shadow .3s; }
.smile-track:focus-visible, .smile-label:focus-visible { outline: 3px solid #fff; outline-offset: 4px; }
.smile-track[aria-checked="true"] { background: var(--bs-primary); box-shadow: inset 0 0 0 2px rgba(255,255,255,.7), 0 0 28px rgba(129,140,248,.7); }
.smile-knob { position: absolute; top: .5rem; left: .5rem; width: 5rem; height: 5rem; border-radius: 50%; background: var(--bs-body-bg); color: var(--bs-body-color); display: grid; place-items: center; box-shadow: 0 4px 12px rgba(16,24,40,.35); transition: transform .35s cubic-bezier(.34,1.4,.64,1), color .3s; }
.smile-track[aria-checked="true"] .smile-knob { transform: translateX(6rem); color: var(--bs-primary); }
.smile-mouth { transition: d .3s; d: path("M13 27 Q20 27 27 27"); }
.smile-track[aria-checked="true"] .smile-mouth { d: path("M12 24 Q20 33 28 24"); }
.smile-label { border: 0; background: none; font-weight: 800; font-size: 1.75rem; color: rgba(255,255,255,.6); min-height: 44px; padding: 0 .25rem; transition: color .2s; }
.smile-label.active { color: #fff; }
.smile-knob svg { width: 100%; height: 100%; }
.smile-chip { display: inline-flex; align-items: center; gap: .6rem; padding: .55rem 1.1rem; border-radius: 99px; font-weight: 600; background: var(--bs-secondary-bg); color: var(--bs-emphasis-color); transition: background-color .3s; }
.smile-chip.is-fun { background: var(--bs-primary-bg-subtle); color: var(--bs-primary-text-emphasis); }
.smile-emoji { display: inline-block; font-size: 1.4rem; line-height: 1; }
.smile-chip.pop .smile-emoji { animation: smilePop .45s cubic-bezier(.34,1.8,.64,1); }
@keyframes smilePop { from { transform: scale(.4) rotate(-20deg); } to { transform: none; } }
@media (max-width: 420px) { .smile-track { --w: 9rem; height: 4.5rem; } .smile-knob { width: 3.5rem; height: 3.5rem; top: .5rem; left: .5rem; } .smile-track[aria-checked="true"] .smile-knob { transform: translateX(4.5rem); } .smile-label { font-size: 1.25rem; } }
@media (prefers-reduced-motion: reduce) { .smile-knob, .smile-track, .smile-mouth, .smile-chip.pop .smile-emoji { transition: none; animation: none; } }
function initSmileToggle() {
const sw = document.getElementById('smileSwitch');
if (!sw) return;
const labels = document.querySelectorAll('[data-smile]');
const chip = document.getElementById('smileStatus');
const emoji = chip.querySelector('.smile-emoji');
const text = document.getElementById('smileText');
const set = (fun) => {
sw.setAttribute('aria-checked', String(fun));
labels.forEach((l) => l.classList.toggle('active', (l.dataset.smile === 'fun') === fun));
emoji.textContent = fun ? '\u{1F604}' : '\u{1F615}';
text.textContent = fun ? 'Currently: Fun. Glad you enjoyed it!' : 'Currently: Bad. Sorry to hear that.';
chip.classList.toggle('is-fun', fun);
chip.classList.remove('pop');
void chip.offsetWidth;
chip.classList.add('pop');
};
sw.addEventListener('click', () => set(sw.getAttribute('aria-checked') !== 'true'));
labels.forEach((l) => l.addEventListener('click', () => set(l.dataset.smile === 'fun')));
}
initSmileToggle();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.