Lens
import { useId, useRef, useState } from 'react';
const ITEMS = [
{ label: 'Day', body: 'Hour by hour, with travel time between events.' },
{ label: 'Week', body: 'Seven columns, today outlined.' },
{ label: 'Month', body: 'The whole month at a glance, busy days shaded.' },
];
const css = `
@keyframes tabs-lens-stretch-a { 0% { transform: scaleX(1); } 35% { transform: scaleX(1.35) scaleY(0.88); } 70% { transform: scaleX(0.94) scaleY(1.04); } 100% { transform: none; } }
@keyframes tabs-lens-stretch-b { 0% { transform: scaleX(1); } 35% { transform: scaleX(1.35) scaleY(0.88); } 70% { transform: scaleX(0.94) scaleY(1.04); } 100% { transform: none; } }
.tabs-lens { display: grid; gap: 14px; width: 246px; }
.tabs-lens__list { position: relative; display: flex; padding: 5px; border-radius: 999px; background: repeating-linear-gradient(115deg, light-dark(#ebedf0, #232529) 0 6px, light-dark(#f3f4f7, #1a1c1f) 6px 12px); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035), inset 0 2px 6px light-dark(rgba(0, 0, 0, 0.11), rgba(0, 0, 0, 0.35)); }
.tabs-lens__tab { position: relative; padding: 0; border: 0; background: none; font: inherit; cursor: pointer; }
.tabs-lens__tab:focus-visible { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; border-radius: 8px; }
.tabs-lens__tab { position: relative; z-index: 1; flex: 1; height: 36px; border-radius: 999px; color: light-dark(#50535b, #c6c8cd); font-size: 13.5px; font-weight: 500; transition: color 0.3s; }
.tabs-lens__tab[aria-selected='true'] { color: light-dark(#373a40, #f1f2f3); }
/* The glass: blurs and brightens what is under it, a bright rim on top and a soft one below. */
.tabs-lens__glass {
position: absolute;
top: 5px;
bottom: 5px;
left: calc(5px + var(--i) * (100% - 10px) / 3);
width: calc((100% - 10px) / 3);
border-radius: 999px;
background: color-mix(in oklab, light-dark(#6d7179, #7d8087) 22%, transparent);
-webkit-backdrop-filter: blur(3px) brightness(1.25) saturate(1.4);
backdrop-filter: blur(3px) brightness(1.25) saturate(1.4);
box-shadow: inset 0 1px 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 55%, transparent), inset 0 -1px 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), inset 0 0 0 1px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 14%, transparent), 0 6px 14px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6));
pointer-events: none;
transition: left 0.55s cubic-bezier(0.3, 1.3, 0.5, 1);
}
.tabs-lens[data-flip='a'] .tabs-lens__glass { animation: tabs-lens-stretch-a 0.55s ease-out; }
.tabs-lens[data-flip='b'] .tabs-lens__glass { animation: tabs-lens-stretch-b 0.55s ease-out; }
.tabs-lens__panel { color: light-dark(#50535b, #c6c8cd); font-size: 13px; text-align: center; }
@media (prefers-reduced-motion: reduce) {
.tabs-lens__glass { transition: none; animation: none !important; }
}
`;
export function LensTabs() {
const id = useId();
const [state, setState] = useState({ sel: 0, prev: -1, flip: 0 });
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const { sel, prev, flip } = state;
const select = (i: number) => {
if (i !== sel) setState((s) => ({ sel: i, prev: s.sel, flip: s.flip + 1 }));
};
const onKeyDown = (e: React.KeyboardEvent) => {
const n = ITEMS.length;
const next = ({ ArrowRight: (sel + 1) % n, ArrowLeft: (sel + n - 1) % n, Home: 0, End: n - 1 } as Record<string, number>)[e.key];
if (next === undefined) return;
e.preventDefault();
select(next);
tabs.current[next]?.focus();
};
return (
<>
<style>
{css}
</style>
<div
className="tabs-lens"
data-armed={flip ? '' : undefined}
data-flip={flip ? (flip % 2 ? 'a' : 'b') : undefined}
style={{ '--i': sel, '--p': prev < 0 ? sel : prev } as React.CSSProperties}
>
<div className="tabs-lens__list" role="tablist" aria-label="View" onKeyDown={onKeyDown}>
{ITEMS.map((t, i) => (
<button
key={t.label}
ref={(el) => {
tabs.current[i] = el;
}}
className="tabs-lens__tab"
type="button"
role="tab"
id={`${id}-tab-${i}`}
aria-selected={sel === i}
aria-controls={`${id}-panel-${i}`}
tabIndex={sel === i ? 0 : -1}
onClick={() => select(i)}
>
{t.label}
</button>
))}
<span className="tabs-lens__glass" aria-hidden="true" />
</div>
{ITEMS.map((t, i) => (
<div key={t.label} className="tabs-lens__panel" role="tabpanel" id={`${id}-panel-${i}`} aria-labelledby={`${id}-tab-${i}`} hidden={sel !== i}>
{t.body}
</div>
))}
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTabsLens(el) on the mounted element instead.
<div class="tabs-lens" style="--i:0;--p:0">
<div class="tabs-lens__list" role="tablist" aria-label="View">
<button class="tabs-lens__tab" type="button" role="tab" id="tabs-lens-tab-0" aria-selected="true" aria-controls="tabs-lens-panel-0" tabindex="0">Day</button>
<button class="tabs-lens__tab" type="button" role="tab" id="tabs-lens-tab-1" aria-selected="false" aria-controls="tabs-lens-panel-1" tabindex="-1">Week</button>
<button class="tabs-lens__tab" type="button" role="tab" id="tabs-lens-tab-2" aria-selected="false" aria-controls="tabs-lens-panel-2" tabindex="-1">Month</button>
<span class="tabs-lens__glass" aria-hidden="true"></span>
</div>
<div class="tabs-lens__panel" role="tabpanel" id="tabs-lens-panel-0" aria-labelledby="tabs-lens-tab-0">Hour by hour, with travel time between events.</div>
<div class="tabs-lens__panel" role="tabpanel" id="tabs-lens-panel-1" aria-labelledby="tabs-lens-tab-1" hidden="">Seven columns, today outlined.</div>
<div class="tabs-lens__panel" role="tabpanel" id="tabs-lens-panel-2" aria-labelledby="tabs-lens-tab-2" hidden="">The whole month at a glance, busy days shaded.</div>
</div>@keyframes tabs-lens-stretch-a { 0% { transform: scaleX(1); } 35% { transform: scaleX(1.35) scaleY(0.88); } 70% { transform: scaleX(0.94) scaleY(1.04); } 100% { transform: none; } }
@keyframes tabs-lens-stretch-b { 0% { transform: scaleX(1); } 35% { transform: scaleX(1.35) scaleY(0.88); } 70% { transform: scaleX(0.94) scaleY(1.04); } 100% { transform: none; } }
.tabs-lens { display: grid; gap: 14px; width: 246px; }
.tabs-lens__list { position: relative; display: flex; padding: 5px; border-radius: 999px; background: repeating-linear-gradient(115deg, light-dark(#ebedf0, #232529) 0 6px, light-dark(#f3f4f7, #1a1c1f) 6px 12px); box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035), inset 0 2px 6px light-dark(rgba(0, 0, 0, 0.11), rgba(0, 0, 0, 0.35)); }
.tabs-lens__tab { position: relative; padding: 0; border: 0; background: none; font: inherit; cursor: pointer; }
.tabs-lens__tab:focus-visible { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; border-radius: 8px; }
.tabs-lens__tab { position: relative; z-index: 1; flex: 1; height: 36px; border-radius: 999px; color: light-dark(#50535b, #c6c8cd); font-size: 13.5px; font-weight: 500; transition: color 0.3s; }
.tabs-lens__tab[aria-selected='true'] { color: light-dark(#373a40, #f1f2f3); }
/* The glass: blurs and brightens what is under it, a bright rim on top and a soft one below. */
.tabs-lens__glass {
position: absolute;
top: 5px;
bottom: 5px;
left: calc(5px + var(--i) * (100% - 10px) / 3);
width: calc((100% - 10px) / 3);
border-radius: 999px;
background: color-mix(in oklab, light-dark(#6d7179, #7d8087) 22%, transparent);
-webkit-backdrop-filter: blur(3px) brightness(1.25) saturate(1.4);
backdrop-filter: blur(3px) brightness(1.25) saturate(1.4);
box-shadow: inset 0 1px 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 55%, transparent), inset 0 -1px 0 color-mix(in oklab, light-dark(#373a40, #f1f2f3) 18%, transparent), inset 0 0 0 1px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 14%, transparent), 0 6px 14px -6px light-dark(rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.6));
pointer-events: none;
transition: left 0.55s cubic-bezier(0.3, 1.3, 0.5, 1);
}
.tabs-lens[data-flip='a'] .tabs-lens__glass { animation: tabs-lens-stretch-a 0.55s ease-out; }
.tabs-lens[data-flip='b'] .tabs-lens__glass { animation: tabs-lens-stretch-b 0.55s ease-out; }
.tabs-lens__panel { color: light-dark(#50535b, #c6c8cd); font-size: 13px; text-align: center; }
@media (prefers-reduced-motion: reduce) {
.tabs-lens__glass { transition: none; animation: none !important; }
}function initTabsLens(root) {
var list = root.querySelector('[role="tablist"]');
var tabs = Array.prototype.slice.call(list.querySelectorAll('[role="tab"]'));
var panels = root.querySelectorAll('[role="tabpanel"]');
var sel = tabs.findIndex(function (tab) { return tab.getAttribute('aria-selected') === 'true'; });
var flip = 0;
function select(i) {
if (i === sel) return;
var prev = sel;
sel = i;
// Armed on the first change, so nothing animates on load; each change flips data-flip
// between a and b, so the keyframes restart every time.
flip += 1;
root.setAttribute('data-armed', '');
root.setAttribute('data-flip', flip % 2 ? 'a' : 'b');
root.style.setProperty('--i', i);
root.style.setProperty('--p', prev);
tabs.forEach(function (tab, k) {
tab.setAttribute('aria-selected', String(k === i));
tab.tabIndex = k === i ? 0 : -1;
});
panels.forEach(function (panel, k) { panel.hidden = k !== i; });
}
function onClick(e) {
var tab = e.target.closest('[role="tab"]');
if (tab) select(tabs.indexOf(tab));
}
function onKeyDown(e) {
var n = tabs.length;
var next = { ArrowRight: (sel + 1) % n, ArrowLeft: (sel + n - 1) % n, Home: 0, End: n - 1 }[e.key];
if (next === undefined) return;
e.preventDefault();
select(next);
tabs[next].focus();
}
list.addEventListener('click', onClick);
list.addEventListener('keydown', onKeyDown);
return function () {
list.removeEventListener('click', onClick);
list.removeEventListener('keydown', onKeyDown);
};
}
document.querySelectorAll('.tabs-lens').forEach(initTabsLens);Goal: Create a "Lens" tab set.
Visual behavior: View tabs with a liquid glass lens: the glass slides to the new tab, stretching as it moves and settling with a wobble, blurring and brightening what is underneath.
Technique:
- The ARIA tabs pattern: a role="tablist" with an aria-label, buttons with role="tab", aria-selected and aria-controls, and role="tabpanel" panels labelled by their tab. Only the selected tab is in the Tab order (roving tabindex); the arrow keys, Home and End move between tabs and select as they go.
- The selected index is a CSS custom property (--i) on the root, so the moving parts are positioned with calc() and transitions alone.
- The glass is a rounded pill with backdrop-filter: blur, brightness and saturate, a bright rim on top and a softer one below.
- Each move stretches it wide and squashes it with a keyframe that settles past rest and back.
- Effects that should replay on every change alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
Accessibility: Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the glass jumps to the tab without stretching.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Lens" tab set.
Visual behavior: View tabs with a liquid glass lens: the glass slides to the new tab, stretching as it moves and settling with a wobble, blurring and brightening what is underneath.
Technique:
- The ARIA tabs pattern: a role="tablist" with an aria-label, buttons with role="tab", aria-selected and aria-controls, and role="tabpanel" panels labelled by their tab. Only the selected tab is in the Tab order (roving tabindex); the arrow keys, Home and End move between tabs and select as they go.
- The selected index is a CSS custom property (--i) on the root, so the moving parts are positioned with calc() and transitions alone.
- The glass is a rounded pill with backdrop-filter: blur, brightness and saturate, a bright rim on top and a softer one below.
- Each move stretches it wide and squashes it with a keyframe that settles past rest and back.
- Effects that should replay on every change alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
Accessibility: Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the glass jumps to the tab without stretching.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
View tabs with a liquid glass lens: the glass slides to the new tab, stretching as it moves and settling with a wobble, blurring and brightening what is underneath.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change, spring
- Techniques
- css variables, filter, keyframes, css transition
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
Real ARIA tabs: arrow keys, Home and End move between tabs, only the selected tab is in the Tab order, and each panel is labelled by its tab. Decoration is aria-hidden. Under prefers-reduced-motion the glass jumps to the tab without stretching.