Skip to content

feat(helper): add Message class to handle message #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export default vuepress(
],
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-function': [
'error',
{ allow: ['protected-constructors'] },
],
'@typescript-eslint/no-floating-promises': [
'error',
{
Expand Down
1 change: 1 addition & 0 deletions tools/helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"./noopComponent": "./lib/client/noopComponent.js",
"./noopModule": "./lib/client/noopModule.js",
"./colors.css": "./lib/client/styles/colors.css",
"./message.css": "./lib/client/styles/message.css",
"./normalize.css": "./lib/client/styles/normalize.css",
"./sr-only.css": "./lib/client/styles/sr-only.css",
"./transition/*.css": "./lib/client/styles/transition/*.css",
Expand Down
73 changes: 73 additions & 0 deletions tools/helper/src/client/styles/message.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
:root {
--message-offset: calc(var(--vp-header-offset, 3.6rem) + 1rem);
--message-timing-duration: 0.3s;
--message-timing-function: ease-in-out;
--message-gap: 0.5rem;
}

@keyframes message-move-in {
0% {
opacity: 0;
transform: translateY(-10px);
}

100% {
opacity: 1;
transform: translateY(0);
}
}

@keyframes message-move-out {
0% {
opacity: 1;
transform: translateY(0);
}

100% {
opacity: 0;
transform: translateY(-100%);
}
}

#message-container {
position: fixed;
inset: var(--message-offset) 0 auto;
z-index: 75;

display: flex;
flex-flow: column;
gap: var(--message-gap);
align-items: center;

text-align: center;
}

.message-item {
display: inline-block;

padding: 8px 10px;
border-radius: 3px;

background: var(--vp-c-bg);
color: var(--vp-c-text);
box-shadow: 0 0 10px 0 var(--vp-c-shadow);

font-size: 14px;

&.move-in {
animation: message-move-in var(--message-timing-duration)
var(--message-timing-function);
}

&.move-out {
animation: message-move-out var(--message-timing-duration)
var(--message-timing-function);
animation-fill-mode: forwards;
}

svg {
position: relative;
bottom: -0.125em;
margin-inline-end: 5px;
}
}
1 change: 1 addition & 0 deletions tools/helper/src/client/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './getHeaders.js'
export * from './isFocusingTextControl.js'
export * from './isKeyMatched.js'
export * from './hasGlobalComponent.js'
export * from './message.js'
export * from './wait.js'
71 changes: 71 additions & 0 deletions tools/helper/src/client/utils/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { keys } from '../../shared/index.js'

const containerId = 'message-container'

export class Message {
private elements: Record<number, HTMLDivElement>

public constructor() {
this.elements = {}
}

public static get containerElement(): HTMLElement {
let containerElement = document.getElementById(containerId)

if (containerElement) return containerElement

containerElement = document.createElement('div')
containerElement.id = containerId
document.body.appendChild(containerElement)

return containerElement
}

public getElement(messageId: number): HTMLDivElement {
return this.elements[messageId]
}

public pop(html: string, duration = 2000, clickToClose = true): number {
const messageId = Date.now()
const messageElement = document.createElement('div')
messageElement.className = 'message-item move-in'
messageElement.innerHTML = html
Message.containerElement.appendChild(messageElement)
this.elements[messageId] = messageElement

if (clickToClose)
messageElement.addEventListener('click', () => {
this.close(messageId)
})

if (duration > 0)
setTimeout(() => {
this.close(messageId)
}, duration)

return messageId
}

public close(messageId?: number): void {
if (messageId) {
const messageElement = this.elements[messageId]

messageElement.classList.remove('move-in')
messageElement.classList.add('move-out')
messageElement.addEventListener('animationend', () => {
messageElement.remove()
delete this.elements[messageId]
})
} else {
keys(this.elements).forEach((id) => {
this.close(Number(id))
})
}
}

public destroy(): void {
const containerElement = document.getElementById(containerId)
if (containerElement) document.body.removeChild(containerElement)
this.elements = {}
}
}
Loading