Skip to content

Commit 942e704

Browse files
element/ak-timestamp: consistency pass and final check
# What - Removes the unused Interface declaration - Documents the observers. - Adds `showElapsed` to the builder - Updates the builder to allow (and typecheck) standard HTMLElement attributes - Fixes the `@csspart` documentation, which was out of date - Commented the `@observer` handlers, since how they’re called is not obvious - Revised the tests to work with Vitest + Playwright.
1 parent bf44865 commit 942e704

File tree

4 files changed

+358
-92
lines changed

4 files changed

+358
-92
lines changed

src/ak-timestamp/ak-timestamp.builder.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { ITimestamp, Timestamp } from "./ak-timestamp.component.js";
1+
import type { ElementRest } from "../types.js";
2+
import { Timestamp } from "./ak-timestamp.component.js";
3+
4+
import { spread } from "@open-wc/lit-helpers";
25

36
import { html } from "lit";
47
import { ifDefined } from "lit/directives/if-defined.js";
58

6-
export type TimestampProps = Partial<ITimestamp>;
9+
export type TimestampProps = ElementRest & Partial<Timestamp>;
710

811
/**
912
* @summary Helper function to create a Timestamp component programmatically
@@ -13,18 +16,30 @@ export type TimestampProps = Partial<ITimestamp>;
1316
* @see {@link Timestamp} - The underlying web component
1417
*/
1518
export function akTimestamp(options: TimestampProps = {}) {
16-
const { date, dateFormat, displaySuffix, is12Hour, locale, raw, displayUTC, timeFormat } =
17-
options;
19+
const {
20+
date,
21+
dateFormat,
22+
displaySuffix,
23+
is12Hour,
24+
locale,
25+
raw,
26+
displayUTC,
27+
timeFormat,
28+
showElapsed,
29+
...rest
30+
} = options;
1831

1932
return html`
2033
<ak-timestamp
34+
${spread(rest)}
2135
date=${ifDefined(date)}
2236
date-format=${ifDefined(dateFormat)}
2337
display-suffix=${ifDefined(displaySuffix)}
24-
?is-12-hour=${is12Hour}
38+
?is-12-hour=${!!is12Hour}
2539
locale=${ifDefined(locale)}
2640
.raw=${ifDefined(raw)}
27-
?display-utc=${displayUTC}
41+
?display-utc=${!!displayUTC}
42+
?show-elapsed=${!!showElapsed}
2843
time-format=${ifDefined(timeFormat)}
2944
></ak-timestamp>
3045
`;

src/ak-timestamp/ak-timestamp.component.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ const mapHas = (
3939
...rest: PropertyKey[]
4040
) => (Array.isArray(keys) ? [...keys, ...rest] : [keys, ...rest]).some((key) => changed.has(key));
4141

42-
/**
43-
* Interface for TimestampOptions
44-
*/
45-
export interface ITimestamp {
46-
date?: string;
47-
raw?: Date;
48-
dateFormat?: TimestampFormat;
49-
timeFormat?: TimestampFormat;
50-
displaySuffix?: string;
51-
is12Hour?: boolean;
52-
locale?: string;
53-
displayUTC?: boolean;
54-
}
55-
5642
/**
5743
* @element ak-timestamp
5844
* @class Timestamp
@@ -83,10 +69,17 @@ export interface ITimestamp {
8369
*
8470
* ## Styling Hooks
8571
*
86-
* @csspart time - The time element that displays the formatted date. Use this to customize the appearance of the timestamp.
72+
* @csspart timestamp - The time element that displays the formatted date.
73+
* @csspart elapsed - If using the elapsed feature, the display of the "time since..." string.
74+
* @csspart warning - A message displayed if the time passed in is unparsable.
75+
*
76+
* ## ARIA Note
8777
*
78+
* @internal
79+
* This component honors `prefers-reduced-motion` when `show-elapsed=true` by only updating the
80+
* component every minute, not every second.
8881
*/
89-
export class Timestamp extends LitElement implements ITimestamp {
82+
export class Timestamp extends LitElement {
9083
public static readonly styles = [styles];
9184

9285
/**
@@ -171,6 +164,7 @@ export class Timestamp extends LitElement implements ITimestamp {
171164

172165
#interval = -1;
173166

167+
// Triggered by the `@observed` decorator.
174168
protected _rawChanged() {
175169
const checkedDate = checkAndValidate(this.raw);
176170
if (checkedDate) {
@@ -179,6 +173,7 @@ export class Timestamp extends LitElement implements ITimestamp {
179173
}
180174
}
181175

176+
// Triggered by the `@observed` decorator.
182177
protected _dateChanged() {
183178
this.#date = match(this.date)
184179
.when(

0 commit comments

Comments
 (0)