Skip to content

Commit 1d82a41

Browse files
committed
Add tooltip for private comment option
1 parent 453fcf8 commit 1d82a41

File tree

8 files changed

+195
-0
lines changed

8 files changed

+195
-0
lines changed

packages/comment-widget/src/base-form.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import './comment-editor';
2323
import { ofetch } from 'ofetch';
2424
import type { CommentEditor } from './comment-editor';
2525
import { cleanHtml } from './utils/html';
26+
import './base-tooltip';
2627

2728
export class BaseForm extends LitElement {
2829
@consume({ context: baseUrlContext })
@@ -251,6 +252,9 @@ export class BaseForm extends LitElement {
251252
<div class="flex items-center gap-2">
252253
<input id="hidden" name="hidden" type="checkbox" />
253254
<label for="hidden" class="text-xs select-none text-text-3 hover:text-text-1 transition-all">${msg('Private')}</label>
255+
<base-tooltip content=${this.currentUser ? msg('Currently logged in. After selecting the private option, comments will only be visible to yourself and the site administrator.') : msg('You are currently anonymous. After selecting the private option, the comment will only be visible to the site administrator.')}>
256+
<i class="i-mingcute:information-line size-3.5 text-text-3 block"></i>
257+
</base-tooltip>
254258
</div>
255259
`
256260
: ''
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import {
2+
autoUpdate,
3+
computePosition,
4+
flip,
5+
offset,
6+
shift,
7+
} from '@floating-ui/dom';
8+
import type { TemplateResult } from 'lit';
9+
import { css, html, LitElement } from 'lit';
10+
import { property } from 'lit/decorators.js';
11+
import baseStyles from './styles/base';
12+
13+
/**
14+
* A simple tooltip component based on Lit and floating-ui.
15+
*
16+
* @example
17+
* ```html
18+
* <base-tooltip content="This is a tooltip">
19+
* <button>Hover me</button>
20+
* </base-tooltip>
21+
* ```
22+
*/
23+
export class BaseTooltip extends LitElement {
24+
/**
25+
* The content to display in the tooltip.
26+
*/
27+
@property({ type: String })
28+
content: string | TemplateResult = '';
29+
30+
private tooltipEl?: HTMLElement;
31+
private cleanupFn?: () => void;
32+
33+
override connectedCallback(): void {
34+
super.connectedCallback();
35+
this.addEventListener('mouseenter', this._showTooltip);
36+
this.addEventListener('mouseleave', this._hideTooltip);
37+
}
38+
39+
override disconnectedCallback(): void {
40+
super.disconnectedCallback();
41+
this.removeEventListener('mouseenter', this._showTooltip);
42+
this.removeEventListener('mouseleave', this._hideTooltip);
43+
44+
// Clean up positioning if needed
45+
if (this.cleanupFn) {
46+
this.cleanupFn();
47+
this.cleanupFn = undefined;
48+
}
49+
}
50+
51+
override firstUpdated(): void {
52+
// Get tooltip element after the component is rendered
53+
this.tooltipEl = this.shadowRoot?.querySelector('.tooltip') as HTMLElement;
54+
}
55+
56+
private _showTooltip = (): void => {
57+
if (!this.tooltipEl) return;
58+
59+
this.tooltipEl.classList.add('show');
60+
this._updatePosition();
61+
};
62+
63+
private _hideTooltip = (): void => {
64+
if (!this.tooltipEl) return;
65+
66+
this.tooltipEl.classList.remove('show');
67+
68+
// Clean up positioning
69+
if (this.cleanupFn) {
70+
this.cleanupFn();
71+
this.cleanupFn = undefined;
72+
}
73+
};
74+
75+
private _updatePosition(): void {
76+
if (!this.tooltipEl) return;
77+
78+
const floating = this.tooltipEl;
79+
80+
// Clean up previous positioning if any
81+
if (this.cleanupFn) {
82+
this.cleanupFn();
83+
}
84+
85+
// Use autoUpdate to reposition on scroll/resize
86+
this.cleanupFn = autoUpdate(this, floating, async () => {
87+
// Calculate the position
88+
const middlewares = [
89+
offset(8), // 8px distance from trigger
90+
flip(), // flip to other side if needed
91+
shift({ padding: 5 }), // shift to keep in viewport
92+
];
93+
94+
// Calculate position
95+
const { x, y } = await computePosition(this, floating, {
96+
placement: 'top', // Default placement is top
97+
middleware: middlewares,
98+
});
99+
100+
// Position the tooltip
101+
Object.assign(floating.style, {
102+
left: `${x}px`,
103+
top: `${y}px`,
104+
});
105+
});
106+
}
107+
108+
override render() {
109+
return html`
110+
<slot></slot>
111+
<div class="tooltip">
112+
${this.content}
113+
</div>
114+
`;
115+
}
116+
117+
static override styles = [
118+
...baseStyles,
119+
css`
120+
:host {
121+
display: inline-block;
122+
}
123+
124+
::slotted(*) {
125+
display: inline-block;
126+
}
127+
128+
.tooltip {
129+
position: absolute;
130+
background: rgba(0, 0, 0, 0.8);
131+
color: white;
132+
padding: 5px 10px;
133+
border-radius: 4px;
134+
font-size: 0.875em;
135+
max-width: 300px;
136+
width: auto;
137+
z-index: 10;
138+
opacity: 0;
139+
pointer-events: none;
140+
transition: opacity 0.2s;
141+
top: 0;
142+
left: 0;
143+
white-space: normal;
144+
word-wrap: break-word;
145+
}
146+
147+
.tooltip.show {
148+
opacity: 1;
149+
}
150+
`,
151+
];
152+
}
153+
154+
customElements.get('base-tooltip') ||
155+
customElements.define('base-tooltip', BaseTooltip);
156+
157+
declare global {
158+
interface HTMLElementTagNameMap {
159+
'base-tooltip': BaseTooltip;
160+
}
161+
}

packages/comment-widget/src/generated/locales/es.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
's3643189d1abbb7f4': `Código`,
2323
's3fb33d17bad61aa9': `Comentario enviado con éxito, pendiente de revisión`,
2424
's4c0e15f9073382e6': `Error al obtener el código de verificación`,
25+
's5184a3f3e2f7b603': `Actualmente anónimo. Después de seleccionar la opción privada, el comentario solo será visible para el administrador del sitio.`,
2526
's523eb9043213ff0d': `Itálica`,
2627
's58a3c1ecd4dd06cf': `Tachado`,
2728
's67749057edb2586b': `Cerrar sesión`,
2829
's6cb61eeccda272d5': `Bloque de código`,
2930
's7437373e541a8037': `Apodo`,
3031
's7584ded3d749c75e': `Cargar más`,
3132
's82665b2ffabc9c0a': `Sitio web`,
33+
's838e512973be01d4': `Actualmente conectado. Después de seleccionar la opción privada, los comentarios solo serán visibles para usted y el administrador del sitio.`,
3234
's84b033b2f7360187': `Por favor, inicie sesión o complete la información primero`,
3335
's98a5f7789c49dd3f': `En revisión`,
3436
's9f2ed66340f019c6': `Escribir un comentario`,

packages/comment-widget/src/generated/locales/zh-CN.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
's3643189d1abbb7f4': `代码`,
2323
's3fb33d17bad61aa9': `评论成功,请等待审核`,
2424
's4c0e15f9073382e6': `获取验证码失败`,
25+
's5184a3f3e2f7b603': `当前是匿名状态,选择私密选项后,评论将仅对网站管理员可见。`,
2526
's523eb9043213ff0d': `斜体`,
2627
's58a3c1ecd4dd06cf': `删除线`,
2728
's67749057edb2586b': `退出登录`,
2829
's6cb61eeccda272d5': `代码块`,
2930
's7437373e541a8037': `昵称`,
3031
's7584ded3d749c75e': `加载更多`,
3132
's82665b2ffabc9c0a': `网站`,
33+
's838e512973be01d4': `当前已登录,选择私密选项后,评论将仅对您和网站管理员可见。`,
3234
's84b033b2f7360187': `请先登录或者完善信息`,
3335
's98a5f7789c49dd3f': `审核中`,
3436
's9f2ed66340f019c6': `编写评论`,

packages/comment-widget/src/generated/locales/zh-TW.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
's3643189d1abbb7f4': `程式碼`,
2323
's3fb33d17bad61aa9': `評論成功,請等待審核`,
2424
's4c0e15f9073382e6': `獲取驗證碼失敗`,
25+
's5184a3f3e2f7b603': `目前是匿名狀態,選擇私密選項後,評論將僅對網站管理員可見。`,
2526
's523eb9043213ff0d': `斜體`,
2627
's58a3c1ecd4dd06cf': `刪除線`,
2728
's67749057edb2586b': `登出`,
2829
's6cb61eeccda272d5': `程式碼區塊`,
2930
's7437373e541a8037': `暱稱`,
3031
's7584ded3d749c75e': `載入更多`,
3132
's82665b2ffabc9c0a': `網站`,
33+
's838e512973be01d4': `目前已登入,選擇私密選項後,評論將僅對您和網站管理員可見。`,
3234
's84b033b2f7360187': `請先登入或者完善資訊`,
3335
's98a5f7789c49dd3f': `審核中`,
3436
's9f2ed66340f019c6': `撰寫評論`,

packages/comment-widget/xliff/es.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@
154154
<source>Private</source>
155155
<target>Íntimo</target>
156156
</trans-unit>
157+
<trans-unit id="s838e512973be01d4">
158+
<source>Currently logged in. After selecting the private option, comments will only be visible to yourself and the site administrator.</source>
159+
<target>Actualmente conectado. Después de seleccionar la opción privada, los comentarios solo serán visibles para usted y el administrador del sitio.</target>
160+
</trans-unit>
161+
<trans-unit id="s5184a3f3e2f7b603">
162+
<source>You are currently anonymous. After selecting the private option, the comment will only be visible to the site administrator.</source>
163+
<target>Actualmente anónimo. Después de seleccionar la opción privada, el comentario solo será visible para el administrador del sitio.</target>
164+
</trans-unit>
157165
</body>
158166
</file>
159167
</xliff>

packages/comment-widget/xliff/zh-CN.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@
154154
<source>Private</source>
155155
<target>私密</target>
156156
</trans-unit>
157+
<trans-unit id="s838e512973be01d4">
158+
<source>Currently logged in. After selecting the private option, comments will only be visible to yourself and the site administrator.</source>
159+
<target>当前已登录,选择私密选项后,评论将仅对您和网站管理员可见。</target>
160+
</trans-unit>
161+
<trans-unit id="s5184a3f3e2f7b603">
162+
<source>You are currently anonymous. After selecting the private option, the comment will only be visible to the site administrator.</source>
163+
<target>当前是匿名状态,选择私密选项后,评论将仅对网站管理员可见。</target>
164+
</trans-unit>
157165
</body>
158166
</file>
159167
</xliff>

packages/comment-widget/xliff/zh-TW.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@
154154
<source>Private</source>
155155
<target>私密</target>
156156
</trans-unit>
157+
<trans-unit id="s838e512973be01d4">
158+
<source>Currently logged in. After selecting the private option, comments will only be visible to yourself and the site administrator.</source>
159+
<target>目前已登入,選擇私密選項後,評論將僅對您和網站管理員可見。</target>
160+
</trans-unit>
161+
<trans-unit id="s5184a3f3e2f7b603">
162+
<source>You are currently anonymous. After selecting the private option, the comment will only be visible to the site administrator.</source>
163+
<target>目前是匿名狀態,選擇私密選項後,評論將僅對網站管理員可見。</target>
164+
</trans-unit>
157165
</body>
158166
</file>
159167
</xliff>

0 commit comments

Comments
 (0)