import { Card, Text, Switch, Group, useMantineTheme, useComputedColorScheme } from '@mantine/core'; import { Rnd } from 'react-rnd'; import { useState } from 'react'; const GRID_SIZE = 30; function hexToRgba(hex: string, alpha: number): string { const sanitized = hex.replace('#', ''); const bigint = parseInt(sanitized, 16); const r = (bigint >> 16) & 255; const g = (bigint >> 8) & 255; const b = bigint & 255; return `rgba(${r}, ${g}, ${b}, ${alpha})`; } interface CardData { id: string; x: number; y: number; width: number; height: number; } const initialCards: CardData[] = [ { id: '1', x: 0, y: 0, width: GRID_SIZE * 2, height: GRID_SIZE * 2 }, { id: '2', x: GRID_SIZE * 2, y: 0, width: GRID_SIZE * 3, height: GRID_SIZE * 2 }, { id: '3', x: GRID_SIZE * 3, y: 0, width: GRID_SIZE * 3, height: GRID_SIZE * 2 }, ]; export const GridDashboard = () => { const theme = useMantineTheme(); const colorScheme = useComputedColorScheme(); // ✅ directamente 'light' o 'dark' const isDark = colorScheme === 'dark'; // Color de la rejilla adaptado al modo del tema const gridBaseColor = isDark ? theme.colors.dark[4] : theme.colors.gray[3]; const gridColor = hexToRgba(gridBaseColor, 0.25); // Ajusta la opacidad aquí const [cards, setCards] = useState(initialCards); const [showGrid, setShowGrid] = useState(true); const updateCard = (id: string, updates: Partial) => { setCards((prev) => prev.map((card) => (card.id === id ? { ...card, ...updates } : card)) ); }; return ( <> setShowGrid(event.currentTarget.checked)} label="Mostrar cuadrícula" />
{cards.map((card) => ( updateCard(card.id, { x: Math.round(d.x / GRID_SIZE) * GRID_SIZE, y: Math.round(d.y / GRID_SIZE) * GRID_SIZE, }) } onResizeStop={(_, __, ref, ___, pos) => updateCard(card.id, { width: Math.round(ref.offsetWidth / GRID_SIZE) * GRID_SIZE, height: Math.round(ref.offsetHeight / GRID_SIZE) * GRID_SIZE, x: Math.round(pos.x / GRID_SIZE) * GRID_SIZE, y: Math.round(pos.y / GRID_SIZE) * GRID_SIZE, }) } > Card {card.id} Mueve o redimensiona ))}
); };