RTL mirror: logical anchor sides
A chat UI whose tooltips, timestamps, reaction badge, presence dot, hint and menu are all placed with logical data-⚓️-* sides (before/after/align-trailing).
<main class="demo">
<header class="intro">
<h1>Mirror it</h1>
<p>
Every tooltip, timestamp, badge, hint and menu here is placed with logical
<code>data-⚓️-*</code> sides: <code>before</code>, <code>after</code>,
<code>align-trailing</code>. Flip <code>dir</code> on <code><html></code> and the whole UI mirrors
with zero RTL CSS; the button only changes that one attribute.
</p>
</header>
<div class="bar">
<button class="flip" type="button" aria-pressed="false">
<span class="flip-icon" aria-hidden="true">⇄</span>
<code class="flip-label" dir="ltr">dir="<span class="flip-value">ltr</span>"</code>
</button>
<label class="xray-toggle"><input type="checkbox" id="xray"> X-ray</label>
</div>
<div class="app" data-⚓️-root>
<nav class="rail" aria-label="Sections">
<ul>
<li data-⚓️-scope-name="--nav">
<a href="#" data-⚓️-name="--nav" aria-label="Inbox">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 13h4l2 3h4l2-3h4M4 13l2-7h12l2 7v5H4z"/></svg>
</a>
<span class="tip" data-⚓️-to="--nav" data-⚓️-side="after" data-⚓️-align-y="center" aria-hidden="true">Inbox</span>
</li>
<li data-⚓️-scope-name="--nav">
<a href="#" data-⚓️-name="--nav" aria-label="Messages" aria-current="page">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 5h16v11H9l-5 4z"/></svg>
</a>
<span class="tip" data-⚓️-to="--nav" data-⚓️-side="after" data-⚓️-align-y="center" aria-hidden="true">Messages</span>
</li>
<li data-⚓️-scope-name="--nav">
<a href="#" data-⚓️-name="--nav" aria-label="Calendar">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16v14H4zM4 10h16M8 3v5M16 3v5"/></svg>
</a>
<span class="tip" data-⚓️-to="--nav" data-⚓️-side="after" data-⚓️-align-y="center" aria-hidden="true">Calendar</span>
</li>
<li data-⚓️-scope-name="--nav">
<a href="#" data-⚓️-name="--nav" aria-label="Settings">
<svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3M5 5l2 2M17 17l2 2M5 19l2-2M17 7l2-2"/></svg>
</a>
<span class="tip" data-⚓️-to="--nav" data-⚓️-side="after" data-⚓️-align-y="center" aria-hidden="true">Settings</span>
</li>
</ul>
</nav>
<section class="chat" aria-label="Conversation with Noor">
<header class="chat-head">
<span class="avatar" data-⚓️-name="--avatar" aria-hidden="true">N</span>
<span class="online" data-⚓️-to="--avatar" data-⚓️-align-trailing data-⚓️-align-bottom aria-hidden="true"></span>
<span class="who"><b>Noor</b><small>online</small></span>
<button class="more" type="button" popovertarget="menu" data-⚓️-name="--more" aria-label="Conversation options">⋯</button>
</header>
<ol class="thread">
<li class="in" data-⚓️-scope-name="--msg">
<p class="bubble" data-⚓️-name="--msg">Did the anchors survive the RTL build?</p>
<time class="stamp" data-⚓️-to="--msg" data-⚓️-side="after" data-⚓️-align-bottom>9:41</time>
</li>
<li class="out" data-⚓️-scope-name="--msg">
<p class="bubble" data-⚓️-name="--msg">Every one. They're all logical sides.</p>
<time class="stamp" data-⚓️-to="--msg" data-⚓️-side="before" data-⚓️-align-bottom>9:42</time>
<span class="react" data-⚓️-to="--msg" data-⚓️-side="above" data-⚓️-align-trailing aria-label="Noor reacted">🔥</span>
</li>
<li class="in" data-⚓️-scope-name="--msg">
<p class="bubble" data-⚓️-name="--msg">No translate hacks?</p>
<time class="stamp" data-⚓️-to="--msg" data-⚓️-side="after" data-⚓️-align-bottom>9:42</time>
</li>
<li class="out" data-⚓️-scope-name="--msg">
<p class="bubble" data-⚓️-name="--msg">Not one. Hit the button.</p>
<time class="stamp" data-⚓️-to="--msg" data-⚓️-side="before" data-⚓️-align-bottom>9:43</time>
</li>
</ol>
<form class="composer">
<input type="text" aria-label="Message" placeholder="Write a message">
<button type="submit" data-⚓️-name="--send" aria-label="Send">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 12h14M13 6l6 6-6 6"/></svg>
</button>
<span class="hint" data-⚓️-to="--send" data-⚓️-side="above" data-⚓️-align-trailing aria-hidden="true">Enter ↵ to send</span>
</form>
</section>
<div id="menu" class="menu" popover="manual" data-⚓️-to="--more" data-⚓️-side="below" data-⚓️-align-trailing>
<button type="button">Mute</button>
<button type="button">Pin chat</button>
<button type="button">Search</button>
</div>
</div>
</main>/* Demo styling only. Every anchored thing is placed by data-⚓️-* attributes
and every offset below is a logical margin, so none of it needs an RTL rule.
The only :dir(rtl) rules are for the X-ray labels and the arrow icon. */
:root {
color-scheme: light;
--bg: oklch(97% 0.006 80);
--ink: oklch(24% 0.02 270);
--muted: oklch(52% 0.02 270);
--line: oklch(89% 0.01 270);
--accent: oklch(55% 0.2 275);
--accent-soft: oklch(93% 0.04 275);
--ease: cubic-bezier(0.25, 1, 0.5, 1);
background: var(--bg);
color: var(--ink);
font: 400 1rem/1.5 system-ui, sans-serif;
}
body { margin: 0; }
.demo {
max-width: 48rem;
margin-inline: auto;
padding: clamp(1rem, 4vw, 2.5rem);
display: grid;
gap: 1.25rem;
overflow-x: clip; /* X-ray labels may poke past the edge on small screens */
}
.intro h1 { margin: 0 0 0.35rem; font-size: 1.5rem; font-weight: 650; letter-spacing: -0.01em; }
.intro p { margin: 0; color: var(--muted); max-width: 62ch; }
code { font: 0.88em ui-monospace, monospace; color: var(--accent); }
.bar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.flip {
display: inline-flex;
align-items: center;
gap: 0.6rem;
padding-block: 0.55rem;
padding-inline: 0.7rem 1rem;
border: 0;
border-radius: 99px;
background: var(--ink);
color: oklch(98% 0 0);
font: 600 0.9rem ui-monospace, monospace;
cursor: pointer;
transition: scale 0.15s var(--ease);
}
.flip:active { scale: 0.96; }
.flip-label { color: inherit; font: inherit; }
/* English copy in an RTL document: let each paragraph take its own
direction so punctuation lands where it should. */
.intro p, .bubble, .composer input { unicode-bidi: plaintext; }
.flip:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.flip-icon {
display: grid;
place-items: center;
width: 1.6rem;
aspect-ratio: 1;
border-radius: 50%;
background: var(--accent);
font-size: 0.95rem;
transition: rotate 0.4s var(--ease);
}
.flip[aria-pressed="true"] .flip-icon { rotate: 180deg; }
.xray-toggle { display: flex; gap: 0.4rem; align-items: center; color: var(--muted); font-size: 0.9rem; cursor: pointer; }
.xray-toggle input { accent-color: var(--accent); }
/* ---------- app shell ---------- */
.app {
display: grid;
grid-template-columns: 4.25rem 1fr;
border: 1px solid var(--line);
border-radius: 1.1rem;
background: oklch(100% 0 0);
box-shadow: 0 1.25rem 3rem -1.5rem oklch(30% 0.05 270 / 0.35);
}
.rail {
padding-block: 1rem;
border-inline-end: 1px solid var(--line);
background: oklch(98.5% 0.004 270);
border-start-start-radius: inherit;
border-end-start-radius: inherit;
}
.rail ul { display: grid; justify-items: center; gap: 0.6rem; margin: 0; padding: 0; list-style: none; }
.rail a {
display: grid;
place-items: center;
width: 2.6rem;
aspect-ratio: 1;
border-radius: 0.8rem;
color: var(--muted);
transition: background 0.2s, color 0.2s;
}
.rail a:hover, .rail a:focus-visible { background: var(--accent-soft); color: var(--accent); }
.rail a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.rail a[aria-current] { background: var(--accent); color: oklch(99% 0 0); }
svg { width: 1.3rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
/* Tooltips: side="after" of their nav link, nub on the inline-start edge. */
.tip {
z-index: 2;
margin-inline-start: 0.7rem;
padding: 0.3rem 0.6rem;
border-radius: 0.45rem;
background: var(--ink);
color: oklch(98% 0 0);
font-size: 0.8rem;
font-weight: 500;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.15s;
}
.tip::before {
content: "";
position: absolute;
inset-block-start: 50%;
inset-inline-start: -0.25rem;
width: 0.5rem;
aspect-ratio: 1;
margin-block-start: -0.25rem;
background: inherit;
rotate: 45deg;
}
li:has(a:hover, a:focus-visible, a[aria-current]) > .tip { opacity: 1; }
/* ---------- conversation ---------- */
.chat { display: grid; grid-template-rows: auto 1fr auto; min-width: 0; }
.chat-head {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.8rem 1rem;
border-block-end: 1px solid var(--line);
}
.avatar {
display: grid;
place-items: center;
width: 2.4rem;
aspect-ratio: 1;
border-radius: 50%;
background: linear-gradient(135deg, oklch(75% 0.14 30), oklch(62% 0.2 350));
color: oklch(99% 0 0);
font-weight: 700;
}
/* Presence dot: pinned to the avatar's trailing-bottom corner. */
.online {
width: 0.8rem;
aspect-ratio: 1;
border-radius: 50%;
background: oklch(72% 0.18 150);
border: 2px solid oklch(100% 0 0);
margin-inline-end: -0.1rem;
margin-block-end: -0.1rem;
}
.who { display: grid; line-height: 1.2; }
.who small { color: var(--muted); }
.more {
margin-inline-start: auto;
width: 2.2rem;
aspect-ratio: 1;
border: 0;
border-radius: 50%;
background: transparent;
color: var(--ink);
font-size: 1.3rem;
line-height: 1;
cursor: pointer;
}
.more:hover, .more:focus-visible { background: var(--accent-soft); }
.thread {
display: grid;
gap: 1.1rem;
align-content: start;
margin: 0;
padding: 1.25rem clamp(3rem, 12vw, 4.5rem) 2rem;
list-style: none;
}
.thread li { display: grid; }
.bubble {
max-width: 26ch;
margin: 0;
padding: 0.55rem 0.85rem;
border-radius: 1.1rem;
font-size: 0.95rem;
line-height: 1.35;
}
.in .bubble { justify-self: start; background: oklch(95% 0.008 270); border-end-start-radius: 0.3rem; }
.out .bubble { justify-self: end; background: var(--accent); color: oklch(99% 0 0); border-end-end-radius: 0.3rem; }
.stamp { margin-inline: 0.45rem; color: var(--muted); font: 0.72rem ui-monospace, monospace; white-space: nowrap; }
/* Reaction: above the bubble's trailing corner, pulled down to overlap it. */
.react {
margin-block-end: -0.75rem;
margin-inline-end: -0.5rem;
padding: 0.05rem 0.3rem;
border-radius: 99px;
background: oklch(100% 0 0);
box-shadow: 0 0.15rem 0.5rem oklch(30% 0.05 270 / 0.25);
font-size: 0.85rem;
}
.composer {
display: flex;
gap: 0.5rem;
padding: 0.8rem 1rem;
border-block-start: 1px solid var(--line);
}
.composer input {
flex: 1;
min-width: 0;
padding: 0.55rem 0.9rem;
border: 1px solid var(--line);
border-radius: 99px;
font: inherit;
font-size: 0.95rem;
}
.composer input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.composer button {
display: grid;
place-items: center;
width: 2.5rem;
aspect-ratio: 1;
border: 0;
border-radius: 50%;
background: var(--accent);
color: oklch(99% 0 0);
cursor: pointer;
}
.composer button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
:dir(rtl) .composer svg { scale: -1 1; }
/* Hint: above the send button, flush with its trailing edge. */
.hint {
margin-block-end: 0.5rem;
padding: 0.25rem 0.55rem;
border-radius: 0.4rem;
background: oklch(92% 0.08 95);
color: oklch(35% 0.06 80);
font: 600 0.72rem ui-monospace, monospace;
white-space: nowrap;
}
/* Menu popover: below the ⋯ button, flush with its trailing edge. */
.menu {
display: none;
margin-block-start: 0.4rem;
padding: 0.35rem;
border: 1px solid var(--line);
border-radius: 0.75rem;
background: oklch(100% 0 0);
box-shadow: 0 0.75rem 2rem -0.5rem oklch(30% 0.05 270 / 0.35);
}
.menu:popover-open { display: grid; }
.menu button {
padding-block: 0.4rem;
padding-inline: 0.7rem 1.6rem;
border: 0;
border-radius: 0.45rem;
background: none;
color: var(--ink);
font: inherit;
font-size: 0.9rem;
text-align: start;
cursor: pointer;
}
.menu button:hover, .menu button:focus-visible { background: var(--accent-soft); }
/* ---------- mirror transition (view transition, set up in the script) ---------- */
.tip, .stamp, .react, .hint, .menu, .online, .avatar, .who, .bubble, .rail, .more,
.composer :is(input, button), .intro h1, .flip, .xray-toggle {
view-transition-name: match-element;
}
::view-transition-group(*) { animation-duration: 0.55s; animation-timing-function: var(--ease); }
/* ---------- X-ray: outline anchors, label each target's side ---------- */
.demo:has(#xray:checked) [data-⚓️-name] { outline: 2px dashed oklch(65% 0.15 220); outline-offset: 2px; }
.demo:has(#xray:checked) [data-⚓️-to] { outline: 2px solid oklch(62% 0.22 330); outline-offset: 1px; }
.demo:has(#xray:checked) [data-⚓️-to]::after {
position: absolute;
inset-block-end: 100%;
inset-inline-start: 0;
margin-block-end: 0.2rem;
padding: 0 0.3rem;
border-radius: 0.25rem;
background: oklch(62% 0.22 330);
color: oklch(99% 0 0);
font: 600 0.62rem/1.5 ui-monospace, monospace;
white-space: nowrap;
opacity: 1;
z-index: 3;
}
.demo:has(#xray:checked) [data-⚓️-side="after"]::after { content: "after → right"; }
.demo:has(#xray:checked) [data-⚓️-side="before"]::after { content: "before → left"; }
.demo:has(#xray:checked) [data-⚓️-side="after"]:dir(rtl)::after { content: "after → left"; }
.demo:has(#xray:checked) [data-⚓️-side="before"]:dir(rtl)::after { content: "before → right"; }
.demo:has(#xray:checked) [data-⚓️-align-trailing]::after { content: attr(data-⚓️-side) " + trailing → right"; }
.demo:has(#xray:checked) [data-⚓️-align-trailing]:dir(rtl)::after { content: attr(data-⚓️-side) " + trailing → left"; }
.demo:has(#xray:checked) .online::after { content: "trailing → right"; }
.demo:has(#xray:checked) .online:dir(rtl)::after { content: "trailing → left"; }
.demo:has(#xray:checked) .tip { opacity: 1; }
.demo:has(#xray:checked) .stamp::after { inset-block-end: auto; inset-block-start: 100%; margin: 0.2rem 0 0; }
@media (prefers-reduced-motion: reduce) {
.flip, .flip-icon, .tip, .rail a { transition: none; }
}
@media (width < 30rem) {
.app { grid-template-columns: 3.4rem 1fr; }
.rail a { width: 2.3rem; }
.thread { padding-inline: 2.75rem; }
}// State only: the button flips one attribute, dir, on <html>. Nothing is
// repositioned by script; the anchored sides are logical so they mirror.
// A view transition animates the swap where supported (skipped for
// reduced motion — then it's an instant mirror).
// It's set on the root element rather than the .app wrapper on purpose: the
// menu is a [popover], so its containing block is the viewport, and anchor()'s
// start/end keywords resolve against the containing block's direction.
const root = document.documentElement;
const flip = document.querySelector(".flip");
const value = document.querySelector(".flip-value");
const reduce = matchMedia("(prefers-reduced-motion: reduce)");
function mirror() {
const next = root.dir === "rtl" ? "ltr" : "rtl";
root.dir = next;
value.textContent = next;
flip.setAttribute("aria-pressed", String(next === "rtl"));
}
flip.addEventListener("click", () => {
if (document.startViewTransition && !reduce.matches) document.startViewTransition(mirror);
else mirror();
});
document.querySelector(".composer").addEventListener("submit", (e) => e.preventDefault());
// Show the menu open so its trailing alignment is visible from the start.
document.getElementById("menu").showPopover();data-⚓️-align-bottomdata-⚓️-align-trailingdata-⚓️-align-ydata-⚓️-namedata-⚓️-rootdata-⚓️-scope-namedata-⚓️-sidedata-⚓️-to