-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathEmojiRandomizer.tsx
More file actions
68 lines (55 loc) · 1.73 KB
/
Copy pathEmojiRandomizer.tsx
File metadata and controls
68 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use client';
import React, { type FC, useEffect, useState } from 'react';
import type { SbBlokData } from '@storyblok/react';
const useIsClient = () => {
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
return isClient;
};
interface EmojiRandomizerProps {
blok: SbBlokData & {
label?: string;
};
}
/**
* A component that displays a label and a random emoji that changes on click
*/
const EmojiRandomizer: FC<EmojiRandomizerProps> = ({ blok }) => {
console.log('EmojiRandomizer', blok);
// List of fun emojis to randomly choose from
const emojis = ['😊', '🎉', '🚀', '✨', '🌈', '🎨', '🎸', '🎮', '🍕', '🌺'];
// State to track current emoji
const [currentEmoji, setCurrentEmoji] = useState(() =>
emojis[Math.floor(Math.random() * emojis.length)],
);
const isClient = useIsClient();
if (!isClient) {
return null;
}
/**
* Generates a new random emoji different from the current one
*/
const randomizeEmoji = () => {
let newEmoji;
do {
newEmoji = emojis[Math.floor(Math.random() * emojis.length)];
} while (newEmoji === currentEmoji);
setCurrentEmoji(newEmoji);
};
return (
<div className="flex flex-col items-center gap-6 p-4 bg-gray-100 dark:bg-gray-900 rounded-lg">
<div className="text-6xl">
{currentEmoji}
</div>
<button
onClick={randomizeEmoji}
className="px-6 py-3 rounded-lg bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white font-small transition-colors duration-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:active:bg-blue-800"
>
{blok.label || 'Randomize Emoji'}
</button>
</div>
);
};
export default EmojiRandomizer;