Add component: Flip Card

#26
Files changed (2) hide show
  1. components/flip-card.json +25 -0
  2. manifest.json +14 -0
components/flip-card.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id": "flip-card",
3
+ "name": "Flip Card",
4
+ "description": "Card matching game with emoji pairs and 3D card flips as a gradio HTML custom component.",
5
+ "author": "tejasashinde",
6
+ "tags": [
7
+ "game",
8
+ "flip cards",
9
+ "memory cards",
10
+ "html component",
11
+ "interactive"
12
+ ],
13
+ "html_template": "\n<div class=\"memory-container\" data-game-id=\"${game_id || '0'}\">\n\n <div class=\"memory-header\">\n <div class=\"memory-title\">\ud83c\udfb4 Flip Card Match</div>\n <div class=\"memory-subtitle\">Find all matching pairs</div>\n </div>\n\n <div class=\"memory-stats\">\n <div class=\"stat-pill\">\n <span class=\"stat-label\">Moves</span>\n <strong id=\"moves-count\">0</strong>\n </div>\n\n <div class=\"stat-pill\">\n <span class=\"stat-label\">Matches</span>\n <strong id=\"matches-count\">0</strong>\n </div>\n </div>\n\n <div\n class=\"memory-grid\"\n id=\"memory-grid\"\n style=\"--grid-cols: ${grid_cols || 2};\"\n >\n ${(() => {\n let cards = [];\n\n try {\n cards = JSON.parse(cards_json || '[]');\n } catch (error) {\n cards = [];\n }\n\n let html = '';\n\n cards.forEach((card, i) => {\n html += `\n <div class=\"memory-card\" data-index=\"${i}\" tabindex=\"0\" role=\"button\" aria-label=\"Memory card\">\n <div class=\"memory-inner\">\n <div class=\"memory-front\">?</div>\n <div class=\"memory-back\">${card}</div>\n </div>\n </div>\n `;\n });\n\n return html;\n })()}\n </div>\n\n <div class=\"game-status ${value ? 'show' : ''}\" id=\"game-status-area\">\n <div class=\"status-text\">\n ${value || 'Find all matching pairs!'}\n </div>\n </div>\n\n <button class=\"reset-button\" id=\"reset-btn\" type=\"button\">\n \ud83d\udd04 RESET GAME\n </button>\n\n</div>\n",
14
+ "css_template": "\n.memory-container {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n display: flex;\n flex-direction: column;\n align-items: center;\n padding: 32px;\n background:\n radial-gradient(circle at top left, rgba(129, 140, 248, 0.28), transparent 35%),\n radial-gradient(circle at bottom right, rgba(244, 114, 182, 0.22), transparent 35%),\n linear-gradient(135deg, #1e293b 0%, #0f172a 100%);\n border-radius: 28px;\n position: relative;\n overflow: hidden;\n min-height: 560px;\n box-sizing: border-box;\n}\n\n.memory-container::before {\n content: \"\";\n position: absolute;\n inset: 0;\n background-image:\n radial-gradient(circle, rgba(255,255,255,0.13) 1px, transparent 1px);\n background-size: 28px 28px;\n opacity: 0.22;\n pointer-events: none;\n}\n\n.memory-header,\n.memory-stats,\n.memory-grid,\n.game-status,\n.reset-button {\n position: relative;\n z-index: 1;\n}\n\n.memory-header {\n text-align: center;\n margin-bottom: 18px;\n}\n\n.memory-title {\n font-size: 30px;\n font-weight: 900;\n color: #ffffff;\n letter-spacing: -0.04em;\n}\n\n.memory-subtitle {\n margin-top: 4px;\n font-size: 15px;\n color: rgba(255,255,255,0.7);\n font-weight: 600;\n}\n\n.memory-stats {\n display: flex;\n gap: 12px;\n margin-bottom: 22px;\n}\n\n.stat-pill {\n min-width: 96px;\n padding: 10px 14px;\n border-radius: 999px;\n background: rgba(255,255,255,0.1);\n border: 1px solid rgba(255,255,255,0.13);\n color: white;\n display: flex;\n align-items: center;\n justify-content: space-between;\n gap: 12px;\n box-shadow: 0 8px 22px rgba(0,0,0,0.18);\n}\n\n.stat-label {\n font-size: 13px;\n color: rgba(255,255,255,0.72);\n font-weight: 700;\n}\n\n.stat-pill strong {\n font-size: 17px;\n color: #fef3c7;\n}\n\n.memory-grid {\n display: grid;\n grid-template-columns: repeat(var(--grid-cols, 2), 130px);\n gap: 16px;\n justify-content: center;\n max-width: 100%;\n}\n\n.memory-card {\n width: 130px;\n height: 130px;\n perspective: 1000px;\n cursor: pointer;\n position: relative;\n outline: none;\n}\n\n.memory-card:focus-visible .memory-inner {\n box-shadow: 0 0 0 4px rgba(250, 204, 21, 0.65);\n border-radius: 22px;\n}\n\n.memory-inner {\n position: relative;\n width: 100%;\n height: 100%;\n transition: transform 0.65s cubic-bezier(0.2, 0.8, 0.2, 1);\n transform-style: preserve-3d;\n will-change: transform;\n}\n\n.memory-card.flipped .memory-inner {\n transform: rotateY(180deg);\n}\n\n.memory-front,\n.memory-back {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n border-radius: 22px;\n display: flex;\n align-items: center;\n justify-content: center;\n font-size: 48px;\n line-height: 1;\n box-shadow: 0 14px 32px rgba(0,0,0,0.35);\n backface-visibility: hidden;\n -webkit-backface-visibility: hidden;\n transform-style: preserve-3d;\n -webkit-transform-style: preserve-3d;\n user-select: none;\n}\n\n.memory-front {\n background:\n radial-gradient(circle at 30% 20%, rgba(255,255,255,0.34), transparent 26%),\n linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);\n color: white;\n transform: rotateY(0deg);\n border: 2px solid rgba(255,255,255,0.18);\n}\n\n.memory-back {\n background:\n radial-gradient(circle at 30% 20%, rgba(255,255,255,0.92), transparent 30%),\n linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);\n color: #111827;\n transform: rotateY(180deg);\n border: 2px solid rgba(15, 23, 42, 0.08);\n}\n\n.memory-card:hover:not(.flipped):not(.matched):not(.game-won) .memory-inner {\n transform: translateY(-4px) scale(1.025);\n}\n\n.memory-card.matched {\n pointer-events: none;\n}\n\n.memory-card.matched .memory-inner {\n animation: matchedPulse 0.9s ease-in-out infinite;\n}\n\n.memory-card.matched .memory-back {\n background:\n radial-gradient(circle at 30% 20%, rgba(255,255,255,0.82), transparent 30%),\n linear-gradient(135deg, #bbf7d0 0%, #86efac 100%);\n border-color: rgba(34,197,94,0.55);\n}\n\n.memory-card.wrong .memory-inner {\n animation: wrongShake 0.38s ease-in-out;\n}\n\n.memory-container.game-complete .memory-card {\n pointer-events: none;\n}\n\n.memory-container.game-complete .memory-card.matched .memory-inner {\n animation: finalWinPulse 1.2s ease-in-out infinite;\n}\n\n@keyframes matchedPulse {\n 0% {\n transform: rotateY(180deg) scale(1);\n }\n 50% {\n transform: rotateY(180deg) scale(1.045);\n }\n 100% {\n transform: rotateY(180deg) scale(1);\n }\n}\n\n@keyframes finalWinPulse {\n 0% {\n transform: rotateY(180deg) scale(1);\n }\n 50% {\n transform: rotateY(180deg) scale(1.035);\n }\n 100% {\n transform: rotateY(180deg) scale(1);\n }\n}\n\n@keyframes wrongShake {\n 0% {\n transform: rotateY(180deg) translateX(0);\n }\n 25% {\n transform: rotateY(180deg) translateX(-8px);\n }\n 50% {\n transform: rotateY(180deg) translateX(8px);\n }\n 75% {\n transform: rotateY(180deg) translateX(-5px);\n }\n 100% {\n transform: rotateY(180deg) translateX(0);\n }\n}\n\n.game-status {\n margin-top: 28px;\n padding: 16px 28px;\n background: rgba(255,255,255,0.08);\n border-radius: 16px;\n border: 1px solid rgba(255,255,255,0.1);\n color: white;\n transition: all 0.4s ease;\n min-width: 280px;\n}\n\n.game-status.show {\n background: rgba(34,197,94,0.15);\n border-color: rgba(34,197,94,0.35);\n box-shadow: 0 14px 36px rgba(34,197,94,0.12);\n}\n\n.status-text {\n font-size: 20px;\n font-weight: 800;\n text-align: center;\n}\n\n.reset-button {\n margin-top: 24px;\n padding: 16px 44px;\n font-size: 18px;\n font-weight: 800;\n color: white;\n background: linear-gradient(135deg, #ef4444 0%, #f97316 100%);\n border: none;\n border-radius: 999px;\n cursor: pointer;\n transition: all 0.3s ease;\n box-shadow: 0 10px 30px rgba(239,68,68,0.35);\n letter-spacing: 0.04em;\n}\n\n.reset-button:hover {\n transform: translateY(-2px);\n box-shadow: 0 14px 36px rgba(239,68,68,0.45);\n}\n\n.reset-button:active {\n transform: translateY(0);\n}\n\n@media (max-width: 720px) {\n .memory-container {\n padding: 24px 14px;\n min-height: 500px;\n }\n\n .memory-title {\n font-size: 24px;\n }\n\n .memory-grid {\n grid-template-columns: repeat(min(var(--grid-cols, 2), 3), 105px);\n gap: 12px;\n }\n\n .memory-card {\n width: 105px;\n height: 105px;\n }\n\n .memory-front,\n .memory-back {\n font-size: 38px;\n border-radius: 18px;\n }\n\n .status-text {\n font-size: 16px;\n }\n\n .game-status {\n min-width: 240px;\n padding: 14px 18px;\n }\n\n .reset-button {\n padding: 13px 30px;\n font-size: 15px;\n }\n}\n\n@media (max-width: 420px) {\n .memory-grid {\n grid-template-columns: repeat(2, 105px);\n }\n}\n",
15
+ "js_on_load": "\nlet flipped = [];\nlet matched = [];\nlet moves = 0;\nlet isLocked = false;\nlet isComplete = false;\nlet cards = [];\nlet activeCardsJson = null;\nlet activeGameId = null;\nlet winEmitTimer = null;\n\nconst FLIP_ANIMATION_MS = 700;\n\nfunction getRoot() {\n return element.querySelector('.memory-container');\n}\n\nfunction getGrid() {\n return element.querySelector('#memory-grid');\n}\n\nfunction getStatusEl() {\n return element.querySelector('#game-status-area');\n}\n\nfunction getMovesCountEl() {\n return element.querySelector('#moves-count');\n}\n\nfunction getMatchesCountEl() {\n return element.querySelector('#matches-count');\n}\n\nfunction readCardsFromProps() {\n try {\n return JSON.parse(props.cards_json || '[]');\n } catch (error) {\n return [];\n }\n}\n\nfunction updateStats() {\n const movesCountEl = getMovesCountEl();\n const matchesCountEl = getMatchesCountEl();\n\n if (movesCountEl) {\n movesCountEl.textContent = String(moves);\n }\n\n if (matchesCountEl) {\n matchesCountEl.textContent = String(Math.floor(matched.length / 2));\n }\n}\n\nfunction updateStatus(message, show) {\n const statusEl = getStatusEl();\n\n if (!statusEl) return;\n\n statusEl.innerHTML = '<div class=\"status-text\">' + message + '</div>';\n\n if (show) {\n statusEl.classList.add('show');\n } else {\n statusEl.classList.remove('show');\n }\n}\n\nfunction getCard(index) {\n const grid = getGrid();\n if (!grid) return null;\n\n return grid.querySelector('[data-index=\"' + index + '\"]');\n}\n\nfunction clearCardState(card) {\n if (!card) return;\n\n card.classList.remove('flipped');\n card.classList.remove('matched');\n card.classList.remove('wrong');\n}\n\nfunction clearAllCards() {\n const grid = getGrid();\n if (!grid) return;\n\n const allCards = grid.querySelectorAll('.memory-card');\n\n allCards.forEach(function(card) {\n clearCardState(card);\n });\n}\n\nfunction resetLocalState() {\n if (winEmitTimer) {\n clearTimeout(winEmitTimer);\n winEmitTimer = null;\n }\n\n flipped = [];\n matched = [];\n moves = 0;\n isLocked = false;\n isComplete = false;\n\n const root = getRoot();\n if (root) {\n root.classList.remove('game-complete');\n }\n\n clearAllCards();\n updateStats();\n updateStatus('Find all matching pairs!', false);\n}\n\nfunction syncFromPropsIfNeeded() {\n const nextCardsJson = props.cards_json || '[]';\n const nextGameId = props.game_id || '0';\n\n if (activeCardsJson !== nextCardsJson || activeGameId !== nextGameId) {\n activeCardsJson = nextCardsJson;\n activeGameId = nextGameId;\n cards = readCardsFromProps();\n resetLocalState();\n }\n}\n\nfunction resetGame() {\n syncFromPropsIfNeeded();\n resetLocalState();\n}\n\nfunction flipBack(cardA, cardB) {\n setTimeout(function() {\n if (cardA) {\n cardA.classList.remove('flipped');\n cardA.classList.remove('wrong');\n }\n\n if (cardB) {\n cardB.classList.remove('flipped');\n cardB.classList.remove('wrong');\n }\n\n flipped = [];\n isLocked = false;\n }, 900);\n}\n\nfunction emitWinAfterFinalFlip(winMessage) {\n if (winEmitTimer) {\n clearTimeout(winEmitTimer);\n }\n\n winEmitTimer = setTimeout(function() {\n props.value = winMessage;\n winEmitTimer = null;\n }, FLIP_ANIMATION_MS + 120);\n}\n\nfunction handleMatch(firstIndex, secondIndex, firstCard, secondCard) {\n matched.push(firstIndex, secondIndex);\n flipped = [];\n\n if (firstCard) {\n firstCard.classList.add('matched');\n }\n\n if (secondCard) {\n secondCard.classList.add('matched');\n }\n\n updateStats();\n\n if (matched.length === cards.length) {\n isComplete = true;\n isLocked = true;\n\n const root = getRoot();\n if (root) {\n root.classList.add('game-complete');\n }\n\n const winMessage = '\ud83c\udf89 You won in ' + moves + ' moves!';\n updateStatus(winMessage, true);\n\n emitWinAfterFinalFlip(winMessage);\n return;\n }\n\n updateStatus('\u2705 Match Found!', true);\n isLocked = false;\n}\n\nfunction handleNoMatch(firstCard, secondCard) {\n updateStatus('\u274c No Match!', true);\n\n if (firstCard) {\n firstCard.classList.add('wrong');\n }\n\n if (secondCard) {\n secondCard.classList.add('wrong');\n }\n\n flipBack(firstCard, secondCard);\n}\n\nfunction handleCardClick(card) {\n syncFromPropsIfNeeded();\n\n if (!card) return;\n if (isLocked) return;\n if (isComplete) return;\n\n const index = Number(card.dataset.index);\n\n if (!Number.isInteger(index)) return;\n if (matched.includes(index)) return;\n if (flipped.includes(index)) return;\n\n card.classList.add('flipped');\n flipped.push(index);\n\n if (flipped.length === 1) {\n updateStatus('Keep going!', true);\n return;\n }\n\n isLocked = true;\n moves += 1;\n updateStats();\n\n const firstIndex = flipped[0];\n const secondIndex = flipped[1];\n\n const firstCard = getCard(firstIndex);\n const secondCard = getCard(secondIndex);\n\n const firstValue = cards[firstIndex];\n const secondValue = cards[secondIndex];\n\n if (firstValue === secondValue) {\n handleMatch(firstIndex, secondIndex, firstCard, secondCard);\n } else {\n handleNoMatch(firstCard, secondCard);\n }\n}\n\nelement.addEventListener('click', function(event) {\n const resetClicked = event.target.closest('#reset-btn');\n\n if (resetClicked && element.contains(resetClicked)) {\n event.preventDefault();\n resetGame();\n return;\n }\n\n const card = event.target.closest('.memory-card');\n\n if (card && element.contains(card)) {\n handleCardClick(card);\n }\n});\n\nelement.addEventListener('keydown', function(event) {\n if (event.key !== 'Enter' && event.key !== ' ') return;\n\n const card = event.target.closest('.memory-card');\n\n if (card && element.contains(card)) {\n event.preventDefault();\n handleCardClick(card);\n }\n});\n\nsyncFromPropsIfNeeded();\n",
16
+ "default_props": {
17
+ "value": "",
18
+ "cards_json": "[\"\ud83c\udf4d\", \"\ud83c\udf4e\", \"\ud83c\udf4d\", \"\ud83c\udf4e\"]",
19
+ "grid_cols": 2,
20
+ "game_id": "1781591595278513145"
21
+ },
22
+ "python_code": "class FlipCard(gr.HTML):\n def __init__(\n self,\n value: str | None = None,\n emojis: list[str] | None = None,\n cards: list[str] | None = None,\n cards_json: str | None = None,\n grid_cols: int | None = None,\n game_id: str | None = None,\n **kwargs: Any,\n ) -> None:\n if emojis is None:\n emojis = DEFAULT_EMOJIS\n\n if cards is None:\n cards = create_cards(emojis)\n\n if cards_json is None:\n cards_json = json.dumps(cards, ensure_ascii=False)\n\n if grid_cols is None:\n grid_cols = compute_grid_cols(len(cards))\n\n if game_id is None:\n game_id = new_game_id()\n\n super().__init__(\n value=value,\n cards_json=cards_json,\n grid_cols=grid_cols,\n game_id=game_id,\n html_template=HTML_TEMPLATE,\n css_template=CSS_TEMPLATE,\n js_on_load=JS_ON_LOAD,\n apply_default_css=False,\n container=False,\n padding=False,\n **kwargs,\n )\n\n def api_info(self) -> dict[str, str]:\n return {\n \"type\": \"string\",\n \"description\": \"Winning message after completing the flip-card memory game.\",\n }\n",
23
+ "head": "",
24
+ "repo_url": "https://github.com/tejasashinde/gradio-flip-card-component"
25
+ }
manifest.json CHANGED
@@ -307,5 +307,19 @@
307
  "Controlnet"
308
  ],
309
  "repo_url": "https://huggingface.co/spaces/elismasilva/gradio_openpose3d_editor"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  }
311
  ]
 
307
  "Controlnet"
308
  ],
309
  "repo_url": "https://huggingface.co/spaces/elismasilva/gradio_openpose3d_editor"
310
+ },
311
+ {
312
+ "id": "flip-card",
313
+ "name": "Flip Card",
314
+ "description": "Card matching game with emoji pairs and 3D card flips as a gradio HTML custom component.",
315
+ "author": "tejasashinde",
316
+ "tags": [
317
+ "game",
318
+ "flip cards",
319
+ "memory cards",
320
+ "html component",
321
+ "interactive"
322
+ ],
323
+ "repo_url": "https://github.com/tejasashinde/gradio-flip-card-component"
324
  }
325
  ]