-
Notifications
You must be signed in to change notification settings - Fork 72
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49f0197
feat(helper): add Message class to handle message
Mister-Hope b0d0c54
feat: add clickToClose
Mister-Hope 1cf9d9c
chore: tweaks
Mister-Hope 8dea7cf
chore: tweaks
Mister-Hope c5979a3
feat: update style
Mister-Hope 005d83f
feat: update style
Mister-Hope 13f85fd
chore: tweaks
Mister-Hope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
@keyframes message-move-in { | ||
0% { | ||
opacity: 0; | ||
transform: translateY(-100%); | ||
} | ||
|
||
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: calc(var(--message-offset, 3.6rem) + 1rem) 0 auto; | ||
z-index: 75; | ||
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 0.3s ease-in-out; | ||
} | ||
|
||
&.move-out { | ||
animation: message-move-out 0.3s ease-in-out; | ||
animation-fill-mode: forwards; | ||
} | ||
|
||
svg { | ||
position: relative; | ||
bottom: -0.125em; | ||
margin-inline-end: 5px; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { keys } from '../../shared/index.js' | ||
|
||
const containerId = 'message-container' | ||
|
||
export class Message { | ||
private elements: Record<number, HTMLDivElement> = {} | ||
|
||
private constructor() {} | ||
|
||
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 pop(html: string, duration = 2000): number { | ||
const messageElement = document.createElement('div') | ||
const messageId = Date.now() | ||
|
||
messageElement.className = 'message-item move-in' | ||
messageElement.innerHTML = html | ||
Message.containerElement.appendChild(messageElement) | ||
this.elements[messageId] = messageElement | ||
|
||
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 = {} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Date.now() to generate a messageId may lead to collisions if multiple messages are created in rapid succession. Consider using a more robust unique ID generator.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pengzhanbo I think the AI is right about this, any suggestions with it?