Icon swap
One glyph turns into another in place: the old one shrinks to half and blurs away while the new one settles on a snappy spring. For icons that say a new state (copied, playing, saved), not for decoration.
Usage
Use it when
- A copy button confirms it worked:
content_copybecomescheckfor 1.6 s (copy button does this). - A toggle whose icon names the state: play and pause, bookmark and saved, show and hide password.
- An icon button whose action changes after use: expand and collapse when the label is not shown.
Use something else when
- Words change (Save, Saved): use Text morph.
- Endorsing with a count and a celebration: use Like.
- Menu icon to close icon: use Menu icon. The three bars fold instead of swapping.
- A chevron that turns with an opening panel: use Height expand. Rotate it on the same spring as the panel.
- A selected state that just fills the icon (tab bar, sidebar): use Sliding indicator.
Where it goes. Inside a button: an icon button, a small plain button next to a label, a code block bar. The swap box is 20 px; both glyphs sit in the same grid cell.
Examples
npm install lampung-design-system
<div class="codeblock" style="width: 100%; max-width: 420px;"><div class="codeblock__bar"><span>Install</span><button class="btn btn--plain btn--sm" data-lds-copy="npm install lampung-design-system"><span class="swap" aria-hidden="true"><span class="ic swap__a">content_copy</span><span class="ic swap__b">check</span></span><span data-lds-copy-label="Copied">Copy</span></button></div><pre>npm install lampung-design-system</pre></div> <button class="icon-btn" aria-label="Copy the class link" data-lds-copy="https://campus.ac.id/join/BIO101"><span class="swap" aria-hidden="true"><span class="ic swap__a">link</span><span class="ic swap__b">check</span></span></button>
<button class="icon-btn icon-btn--accent" aria-pressed="false" aria-label="Play lecture" onclick="this.querySelector('.swap').classList.toggle('is-swapped'); const p = this.getAttribute('aria-pressed') !== 'true'; this.setAttribute('aria-pressed', String(p)); this.setAttribute('aria-label', p ? 'Pause lecture' : 'Play lecture');"><span class="swap" aria-hidden="true"><span class="ic swap__a">play_arrow</span><span class="ic swap__b">pause</span></span></button>
<span class="card__sub">Lecture 6: Cell membranes, 48 min</span><button class="icon-btn" aria-pressed="false" aria-label="Save lesson" onclick="this.querySelector('.swap').classList.toggle('is-swapped'); const p = this.getAttribute('aria-pressed') !== 'true'; this.setAttribute('aria-pressed', String(p)); this.setAttribute('aria-label', p ? 'Saved lesson' : 'Save lesson');"><span class="swap" aria-hidden="true"><span class="ic swap__a">bookmark</span><span class="ic ic--fill swap__b">bookmark</span></span></button>
<button class="icon-btn" aria-pressed="false" aria-label="Show password" onclick="this.querySelector('.swap').classList.toggle('is-swapped'); const p = this.getAttribute('aria-pressed') !== 'true'; this.setAttribute('aria-pressed', String(p)); this.setAttribute('aria-label', p ? 'Hide password' : 'Show password');"><span class="swap" aria-hidden="true"><span class="ic swap__a">visibility</span><span class="ic swap__b">visibility_off</span></span></button>Anatomy
| Part | What it is |
|---|---|
.swap | Required. aria-hidden inline grid, 20 × 20. |
.swap__a | Required. The resting glyph. |
.swap__b | Required. The other glyph (add ic--fill for a filled state). |
.is-swapped | On .swap: shows swap__b. |
Motion and microinteractions
- Leaving glyph: to
scale(0.5)withblur(4px)and opacity 0; opacity and blur over--d-fast(160 ms) on--ease-standard, scale on--spring-snappy. - Arriving glyph: from the same state to
noneon--spring-snappy(--d-snappy, 360 ms) while it fades in over--d-fast. Both run at once, so the swap reads as one object changing. - Interruption: toggling
.is-swappedagain mid-swap reverses both glyphs from where they are. - Reduced motion: a 120 ms cross-fade with no scale.
Accessibility
- The swap is
aria-hidden. The button carries the meaning:aria-pressedfor toggles, and anaria-labelthat names the action now available ("Pause lecture"). - Copy announces nothing by itself; when it matters, keep a visible label that changes (
data-lds-copy-label) or add a polite status.
Do and don't
Do
- Swap between two glyphs that mean two states of the same thing.
- Change
aria-labeloraria-pressedwith the glyph. - Use
ic--fillonswap__bfor a filled "on" state.
Don't
- Rotate or flip icons to swap them.
- Swap icons on hover.
- Use the swap for menu to close; use menu icon.
Behavior and code
Plain HTML. CSS only: toggle .is-swapped on .swap. [data-lds-copy] does it for copy buttons (and restores after 1.6 s, with data-lds-copy-label for the visible label).