Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | 3x | import type { ElementType, ReactNode } from 'react';
import type moment from 'moment';
import type { ContentRenderer } from './renderers/types';
/**
* Any value moment.js accepts as a date (string, number, Date, moment instance, etc.).
*/
export type DateInput = moment.MomentInput;
/**
* Format string or array of formats used when parsing non-standard date strings.
*/
export type ParseInput = moment.MomentFormatSpecification;
/**
* When true, uses moment's default calendar formatting; otherwise a custom calendar spec.
*/
export type CalendarInput = boolean | moment.CalendarSpec;
/**
* When formatting durations, trims empty largest-magnitude tokens (boolean or trim template).
*/
export type TrimInput = boolean | string;
/**
* Values produced by built-in/custom renderers and accepted by filters.
*/
export type MomentContent = string | number;
/**
* Transforms rendered content before it is displayed.
*/
export type Filter = (content: MomentContent) => MomentContent;
/**
* Called whenever the displayed content changes (including on interval updates).
*/
export type OnChangeFn = (content: MomentContent) => void;
/**
* Shared content-formatting props used across multiple output modes.
*/
export interface ContentBaseProps {
/**
* moment.js format string for the displayed output.
*/
format?: string;
/**
* With relative-time props, omits the "ago" / "in" suffix from the output.
*/
ago?: boolean;
}
/**
* Props for custom content renderers.
*/
export interface CustomRenderersProps {
/**
* Array of custom ContentRenderer functions invoked before the built-in
* renderers. The first renderer (custom or built-in) to return a
* non-undefined value wins. Falls back to the nearest provider's renderers.
*/
renderers?: ContentRenderer[];
}
/**
* Props for custom content filters.
*/
export interface CustomFiltersProps {
/**
* Custom filters applied in order after formatting.
* Falls back to the nearest provider's filters.
*/
filters?: Filter[];
}
/**
* Props that adjust the parsed date before content rendering.
*/
export interface DateAdjustmentProps {
/**
* Duration to add to the date before formatting (e.g. `{ hours: 12 }`).
*/
add?: moment.DurationInputObject;
/**
* Duration to subtract from the date before formatting.
*/
subtract?: moment.DurationInputObject;
}
/**
* Props for relative-to-now content output.
*/
export interface FromNowContentProps {
/**
* Displays the date relative to now (e.g. "5 minutes ago").
*/
fromNow?: boolean;
/**
* Like `fromNow`, but uses compact units (e.g. "1h", "2d").
*/
fromNowShort?: boolean;
/**
* Shows relative time only while within this many milliseconds; then uses `format`.
*/
fromNowDuring?: number;
}
/**
* Props for interval-from content output.
*/
export interface FromContentProps {
/**
* Displays the interval from this date to the component's date.
*/
from?: DateInput;
}
/**
* Props for interval-to content output.
*/
export interface ToContentProps {
/**
* Displays the interval from the component's date to now.
*/
toNow?: boolean;
/**
* Displays the interval from the component's date to this date.
*/
to?: DateInput;
}
/**
* Props for calendar-style content output.
*/
export interface CalendarContentProps {
/**
* Renders a calendar-style string (e.g. "Yesterday at 3:00 PM").
*/
calendar?: CalendarInput;
}
/**
* Props for numeric diff content output.
*/
export interface DiffContentProps {
/**
* Renders the numeric difference between this date and the component's date.
*/
diff?: DateInput;
/**
* Unit of time for `diff` output (e.g. `'days'`, `'years'`).
*/
unit?: moment.unitOfTime.Diff;
/**
* When true, `diff` returns a floating-point value instead of truncating.
*/
decimal?: boolean;
}
/**
* Props for duration content output.
*/
export interface DurationContentProps {
/**
* Renders the duration between this date and the component's date.
*/
duration?: DateInput;
/**
* Renders the duration between now and the component's date.
*/
durationFromNow?: boolean;
/**
* Passed to moment-duration-format when formatting duration output.
*/
trim?: TrimInput;
}
/**
* Props for parsing and interpreting the date input.
*/
export interface DateInputProps {
/**
* Date to display; falls back to `children` when omitted.
*/
date?: DateInput;
/**
* How to parse the date input when it is not in a standard format.
*/
parse?: ParseInput;
/**
* Treats the date input as a Unix timestamp in seconds.
*/
unix?: boolean;
/**
* Parses and displays the date in UTC.
*/
utc?: boolean;
/**
* Converts the parsed date to the local timezone.
*/
local?: boolean;
/**
* IANA timezone name; requires `moment-timezone`.
*/
tz?: string;
/**
* BCP 47 locale string for formatting and parsing.
*/
locale?: string;
/**
* Date input when no `date` prop is given; otherwise standard React children.
* Accepts strings, numbers, `Date`, moment instances, etc.
*/
children?: ReactNode | DateInput;
}
/**
* Props for component rendering, lifecycle, and DOM forwarding.
*/
export interface RenderProps {
/**
* HTML element type to render (defaults to the settings element, usually `'time'`).
*/
element?: ElementType | null;
/**
* Adds a `title` attribute with a formatted version of the date.
*/
withTitle?: boolean;
/**
* moment.js format string used for the `title` attribute when `withTitle` is set.
*/
titleFormat?: string;
/**
* Milliseconds between automatic re-renders; `0` disables updates (default 60000).
*/
interval?: number;
/**
* Callback invoked when displayed content changes.
*/
onChange?: OnChangeFn;
/**
* Additional props forwarded to the rendered DOM element.
*/
[key: string]: unknown;
}
/**
* Props accepted by the `<Moment>` component.
*/
export interface MomentProps
extends ContentBaseProps,
DateAdjustmentProps,
FromNowContentProps,
FromContentProps,
ToContentProps,
CalendarContentProps,
DiffContentProps,
DurationContentProps,
CustomRenderersProps,
CustomFiltersProps,
DateInputProps,
RenderProps {}
/**
* A mounted `<Moment>` instance registered with the shared pooled timer.
*/
export interface PoolEntry {
/**
* Returns the instance's current props (read on each pooled tick).
*/
getProps: () => MomentProps;
/**
* Triggers a re-render and optional `onChange` for this instance.
*/
update: () => void;
}
/**
* Default settings shared by all `<Moment>` instances unless a prop overrides them.
*/
export interface MomentSettings {
/**
* Custom moment module instance; lazily defaults to the bundled `moment`.
*/
moment: typeof moment | null;
/**
* Default locale applied when a component omits the `locale` prop.
*/
locale: string | null;
/**
* Default `local` flag applied when a component omits the `local` prop.
*/
local: boolean | null;
/**
* Default format string applied when a component omits the `format` prop.
*/
format: string | null;
/**
* Default parse spec applied when a component omits the `parse` prop.
*/
parse: ParseInput | null;
/**
* Default custom filters applied when a component omits the `filters` prop.
*/
filters: Filter[] | null;
/**
* Default custom renderers applied when a component omits the `renderers` prop.
*/
renderers: ContentRenderer[] | null;
/**
* Default element type rendered when a component omits the `element` prop.
*/
element: ElementType;
/**
* Default timezone applied when a component omits the `tz` prop.
*/
timezone: string | null;
}
/**
* Props accepted by `<MomentProvider>`.
*/
export interface MomentProviderProps {
/**
* Child components that receive these defaults.
*/
children: ReactNode;
/**
* When true, one shared timer updates all descendant `<Moment>` instances.
*/
pool?: boolean;
/**
* Milliseconds between pooled timer ticks (default 60000).
*/
interval?: number;
/**
* Custom moment module instance.
*/
moment?: typeof moment | null;
/**
* Default locale applied when a component omits the `locale` prop.
*/
locale?: string | null;
/**
* Default `local` flag applied when a component omits the `local` prop.
*/
local?: boolean | null;
/**
* Default format string applied when a component omits the `format` prop.
*/
format?: string | null;
/**
* Default parse spec applied when a component omits the `parse` prop.
*/
parse?: ParseInput | null;
/**
* Default custom filters applied when a component omits the `filters` prop.
*/
filters?: Filter[] | null;
/**
* Default custom renderers applied when a component omits the `renderers` prop.
*/
renderers?: ContentRenderer[] | null;
/**
* Default element type rendered when a component omits the `element` prop.
*/
element?: ElementType;
/**
* Default timezone applied when a component omits the `tz` prop.
*/
timezone?: string | null;
}
/**
* Known `<Moment>` prop names; used to strip them before forwarding DOM attributes.
*/
export const momentPropKeys: Record<string, true> = {
element: true,
date: true,
parse: true,
format: true,
add: true,
subtract: true,
ago: true,
fromNow: true,
fromNowShort: true,
fromNowDuring: true,
from: true,
toNow: true,
to: true,
calendar: true,
unix: true,
utc: true,
local: true,
tz: true,
withTitle: true,
titleFormat: true,
locale: true,
interval: true,
diff: true,
duration: true,
durationFromNow: true,
trim: true,
unit: true,
decimal: true,
filters: true,
renderers: true,
onChange: true
};
|