-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeago helper function #780
Conversation
export function relativeTime (date) { | ||
// create element | ||
const el = document.createElement('time'); | ||
el.dateTime = date.toISOString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just to make sure we know what's going on here - putting the dateTime
property on an element allows you to use the .timeago()
jquery thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's correct. timeago
is a plugin from npm; it's not directly obvious where we load it from, but you can read more about it here: https://www.npmjs.com/package/timeago
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it looks good
Adds a helper function
TBui.relativeTime()
which creates<time>
elements that are automatically rendered as relative times, without UI code being required to manually invoketimeago
. This should help avoid issues like #777 in the future.timeago
needs to be run once the target element is already in the DOM, so this implementation keeps track of the elements the helper function has created and uses aMutationObserver
to calltimeago
automatically once they are in the DOM. This is especially helpful for the submission/comment UI builder functions, which until now required consuming code to manually$result.find('time').timeago()
after the result was added to the DOM.