Skip to content

Commit db5e8a1

Browse files
committed
automatically provide localised Date for SSR in RelativeTime
1 parent 4b823ff commit db5e8a1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
import React from 'react'
12
import {RelativeTimeElement} from '@github/relative-time-element'
2-
import type {ComponentProps} from '../utils/types'
33
import {createComponent} from '../utils/custom-element'
44

5-
const RelativeTime = createComponent(RelativeTimeElement, 'relative-time')
5+
const RelativeTimeComponent = createComponent(RelativeTimeElement, 'relative-time')
6+
7+
export type RelativeTimeProps = ComponentProps<typeof RelativeTimeComponent>
8+
9+
const localeOptions: Intl.DateTimeFormatOptions = {month: 'short', day: 'numeric', year: 'numeric'}
10+
function RelativeTime({date, ...props}: RelativeTimeProps) {
11+
return (
12+
<RelativeTimeComponent {...props} date={date}>
13+
${date?.toLocaleDateString('en', localeOptions) || ''}
14+
</RelativeTimeComponent>
15+
)
16+
}
617

7-
export type RelativeTimeProps = ComponentProps<typeof RelativeTime>
818
export default RelativeTime

0 commit comments

Comments
 (0)