-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically provide localised Date for SSR in RelativeTime (#4305)
* automatically provide localised Date for SSR in RelativeTime * changeset * fix and add tests * format * fix types
- Loading branch information
Showing
3 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': patch | ||
--- | ||
|
||
Render SSR date for RelativeTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import React from 'react' | ||
import {RelativeTimeElement} from '@github/relative-time-element' | ||
import type {ComponentProps} from '../utils/types' | ||
import {createComponent} from '../utils/custom-element' | ||
|
||
const RelativeTime = createComponent(RelativeTimeElement, 'relative-time') | ||
const RelativeTimeComponent = createComponent(RelativeTimeElement, 'relative-time') | ||
|
||
export type RelativeTimeProps = ComponentProps<typeof RelativeTime> | ||
const localeOptions: Intl.DateTimeFormatOptions = {month: 'short', day: 'numeric', year: 'numeric'} | ||
function RelativeTime({date, datetime, ...props}: RelativeTimeProps) { | ||
if (datetime) date = new Date(datetime) | ||
return ( | ||
<RelativeTimeComponent {...props} date={date}> | ||
{date?.toLocaleDateString('en', localeOptions) || ''} | ||
</RelativeTimeComponent> | ||
) | ||
} | ||
|
||
export type RelativeTimeProps = ComponentProps<typeof RelativeTimeComponent> | ||
export default RelativeTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters