@contentful/f36-datetime 5.0.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -4,7 +4,8 @@ import S from 'dayjs/plugin/utc.js';
4
4
  import A from 'dayjs/plugin/relativeTime.js';
5
5
  import L from 'dayjs/plugin/calendar.js';
6
6
 
7
- var W=Object.defineProperty,b=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var Y=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var P=(e,a,t)=>a in e?W(e,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[a]=t,f=(e,a)=>{for(var t in a||(a={}))Y.call(a,t)&&P(e,t,a[t]);if(p)for(var t of p(a))v.call(a,t)&&P(e,t,a[t]);return e},D=(e,a)=>b(e,H(a));var s=(e,a)=>{var t={};for(var r in e)Y.call(e,r)&&a.indexOf(r)<0&&(t[r]=e[r]);if(e!=null&&p)for(var r of p(e))a.indexOf(r)<0&&v.call(e,r)&&(t[r]=e[r]);return t};o.extend(S);function T(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return o(e).format(t)}function m(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return o(e).utc().format(t)}o.extend(S);o.extend(A);o.extend(L);function i(e,a=new Date){return o(e).from(a)}function x(e,a=new Date){return o(e).isSame(a,"day")?i(e,a):o(e).calendar(a,{sameElse:"DD MMM YYYY"})}var F=(R,d)=>{var c=R,{date:e,format:a="full",testId:t="cf-ui-date-time"}=c,r=s(c,["date","format","testId"]);let n=m(e);return h.createElement("time",D(f({dateTime:n,"data-test-id":t},r),{ref:d}),T(e,a))},j=h.forwardRef(F);o.extend(S);o.extend(A);o.extend(L);var Z=(c,R)=>{var n=c,{date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:r="cf-ui-relative-date-time"}=n,d=s(n,["date","baseDate","isRelativeToCurrentWeek","testId"]);let g=new Date,y=a!=null?a:g,w=o(e),E=m(e),u;return t&&!w.isSame(y,"day")?u=x(e,y):u=i(e,y),h.createElement("time",D(f({dateTime:E,"data-test-id":r},d),{ref:R}),u)},q=h.forwardRef(Z);
7
+ var W=Object.defineProperty,b=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var Y=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var P=(e,a,t)=>a in e?W(e,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[a]=t,f=(e,a)=>{for(var t in a||(a={}))Y.call(a,t)&&P(e,t,a[t]);if(p)for(var t of p(a))v.call(a,t)&&P(e,t,a[t]);return e},D=(e,a)=>b(e,H(a));var s=(e,a)=>{var t={};for(var r in e)Y.call(e,r)&&a.indexOf(r)<0&&(t[r]=e[r]);if(e!=null&&p)for(var r of p(e))a.indexOf(r)<0&&v.call(e,r)&&(t[r]=e[r]);return t};o.extend(S);function T(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return o(e).format(t)}function m(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return o(e).format(t)}o.extend(S);o.extend(A);o.extend(L);function i(e,a=new Date){return o(e).from(a)}function x(e,a=new Date){return o(e).isSame(a,"day")?i(e,a):o(e).calendar(a,{sameElse:"DD MMM YYYY"})}var F=(R,d)=>{var c=R,{date:e,format:a="full",testId:t="cf-ui-date-time"}=c,r=s(c,["date","format","testId"]);let n=m(e);return h.createElement("time",D(f({dateTime:n,"data-test-id":t},r),{ref:d}),T(e,a))},j=h.forwardRef(F);o.extend(S);o.extend(A);o.extend(L);var Z=(c,R)=>{var n=c,{date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:r="cf-ui-relative-date-time"}=n,d=s(n,["date","baseDate","isRelativeToCurrentWeek","testId"]);let g=new Date,y=a!=null?a:g,w=o(e),E=m(e),u;return t&&!w.isSame(y,"day")?u=x(e,y):u=i(e,y),h.createElement("time",D(f({dateTime:E,"data-test-id":r},d),{ref:R}),u)},q=h.forwardRef(Z);
8
8
 
9
9
  export { j as DateTime, q as RelativeDateTime, T as formatDateAndTime, m as formatMachineReadableDateTime, i as formatRelativeDateTime };
10
+ //# sourceMappingURL=out.js.map
10
11
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/DateTime/DateTime.tsx","../../src/utils/formatDateTimeUtils.ts","../../src/utils/relativeDateTimeUtils.ts","../../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","_a","ref","_b","testId","otherProps","__objRest","machineReadableDate","__spreadProps","__spreadValues","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"+kBAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,cACX,UACG,UACHA,EAAW,cACX,UACG,OACHA,EAAW,SACX,UACG,kBACHA,EAAW,kCACX,cAEAA,EAAW,+BAGf,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,aACX,UACG,UACHA,EAAW,QACX,UACG,OACHA,EAAW,eACX,cAEAA,EAAW,6BAGf,OAAOL,EAAMG,CAAI,EAAE,IAAI,EAAE,OAAOE,CAAQ,CAC1C,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChBC,EAMAC,IACG,CAPH,IAAAC,EAAAF,EACE,MAAAV,EACA,OAAAC,EAAS,OACT,OAAAY,EAAS,iBA7Bb,EA0BED,EAIKE,EAAAC,EAJLH,EAIK,CAHH,OACA,SACA,WAKF,IAAMI,EAAsBb,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJZ,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKakB,EAAWvB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMe,EAAoB,CACxBV,EAOAC,IACG,CARH,IAAAC,EAAAF,EACE,MAAAV,EACA,SAAAO,EACA,wBAAAc,EAA0B,GAC1B,OAAAR,EAAS,0BAnDb,EA+CED,EAKKE,EAAAC,EALLH,EAKK,CAJH,OACA,WACA,0BACA,WAKF,IAAMU,EAAM,IAAI,KACVC,EAAgBhB,GAAA,KAAAA,EAAYe,EAC5BE,EAAY3B,EAAMG,CAAI,EACtBgB,EAAsBb,EAA8BH,CAAI,EAE1DyB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAejB,EAAoCR,EAAMuB,CAAa,EAGtEE,EAAenB,EAAuBN,EAAMuB,CAAa,EAIzD3B,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJc,CACH,CAEJ,EAMaC,EAAmB9B,EAAM,WAAWwB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
1
+ {"version":3,"sources":["../../src/DateTime/DateTime.tsx","../../src/utils/formatDateTimeUtils.ts","../../src/utils/relativeDateTimeUtils.ts","../../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","_a","ref","_b","testId","otherProps","__objRest","machineReadableDate","__spreadProps","__spreadValues","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"+kBAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,EAAQ,CACd,IAAK,MACHC,EAAW,cACX,MACF,IAAK,UACHA,EAAW,cACX,MACF,IAAK,OACHA,EAAW,SACX,MACF,IAAK,kBACHA,EAAW,kCACX,MACF,QACEA,EAAW,8BACf,CAEA,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,EAAQ,CACd,IAAK,MACHC,EAAW,aACX,MACF,IAAK,UACHA,EAAW,QACX,MACF,IAAK,OACHA,EAAW,eACX,MACF,QACEA,EAAW,4BACf,CAEA,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChBC,EAMAC,IACG,CAPH,IAAAC,EAAAF,EACE,MAAAV,EACA,OAAAC,EAAS,OACT,OAAAY,EAAS,iBA7Bb,EA0BED,EAIKE,EAAAC,EAJLH,EAIK,CAHH,OACA,SACA,WAKF,IAAMI,EAAsBb,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJZ,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKakB,EAAWvB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMe,EAAoB,CACxBV,EAOAC,IACG,CARH,IAAAC,EAAAF,EACE,MAAAV,EACA,SAAAO,EACA,wBAAAc,EAA0B,GAC1B,OAAAR,EAAS,0BAnDb,EA+CED,EAKKE,EAAAC,EALLH,EAKK,CAJH,OACA,WACA,0BACA,WAKF,IAAMU,EAAM,IAAI,KACVC,EAAgBhB,GAAA,KAAAA,EAAYe,EAC5BE,EAAY3B,EAAMG,CAAI,EACtBgB,EAAsBb,EAA8BH,CAAI,EAE1DyB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAejB,EAAoCR,EAAMuB,CAAa,EAGtEE,EAAenB,EAAuBN,EAAMuB,CAAa,EAIzD3B,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJc,CACH,CAEJ,EAMaC,EAAmB9B,EAAM,WAAWwB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
@@ -0,0 +1,99 @@
1
+ import React from 'react';
2
+ import { PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';
3
+
4
+ declare type DateType = Date | string | number;
5
+ declare type DateFormat = 'fullWithSeconds' | 'full' | 'time' | 'day' | 'weekday';
6
+
7
+ interface DateTimeOwnProps extends CommonProps {
8
+ /**
9
+ * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
10
+ */
11
+ date: DateType;
12
+ /**
13
+ * The format in which the date will be presented
14
+ *
15
+ * @default full
16
+ **/
17
+ format?: DateFormat;
18
+ }
19
+ declare type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;
20
+ /**
21
+ * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag
22
+ */
23
+ declare const DateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof DateTimeOwnProps> & DateTimeOwnProps & React.RefAttributes<HTMLTimeElement>>;
24
+
25
+ interface RelativeDateTimeInternalProps extends CommonProps {
26
+ /**
27
+ * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
28
+ */
29
+ date: DateType;
30
+ /**
31
+ * If a value is passed to baseDate, then the component will compare both dates and return the time between them.
32
+ * If no value is passed then the date will be compared to "now"
33
+ *
34
+ * @default "Now"
35
+ */
36
+ baseDate?: DateType;
37
+ /**
38
+ * Sets the date to be relative only if it is in the current week
39
+ * @default false
40
+ */
41
+ isRelativeToCurrentWeek?: boolean;
42
+ }
43
+ declare type RelativeDateTimeProps = PropsWithHTMLElement<RelativeDateTimeInternalProps, 'time'>;
44
+ /**
45
+ * The RelativeDateTime will show a `date` relative to "now" or to the `baseDate`
46
+ * (e.g. in a day, in one month, one month ago, etc).
47
+ */
48
+ declare const RelativeDateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof RelativeDateTimeInternalProps> & RelativeDateTimeInternalProps & React.RefAttributes<HTMLTimeElement>>;
49
+
50
+ /**
51
+ * A funtion that will return a formatted date string. The format will dependend on the option
52
+ * passed in the second argument.
53
+ * By default, it will return a string with Forma 36’s "full" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)
54
+ *
55
+ * @param {DateType} date - the date to be formatted
56
+ * @param {DateFormat} format - the desired format ("full", "day", "weekday", or "time")
57
+ * @returns a formatted date
58
+ *
59
+ * @example
60
+ * formatDateAndTime('2021-08-17T15:45:00') // returns "Tue, 17 Aug 2021 at 3:45 PM"
61
+ *
62
+ * @example
63
+ * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns "17 Aug 2021"
64
+ */
65
+ declare function formatDateAndTime(date: DateType, format?: DateFormat): string;
66
+ /**
67
+ * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag
68
+ * By default, it will return a string with "YYYY-MM-DDTHH:mm:ss.SSS[Z]" format
69
+ *
70
+ * @param {DateType} date - the date to be formatted
71
+ * @param {DateFormat} format - the desired format ("full", "day", "weekday", or "time")
72
+ * @returns a formatted date
73
+ *
74
+ * @example
75
+ * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z
76
+ *
77
+ * @example
78
+ * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13
79
+ */
80
+ declare function formatMachineReadableDateTime(date: DateType, format?: DateFormat): string;
81
+
82
+ /**
83
+ * A function that will return a string with how far a given date is in the past or future,
84
+ * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.
85
+ *
86
+ * @param {DateType} date - the date to be formatted
87
+ * @param {DateFormat} baseDate - the date that should be used as a reference (default is "today")
88
+ * @returns a relative date
89
+ *
90
+ * @example
91
+ * // Considering today as 18.08.2021
92
+ * formatRelativeDateTime('2021-08-17T15:45:00') // returns "a day ago"
93
+ *
94
+ * @example
95
+ * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns "in a day"
96
+ */
97
+ declare function formatRelativeDateTime(date: DateType, baseDate?: DateType): string;
98
+
99
+ export { DateTime, DateTimeProps, RelativeDateTime, RelativeDateTimeProps, formatDateAndTime, formatMachineReadableDateTime, formatRelativeDateTime };
package/dist/index.js CHANGED
@@ -1,26 +1,25 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var h = require('react');
6
4
  var o = require('dayjs');
7
5
  var S = require('dayjs/plugin/utc.js');
8
6
  var A = require('dayjs/plugin/relativeTime.js');
9
7
  var L = require('dayjs/plugin/calendar.js');
10
8
 
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
10
 
13
- var h__default = /*#__PURE__*/_interopDefaultLegacy(h);
14
- var o__default = /*#__PURE__*/_interopDefaultLegacy(o);
15
- var S__default = /*#__PURE__*/_interopDefaultLegacy(S);
16
- var A__default = /*#__PURE__*/_interopDefaultLegacy(A);
17
- var L__default = /*#__PURE__*/_interopDefaultLegacy(L);
11
+ var h__default = /*#__PURE__*/_interopDefault(h);
12
+ var o__default = /*#__PURE__*/_interopDefault(o);
13
+ var S__default = /*#__PURE__*/_interopDefault(S);
14
+ var A__default = /*#__PURE__*/_interopDefault(A);
15
+ var L__default = /*#__PURE__*/_interopDefault(L);
18
16
 
19
- var W=Object.defineProperty,b=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var Y=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var P=(e,a,t)=>a in e?W(e,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[a]=t,f=(e,a)=>{for(var t in a||(a={}))Y.call(a,t)&&P(e,t,a[t]);if(p)for(var t of p(a))v.call(a,t)&&P(e,t,a[t]);return e},D=(e,a)=>b(e,H(a));var s=(e,a)=>{var t={};for(var r in e)Y.call(e,r)&&a.indexOf(r)<0&&(t[r]=e[r]);if(e!=null&&p)for(var r of p(e))a.indexOf(r)<0&&v.call(e,r)&&(t[r]=e[r]);return t};o__default["default"].extend(S__default["default"]);function T(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return o__default["default"](e).format(t)}function m(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return o__default["default"](e).utc().format(t)}o__default["default"].extend(S__default["default"]);o__default["default"].extend(A__default["default"]);o__default["default"].extend(L__default["default"]);function i(e,a=new Date){return o__default["default"](e).from(a)}function x(e,a=new Date){return o__default["default"](e).isSame(a,"day")?i(e,a):o__default["default"](e).calendar(a,{sameElse:"DD MMM YYYY"})}var F=(R,d)=>{var c=R,{date:e,format:a="full",testId:t="cf-ui-date-time"}=c,r=s(c,["date","format","testId"]);let n=m(e);return h__default["default"].createElement("time",D(f({dateTime:n,"data-test-id":t},r),{ref:d}),T(e,a))},j=h__default["default"].forwardRef(F);o__default["default"].extend(S__default["default"]);o__default["default"].extend(A__default["default"]);o__default["default"].extend(L__default["default"]);var Z=(c,R)=>{var n=c,{date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:r="cf-ui-relative-date-time"}=n,d=s(n,["date","baseDate","isRelativeToCurrentWeek","testId"]);let g=new Date,y=a!=null?a:g,w=o__default["default"](e),E=m(e),u;return t&&!w.isSame(y,"day")?u=x(e,y):u=i(e,y),h__default["default"].createElement("time",D(f({dateTime:E,"data-test-id":r},d),{ref:R}),u)},q=h__default["default"].forwardRef(Z);
17
+ var W=Object.defineProperty,b=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var Y=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var P=(e,a,t)=>a in e?W(e,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[a]=t,f=(e,a)=>{for(var t in a||(a={}))Y.call(a,t)&&P(e,t,a[t]);if(p)for(var t of p(a))v.call(a,t)&&P(e,t,a[t]);return e},D=(e,a)=>b(e,H(a));var s=(e,a)=>{var t={};for(var r in e)Y.call(e,r)&&a.indexOf(r)<0&&(t[r]=e[r]);if(e!=null&&p)for(var r of p(e))a.indexOf(r)<0&&v.call(e,r)&&(t[r]=e[r]);return t};o__default.default.extend(S__default.default);function T(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return o__default.default(e).format(t)}function m(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return o__default.default(e).format(t)}o__default.default.extend(S__default.default);o__default.default.extend(A__default.default);o__default.default.extend(L__default.default);function i(e,a=new Date){return o__default.default(e).from(a)}function x(e,a=new Date){return o__default.default(e).isSame(a,"day")?i(e,a):o__default.default(e).calendar(a,{sameElse:"DD MMM YYYY"})}var F=(R,d)=>{var c=R,{date:e,format:a="full",testId:t="cf-ui-date-time"}=c,r=s(c,["date","format","testId"]);let n=m(e);return h__default.default.createElement("time",D(f({dateTime:n,"data-test-id":t},r),{ref:d}),T(e,a))},j=h__default.default.forwardRef(F);o__default.default.extend(S__default.default);o__default.default.extend(A__default.default);o__default.default.extend(L__default.default);var Z=(c,R)=>{var n=c,{date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:r="cf-ui-relative-date-time"}=n,d=s(n,["date","baseDate","isRelativeToCurrentWeek","testId"]);let g=new Date,y=a!=null?a:g,w=o__default.default(e),E=m(e),u;return t&&!w.isSame(y,"day")?u=x(e,y):u=i(e,y),h__default.default.createElement("time",D(f({dateTime:E,"data-test-id":r},d),{ref:R}),u)},q=h__default.default.forwardRef(Z);
20
18
 
21
19
  exports.DateTime = j;
22
20
  exports.RelativeDateTime = q;
23
21
  exports.formatDateAndTime = T;
24
22
  exports.formatMachineReadableDateTime = m;
25
23
  exports.formatRelativeDateTime = i;
24
+ //# sourceMappingURL=out.js.map
26
25
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/DateTime/DateTime.tsx","../src/utils/formatDateTimeUtils.ts","../src/utils/relativeDateTimeUtils.ts","../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","_a","ref","_b","testId","otherProps","__objRest","machineReadableDate","__spreadProps","__spreadValues","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"+kBAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,cACX,UACG,UACHA,EAAW,cACX,UACG,OACHA,EAAW,SACX,UACG,kBACHA,EAAW,kCACX,cAEAA,EAAW,+BAGf,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,aACX,UACG,UACHA,EAAW,QACX,UACG,OACHA,EAAW,eACX,cAEAA,EAAW,6BAGf,OAAOL,EAAMG,CAAI,EAAE,IAAI,EAAE,OAAOE,CAAQ,CAC1C,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChBC,EAMAC,IACG,CAPH,IAAAC,EAAAF,EACE,MAAAV,EACA,OAAAC,EAAS,OACT,OAAAY,EAAS,iBA7Bb,EA0BED,EAIKE,EAAAC,EAJLH,EAIK,CAHH,OACA,SACA,WAKF,IAAMI,EAAsBb,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJZ,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKakB,EAAWvB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMe,EAAoB,CACxBV,EAOAC,IACG,CARH,IAAAC,EAAAF,EACE,MAAAV,EACA,SAAAO,EACA,wBAAAc,EAA0B,GAC1B,OAAAR,EAAS,0BAnDb,EA+CED,EAKKE,EAAAC,EALLH,EAKK,CAJH,OACA,WACA,0BACA,WAKF,IAAMU,EAAM,IAAI,KACVC,EAAgBhB,GAAA,KAAAA,EAAYe,EAC5BE,EAAY3B,EAAMG,CAAI,EACtBgB,EAAsBb,EAA8BH,CAAI,EAE1DyB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAejB,EAAoCR,EAAMuB,CAAa,EAGtEE,EAAenB,EAAuBN,EAAMuB,CAAa,EAIzD3B,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJc,CACH,CAEJ,EAMaC,EAAmB9B,EAAM,WAAWwB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
1
+ {"version":3,"sources":["../src/DateTime/DateTime.tsx","../src/utils/formatDateTimeUtils.ts","../src/utils/relativeDateTimeUtils.ts","../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","_a","ref","_b","testId","otherProps","__objRest","machineReadableDate","__spreadProps","__spreadValues","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"+kBAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,EAAQ,CACd,IAAK,MACHC,EAAW,cACX,MACF,IAAK,UACHA,EAAW,cACX,MACF,IAAK,OACHA,EAAW,SACX,MACF,IAAK,kBACHA,EAAW,kCACX,MACF,QACEA,EAAW,8BACf,CAEA,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,EAAQ,CACd,IAAK,MACHC,EAAW,aACX,MACF,IAAK,UACHA,EAAW,QACX,MACF,IAAK,OACHA,EAAW,eACX,MACF,QACEA,EAAW,4BACf,CAEA,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChBC,EAMAC,IACG,CAPH,IAAAC,EAAAF,EACE,MAAAV,EACA,OAAAC,EAAS,OACT,OAAAY,EAAS,iBA7Bb,EA0BED,EAIKE,EAAAC,EAJLH,EAIK,CAHH,OACA,SACA,WAKF,IAAMI,EAAsBb,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJZ,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKakB,EAAWvB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMe,EAAoB,CACxBV,EAOAC,IACG,CARH,IAAAC,EAAAF,EACE,MAAAV,EACA,SAAAO,EACA,wBAAAc,EAA0B,GAC1B,OAAAR,EAAS,0BAnDb,EA+CED,EAKKE,EAAAC,EALLH,EAKK,CAJH,OACA,WACA,0BACA,WAKF,IAAMU,EAAM,IAAI,KACVC,EAAgBhB,GAAA,KAAAA,EAAYe,EAC5BE,EAAY3B,EAAMG,CAAI,EACtBgB,EAAsBb,EAA8BH,CAAI,EAE1DyB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAejB,EAAoCR,EAAMuB,CAAa,EAGtEE,EAAenB,EAAuBN,EAAMuB,CAAa,EAIzD3B,EAAA,cAAC,OAAAqB,EAAAC,EAAA,CACC,SAAUF,EACV,eAAcH,GACVC,GAHL,CAIC,IAAKH,IAEJc,CACH,CAEJ,EAMaC,EAAmB9B,EAAM,WAAWwB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@contentful/f36-datetime",
3
- "version": "5.0.0",
3
+ "version": "5.1.1",
4
4
  "description": "Forma 36: DateTime component",
5
5
  "scripts": {
6
6
  "build": "tsup"
7
7
  },
8
8
  "dependencies": {
9
- "@contentful/f36-core": "^5.0.0",
10
- "@contentful/f36-tokens": "^4.0.1",
9
+ "@contentful/f36-core": "^5.1.1",
10
+ "@contentful/f36-tokens": "^5.1.0",
11
11
  "dayjs": "^1.11.5",
12
12
  "emotion": "^10.0.17"
13
13
  },
14
14
  "peerDependencies": {
15
- "react": ">=16.8"
15
+ "react": ">=16.8",
16
+ "react-dom": ">=16.8"
16
17
  },
17
18
  "license": "MIT",
18
19
  "files": [
@@ -26,9 +27,10 @@
26
27
  "browserslist": "extends @contentful/browserslist-config",
27
28
  "repository": {
28
29
  "type": "git",
29
- "url": "https://github.com/contentful/forma-36"
30
+ "url": "git+https://github.com/contentful/forma-36.git"
30
31
  },
31
32
  "publishConfig": {
33
+ "registry": "https://npm.pkg.github.com/",
32
34
  "access": "public"
33
35
  }
34
36
  }