Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-pots-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

RelativeTime: Adds `noTitle` prop if you want to not render the `title` attribute with full date time.
6 changes: 6 additions & 0 deletions packages/react/src/RelativeTime/RelativeTime.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
"name": "ref",
"type": "React.RefObject<RelativeTimeElement>"
},
{
"name": "noTitle",
"type": "boolean",
"defaultValue": "",
"description": "Removes the `title` attribute provided on the element by default"
Copy link
Copy Markdown
Member

@siddharthkp siddharthkp Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! 👋

For the docs, can you also add when is it recommended to skip title?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this description? Or are you talking about updating the in-progress docs PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally both places, but either is fine for now :)

},
{
"name": "as",
"type": "React.ElementType",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import RelativeTime from './RelativeTime'
import Link from '../Link'
import {Tooltip} from '../TooltipV2'

export default {
title: 'Components/RelativeTime/Examples',
component: RelativeTime,
}

export const NoTitleAttribute = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />

export const LinkWithTooltip = () => (
<Tooltip text={new Date('2020-01-01T00:00:00Z').toString()}>
<Link href="https://www.github.com">
<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />
</Link>
</Tooltip>
)
2 changes: 1 addition & 1 deletion packages/react/src/RelativeTime/RelativeTime.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const meta: Meta = {
},
}

export const Default: Story = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} />
export const Default: Story = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />

export const Playground: Story = args => {
const {date, ...rest} = args
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/RelativeTime/RelativeTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {createComponent} from '../utils/create-component'
const RelativeTimeComponent = createComponent(RelativeTimeElement, 'relative-time')

const localeOptions: Intl.DateTimeFormatOptions = {month: 'short', day: 'numeric', year: 'numeric'}
function RelativeTime({date, datetime, children, ...props}: RelativeTimeProps) {
function RelativeTime({date, datetime, children, noTitle, ...props}: RelativeTimeProps) {
if (datetime) date = new Date(datetime)
return (
<RelativeTimeComponent {...props} date={date}>
<RelativeTimeComponent {...props} date={date} no-title={noTitle ? '' : undefined}>
{children || date?.toLocaleDateString('en', localeOptions) || ''}
</RelativeTimeComponent>
)
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/__tests__/RelativeTime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ describe('RelativeTime', () => {
'server rendered date',
])
})

it('does not render no-title attribute by default', () => {
const date = new Date('2024-03-07T12:22:48.123Z')
const {container} = HTMLRender(<RelativeTime date={date} />)
expect(container.firstChild).not.toHaveAttribute('no-title')
})

it('adds no-title attribute if noTitle={true}', () => {
const date = new Date('2024-03-07T12:22:48.123Z')
const {container} = HTMLRender(<RelativeTime date={date} noTitle={true} />)
expect(container.firstChild).toHaveAttribute('no-title')
})
})