03같은 태그를 다시 넣으면 기존 칩이 깜빡이는 입력
새로 입력한 글자를 이미 있는 칩들과 하나씩 비교해 같은 것이 있으면 새 칩을 만들지 않고 그 기존 칩만 잠깐 배경색을 밝혔다 되돌려 이미 있다는 걸 알려준다.
쓰임재고 SKU 태그·중복 방지가 필요한 카테고리 입력
normfindDupmerge

<main class="stage">
<div class="td is-demo" id="td">
<div class="td__row" id="tdRow">
<input type="text" class="td__input" id="tdInput" aria-label="태그 입력, 이미 있으면 새로 만들지 않고 그 칩이 빛난다" placeholder="태그 입력">
<button type="button" class="td__add" id="tdAdd" aria-label="태그 추가">+</button>
</div>
<p class="td__meta">태그 <b id="tdCount">0</b>개<span class="td__dup" id="tdDup" hidden> · 이미 있음</span></p>
<p class="td__sr" role="status" aria-live="polite" id="tdLive"></p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 계산 겹: findDup() 이 입력 문자열을 정규화(trim+lowercase)해 기존 칩 전부와 견준다.
// 같은 게 있으면 새 칩을 만들지 않고 그 칩에 td__chip--merge 를 다시 걸어(reflow) 감쇠 펄스 +
// 링 글로우를 재생한다 — 병합은 "새로 안 만든다"이지 두 칩을 하나로 접는 연출이 아니다.
@include stage($stage-orange);
.td { width: min(100%, 300px); display: flex; flex-direction: column; gap: $sp-2; }
.td__row {
display: flex; flex-wrap: wrap; align-items: center; gap: $sp-2;
background: rgba($subject-cream, .14); border: 1px solid rgba($subject-cream, .4);
border-radius: $r-control; padding: $sp-2 $sp-3;
}
.td__input {
flex: 1 1 auto; min-width: 0; border: none; background: transparent; color: $subject-cream;
font-size: 12px; outline: none;
}
.td__input::placeholder { color: rgba($subject-cream, .7); }
.td__add {
flex: 0 0 auto; width: 26px; height: 26px; border: none; border-radius: 50%;
background: $subject-cream; color: $subject-ink; font-size: 15px; line-height: 1; cursor: pointer;
display: grid; place-items: center; transition: transform $dur-instant $ease-out;
}
.td__add:active { transform: scale(.88); }
.td__add:focus-visible { outline: 2px solid $subject-ink; outline-offset: 2px; }
.td__chip {
display: inline-flex; align-items: center; gap: $sp-1; height: 24px; padding: 0 $sp-1 0 $sp-3;
background: $subject-cream; color: $subject-ink; border-radius: $r-pill;
font-size: 12px; font-weight: 700; white-space: nowrap;
transform: scale(1); opacity: 1; transition: transform $dur-quick $ease-pop, opacity $dur-quick $ease-out;
}
.td__chip--enter { transform: scale(.6); opacity: 0; }
.td__chip--merge { animation: tdMerge $dur-enter $ease-spring 1; }
.td__chipX {
width: 16px; height: 16px; border: none; border-radius: 50%; background: rgba($subject-ink, .16);
color: $subject-ink; font-size: 11px; line-height: 1; cursor: pointer; display: grid; place-items: center;
}
.td__chipX:focus-visible { outline: 2px solid $subject-ink; outline-offset: 2px; }
.td__meta { margin: 0; font-size: 11px; color: rgba($subject-cream, .85); }
.td__meta b { color: $subject-cream; font-weight: 700; }
.td__dup { color: $subject-cream; font-weight: 700; }
.td__sr {
position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); margin: -1px; padding: 0; border: 0;
}
// 감쇠 스프링 펄스 + 링 글로우: 크기가 1→1.16→0.95→1.05→1 로 오버슈트하며 그림자 링이 함께 사그라든다
@keyframes tdMerge {
0% { transform: scale(1); box-shadow: 0 0 0 0 rgba($subject-blue-deep, 0); }
14% { transform: scale(1.16); box-shadow: 0 0 0 6px rgba($subject-blue-deep, .45); }
32% { transform: scale(.95); box-shadow: 0 0 0 3px rgba($subject-blue-deep, .3); }
50% { transform: scale(1.05); box-shadow: 0 0 0 5px rgba($subject-blue-deep, .18); }
70% { transform: scale(.99); box-shadow: 0 0 0 2px rgba($subject-blue-deep, .1); }
100% { transform: scale(1); box-shadow: 0 0 0 0 rgba($subject-blue-deep, 0); }
}
@media (max-width: 340px) {
.td { width: 100%; }
.td__row { padding: $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.td__chip { transition: none; }
.td__chip--merge { animation: none; }
.td__add { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.td {
width: min(100%, 300px);
display: flex;
flex-direction: column;
gap: 8px;
}
.td__row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
background: rgba(255, 247, 230, 0.14);
border: 1px solid rgba(255, 247, 230, 0.4);
border-radius: 12px;
padding: 8px 12px;
}
.td__input {
flex: 1 1 auto;
min-width: 0;
border: none;
background: transparent;
color: #fff7e6;
font-size: 12px;
outline: none;
}
.td__input::placeholder {
color: rgba(255, 247, 230, 0.7);
}
.td__add {
flex: 0 0 auto;
width: 26px;
height: 26px;
border: none;
border-radius: 50%;
background: #fff7e6;
color: #17141a;
font-size: 15px;
line-height: 1;
cursor: pointer;
display: grid;
place-items: center;
transition: transform 140ms ease-out;
}
.td__add:active {
transform: scale(0.88);
}
.td__add:focus-visible {
outline: 2px solid #17141a;
outline-offset: 2px;
}
.td__chip {
display: inline-flex;
align-items: center;
gap: 4px;
height: 24px;
padding: 0 4px 0 12px;
background: #fff7e6;
color: #17141a;
border-radius: 999px;
font-size: 12px;
font-weight: 700;
white-space: nowrap;
transform: scale(1);
opacity: 1;
transition: transform 220ms cubic-bezier(0.2, 1.4, 0.4, 1), opacity 220ms ease-out;
}
.td__chip--enter {
transform: scale(0.6);
opacity: 0;
}
.td__chip--merge {
animation: tdMerge 600ms cubic-bezier(0.2, 0.8, 0.2, 1) 1;
}
.td__chipX {
width: 16px;
height: 16px;
border: none;
border-radius: 50%;
background: rgba(23, 20, 26, 0.16);
color: #17141a;
font-size: 11px;
line-height: 1;
cursor: pointer;
display: grid;
place-items: center;
}
.td__chipX:focus-visible {
outline: 2px solid #17141a;
outline-offset: 2px;
}
.td__meta {
margin: 0;
font-size: 11px;
color: rgba(255, 247, 230, 0.85);
}
.td__meta b {
color: #fff7e6;
font-weight: 700;
}
.td__dup {
color: #fff7e6;
font-weight: 700;
}
.td__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
margin: -1px;
padding: 0;
border: 0;
}
@keyframes tdMerge {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(10, 79, 232, 0);
}
14% {
transform: scale(1.16);
box-shadow: 0 0 0 6px rgba(10, 79, 232, 0.45);
}
32% {
transform: scale(0.95);
box-shadow: 0 0 0 3px rgba(10, 79, 232, 0.3);
}
50% {
transform: scale(1.05);
box-shadow: 0 0 0 5px rgba(10, 79, 232, 0.18);
}
70% {
transform: scale(0.99);
box-shadow: 0 0 0 2px rgba(10, 79, 232, 0.1);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(10, 79, 232, 0);
}
}
@media (max-width: 340px) {
.td {
width: 100%;
}
.td__row {
padding: 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.td__chip {
transition: none;
}
.td__chip--merge {
animation: none;
}
.td__add {
transition: none;
}
}(function () {
var WORDS = ['아이디어', '초안', '아이디어', '검토'];
var ADD_MS = 620, PAUSE_MS = 900;
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var root = document.getElementById('td');
var row = document.getElementById('tdRow');
var input = document.getElementById('tdInput');
var addBtn = document.getElementById('tdAdd');
var countEl = document.getElementById('tdCount');
var dupEl = document.getElementById('tdDup');
var live = document.getElementById('tdLive');
var seq = 0;
function norm(text) { return text.trim().toLowerCase(); }
function chips() { return Array.prototype.slice.call(row.querySelectorAll('.td__chip')); }
function render() { countEl.textContent = chips().length; }
function human() { if (root.classList.contains('is-demo')) root.classList.remove('is-demo'); }
function findDup(text) {
// 계산 겹: 정규화한 문자열을 기존 칩 전부와 견줘 같은 것을 찾는다 — 대소문자·앞뒤 공백은 다르게 치지 않는다
var key = norm(text);
var list = chips();
for (var i = 0; i < list.length; i++) {
if (norm(list[i].dataset.text) === key) return list[i];
}
return null;
}
function merge(chip) {
// 계산 겹: 새 칩을 만들지 않고 이미 있는 칩에 감쇠 스프링 펄스를 다시 건다(reflow 로 재생 보장)
chip.classList.remove('td__chip--merge');
void chip.offsetWidth;
chip.classList.add('td__chip--merge');
dupEl.hidden = false;
live.textContent = chip.dataset.text + ' 은 이미 있다';
if (reduce) render();
}
function add(text) {
if (!text) return;
var dup = findDup(text);
if (dup) { merge(dup); return; }
dupEl.hidden = true;
seq++;
var chip = document.createElement('span');
chip.className = 'td__chip td__chip--enter';
chip.id = 'tdChip' + seq;
chip.dataset.text = text;
chip.innerHTML = '<span class="td__chipText"></span><button type="button" class="td__chipX">×</button>';
chip.querySelector('.td__chipText').textContent = text;
chip.querySelector('.td__chipX').setAttribute('aria-label', '태그 삭제: ' + text);
chip.querySelector('.td__chipX').addEventListener('click', function () { human(); chip.remove(); render(); });
row.insertBefore(chip, input);
requestAnimationFrame(function () { chip.classList.remove('td__chip--enter'); });
render();
live.textContent = text + ' 추가 · 태그 ' + chips().length + '개';
}
addBtn.addEventListener('click', function () { human(); add(input.value.trim()); input.value = ''; });
input.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && input.value.trim()) { human(); add(input.value.trim()); input.value = ''; }
});
var timer = 0;
function schedule(fn, ms) { timer = window.setTimeout(fn, ms); }
function loop(i) {
if (!root.classList.contains('is-demo')) return;
if (i < WORDS.length) {
add(WORDS[i]);
schedule(function () { loop(i + 1); }, ADD_MS);
} else {
chips().forEach(function (c) { c.remove(); });
dupEl.hidden = true;
render();
schedule(function () { loop(0); }, PAUSE_MS);
}
}
render();
if (!reduce) {
schedule(function () { loop(0); }, 260);
} else {
root.classList.remove('is-demo');
}
})();// TagDuplicateHighlightMerge — vanilla 판(run/397/demo/03-tag-input-duplicate-highlight-merge/index.html) 의 React 포트.
// findDup() 이 정규화(trim+lowercase)해 기존 칩과 견준다 — 같은 게 있으면 새로 안 만들고
// 그 칩의 merge key 를 올려 재생(리마운트)시켜 감쇠 펄스+글로우를 다시 건다.
import { useEffect, useRef, useState } from "react";
import styles from "./TagDuplicateHighlightMerge.module.scss";
type Box<T> = { current: T };
type ChangeEvt = { target: { value: string } };
type KeyEvt = { key: string };
const WORDS = ["아이디어", "초안", "아이디어", "검토"];
const ADD_MS = 620, PAUSE_MS = 900;
type Chip = { id: number; text: string; entering: boolean; mergeKey: number };
function norm(text: string): string {
return text.trim().toLowerCase();
}
export default function TagDuplicateHighlightMerge() {
const timerRef = useRef(0) as unknown as Box<number>;
const seqRef = useRef(1) as unknown as Box<number>;
const mergeSeqRef = useRef(0) as unknown as Box<number>;
const isDemoRef = useRef(true) as unknown as Box<boolean>;
const chipsRef = useRef<Chip[]>([]) as unknown as Box<Chip[]>;
const [chips, setChips] = useState<Chip[]>([]);
const [draft, setDraft] = useState("");
const [dup, setDup] = useState(false);
const [isDemo, setIsDemo] = useState(true);
const [live, setLive] = useState("");
useEffect(() => { chipsRef.current = chips; }, [chips]);
function human() {
if (isDemoRef.current) { isDemoRef.current = false; setIsDemo(false); }
}
function add(text: string) {
if (!text) return;
const key = norm(text);
const found = chipsRef.current.find((c) => norm(c.text) === key);
if (found) {
// 계산 겹: 새 칩을 만들지 않고 있는 칩의 mergeKey 만 올려 펄스를 다시 재생한다
mergeSeqRef.current++;
const mk = mergeSeqRef.current;
setChips((cs) => cs.map((c) => (c.id === found.id ? { ...c, mergeKey: mk } : c)));
setDup(true);
setLive(found.text + " 은 이미 있다");
return;
}
setDup(false);
const id = seqRef.current++;
setChips((cs) => [...cs, { id, text, entering: true, mergeKey: 0 }]);
setLive(text + " 추가 · 태그 " + (chipsRef.current.length + 1) + "개");
window.setTimeout(() => {
setChips((cs) => cs.map((c) => (c.id === id ? { ...c, entering: false } : c)));
}, 20);
}
function removeChip(id: number) {
human();
setChips((cs) => cs.filter((c) => c.id !== id));
}
useEffect(() => {
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function loop(i: number) {
if (!isDemoRef.current) return;
if (i < WORDS.length) {
add(WORDS[i]);
timerRef.current = window.setTimeout(() => loop(i + 1), ADD_MS);
} else {
setChips([]);
setDup(false);
timerRef.current = window.setTimeout(() => loop(0), PAUSE_MS);
}
}
if (!reduce) {
timerRef.current = window.setTimeout(() => loop(0), 260);
} else {
isDemoRef.current = false;
setIsDemo(false);
}
return () => window.clearTimeout(timerRef.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function onAdd() { human(); add(draft.trim()); setDraft(""); }
function onInput(e: ChangeEvt) { setDraft(e.target.value); }
function onKeyDown(e: KeyEvt) {
if (e.key === "Enter" && draft.trim()) { human(); add(draft.trim()); setDraft(""); }
}
return (
<main className={styles.stage}>
<div className={styles.td + " " + (isDemo ? styles.isDemo : "")}>
<div className={styles.row}>
{chips.map((c) => (
<span
key={c.id + "-" + c.mergeKey}
className={styles.chip + (c.entering ? " " + styles.chipEnter : "") + (c.mergeKey ? " " + styles.chipMerge : "")}
>
<span>{c.text}</span>
<button type="button" className={styles.chipX} aria-label={"태그 삭제: " + c.text} onClick={() => removeChip(c.id)}>×</button>
</span>
))}
<input
type="text" className={styles.input} aria-label="태그 입력, 이미 있으면 새로 만들지 않고 그 칩이 빛난다"
placeholder="태그 입력" value={draft} onChange={onInput} onKeyDown={onKeyDown}
/>
<button type="button" className={styles.add} aria-label="태그 추가" onClick={onAdd}>+</button>
</div>
<p className={styles.meta}>
태그 <b>{chips.length}</b>개
{dup ? <span className={styles.dup}> · 이미 있음</span> : null}
</p>
<p className={styles.sr} role="status">{live}</p>
</div>
</main>
);
}03 같은 태그를 다시 넣으면 기존 칩이 깜빡이는 입력 — 측정 누적면적 11.464% · 움직인 프레임 9/23GODRICH 원작 · zip 에 SCSS·React 포함 · 태그 입력 ui 9가지 — 칩 UI 코드로 만들기
이 데모가 속한 글
태그 입력 ui 9가지 — 칩 UI 코드로 만들기태그 입력 UI 아홉 가지를 나란히 놓고 보면, 칩 목록 끝에 「+3」 표시가 뜨는 지점부터 본다 — 줄 너비 값이 칩 개수를 계산해 넘친 만큼 접고, 쉼표로 붙여넣은 문자열은 구분자 값으로 나눈 개수만큼 칩을 한 이 글의 데모 전부 보기


