상대 시간 (Relative Time)
SlRelativeTime는 현재 날짜와 시간을 기준으로 한 현지화된 상대 시간 문구를 출력해 주는 요소예요. 현지화는 브라우저의 Intl.RelativeTimeFormat API로 처리되므로 별도의 언어 팩이 필요하지 않습니다.
출처: 문서
본문
<!-- Shoelace 2 release date 🎉 -->
<sl-relative-time date="2020-07-15T09:17:00-04:00"></sl-relative-time>
React에서는 이렇게 사용할 수 있어요.
import SlRelativeTime from '@shoelace-style/shoelace/dist/react/relative-time';
const App = () => <SlRelativeTime date="2020-07-15T09:17:00-04:00" />;
date 속성은 날짜/시간을 어느 시점 기준으로 계산할지를 결정해요. Date.parse()가 해석할 수 있는 문자열이어야 하며, JavaScript로 설정할 때는 Date 객체를 넘길 수도 있어요.
문자열을 사용할 때는 03/04/2020처럼 모호한 날짜는 피하는 게 좋아요. 이런 값은 사용자의 브라우저와 로케일에 따라 3월 4일로 해석될 수도 있고 4월 3일로 해석될 수도 있거든요. 대신 항상 유효한 ISO 8601 날짜/시간 문자열을 사용해서 모든 클라이언트에서 날짜가 제대로 파싱되도록 하는 게 좋습니다.
예시 (Examples)
시간 동기화 (Keeping Time in Sync)
sync 속성을 사용하면 시간이 지남에 따라 표시되는 값이 자동으로 갱신돼요.
<div class="relative-time-sync">
<sl-relative-time sync></sl-relative-time>
</div>
<script>
const container = document.querySelector('.relative-time-sync');
const relativeTime = container.querySelector('sl-relative-time');
relativeTime.date = new Date(new Date().getTime() - 60000);
</script>
import SlRelativeTime from '@shoelace-style/shoelace/dist/react/relative-time';
const date = new Date(new Date().getTime() - 60000);
const App = () => <SlRelativeTime date={date} sync />;
표시 형식 (Formatting Styles)
format 속성을 사용하면 시간이 표시되는 방식을 바꿀 수 있어요. 일부 로케일에서는 narrow와 short 형식이 같은 값으로 표시될 수 있다는 점을 참고하세요.
<sl-relative-time date="2020-07-15T09:17:00-04:00" format="narrow"></sl-relative-time><br />
<sl-relative-time date="2020-07-15T09:17:00-04:00" format="short"></sl-relative-time><br />
<sl-relative-time date="2020-07-15T09:17:00-04:00" format="long"></sl-relative-time>
import SlRelativeTime from '@shoelace-style/shoelace/dist/react/relative-time';
const App = () => (
<>
<SlRelativeTime date="2020-07-15T09:17:00-04:00" format="narrow" />
<br />
<SlRelativeTime date="2020-07-15T09:17:00-04:00" format="short" />
<br />
<SlRelativeTime date="2020-07-15T09:17:00-04:00" format="long" />
</>
);
현지화 (Localization)
lang 속성으로 원하는 로케일을 지정할 수 있어요.
English: <sl-relative-time date="2020-07-15T09:17:00-04:00" lang="en-US"></sl-relative-time><br />
Chinese: <sl-relative-time date="2020-07-15T09:17:00-04:00" lang="zh-CN"></sl-relative-time><br />
German: <sl-relative-time date="2020-07-15T09:17:00-04:00" lang="de"></sl-relative-time><br />
Greek: <sl-relative-time date="2020-07-15T09:17:00-04:00" lang="el"></sl-relative-time><br />
Russian: <sl-relative-time date="2020-07-15T09:17:00-04:00" lang="ru"></sl-relative-time>
import SlRelativeTime from '@shoelace-style/shoelace/dist/react/relative-time';
const App = () => (
<>
English: <SlRelativeTime date="2020-07-15T09:17:00-04:00" lang="en-US" />
<br />
Chinese: <SlRelativeTime date="2020-07-15T09:17:00-04:00" lang="zh-CN" />
<br />
German: <SlRelativeTime date="2020-07-15T09:17:00-04:00" lang="de" />
<br />
Greek: <SlRelativeTime date="2020-07-15T09:17:00-04:00" lang="el" />
<br />
Russian: <SlRelativeTime date="2020-07-15T09:17:00-04:00" lang="ru" />
</>
);
불러오기 (Importing)
autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.
Script 태그로 CDN에서 이 컴포넌트를 불러오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/relative-time/relative-time.js"></script>
JavaScript import 구문으로 CDN에서 불러오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/relative-time/relative-time.js';
번들러(bundler)를 사용해 불러오려면:
import '@shoelace-style/shoelace/dist/components/relative-time/relative-time.js';
React 컴포넌트로 불러오려면:
import SlRelativeTime from '@shoelace-style/shoelace/dist/react/relative-time';
속성 (Properties)
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
date |
The date from which to calculate time from. If not set, the current date and time will be used. When passing a string, it’s strongly recommended to use the ISO 8601 format to ensure timezones are handled correctly. To convert a date to this format in JavaScript, use date.toISOString(). |
Date | string |
new Date() |
|
format |
The formatting style to use. | 'long' | 'short' | 'narrow' |
'long' |
|
numeric |
When auto, values such as “yesterday” and “tomorrow” will be shown when possible. When always, values such as “1 day ago” and “in 1 day” will be shown. |
'always' | 'auto' |
'auto' |
|
sync |
Keep the displayed value up to date as time passes. | boolean |
false |
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.