Tailwind CSS 클립보드 복사 (Copy to Clipboard)
클립보드 복사(copy to clipboard) 컴포넌트는 버튼 같은 트리거 요소를 한 번 클릭하는 것만으로 텍스트, 코드 줄, 연락처 정보 등 어떤 데이터든 클립보드로 복사할 수 있게 해줘요. 입력 필드, 텍스트 영역, 코드 블록, 폼의 주소 필드에서 텍스트를 복사하는 데 사용할 수 있습니다. 코드 설치 명령어, API 키, URL, 주소, 연락처, 모달 안의 강좌 공유 URL 등을 복사하는 용도로 자주 쓰여요.
출처: 문서
본문
기본 클립보드 복사 (Default copy to clipboard)
트리거 요소(예: 버튼)에 data-copy-to-clipboard-target="elementID" 데이터 속성을 추가하면 JS 동작이 초기화돼요. 여기서 elementID는 소스 텍스트가 있는 요소(예: 입력 필드)의 ID입니다. 버튼 클릭으로 입력 텍스트 필드의 내용을 복사하고, 아래 JavaScript 탭의 코드를 적용하면 Flowbite JS API의 updateOnCopyCallback() 콜백을 기반으로 버튼 텍스트가 바뀝니다.
<div class="grid grid-cols-8 gap-2 w-full max-w-[23rem]">
<label for="npm-install" class="sr-only">Label</label>
<input id="npm-install" type="text" class="col-span-6 bg-neutral-secondary-medium border border-default-medium text-body text-sm rounded-base focus:ring-brand focus:border-brand block w-full px-3 py-2.5 shadow-xs placeholder:text-body" value="npm install flowbite" disabled readonly>
<button data-copy-to-clipboard-target="npm-install" class="col-span-2 col-span-2 text-white bg-brand box-border border border-transparent hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium shadow-xs font-medium leading-5 rounded-base text-sm py-2.5 focus:outline-none sm:w-auto">
<span id="default-message">Copy</span>
<span id="success-message" class="hidden">
<div class="inline-flex items-center">
<svg class="w-3 h-3 me-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 11.917 9.724 16.5 19 7.5"/></svg>
Copied!
</div>
</span>
</button>
</div>
window.addEventListener('load', function() {
const clipboard = FlowbiteInstances.getInstance('CopyClipboard', 'npm-install');
const $defaultMessage = document.getElementById('default-message');
const $successMessage = document.getElementById('success-message');
clipboard.updateOnCopyCallback((clipboard) => {
$defaultMessage.classList.add('hidden');
$successMessage.classList.remove('hidden');
// reset to default state
setTimeout(() => {
$defaultMessage.classList.remove('hidden');
$successMessage.classList.add('hidden');
}, 2000);
});
})
아이콘 버튼이 있는 복사 (Copy with icon button)
입력 필드 안 오른쪽 끝에 아이콘 버튼을 배치해 복사하는 예시예요. 아이콘과 툴팁을 사용해 복사 상태를 표시합니다.
<div class="w-full max-w-[16rem]">
<div class="relative">
<label for="npm-install-copy-button" class="sr-only">Label</label>
<input id="npm-install-copy-button" type="text" class="col-span-6 bg-neutral-secondary-medium border border-default-medium text-body text-sm rounded-base focus:ring-brand focus:border-brand block w-full px-3 py-2.5 shadow-xs placeholder:text-body" value="npm install flowbite" disabled readonly>
<button data-copy-to-clipboard-target="npm-install-copy-button" data-tooltip-target="tooltip-copy-npm-install-copy-button" class="absolute end-2 top-1/2 -translate-y-1/2 text-body hover:bg-neutral-quaternary rounded p-1.5 inline-flex items-center justify-center">
<span id="default-icon">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 5h6m-6 4h6M10 3v4h4V3h-4Z"/></svg>
</span>
<span id="success-icon" class="hidden">
<svg class="w-4 h-4 text-fg-brand" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z"/></svg>
</span>
</button>
<div id="tooltip-copy-npm-install-copy-button" role="tooltip" class="absolute z-10 invisible inline-block px-3 py-2 text-sm font-medium text-white transition-opacity duration-300 bg-dark rounded-base shadow-xs opacity-0 tooltip">
<span id="default-tooltip-message">Copy to clipboard</span>
<span id="success-tooltip-message" class="hidden">Copied!</span>
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</div>
</div>
window.addEventListener('load', function () {
const clipboard = FlowbiteInstances.getInstance('CopyClipboard', 'npm-install-copy-button');
const tooltip = FlowbiteInstances.getInstance('Tooltip', 'tooltip-copy-npm-install-copy-button');
const $defaultIcon = document.getElementById('default-icon');
const $successIcon = document.getElementById('success-icon');
const $defaultTooltipMessage = document.getElementById('default-tooltip-message');
const $successTooltipMessage = document.getElementById('success-tooltip-message');
clipboard.updateOnCopyCallback((clipboard) => {
showSuccess();
// reset to default state
setTimeout(() => {
resetToDefault();
}, 2000);
})
const showSuccess = () => {
$defaultIcon.classList.add('hidden');
$successIcon.classList.remove('hidden');
$defaultTooltipMessage.classList.add('hidden');
$successTooltipMessage.classList.remove('hidden');
tooltip.show();
}
const resetToDefault = () => {
$defaultIcon.classList.remove('hidden');
$successIcon.classList.add('hidden');
$defaultTooltipMessage.classList.remove('hidden');
$successTooltipMessage.classList.add('hidden');
tooltip.hide();
}
})
텍스트 버튼이 있는 복사 (Copy with text button)
입력 필드 안에 "Copy" 텍스트 버튼을 배치하는 예시예요.
<div class="w-full max-w-[18rem]">
<div class="relative">
<label for="npm-install-copy-text" class="sr-only">Label</label>
<input id="npm-install-copy-text" type="text" class="col-span-6 bg-neutral-secondary-medium border border-default-medium text-body text-sm rounded-base focus:ring-brand focus:border-brand block w-full px-3 py-2.5 shadow-xs placeholder:text-body" value="npm install flowbite" disabled readonly>
<button data-copy-to-clipboard-target="npm-install-copy-text" class="absolute flex items-center end-1.5 top-1/2 -translate-y-1/2 text-body bg-neutral-primary-strong border border-default-strong hover:bg-neutral-secondary-strong/70 hover:text-heading focus:ring-4 focus:ring-neutral-tertiary-soft font-medium leading-5 rounded text-xs px-3 py-1.5 focus:outline-none">
<span id="default-message">
<span class="flex items-center">
<svg class="w-4 h-4 me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 5h6m-6 4h6M10 3v4h4V3h-4Z"/></svg>
<span class="text-xs font-semibold">Copy</span>
</span>
</span>
<span id="success-message" class="hidden">
<span class="flex items-center">
<svg class="w-4 h-4 text-fg-brand me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z"/></svg>
<span class="text-xs font-semibold text-fg-brand">Copied</span>
</span>
</span>
</button>
</div>
</div>
window.addEventListener('load', function () {
const clipboard = FlowbiteInstances.getInstance('CopyClipboard', 'npm-install-copy-text');
const $defaultMessage = document.getElementById('default-message');
const $successMessage = document.getElementById('success-message');
clipboard.updateOnCopyCallback((clipboard) => {
showSuccess();
// reset to default state
setTimeout(() => {
resetToDefault();
}, 2000);
})
const showSuccess = () => {
$defaultMessage.classList.add('hidden');
$successMessage.classList.remove('hidden');
}
const resetToDefault = () => {
$defaultMessage.classList.remove('hidden');
$successMessage.classList.add('hidden');
}
})
URL 접두사가 있는 복사 (Copy with URL prefix)
입력 필드 앞에 "URL" 접두어를 두고 복사 버튼을 배치하는 예시예요.
<div class="w-full max-w-sm">
<label for="website-url" class="block mb-2.5 text-sm font-medium text-heading">Verify your website:</label>
<div class="flex items-stretch rounded-base shadow-xs">
<!-- Prefix -->
<span class="shrink-0 z-10 inline-flex items-center px-3 py-2.5 text-sm text-body bg-neutral-tertiary border border-default-medium border-e-0 rounded-s-base">URL</span>
<!-- Input -->
<div class="relative w-full">
<input id="website-url" type="text" aria-describedby="helper-text-explanation" class="bg-neutral-secondary-medium border border-default-medium border-e-0 text-body text-sm focus:ring-brand focus:border-brand block w-full px-3 py-2.5 placeholder:text-body" value="https://flowbite.com" readonly disabled />
</div>
<!-- Button -->
<button data-tooltip-target="tooltip-website-url" data-copy-to-clipboard-target="website-url" type="button" class="shrink-0 z-10 inline-flex items-center justify-center text-white bg-brand hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium font-medium rounded-e-base text-sm px-4 py-2.5 focus:outline-none border border-brand border-s-0">
<span id="default-icon">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-3 5h3m-6 0h.01M12 16h3m-6 0h.01M10 3v4h4V3h-4Z" /></svg>
</span>
<span id="success-icon" class="hidden">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z" /></svg>
</span>
</button>
<!-- Tooltip -->
<div id="tooltip-website-url" role="tooltip"
class="absolute z-10 invisible inline-block px-3 py-2 text-sm font-medium text-white transition-opacity duration-300 bg-dark rounded-base shadow-xs opacity-0 tooltip">
<span id="default-tooltip-message">Copy link</span>
<span id="success-tooltip-message" class="hidden">Copied!</span>
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</div>
<p id="helper-text-explanation" class="mt-2.5 text-sm text-body">Security certificate is required for approval</p>
</div>
window.addEventListener('load', function () {
const clipboard = FlowbiteInstances.getInstance('CopyClipboard', 'website-url');
const tooltip = FlowbiteInstances.getInstance('Tooltip', 'tooltip-website-url');
const $defaultIcon = document.getElementById('default-icon');
const $successIcon = document.getElementById('success-icon');
const $defaultTooltipMessage = document.getElementById('default-tooltip-message');
const $successTooltipMessage = document.getElementById('success-tooltip-message');
clipboard.updateOnCopyCallback((clipboard) => {
showSuccess();
// reset to default state
setTimeout(() => {
resetToDefault();
}, 2000);
})
const showSuccess = () => {
$defaultIcon.classList.add('hidden');
$successIcon.classList.remove('hidden');
$defaultTooltipMessage.classList.add('hidden');
$successTooltipMessage.classList.remove('hidden');
tooltip.show();
}
const resetToDefault = () => {
$defaultIcon.classList.remove('hidden');
$successIcon.classList.add('hidden');
$defaultTooltipMessage.classList.remove('hidden');
$successTooltipMessage.classList.add('hidden');
tooltip.hide();
}
})
URL 단축기 (URL shortener)
"Generate" 버튼으로 URL을 생성하고 복사하는 예시예요.
<div class="w-full max-w-sm">
<label for="url-shortener" class="block mb-2.5 text-sm font-medium text-heading">Shorten URL:</label>
<div class="flex items-stretch shadow-xs rounded-base">
<!-- Generate Button -->
<button class="shrink-0 z-10 inline-flex items-center justify-center text-white bg-brand hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium font-medium rounded-s-base text-sm px-4 py-2.5 focus:outline-none border border-brand" type="button">
Generate
</button>
<!-- Input -->
<div class="relative w-full">
<input id="url-shortener" type="text" aria-describedby="helper-text-explanation" class="bg-neutral-secondary-medium border border-default-medium border-s-0 border-e-0 text-body text-sm focus:ring-brand focus:border-brand block w-full px-3 py-2.5 placeholder:text-body" value="https://bit.ly/3U2SXcF" readonly disabled/>
</div>
<!-- Button -->
<button data-tooltip-target="tooltip-url-shortener" data-copy-to-clipboard-target="url-shortener" class="shrink-0 z-10 inline-flex items-center justify-center text-body bg-neutral-secondary-medium box-border border border-default-medium border-s-0 hover:bg-neutral-tertiary-medium hover:text-heading focus:ring-4 focus:ring-neutral-tertiary font-medium leading-5 rounded-e-base text-sm px-4 py-2.5 focus:outline-none" type="button">
<span id="default-icon">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-3 5h3m-6 0h.01M12 16h3m-6 0h.01M10 3v4h4V3h-4Z" /></svg>
</span>
<span id="success-icon" class="hidden text-fg-brands">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z" /></svg>
</span>
</button>
<!-- Tooltip -->
<div id="tooltip-url-shortener" role="tooltip" class="absolute z-10 invisible inline-block px-3 py-2 text-sm font-medium text-white transition-opacity duration-300 bg-dark rounded-base shadow-xs opacity-0 tooltip">
<span id="default-tooltip-message">Copy link</span>
<span id="success-tooltip-message" class="hidden">Copied!</span>
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</div>
<p id="helper-text-explanation" class="mt-2.5 text-sm text-body">Make sure that your URL is valid</p>
</div>
window.addEventListener('load', function() {
const clipboard = FlowbiteInstances.getInstance('CopyClipboard', 'url-shortener');
const tooltip = FlowbiteInstances.getInstance('Tooltip', 'tooltip-url-shortener');
const $defaultIcon = document.getElementById('default-icon');
const $successIcon = document.getElementById('success-icon');
const $defaultTooltipMessage = document.getElementById('default-tooltip-message');
const $successTooltipMessage = document.getElementById('success-tooltip-message');
clipboard.updateOnCopyCallback((clipboard) => {
showSuccess();
// reset to default state
setTimeout(() => {
resetToDefault();
}, 2000);
})
const showSuccess = () => {
$defaultIcon.classList.add('hidden');
$successIcon.classList.remove('hidden');
$defaultTooltipMessage.classList.add('hidden');
$successTooltipMessage.classList.remove('hidden');
tooltip.show();
}
const resetToDefault = () => {
$defaultIcon.classList.remove('hidden');
$successIcon.classList.add('hidden');
$defaultTooltipMessage.classList.remove('hidden');
$successTooltipMessage.classList.add('hidden');
tooltip.hide();
}
})
코드 블록 복사 (Copy a code block)
카드 안 코드 블록을 복사하는 예시예요. data-copy-to-clipboard-content-type="innerHTML"과 data-copy-to-clipboard-html-entities="true" 속성으로 코드 내용을 복사합니다.
<div class="w-full max-w-lg">
<div class="mb-2 flex justify-between items-center">
<p class="block mb-2.5 text-sm font-medium text-heading">Card example with CTA button:</p>
</div>
<div class="relative bg-neutral-secondary-medium border border-default-medium rounded-base p-4 h-64">
<div class="overflow-scroll max-h-full">
<pre><code id="code-block" class="text-sm text-body whitespace-pre">'use client';
import Link from 'next/link';
import { Navbar } from 'flowbite-react';
function Component() {
return (
<Navbar fluid rounded>
<Navbar.Brand as={Link} href="https://flowbite-react.com">
<img src="/favicon.svg" className="mr-3 h-6 sm:h-9" alt="Flowbite React Logo" />
<span className="self-center whitespace-nowrap text-xl font-semibold dark:text-white">Flowbite React</span>
</Navbar.Brand>
<Navbar.Toggle />
<Navbar.Collapse>
<Navbar.Link href="#" active>
Home
</Navbar.Link>
<Navbar.Link as={Link} href="#">
About
</Navbar.Link>
<Navbar.Link href="#">Services</Navbar.Link>
<Navbar.Link href="#">Pricing</Navbar.Link>
<Navbar.Link href="#">Contact</Navbar.Link>
</Navbar.Collapse>
</Navbar>
);
}
</code></pre>
</div>
<div class="absolute top-2 end-2">
<button data-copy-to-clipboard-target="code-block" data-copy-to-clipboard-content-type="innerHTML" data-copy-to-clipboard-html-entities="true" class="flex items-center text-body bg-neutral-primary-strong border border-default-strong hover:bg-neutral-secondary-strong/70 hover:text-heading focus:ring-4 focus:ring-neutral-tertiary-soft font-medium leading-5 rounded text-xs px-3 py-1.5 focus:outline-none">
<span id="default-message">
<span class="flex items-center">
<svg class="w-4 h-4 me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 5h6m-6 4h6M10 3v4h4V3h-4Z"/></svg>
<span class="text-xs font-semibold">Copy</span>
</span>
</span>
<span id="success-message" class="hidden">
<span class="flex items-center">
<svg class="w-4 h-4 text-fg-brand me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z"/></svg>
<span class="text-xs font-semibold text-fg-brand">Copied</span>
</span>
</span>
</button>
</div>
</div>
<p class="mt-2.5 text-sm text-body">Configure Tailwind CSS and Flowbite before copying the code</p>
</div>
연락처 정보 복사 (Copy contact details)
연락처 정보를 담은 address 요소를 복사하는 예시예요. data-copy-to-clipboard-content-type="textContent" 속성으로 텍스트 내용만 복사합니다.
<div class="w-full max-w-md bg-neutral-primary-soft border border-default shadow-xs rounded-base p-4 sm:p-6">
<h2 class="text-lg font-semibold text-heading mb-4">Contact details</h2>
<address class="relative bg-neutral-secondary-medium p-4 rounded-base border border-default-medium font-italic grid grid-cols-2">
<div class="space-y-2 text-body leading-loose hidden sm:block">
Name <br />
Email <br />
Phone Number
</div>
<div id="contact-details" class="space-y-2 text-heading font-medium leading-loose">
Bonnie Green <br />
name@flowbite.com <br />
+ 12 345 67890
</div>
<button data-copy-to-clipboard-target="contact-details" data-copy-to-clipboard-content-type="textContent" data-tooltip-target="tooltip-contact-details" class="absolute end-2 top-2 text-body hover:bg-neutral-quaternary hover:text-heading rounded-base p-2 inline-flex items-center justify-center">
<span id="default-icon-contact-details">
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 5h6m-6 4h6M10 3v4h4V3h-4Z"/></svg>
</span>
<span id="success-icon-contact-details" class="hidden">
<svg class="w-4 h-4 text-fg-brand" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 4h3a1 1 0 0 1 1 1v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h3m0 3h6m-6 7 2 2 4-4m-5-9v4h4V3h-4Z"/></svg>
</span>
</button>
<div id="tooltip-contact-details" role="tooltip" class="absolute z-10 invisible inline-block px-3 py-2 text-sm font-medium text-white transition-opacity duration-300 bg-dark rounded-base shadow-xs opacity-0 tooltip">
<span id="default-tooltip-message-contact-details">Copy to clipboard</span>
<span id="success-tooltip-message-contact-details" class="hidden">Copied!</span>
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</address>
</div>