본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Copy Button

원문 보기 위키 갱신

Shoelace의 Copy Button 컴포넌트(sl-copy-button)는 사용자가 트리거를 클릭하면 텍스트 데이터를 클립보드에 복사해주는 버튼이에요. 복사 성공/실패 상태를 툴팁(tooltip)과 아이콘으로 피드백하고, 다른 요소의 값에서 데이터를 가져올 수도 있답니다. (실험적(experimental) 컴포넌트)

출처: 문서

본문

사용자가 트리거를 클릭하면 텍스트 데이터를 클립보드로 복사해요.

<sl-copy-button value="Shoelace rocks!"></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const App = () => (
  <SlCopyButton value="Shoelace rocks!" />
);

Examples

Custom Labels

Copy button은 툴팁(tooltip)으로 피드백을 표시해요. copy-label, success-label, error-label 속성(attribute)으로 레이블을 커스터마이즈할 수 있어요.

<sl-copy-button
  value="Custom labels are easy"
  copy-label="Click to copy"
  success-label="You did it!"
  error-label="Whoops, your browser doesn't support this!"
></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const App = () => (
  <SlCopyButton
    value="Custom labels are easy"
    copy-label="Click to copy"
    success-label="You did it!"
    error-label="Whoops, your browser doesn't support this!"
  />
);

Custom Icons

copy-icon, success-icon, error-icon 슬롯을 사용하면 각 상태에 표시되는 아이콘을 커스터마이즈할 수 있어요. sl-icon이나 자신만의 이미지를 사용할 수 있어요.

<sl-copy-button value="Copied from a custom button">
  <sl-icon slot="copy-icon" name="clipboard"></sl-icon>
  <sl-icon slot="success-icon" name="clipboard-check"></sl-icon>
  <sl-icon slot="error-icon" name="clipboard-x"></sl-icon>
</sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
import { SlIcon } from '@shoelace-style/shoelace/dist/react/icon';
const App = () => (
  <>
    <SlCopyButton value="Copied from a custom button">
      <SlIcon slot="copy-icon" name="clipboard" />
      <SlIcon slot="success-icon" name="clipboard-check" />
      <SlIcon slot="error-icon" name="clipboard-x" />
    </SlCopyButton>
  </>
);

Copying Values From Other Elements

보통 복사되는 데이터는 컴포넌트의 value 속성(attribute)에서 오지만, 같은 문서 안에 있는 어떤 요소든 그 id를 from 속성(attribute)에 제공하면 그 요소에서 데이터를 복사할 수 있어요.

from 속성(attribute)을 사용할 때는 기본적으로 요소의 textContent가 복사돼요. attribute나 property 수정자(modifier)를 전달하면 요소의 attribute나 property에서 데이터를 복사할 수 있어요.

attribute에서 데이터를 복사하려면 from="id[attr]"을 사용하세요. 여기서 id는 대상 요소의 id이고 attr은 복사할 attribute의 이름이에요. property에서 데이터를 복사하려면 from="id.prop"을 사용하세요. 여기서 id는 대상 요소의 id이고 prop은 복사할 property의 이름이에요.

<!-- Copies the span's textContent -->
<span id="my-phone">+1 (234) 456-7890</span>
<sl-copy-button from="my-phone"></sl-copy-button>
<br><br>
<!-- Copies the input's "value" property -->
<sl-input id="my-input" type="text" value="User input" style="display: inline-block; max-width: 300px;"></sl-input>
<sl-copy-button from="my-input.value"></sl-copy-button>
<br><br>
<!-- Copies the link's "href" attribute -->
<a id="my-link" href="https://shoelace.style/">Shoelace Website</a>
<sl-copy-button from="my-link[href]"></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
import { SlInput } from '@shoelace-style/shoelace/dist/react/input';
const App = () => (
  <>
    {/* Copies the span's textContent */}
    <span id="my-phone">+1 (234) 456-7890</span>
    <SlCopyButton from="my-phone" />
    <br /><br />
    {/* Copies the input's "value" property */}
    <SlInput id="my-input" type="text" />
    <SlCopyButton from="my-input.value" />
    <br /><br />
    {/* Copies the link's "href" attribute */}
    <a id="my-link" href="https://shoelace.style/">Shoelace Website</a>
    <SlCopyButton from="my-link[href]" />
  </>
);

Handling Errors

값이 빈 문자열이거나, from 속성(attribute)이 존재하지 않는 id를 가리키거나, 브라우저가 어떤 이유로든 작업을 거부하면 복사 오류가 발생해요. 이 경우 sl-error 이벤트가 발생해요.

이 예시는 복사 오류가 발생했을 때 어떤 일이 벌어지는지 보여줘요. error-label 속성(attribute)과 error-icon 슬롯으로 각각 오류 레이블과 아이콘을 커스터마이즈할 수 있어요.

<sl-copy-button from="i-do-not-exist"></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const App = () => (
  <SlCopyButton from="i-do-not-exist" />
);

Disabled

disabled 속성(attribute)을 추가하면 복사 버튼을 비활성화할 수 있어요.

<sl-copy-button value="You can't copy me" disabled></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const App = () => (
  <SlCopyButton value="You can't copy me" disabled />
);

Changing Feedback Duration

복사 후에 성공 표시가 잠시 보여져요. feedback-duration 속성(attribute)으로 표시가 보이는 시간을 커스터마이즈할 수 있어요.

