Skip to content

Commit 127c761

Browse files
committed
refactor: use a pure css solution for on hover timestamps
1 parent e39a859 commit 127c761

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

packages/core/demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ <h3 class="title">With subsequent messages</h3>
173173
<discord-message profile="favna" message-body-only timestamp="12:39"
174174
>That's how Discord handles multiple messages from the same author as well</discord-message
175175
>
176-
<discord-message profile="favna" message-body-only timestamp="12:39"
176+
<discord-message profile="favna" message-body-only timestamp="12:40"
177177
>Just keep in mind that Discord does some funky stuff like with time between messages, this library doesn't automatically
178178
change what is displayed!</discord-message
179179
>

packages/core/src/components/discord-message/DiscordMessage.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { consume } from '@lit/context';
22
import { css, html, LitElement } from 'lit';
33
import { customElement, property } from 'lit/decorators.js';
4+
import { classMap } from 'lit/directives/class-map.js';
45
import { ifDefined } from 'lit/directives/if-defined.js';
5-
import { createRef, ref, type Ref } from 'lit/directives/ref.js';
66
import { when } from 'lit/directives/when.js';
77
import { avatars, profiles } from '../../config.js';
88
import type { DiscordMessageProps, Profile, LightTheme, DiscordTimestamp } from '../../types.js';
@@ -49,7 +49,6 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
4949
}
5050
5151
:host([message-body-only]) {
52-
min-height: 24px !important;
5352
margin-top: 0px !important;
5453
padding-top: 0.125rem !important;
5554
padding-bottom: 0.0625rem !important;
@@ -131,6 +130,11 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
131130
width: 56px;
132131
}
133132
133+
:host(:hover) .discord-message-timestamp-hover::before {
134+
content: attr(datetime);
135+
min-height: 24px !important;
136+
}
137+
134138
:host([light-theme]) .discord-message-timestamp {
135139
color: #747f8d;
136140
}
@@ -248,8 +252,6 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
248252
}
249253
`;
250254

251-
protected messageBodyOnlyTimeRef: Ref<HTMLTimeElement> = createRef();
252-
253255
/**
254256
* The id of the profile data to use.
255257
*/
@@ -381,19 +383,6 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
381383
);
382384
}
383385

384-
protected handleMessageEnter() {
385-
if (this.messageBodyOnly && this.messageBodyOnlyTimeRef.value) {
386-
const computedTimestamp = handleTimestamp(this.timestamp, true, this.twentyFour);
387-
this.messageBodyOnlyTimeRef.value.textContent = computedTimestamp ?? '';
388-
}
389-
}
390-
391-
protected handleMessageLeave() {
392-
if (this.messageBodyOnlyTimeRef.value) {
393-
this.messageBodyOnlyTimeRef.value.textContent = '';
394-
}
395-
}
396-
397386
protected override render() {
398387
const defaultData: Profile = {
399388
author: this.author,
@@ -413,29 +402,22 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
413402

414403
return html`
415404
<slot name="reply"></slot>
416-
<div class="discord-message-inner" @mouseenter=${this.handleMessageEnter} @mouseleave=${this.handleMessageLeave}>
405+
<div class="discord-message-inner">
417406
${when(
418407
this.compactMode && !this.messageBodyOnly,
419408
() => html`<time datetime="${ifDefined(computedTimestamp)}" class="discord-message-timestamp">${computedTimestamp}</time>`,
420409
() => null
421410
)}
422411
${when(
423-
this.messageBodyOnly && !this.compactMode,
424-
() =>
425-
html`<time
426-
${ref(this.messageBodyOnlyTimeRef)}
427-
datetime="${ifDefined(computedTimestamp)}"
428-
class="discord-message-timestamp discord-message-body-only-indent"
429-
></time>`,
430-
() => null
431-
)}
432-
${when(
433-
this.compactMode && this.messageBodyOnly,
412+
this.messageBodyOnly,
434413
() =>
435414
html`<time
436-
${ref(this.messageBodyOnlyTimeRef)}
437415
datetime="${ifDefined(computedTimestamp)}"
438-
class="discord-message-timestamp"
416+
class=${classMap({
417+
'discord-message-timestamp': true,
418+
'discord-message-timestamp-hover': true,
419+
'discord-message-body-only-indent': !this.compactMode
420+
})}
439421
></time>`,
440422
() => null
441423
)}

0 commit comments

Comments
 (0)