Initial screen starter scaffold

This commit is contained in:
zhougf committed 2026-05-29 00:26:43 +08:00
commit d136b9dddc
49 files changed
+2966

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
{
"name": "@dev-screen-starter/screen-core",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"build": "tsc --noEmit",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"react": "^19.1.1"
},
"devDependencies": {
"@types/react": "^19.1.16",
"typescript": "^5.9.2"
}
}
+18
View File
@@ -0,0 +1,18 @@
type MetricCardProps = {
label: string
value: string | number
unit?: string
tone?: 'blue' | 'green' | 'amber'
}
export function MetricCard({ label, value, unit, tone = 'blue' }: MetricCardProps) {
return (
<div className={`metric-card tone-${tone}`}>
<span>{label}</span>
<strong>
{value}
{unit ? <small>{unit}</small> : null}
</strong>
</div>
)
}
+16
View File
@@ -0,0 +1,16 @@
import type { ReactNode } from 'react'
type PanelProps = {
title?: string
className?: string
children: ReactNode
}
export function Panel({ title, className = '', children }: PanelProps) {
return (
<section className={`screen-panel ${className}`}>
{title ? <header>{title}</header> : null}
<div className="screen-panel-body">{children}</div>
</section>
)
}
+37
View File
@@ -0,0 +1,37 @@
import { type CSSProperties, type ReactNode, useEffect, useMemo, useState } from 'react'
import { calculateStageScale, type ScaleMode } from './scale'
type ScreenStageProps = {
width?: number
height?: number
scaleMode?: ScaleMode
background?: string
children: ReactNode
}
export function ScreenStage({ width = 1920, height = 1080, scaleMode = 'fit', background = '#061826', children }: ScreenStageProps) {
const [viewport, setViewport] = useState({ width: window.innerWidth, height: window.innerHeight })
useEffect(() => {
const onResize = () => setViewport({ width: window.innerWidth, height: window.innerHeight })
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [])
const scale = useMemo(() => calculateStageScale({ width, height, scaleMode }, viewport.width, viewport.height), [height, scaleMode, viewport.height, viewport.width, width])
const stageStyle: CSSProperties = {
width,
height,
background,
transform: `translate(-50%, -50%) scale(${scale.scaleX}, ${scale.scaleY})`
}
return (
<main className="screen-viewport">
<section className="screen-stage" style={stageStyle}>
{children}
</section>
</main>
)
}
+4
View File
@@ -0,0 +1,4 @@
export * from './scale'
export * from './ScreenStage'
export * from './Panel'
export * from './MetricCard'
+30
View File
@@ -0,0 +1,30 @@
export type ScaleMode = 'fit' | 'fill' | 'stretch' | 'none'
export type ScreenSize = {
width: number
height: number
scaleMode?: ScaleMode
}
export type StageScale = {
scaleX: number
scaleY: number
}
export function calculateStageScale(screen: ScreenSize, viewportWidth: number, viewportHeight: number): StageScale {
const scaleMode = screen.scaleMode ?? 'fit'
if (scaleMode === 'none') {
return { scaleX: 1, scaleY: 1 }
}
const scaleX = viewportWidth / screen.width
const scaleY = viewportHeight / screen.height
if (scaleMode === 'stretch') {
return { scaleX, scaleY }
}
const scale = scaleMode === 'fill' ? Math.max(scaleX, scaleY) : Math.min(scaleX, scaleY)
return { scaleX: scale, scaleY: scale }
}
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx"
},
"include": ["src"]
}