<sl-copy-button value="Shoelace rocks!" feedback-duration="250"></sl-copy-button>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const App = () => (
  <SlCopyButton value="Shoelace rocks!" feedback-duration={250} />
);

Custom Styles

CSS로 버튼을 마음대로 커스터마이즈할 수 있어요.

<sl-copy-button value="I'm so stylish" class="custom-styles">
  <sl-icon slot="copy-icon" name="asterisk"></sl-icon>
  <sl-icon slot="success-icon" name="check-lg"></sl-icon>
  <sl-icon slot="error-icon" name="x-lg"></sl-icon>
</sl-copy-button>
<style>
  .custom-styles {
    --success-color: white;
    --error-color: white;
    color: white;
  }
  .custom-styles::part(button) {
    background-color: #ff1493;
    border: solid 4px #ff7ac1;
    border-right-color: #ad005c;
    border-bottom-color: #ad005c;
    border-radius: 0;
    transition: 100ms scale ease-in-out, 100ms translate ease-in-out;
  }
  .custom-styles::part(button):hover {
    scale: 1.1;
  }
  .custom-styles::part(button):active {
    translate: 0 2px;
  }
  .custom-styles::part(button):focus-visible {
    outline: dashed 2px deeppink;
    outline-offset: 4px;
  }
</style>
import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button';
const css = `
  .custom-styles {
    --success-color: white;
    --error-color: white;
    color: white;
  }
  .custom-styles::part(button) {
    background-color: #ff1493;
    border: solid 4px #ff7ac1;
    border-right-color: #ad005c;
    border-bottom-color: #ad005c;
    border-radius: 0;
    transition: 100ms scale ease-in-out, 100ms translate ease-in-out;
  }
  .custom-styles::part(button):hover {
    scale: 1.1;
  }
  .custom-styles::part(button):active {
    translate: 0 2px;
  }
  .custom-styles::part(button):focus-visible {
    outline: dashed 2px deeppink;
    outline-offset: 4px;
  }
`;
const App = () => (
  <>
    <SlCopyButton value="I'm so stylish" className="custom-styles" />
    <style>{css}</style>
  </>
);

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 무시해도 돼요. 그 외의 경우 아래 스니펫 중 하나를 골라 이 컴포넌트만 골라(cherry pick) 가져올 수 있답니다.

Script / Import / Bundler / React

CDN에서 script 태그로 이 컴포넌트를 가져오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/copy-button/copy-button.js"></script>

CDN에서 JavaScript import로 이 컴포넌트를 가져오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/copy-button/copy-button.js';

bundler를 사용해 이 컴포넌트를 가져오려면:

import '@shoelace-style/shoelace/dist/components/copy-button/copy-button.js';

이 컴포넌트를 React 컴포넌트로 가져오려면:

import SlCopyButton from '@shoelace-style/shoelace/dist/react/copy-button';

Slots

Name Description
copy-icon The icon to show in the default copy state. Works best with sl-icon.
success-icon The icon to show when the content is copied. Works best with sl-icon.
error-icon The icon to show when a copy error occurs. Works best with sl-icon.

슬롯(slots) 사용에 대해 더 알아보기.

Properties

Name Description Reflects Type Default
value The text value to copy. string ''
from An id that references an element in the same document from which data will be copied. If both this and value are present, this value will take precedence. By default, the target element’s textContent will be copied. To copy an attribute, append the attribute name wrapped in square brackets, e.g. from="el[value]". To copy a property, append a dot and the property name, e.g. from="el.value". string ''
disabled Disables the copy button. boolean false
copyLabel / copy-label A custom label to show in the tooltip. string ''
successLabel / success-label A custom label to show in the tooltip after copying. string ''
errorLabel / error-label A custom label to show in the tooltip when a copy error occurs. string ''
feedbackDuration / feedback-duration The length of time to show feedback before restoring the default trigger. number 1000
tooltipPlacement / tooltip-placement The preferred placement of the tooltip. 'top' | 'right' | 'bottom' | 'left' 'top'
hoist Enable this option to prevent the tooltip from being clipped when the component is placed inside a container with overflow: auto|hidden|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios. boolean false
updateComplete A read-only promise that resolves when the component has finished updating.

속성과 프로퍼티(attributes and properties)에 대해 더 알아보기.

Events

Name React Event Description Event Detail
sl-copy onSlCopy Emitted when the data has been copied. -
sl-error onSlError Emitted when the data could not be copied. -

이벤트(events)에 대해 더 알아보기.

Custom Properties

Name Description Default
--success-color The color to use for success feedback.
--error-color The color to use for error feedback.

CSS 커스텀 프로퍼티(custom properties) 커스터마이징에 대해 더 알아보기.

Parts

Name Description
button The internal <sl-button> element.
copy-icon The container that holds the copy icon.
success-icon The container that holds the success icon.
error-icon The container that holds the error icon.
tooltip__base The tooltip’s exported base part.
tooltip__base__popup The tooltip’s exported popup part.
tooltip__base__arrow The tooltip’s exported arrow part.
tooltip__body The tooltip’s exported body part.

CSS 파트(parts) 커스터마이징에 대해 더 알아보기.

Animations

Name Description
copy.in The animation to use when feedback icons animate in.
copy.out The animation to use when feedback icons animate out.

애니메이션(animations) 커스터마이징에 대해 더 알아보기.

Dependencies

이 컴포넌트는 다음 의존성들을 자동으로 가져와요.

  • sl-icon
  • sl-tooltip
  • sl-visually-hidden

더 알아보기 (Learn more)