Skip to content

Interactive dashboard

This example creates a dashboard with chart-like cards, interactive buttons, and a reset action. It follows the structure used by the Mosyn demo.

Card.tsx
import { useState } from 'react';
type CardProps = {
label: string;
tone: string;
};
export function Card({ label, tone }: CardProps) {
const [count, setCount] = useState(0);
return (
<section className={`card card-${tone}`}>
<strong>{label}</strong>
<button className="no-drag" type="button" onClick={() => setCount((value) => value + 1)}>
Clicked {count} times
</button>
</section>
);
}

The button has no-drag because it is an application control, not a handle for moving the tile.

tiles.tsx
import type { Tile } from 'mosyn';
import { Card } from './Card';
export function createTiles(): Tile[] {
return [
{ id: 'alpha', x: 1, y: 1, w: 38, h: 18, renderer: <Card label="Alpha" tone="blue" /> },
{ id: 'beta', x: 41, y: 1, w: 38, h: 18, renderer: <Card label="Beta" tone="green" /> },
{ id: 'gamma', x: 81, y: 1, w: 38, h: 18, renderer: <Card label="Gamma" tone="amber" /> },
{ id: 'delta', x: 1, y: 21, w: 38, h: 24, renderer: <Card label="Delta" tone="cyan" /> },
];
}
Dashboard.tsx
import { useMemo, useState } from 'react';
import { Mosyn, MosynManager } from 'mosyn';
import { createTiles } from './tiles';
export function Dashboard() {
const [version, setVersion] = useState(0);
const tiles = useMemo(createTiles, [version]);
return (
<main className="dashboard">
<header className="dashboard-header">
<h1>Dashboard</h1>
<button type="button" onClick={() => setVersion((value) => value + 1)}>
Reset layout
</button>
</header>
<section className="dashboard-grid">
<MosynManager key={version}>
<Mosyn tiles={tiles} DragCancel={['button', '.no-drag']} />
</MosynManager>
</section>
</main>
);
}

Resetting the provider deliberately creates a new live layout from the original tile definitions.

dashboard.css
.dashboard {
display: flex;
min-height: 100vh;
flex-direction: column;
}
.dashboard-grid {
display: flex;
flex: 1;
min-height: 0;
}
.card {
display: flex;
width: 100%;
height: 100%;
padding: 1rem;
flex-direction: column;
justify-content: space-between;
}