@tryghost/shade 0.1.0-alpha.31 → 0.1.0-alpha.33

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/README.md CHANGED
@@ -19,7 +19,7 @@ integration. Shade does not ship a compiled stylesheet.
19
19
  For an existing React/Vite app:
20
20
 
21
21
  ```sh
22
- pnpm add @tryghost/shade@alpha react@18 react-dom@18
22
+ pnpm add @tryghost/shade react@18 react-dom@18
23
23
  pnpm add -D tailwindcss@^4.1 @tailwindcss/vite@^4.1
24
24
  ```
25
25
 
@@ -130,8 +130,8 @@ var formatDisplayTime = (dateString, timezone) => {
130
130
  };
131
131
  /**
132
132
  * Formats a date based on the range, in the browser's time zone
133
- * - For ranges above 365 days: shows month and year (e.g. "Apr 2025")
134
- * - For ranges of 91 days or more: shows "Week of [date]"
133
+ * - For ranges of 365 days or more: shows month and year (e.g. "Apr 2025")
134
+ * - For ranges of 90 days or more: shows "Week of [date]"
135
135
  * - For other ranges: shows the date in the same format as formatDisplayDate
136
136
  */
137
137
  var formatDisplayDateWithRange = (date, range, showHours = false, hoursOnly = false) => {
@@ -141,8 +141,8 @@ var formatDisplayDateWithRange = (date, range, showHours = false, hoursOnly = fa
141
141
  const parts = getDateTimeParts(parsed);
142
142
  if (range === 1 && hoursOnly) return formatTime(parts);
143
143
  else if (range === 1 && showHours) return `${MONTHS[parts.month]} ${parts.day}, ${formatTime(parts)}`;
144
- else if (range > 365) return `${MONTHS[parts.month]} ${parts.year}`;
145
- else if (range >= 91) return `Week of ${formatDate(parts)}`;
144
+ else if (range >= 365) return `${MONTHS[parts.month]} ${parts.year}`;
145
+ else if (range >= 90) return `Week of ${formatDate(parts)}`;
146
146
  return formatDate(parts);
147
147
  };
148
148
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"date-utils.js","names":[],"sources":["../../src/lib/date-utils.ts"],"sourcesContent":["const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];\n\nconst DAY_MS = 24 * 60 * 60 * 1000;\n\n// Shown for a date that can't be parsed, so it can't be mistaken for a missing date\nconst INVALID_DATE = 'Invalid date';\n\ninterface DateTimeParts {\n year: number;\n month: number;\n day: number;\n hour: number;\n minute: number;\n second: number;\n}\n\nconst zonedFormatters = new Map<string, Intl.DateTimeFormat>();\n\n// A time zone the browser doesn't know falls back to UTC, as it did with moment-timezone\nconst getZonedFormatter = (timeZone: string): Intl.DateTimeFormat => {\n let formatter = zonedFormatters.get(timeZone);\n if (!formatter) {\n try {\n formatter = new Intl.DateTimeFormat('en-US', {\n timeZone,\n hourCycle: 'h23',\n year: 'numeric',\n month: 'numeric',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n second: 'numeric',\n });\n } catch (error) {\n if (!(error instanceof RangeError)) {\n throw error;\n }\n formatter = getZonedFormatter('UTC');\n }\n zonedFormatters.set(timeZone, formatter);\n }\n return formatter;\n};\n\n// Without a time zone, the parts are in the browser's local time\nconst getDateTimeParts = (date: Date, timeZone?: string): DateTimeParts => {\n if (!timeZone) {\n return {\n year: date.getFullYear(),\n month: date.getMonth(),\n day: date.getDate(),\n hour: date.getHours(),\n minute: date.getMinutes(),\n second: date.getSeconds(),\n };\n }\n\n const parts = getZonedFormatter(timeZone).formatToParts(date);\n const get = (type: Intl.DateTimeFormatPartTypes) =>\n Number(parts.find((part) => part.type === type)?.value);\n\n return {\n year: get('year'),\n month: get('month') - 1,\n day: get('day'),\n hour: get('hour'),\n minute: get('minute'),\n second: get('second'),\n };\n};\n\n// Unlike Date.UTC(), this doesn't read years 0-99 as 1900-1999\nconst toUTCTime = ({ year, month, day, hour, minute, second }: DateTimeParts): number => {\n const date = new Date(0);\n date.setUTCFullYear(year, month, day);\n date.setUTCHours(hour, minute, second);\n return date.getTime();\n};\n\n// Wall-clock time that a time zone skips, such as 2:30am when clocks go forward, moves forward by\n// the gap. Wall-clock time that it repeats reads as the first occurrence. Both match moment-timezone.\nconst fromWallClockTime = (parts: DateTimeParts, timeZone?: string): Date => {\n if (!timeZone) {\n // Local Date setters resolve skipped and repeated times the same way\n const date = new Date(0);\n date.setFullYear(parts.year, parts.month, parts.day);\n date.setHours(parts.hour, parts.minute, parts.second, 0);\n return date;\n }\n\n const wallClockTime = toUTCTime(parts);\n const readWallClockTime = (time: number) => toUTCTime(getDateTimeParts(new Date(time), timeZone));\n const getOffset = (time: number) => readWallClockTime(time) - time;\n\n // The offsets a day either side are the ones in use on each side of any change at this time\n const withOffsetBefore = wallClockTime - getOffset(wallClockTime - DAY_MS);\n const withOffsetAfter = wallClockTime - getOffset(wallClockTime + DAY_MS);\n if (withOffsetBefore === withOffsetAfter) {\n return new Date(withOffsetBefore);\n }\n\n const occurrences = [withOffsetBefore, withOffsetAfter]\n .filter((time) => readWallClockTime(time) === wallClockTime)\n .sort((a, b) => a - b);\n return new Date(occurrences[0] ?? withOffsetBefore);\n};\n\n// ISO 8601 dates and date-times in the forms moment reads, in the extended\n// (`2025-04-15T13:05:00+01:00`) and basic (`20250415T130500+0100`) formats\nconst ISO_EXTENDED_DATE_TIME =\n /^(?<year>\\d{4})-(?<month>\\d{2})(?:-(?<day>\\d{2})(?:[T ](?<hour>\\d{2})(?::(?<minute>\\d{2})(?::(?<second>\\d{2})(?:[.,]\\d+)?)?)?(?<offset>Z|(?<offsetSign>[+-])(?<offsetHours>\\d{2}):?(?<offsetMinutes>\\d{2})?)?)?)?$/;\nconst ISO_BASIC_DATE_TIME =\n /^(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})(?:[T ](?<hour>\\d{2})(?:(?<minute>\\d{2})(?:(?<second>\\d{2})(?:[.,]\\d+)?)?)?(?<offset>Z|(?<offsetSign>[+-])(?<offsetHours>\\d{2}):?(?<offsetMinutes>\\d{2})?)?)?$/;\n\n// An ISO 8601 date or date-time without an offset is wall-clock time in the time zone, as moment\n// reads it. `new Date()` alone would read a date-only string as UTC midnight.\nconst parseDate = (value: string, timeZone?: string): Date | null => {\n const match = ISO_EXTENDED_DATE_TIME.exec(value) ?? ISO_BASIC_DATE_TIME.exec(value);\n if (!match?.groups) {\n const date = new Date(value);\n return isNaN(date.getTime()) ? null : date;\n }\n\n const {\n year,\n month,\n day = '1',\n hour = '0',\n minute = '0',\n second = '0',\n offset,\n offsetSign,\n offsetHours = '0',\n offsetMinutes = '0',\n } = match.groups;\n const parts = {\n year: Number(year),\n month: Number(month) - 1,\n day: Number(day),\n hour: Number(hour),\n minute: Number(minute),\n second: Number(second),\n };\n\n // The formats only check the shape, so reject fields that would roll over, such as 30 February or 24:00\n const utc = new Date(toUTCTime(parts));\n const isValid =\n utc.getUTCMonth() === parts.month &&\n utc.getUTCDate() === parts.day &&\n utc.getUTCHours() === parts.hour &&\n utc.getUTCMinutes() === parts.minute &&\n utc.getUTCSeconds() === parts.second &&\n Number(offsetHours) < 24 &&\n Number(offsetMinutes) < 60;\n if (!isValid) {\n return null;\n }\n\n if (!offset) {\n return fromWallClockTime(parts, timeZone);\n }\n const offsetMs = (Number(offsetHours) * 60 + Number(offsetMinutes)) * 60 * 1000;\n return new Date(utc.getTime() - (offsetSign === '-' ? -offsetMs : offsetMs));\n};\n\n// Result is in the format of `7:09pm`\nconst formatTime = ({ hour, minute }: DateTimeParts): string =>\n `${hour % 12 || 12}:${String(minute).padStart(2, '0')}${hour < 12 ? 'am' : 'pm'}`;\n\n// Result is in the format of `12 Jun 2025`, without the year when it's the current year\nconst formatDate = ({ year, month, day }: DateTimeParts, timeZone?: string): string => {\n const date = `${day} ${MONTHS[month]}`;\n return year === getDateTimeParts(new Date(), timeZone).year ? date : `${date} ${year}`;\n};\n\n// Format date for UI, result is in the format of `12 Jun 2025`\n// Timestamps with an offset are shown in the given time zone, or in UTC without one\nexport const formatDisplayDate = (dateString: string, timezone?: string): string => {\n // If the date is a Date object, convert it to a string\n // @ts-expect-error This should error if dateString is not a string, but for some reason Typescript isn't catching this\n if (dateString instanceof Date) {\n dateString = dateString.toISOString();\n }\n // Fallback to empty string if dateString is an unexpected type. Better to fallback to empty string than to crash the app\n if (!dateString || dateString.length === 0 || typeof dateString !== 'string') {\n return '';\n }\n\n const timeZone = timezone || 'UTC';\n const date = parseDate(dateString, timeZone);\n return date ? formatDate(getDateTimeParts(date, timeZone), timeZone) : INVALID_DATE;\n};\n\n/**\n * Format a plain time in a given time zone.\n *\n * @example\n * formatDisplayTime('2020-04-20T18:09:12.345Z', 'Africa/Lagos')\n * // 7:09pm\n */\nexport const formatDisplayTime = (dateString: string, timezone: string): string => {\n if (!dateString) {\n return '';\n }\n\n const timeZone = timezone || 'UTC';\n const date = parseDate(dateString, timeZone);\n return date ? formatTime(getDateTimeParts(date, timeZone)) : INVALID_DATE;\n};\n\n/**\n * Formats a date based on the range, in the browser's time zone\n * - For ranges above 365 days: shows month and year (e.g. \"Apr 2025\")\n * - For ranges of 91 days or more: shows \"Week of [date]\"\n * - For other ranges: shows the date in the same format as formatDisplayDate\n */\nexport const formatDisplayDateWithRange = (\n date: string,\n range: number,\n showHours: boolean = false,\n hoursOnly: boolean = false,\n): string => {\n if (!date) {\n return '';\n }\n\n const parsed = parseDate(date);\n if (!parsed) {\n return INVALID_DATE;\n }\n const parts = getDateTimeParts(parsed);\n\n if (range === 1 && hoursOnly) {\n return formatTime(parts);\n } else if (range === 1 && showHours) {\n return `${MONTHS[parts.month]} ${parts.day}, ${formatTime(parts)}`;\n } else if (range > 365) {\n return `${MONTHS[parts.month]} ${parts.year}`;\n } else if (range >= 91) {\n return `Week of ${formatDate(parts)}`;\n }\n return formatDate(parts);\n};\n"],"mappings":";AAAA,IAAM,SAAS;CAAC;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;AAAK;AAElG,IAAM,SAAS,OAAU,KAAK;AAG9B,IAAM,eAAe;AAWrB,IAAM,kCAAkB,IAAI,IAAiC;AAG7D,IAAM,qBAAqB,aAA0C;CACnE,IAAI,YAAY,gBAAg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C,MACH,OAAO;CAGT,MAAM,SAAS,UAAU,IAAI;CAC7B,IAAI,CAAC,QACH,OAAO;CAET,MAAM,QAAQ,iBAAiB,MAAM;CAErC,IAAI,UAAU,KAAK,WACjB,OAAO,WAAW,KAAK;MAClB,IAAI,UAAU,KAAK,WACxB,OAAO,GAAG,OAAO,MAAM,OAAO,GAAG,MAAM,IAAI,IAAI,WAAW,KAAK;MAC1D,IAAI,QAAQ,KACjB,OAAO,GAAG,OAAO,MAAM,OAAO,GAAG,MAAM;MAClC,IAAI,SAAS,IAClB,OAAO,WAAW,WAAW,KAAK;CAEpC,OAAO,WAAW,KAAK;AACzB"}
1
+ {"version":3,"file":"date-utils.js","names":[],"sources":["../../src/lib/date-utils.ts"],"sourcesContent":["const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];\n\nconst DAY_MS = 24 * 60 * 60 * 1000;\n\n// Shown for a date that can't be parsed, so it can't be mistaken for a missing date\nconst INVALID_DATE = 'Invalid date';\n\ninterface DateTimeParts {\n year: number;\n month: number;\n day: number;\n hour: number;\n minute: number;\n second: number;\n}\n\nconst zonedFormatters = new Map<string, Intl.DateTimeFormat>();\n\n// A time zone the browser doesn't know falls back to UTC, as it did with moment-timezone\nconst getZonedFormatter = (timeZone: string): Intl.DateTimeFormat => {\n let formatter = zonedFormatters.get(timeZone);\n if (!formatter) {\n try {\n formatter = new Intl.DateTimeFormat('en-US', {\n timeZone,\n hourCycle: 'h23',\n year: 'numeric',\n month: 'numeric',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n second: 'numeric',\n });\n } catch (error) {\n if (!(error instanceof RangeError)) {\n throw error;\n }\n formatter = getZonedFormatter('UTC');\n }\n zonedFormatters.set(timeZone, formatter);\n }\n return formatter;\n};\n\n// Without a time zone, the parts are in the browser's local time\nconst getDateTimeParts = (date: Date, timeZone?: string): DateTimeParts => {\n if (!timeZone) {\n return {\n year: date.getFullYear(),\n month: date.getMonth(),\n day: date.getDate(),\n hour: date.getHours(),\n minute: date.getMinutes(),\n second: date.getSeconds(),\n };\n }\n\n const parts = getZonedFormatter(timeZone).formatToParts(date);\n const get = (type: Intl.DateTimeFormatPartTypes) =>\n Number(parts.find((part) => part.type === type)?.value);\n\n return {\n year: get('year'),\n month: get('month') - 1,\n day: get('day'),\n hour: get('hour'),\n minute: get('minute'),\n second: get('second'),\n };\n};\n\n// Unlike Date.UTC(), this doesn't read years 0-99 as 1900-1999\nconst toUTCTime = ({ year, month, day, hour, minute, second }: DateTimeParts): number => {\n const date = new Date(0);\n date.setUTCFullYear(year, month, day);\n date.setUTCHours(hour, minute, second);\n return date.getTime();\n};\n\n// Wall-clock time that a time zone skips, such as 2:30am when clocks go forward, moves forward by\n// the gap. Wall-clock time that it repeats reads as the first occurrence. Both match moment-timezone.\nconst fromWallClockTime = (parts: DateTimeParts, timeZone?: string): Date => {\n if (!timeZone) {\n // Local Date setters resolve skipped and repeated times the same way\n const date = new Date(0);\n date.setFullYear(parts.year, parts.month, parts.day);\n date.setHours(parts.hour, parts.minute, parts.second, 0);\n return date;\n }\n\n const wallClockTime = toUTCTime(parts);\n const readWallClockTime = (time: number) => toUTCTime(getDateTimeParts(new Date(time), timeZone));\n const getOffset = (time: number) => readWallClockTime(time) - time;\n\n // The offsets a day either side are the ones in use on each side of any change at this time\n const withOffsetBefore = wallClockTime - getOffset(wallClockTime - DAY_MS);\n const withOffsetAfter = wallClockTime - getOffset(wallClockTime + DAY_MS);\n if (withOffsetBefore === withOffsetAfter) {\n return new Date(withOffsetBefore);\n }\n\n const occurrences = [withOffsetBefore, withOffsetAfter]\n .filter((time) => readWallClockTime(time) === wallClockTime)\n .sort((a, b) => a - b);\n return new Date(occurrences[0] ?? withOffsetBefore);\n};\n\n// ISO 8601 dates and date-times in the forms moment reads, in the extended\n// (`2025-04-15T13:05:00+01:00`) and basic (`20250415T130500+0100`) formats\nconst ISO_EXTENDED_DATE_TIME =\n /^(?<year>\\d{4})-(?<month>\\d{2})(?:-(?<day>\\d{2})(?:[T ](?<hour>\\d{2})(?::(?<minute>\\d{2})(?::(?<second>\\d{2})(?:[.,]\\d+)?)?)?(?<offset>Z|(?<offsetSign>[+-])(?<offsetHours>\\d{2}):?(?<offsetMinutes>\\d{2})?)?)?)?$/;\nconst ISO_BASIC_DATE_TIME =\n /^(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})(?:[T ](?<hour>\\d{2})(?:(?<minute>\\d{2})(?:(?<second>\\d{2})(?:[.,]\\d+)?)?)?(?<offset>Z|(?<offsetSign>[+-])(?<offsetHours>\\d{2}):?(?<offsetMinutes>\\d{2})?)?)?$/;\n\n// An ISO 8601 date or date-time without an offset is wall-clock time in the time zone, as moment\n// reads it. `new Date()` alone would read a date-only string as UTC midnight.\nconst parseDate = (value: string, timeZone?: string): Date | null => {\n const match = ISO_EXTENDED_DATE_TIME.exec(value) ?? ISO_BASIC_DATE_TIME.exec(value);\n if (!match?.groups) {\n const date = new Date(value);\n return isNaN(date.getTime()) ? null : date;\n }\n\n const {\n year,\n month,\n day = '1',\n hour = '0',\n minute = '0',\n second = '0',\n offset,\n offsetSign,\n offsetHours = '0',\n offsetMinutes = '0',\n } = match.groups;\n const parts = {\n year: Number(year),\n month: Number(month) - 1,\n day: Number(day),\n hour: Number(hour),\n minute: Number(minute),\n second: Number(second),\n };\n\n // The formats only check the shape, so reject fields that would roll over, such as 30 February or 24:00\n const utc = new Date(toUTCTime(parts));\n const isValid =\n utc.getUTCMonth() === parts.month &&\n utc.getUTCDate() === parts.day &&\n utc.getUTCHours() === parts.hour &&\n utc.getUTCMinutes() === parts.minute &&\n utc.getUTCSeconds() === parts.second &&\n Number(offsetHours) < 24 &&\n Number(offsetMinutes) < 60;\n if (!isValid) {\n return null;\n }\n\n if (!offset) {\n return fromWallClockTime(parts, timeZone);\n }\n const offsetMs = (Number(offsetHours) * 60 + Number(offsetMinutes)) * 60 * 1000;\n return new Date(utc.getTime() - (offsetSign === '-' ? -offsetMs : offsetMs));\n};\n\n// Result is in the format of `7:09pm`\nconst formatTime = ({ hour, minute }: DateTimeParts): string =>\n `${hour % 12 || 12}:${String(minute).padStart(2, '0')}${hour < 12 ? 'am' : 'pm'}`;\n\n// Result is in the format of `12 Jun 2025`, without the year when it's the current year\nconst formatDate = ({ year, month, day }: DateTimeParts, timeZone?: string): string => {\n const date = `${day} ${MONTHS[month]}`;\n return year === getDateTimeParts(new Date(), timeZone).year ? date : `${date} ${year}`;\n};\n\n// Format date for UI, result is in the format of `12 Jun 2025`\n// Timestamps with an offset are shown in the given time zone, or in UTC without one\nexport const formatDisplayDate = (dateString: string, timezone?: string): string => {\n // If the date is a Date object, convert it to a string\n // @ts-expect-error This should error if dateString is not a string, but for some reason Typescript isn't catching this\n if (dateString instanceof Date) {\n dateString = dateString.toISOString();\n }\n // Fallback to empty string if dateString is an unexpected type. Better to fallback to empty string than to crash the app\n if (!dateString || dateString.length === 0 || typeof dateString !== 'string') {\n return '';\n }\n\n const timeZone = timezone || 'UTC';\n const date = parseDate(dateString, timeZone);\n return date ? formatDate(getDateTimeParts(date, timeZone), timeZone) : INVALID_DATE;\n};\n\n/**\n * Format a plain time in a given time zone.\n *\n * @example\n * formatDisplayTime('2020-04-20T18:09:12.345Z', 'Africa/Lagos')\n * // 7:09pm\n */\nexport const formatDisplayTime = (dateString: string, timezone: string): string => {\n if (!dateString) {\n return '';\n }\n\n const timeZone = timezone || 'UTC';\n const date = parseDate(dateString, timeZone);\n return date ? formatTime(getDateTimeParts(date, timeZone)) : INVALID_DATE;\n};\n\n/**\n * Formats a date based on the range, in the browser's time zone\n * - For ranges of 365 days or more: shows month and year (e.g. \"Apr 2025\")\n * - For ranges of 90 days or more: shows \"Week of [date]\"\n * - For other ranges: shows the date in the same format as formatDisplayDate\n */\nexport const formatDisplayDateWithRange = (\n date: string,\n range: number,\n showHours: boolean = false,\n hoursOnly: boolean = false,\n): string => {\n if (!date) {\n return '';\n }\n\n const parsed = parseDate(date);\n if (!parsed) {\n return INVALID_DATE;\n }\n const parts = getDateTimeParts(parsed);\n\n if (range === 1 && hoursOnly) {\n return formatTime(parts);\n } else if (range === 1 && showHours) {\n return `${MONTHS[parts.month]} ${parts.day}, ${formatTime(parts)}`;\n } else if (range >= 365) {\n return `${MONTHS[parts.month]} ${parts.year}`;\n } else if (range >= 90) {\n return `Week of ${formatDate(parts)}`;\n }\n return formatDate(parts);\n};\n"],"mappings":";AAAA,IAAM,SAAS;CAAC;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;CAAO;AAAK;AAElG,IAAM,SAAS,OAAU,KAAK;AAG9B,IAAM,eAAe;AAWrB,IAAM,kCAAkB,IAAI,IAAiC;AAG7D,IAAM,qBAAqB,aAA0C;CACnE,IAAI,YAAY,gBAAgB,IAAI,QAAQ;CAC5C,IAAI,CAAC,WAAW;EACd,IAAI;GACF,YAAY,IAAI,KAAK,eAAe,SAAS;IAC3C;IACA,WAAW;IACX,MAAM;IACN,OAAO;IACP,KAAK;IACL,MAAM;IACN,QAAQ;IACR,QAAQ;GACV,CAAC;EACH,SAAS,OAAO;GACd,IAAI,EAAE,iBAAiB,aACrB,MAAM;GAER,YAAY,kBAAkB,KAAK;EACrC;EACA,gBAAgB,IAAI,UAAU,SAAS;CACzC;CACA,OAAO;AACT;AAGA,IAAM,oBAAoB,MAAY,aAAqC;CACzE,IAAI,CAAC,UACH,OAAO;EACL,MAAM,KAAK,YAAY;EACvB,OAAO,KAAK,SAAS;EACrB,KAAK,KAAK,QAAQ;EAClB,MAAM,KAAK,SAAS;EACpB,QAAQ,KAAK,WAAW;EACxB,QAAQ,KAAK,WAAW;CAC1B;CAGF,MAAM,QAAQ,kBAAkB,QAAQ,CAAC,CAAC,cAAc,IAAI;CAC5D,MAAM,OAAO,SACX,OAAO,MAAM,MAAM,SAAS,KAAK,SAAS,IAAI,CAAC,EAAE,KAAK;CAExD,OAAO;EACL,MAAM,IAA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B;CAGlF,IAAI,sBAAsB,MACxB,aAAa,WAAW,YAAY;CAGtC,IAAI,CAAC,cAAc,WAAW,WAAW,KAAK,OAAO,eAAe,UAClE,OAAO;CAGT,MAAM,WAAW,YAAY;CAC7B,MAAM,OAAO,UAAU,YAAY,QAAQ;CAC3C,OAAO,OAAO,WAAW,iBAAiB,MAAM,QAAQ,GAAG,QAAQ,IAAI;AACzE;;;;;;;;AASA,IAAa,qBAAqB,YAAoB,aAA6B;CACjF,IAAI,CAAC,YACH,OAAO;CAGT,MAAM,WAAW,YAAY;CAC7B,MAAM,OAAO,UAAU,YAAY,QAAQ;CAC3C,OAAO,OAAO,WAAW,iBAAiB,MAAM,QAAQ,CAAC,IAAI;AAC/D;;;;;;;AAQA,IAAa,8BACX,MACA,OACA,YAAqB,OACrB,YAAqB,UACV;CACX,IAAI,CAAC,MACH,OAAO;CAGT,MAAM,SAAS,UAAU,IAAI;CAC7B,IAAI,CAAC,QACH,OAAO;CAET,MAAM,QAAQ,iBAAiB,MAAM;CAErC,IAAI,UAAU,KAAK,WACjB,OAAO,WAAW,KAAK;MAClB,IAAI,UAAU,KAAK,WACxB,OAAO,GAAG,OAAO,MAAM,OAAO,GAAG,MAAM,IAAI,IAAI,WAAW,KAAK;MAC1D,IAAI,SAAS,KAClB,OAAO,GAAG,OAAO,MAAM,OAAO,GAAG,MAAM;MAClC,IAAI,SAAS,IAClB,OAAO,WAAW,WAAW,KAAK;CAEpC,OAAO,WAAW,KAAK;AACzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/shade",
3
- "version": "0.1.0-alpha.31",
3
+ "version": "0.1.0-alpha.33",
4
4
  "description": "Ghost design system for React apps",
5
5
  "license": "MIT",
6
6
  "author": "Ghost Foundation",
@@ -9,8 +9,8 @@ export declare const formatDisplayDate: (dateString: string, timezone?: string)
9
9
  export declare const formatDisplayTime: (dateString: string, timezone: string) => string;
10
10
  /**
11
11
  * Formats a date based on the range, in the browser's time zone
12
- * - For ranges above 365 days: shows month and year (e.g. "Apr 2025")
13
- * - For ranges of 91 days or more: shows "Week of [date]"
12
+ * - For ranges of 365 days or more: shows month and year (e.g. "Apr 2025")
13
+ * - For ranges of 90 days or more: shows "Week of [date]"
14
14
  * - For other ranges: shows the date in the same format as formatDisplayDate
15
15
  */
16
16
  export declare const formatDisplayDateWithRange: (date: string, range: number, showHours?: boolean, hoursOnly?: boolean) => string;