Skip to content

Commit

Permalink
feat: improve snackbar messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rickstaa committed Jun 2, 2023
1 parent a1f70f0 commit e3f6360
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 25 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,45 @@ const App = () => {
* Handles selection of the emoji.
*
* @param selectedEmoji Selected emoji.
*
* @description Copies the unicode or shortcode to the clipboard. Depends on the
* copyUnicode state and whether the shift key is pressed.
*/
const handleEmojiSelect = (selectedEmoji: Emoji, event: PointerEvent) => {
// Copy emoji shortcode or unicode to clipboard.
// NOTE: Depends on whether the shift key is pressed and the copyUnicode state.
let copyText;
if (event.shiftKey) {
copyText = copyUnicode
? selectedEmoji.shortcodes
: unifiedToUnicodeEmoji(selectedEmoji.unified);
: unifiedToUnicodeEmoji(selectedEmoji?.unified);
} else {
copyText = copyUnicode
? unifiedToUnicodeEmoji(selectedEmoji.unified)
? unifiedToUnicodeEmoji(selectedEmoji?.unified)
: selectedEmoji.shortcodes;
}
navigator.clipboard.writeText(copyText);

// Display snackbar.
// Create snackbar message.
let snackbarMessage: string;
if (copyUnicode) {
snackbarMessage = `Emoji ${
event.shiftKey ? "shortcode" : "unicode"
} copied to clipboard. Hold shift for ${
event.shiftKey ? "unicode" : "shortcode"
}.`;
if (copyText === "") {
copyText = selectedEmoji.shortcodes;
snackbarMessage =
"Emoji 'shortcode' copied to clipboard since 'unicode' is not available.";
} else {
snackbarMessage = `Emoji ${
event.shiftKey ? "unicode" : "shortcode"
} copied to clipboard. Hold shift for ${
event.shiftKey ? "shortcode" : "unicode"
}.`;
const copyTypeStrings = copyUnicode
? ["unicode", "shortcode"]
: ["shortcode", "unicode"];
if (event.shiftKey) {
snackbarMessage = `Emoji '${copyTypeStrings[1]}' copied to clipboard.`;
} else {
snackbarMessage = `Emoji '${copyTypeStrings[0]}' copied to clipboard. ${
selectedEmoji.unified
? "Hold shift for '" + copyTypeStrings[1] + "."
: ""
}`;
}
}

// Copy to clipboard and display snackbar.
navigator.clipboard.writeText(copyText);
setSnackPack((prev) => [
...prev,
{
Expand Down
4 changes: 3 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
/**
* Convert unified to unicode emoji.
* @param unified - the unified string.
* @returns the unicode emoji.
* @returns the unicode emoji. Empty if the unified string could not be parsed.
*/
const unifiedToUnicodeEmoji = (unified: string) => {
if (!unified) return "";

return String.fromCodePoint(
...unified.split("-").map((str: string) => parseInt(str, 16))
);
Expand Down

1 comment on commit e3f6360

@vercel
Copy link

@vercel vercel bot commented on e3f6360 Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.