A function signature types in, a faint ghost suggestion appears below it with a Tab hint, then the key is pressed and the suggestion turns into real code. Good for AI coding tool pages.
Also known as: ghost text suggestion, inline code completion css, ai code suggestion animation.
function add(a, b) {
return a + b;Tab
}
<div class="txt-ai-code-autocomplete">
<p><span class="txt-ai-code-autocomplete-typed"><span class="txt-ai-code-autocomplete-k">function</span> add(a, b) {</span><span class="txt-ai-code-autocomplete-caret" aria-hidden="true"></span></p>
<p><span class="txt-ai-code-autocomplete-ghost"> <span class="txt-ai-code-autocomplete-k">return</span> a + b;</span><kbd class="txt-ai-code-autocomplete-tab" aria-hidden="true">Tab</kbd></p>
<p><span class="txt-ai-code-autocomplete-ghost">}</span><span class="txt-ai-code-autocomplete-caret txt-ai-code-autocomplete-end" aria-hidden="true"></span></p>
</div>.txt-ai-code-autocomplete {
--c: var(--txt-color, #3b82f6);
font-size: var(--txt-size, 32px);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
display: inline-block;
box-sizing: border-box;
min-width: 13em;
padding: 0.7em 1em;
border-radius: 0.6em;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: calc(var(--txt-size, 32px) * 0.5);
font-weight: 500;
line-height: 1.7;
text-align: left;
background: color-mix(in srgb, currentColor 5%, transparent);
box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 14%, transparent);}
.txt-ai-code-autocomplete p {
margin: 0;
white-space: pre;
}
.txt-ai-code-autocomplete-k {
color: var(--c);
}
.txt-ai-code-autocomplete-typed {
display: inline-block;
vertical-align: top;
width: 0;
overflow: hidden;
/* 20 = characters in the first line */
animation: txt-ai-code-autocomplete-type 1.1s steps(20) 0.3s forwards;
}
.txt-ai-code-autocomplete-caret {
display: inline-block;
vertical-align: top;
width: 0.12em;
height: 1.2em;
margin-top: 0.25em;
background: var(--c);
animation:
txt-ai-code-autocomplete-blink 1s step-end 1.4s infinite,
txt-ai-code-autocomplete-off 0s 3.1s forwards;
}
.txt-ai-code-autocomplete-end {
visibility: hidden;
animation:
txt-ai-code-autocomplete-on 0s 3.1s forwards,
txt-ai-code-autocomplete-blink 1s step-end 3.1s infinite;
}
/* ghost text: faint and italic, then accepted */
.txt-ai-code-autocomplete-ghost {
opacity: 0;
font-style: italic;
animation: txt-ai-code-autocomplete-ghost 1.4s ease-out 1.7s forwards;
}
.txt-ai-code-autocomplete-ghost .txt-ai-code-autocomplete-k {
color: inherit;
animation: txt-ai-code-autocomplete-accept 0s 3.1s forwards;
}
.txt-ai-code-autocomplete-tab {
display: inline-block;
margin-left: 1.2ch;
padding: 0 0.45em;
border-radius: 0.3em;
font: inherit;
font-size: 0.75em;
line-height: 1.6;
vertical-align: 0.1em;
color: var(--c);
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 50%, transparent);
opacity: 0;
animation: txt-ai-code-autocomplete-press 1.5s ease-out 1.8s forwards;
}
@keyframes txt-ai-code-autocomplete-type {
99.9%, to { width: 20ch; }
}
@keyframes txt-ai-code-autocomplete-blink {
50% { opacity: 0; }
}
@keyframes txt-ai-code-autocomplete-off {
to { visibility: hidden; }
}
@keyframes txt-ai-code-autocomplete-on {
to { visibility: visible; }
}
@keyframes txt-ai-code-autocomplete-ghost {
0% { opacity: 0; }
18%, 90% { opacity: 0.4; font-style: italic; }
100% { opacity: 1; font-style: normal; }
}
@keyframes txt-ai-code-autocomplete-accept {
to { color: var(--c); }
}
@keyframes txt-ai-code-autocomplete-press {
0% { opacity: 0; transform: translateY(0.3em); }
15%, 70% { opacity: 1; transform: none; }
78% { transform: scale(0.85); color: #fff; background: var(--c); }
86% { opacity: 1; transform: none; color: #fff; background: var(--c); }
100% { opacity: 0; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
.txt-ai-code-autocomplete, .txt-ai-code-autocomplete *, .txt-ai-code-autocomplete::before, .txt-ai-code-autocomplete::after, .txt-ai-code-autocomplete *::before, .txt-ai-code-autocomplete *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--txt-color on the element or a parent.
Currently previewing #3b82f6.--txt-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 40px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.