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

포맷 바이트 (Format Bytes)

원문 보기 위키 갱신

숫자를 사람이 읽기 좋은 바이트(bytes) 값으로 포맷해 주는 컴포넌트예요. 파일 크기처럼 큰 숫자를 "1.2 KB"처럼 보기 좋게 표시할 때 아주 유용해요. _ | SlFormatBytes, Shoelace 2.0부터 제공되고 있으며 stable 상태예요.

출처: 문서

본문

| SlFormatBytes — Since 2.0, stable

숫자를 사람이 읽기 좋은 바이트 값으로 포맷해 줘요.

<div class="format-bytes-overview">
  The file is <sl-format-bytes value="1000"></sl-format-bytes> in size. <br /><br />
  <sl-input type="number" value="1000" label="Number to Format" style="max-width: 180px;"></sl-input>
</div>
<script>
  const container = document.querySelector('.format-bytes-overview');
  const formatter = container.querySelector('sl-format-bytes');
  const input = container.querySelector('sl-input');
  input.addEventListener('sl-input', () => (formatter.value = input.value || 0));
</script>
import { useState } from 'react';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlFormatBytes from '@shoelace-style/shoelace/dist/react/format-bytes';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => {
  const [value, setValue] = useState(1000);
  return (
    <>
      The file is <SlFormatBytes value={value} /> in size.
      <br />
      <br />
      <SlInput
        type="number"
        value={value}
        label="Number to Format"
        style={{ maxWidth: '180px' }}
        onSlInput={event => setValue(event.target.value)}
      />
    </>
  );
};

Examples

Formatting Bytes

value 속성에 숫자를 설정하면 그 값을 바이트 단위로 보여줘요.

<sl-format-bytes value="12"></sl-format-bytes><br />
<sl-format-bytes value="1200"></sl-format-bytes><br />
<sl-format-bytes value="1200000"></sl-format-bytes><br />
<sl-format-bytes value="1200000000"></sl-format-bytes>
import SlFormatBytes from '@shoelace-style/shoelace/dist/react/format-bytes';
const App = () => (
  <>
    <SlFormatBytes value="12" />
    <br />
    <SlFormatBytes value="1200" />
    <br />
    <SlFormatBytes value="1200000" />
    <br />
    <SlFormatBytes value="1200000000" />
  </>
);

Formatting Bits

값을 비트(bit) 단위로 보여주려면 unit 속성을 bit로 설정하면 돼요.

<sl-format-bytes value="12" unit="bit"></sl-format-bytes><br />
<sl-format-bytes value="1200" unit="bit"></sl-format-bytes><br />
<sl-format-bytes value="1200000" unit="bit"></sl-format-bytes><br />
<sl-format-bytes value="1200000000" unit="bit"></sl-format-bytes>
import SlFormatBytes from '@shoelace-style/shoelace/dist/react/format-bytes';
const App = () => (
  <>
    <SlFormatBytes value="12" unit="bit" />
    <br />
    <SlFormatBytes value="1200" unit="bit" />
    <br />
    <SlFormatBytes value="1200000" unit="bit" />
    <br />
    <SlFormatBytes value="1200000000" unit="bit" />
  </>
);

Localization

lang 속성으로 숫자 포맷의 로케일(locale)을 지정할 수 있어요.

<sl-format-bytes value="12" lang="de"></sl-format-bytes><br />
<sl-format-bytes value="1200" lang="de"></sl-format-bytes><br />
<sl-format-bytes value="1200000" lang="de"></sl-format-bytes><br />
<sl-format-bytes value="1200000000" lang="de"></sl-format-bytes>
import SlFormatBytes from '@shoelace-style/shoelace/dist/react/format-bytes';
const App = () => (
  <>
    <SlFormatBytes value="12" lang="de" />
    <br />
    <SlFormatBytes value="1200" lang="de" />
    <br />
    <SlFormatBytes value="1200000" lang="de" />
    <br />
    <SlFormatBytes value="1200000000" lang="de" />
  </>
);

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 건너뛰어도 돼요. 그 외에는 아래 스니펫 중 원하는 걸 골라 이 컴포넌트만 골라 가져올 수 있어요.

Script / Import / Bundler / React 방식이 준비되어 있어요.

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

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

CDN에서 JavaScript import로 가져오려면:

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

Bundler를 사용해 가져오려면:

import '@shoelace-style/shoelace/dist/components/format-bytes/format-bytes.js';

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

import SlFormatBytes from '@shoelace-style/shoelace/dist/react/format-bytes';

Properties

Name Description Reflects Type Default
value The number to format in bytes. number 0
unit The type of unit to display. 'byte' | 'bit' 'byte'
display Determines how to display the result, e.g. “100 bytes”, “100 b”, or “100b”. 'long' | 'short' | 'narrow' 'short'
updateComplete A read-only promise that resolves when the component has
finished updating.

속성과 프로퍼티에 대해 더 알아보세요.

더 알아보기 (Learn more)