diff --git a/.changeset/add-chart-tokens-and-charts-package.md b/.changeset/add-chart-tokens-and-charts-package.md new file mode 100644 index 00000000..3968c192 --- /dev/null +++ b/.changeset/add-chart-tokens-and-charts-package.md @@ -0,0 +1,5 @@ +--- +"@tiny-design/tokens": patch +--- + +Add chart color tokens (chart-1 through chart-5) for light and dark themes diff --git a/.changeset/config.json b/.changeset/config.json index 3714b850..20fb4565 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -6,7 +6,8 @@ [ "@tiny-design/react", "@tiny-design/icons", - "@tiny-design/tokens" + "@tiny-design/tokens", + "@tiny-design/charts" ] ], "linked": [], diff --git a/apps/docs/package.json b/apps/docs/package.json index 44f04ab0..d8fbb9d0 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -10,6 +10,7 @@ "dependencies": { "@mdx-js/react": "^3.1.1", "@stackblitz/sdk": "^1.11.0", + "@tiny-design/charts": "workspace:*", "@tiny-design/icons": "workspace:*", "@tiny-design/react": "workspace:*", "@tiny-design/tokens": "workspace:*", @@ -17,7 +18,8 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.0.0", - "react-runner": "^1.0.5" + "react-runner": "^1.0.5", + "recharts": "^2.15.0" }, "devDependencies": { "@mdx-js/rollup": "^3.1.1", diff --git a/apps/docs/src/components/sidebar-menu/index.tsx b/apps/docs/src/components/sidebar-menu/index.tsx index cb54b489..f9c5b9aa 100755 --- a/apps/docs/src/components/sidebar-menu/index.tsx +++ b/apps/docs/src/components/sidebar-menu/index.tsx @@ -46,6 +46,7 @@ export const SidebarMenu = (props: Props): React.ReactElement => { isActive ? 'sidebar-menu__menu-item_active' : '' }> {item.title} + {item.tag && item.tag} ))} @@ -62,6 +63,7 @@ export const SidebarMenu = (props: Props): React.ReactElement => { isActive ? 'sidebar-menu__menu-item_active' : '' }> {router.title} + {router.tag && router.tag} ); diff --git a/apps/docs/src/components/sidebar-menu/sidebar-menu.scss b/apps/docs/src/components/sidebar-menu/sidebar-menu.scss index 14ec3064..f6351c7d 100755 --- a/apps/docs/src/components/sidebar-menu/sidebar-menu.scss +++ b/apps/docs/src/components/sidebar-menu/sidebar-menu.scss @@ -34,7 +34,6 @@ } &__menu-item { - line-height: 40px; font-size: 14px; position: relative; padding-left: 50px; @@ -43,6 +42,11 @@ text-decoration: none; color: var(--ty-color-text); transition: all 300ms; + display: flex; + align-items: center; + justify-content: space-between; + height: 40px; + padding-right: 16px; &:hover { color: var(--ty-color-primary); @@ -96,6 +100,11 @@ padding: 0; } + &__tag { + font-size: 10px; + text-transform: uppercase; + } + &__backdrop { display: none; } diff --git a/apps/docs/src/index.tsx b/apps/docs/src/index.tsx index 891fd4a6..5e2a1d97 100755 --- a/apps/docs/src/index.tsx +++ b/apps/docs/src/index.tsx @@ -3,6 +3,7 @@ import { createRoot } from 'react-dom/client'; import { BrowserRouter, Route, Routes } from 'react-router-dom'; import { MDXProvider } from '@mdx-js/react'; import '@tiny-design/react/style/index.scss'; +import '@tiny-design/charts/style/index.scss'; import './index.scss'; import './utils/theme-persistence'; diff --git a/apps/docs/src/routers.tsx b/apps/docs/src/routers.tsx index 14cac092..2cf16675 100755 --- a/apps/docs/src/routers.tsx +++ b/apps/docs/src/routers.tsx @@ -1,4 +1,5 @@ -import { lazy } from 'react'; +import React, { lazy } from 'react'; +import { Tag } from '@tiny-design/react'; import { SiteLocale } from './locale/types'; export interface RouterItem { @@ -6,6 +7,7 @@ export interface RouterItem { route?: string; component?: any; children?: RouterItem[]; + tag?: React.ReactNode; } type LocalizedLazy = { en: any; zh: any }; @@ -147,6 +149,7 @@ const c = { inputOTP: ll(() => import('../../../packages/react/src/input-otp/index.md'), () => import('../../../packages/react/src/input-otp/index.zh_CN.md')), overlay: ll(() => import('../../../packages/react/src/overlay/index.md'), () => import('../../../packages/react/src/overlay/index.zh_CN.md')), waterfall: ll(() => import('../../../packages/react/src/waterfall/index.md'), () => import('../../../packages/react/src/waterfall/index.zh_CN.md')), + chart: ll(() => import('../../../packages/charts/src/index.md'), () => import('../../../packages/charts/src/index.zh_CN.md')), }; export const getGuideMenu = (s: SiteLocale): RouterItem[] => { @@ -168,8 +171,8 @@ export const getGuideMenu = (s: SiteLocale): RouterItem[] => { { title: s.guideMenu.groups.ai, children: [ - { title: s.guideMenu.mcpServer, route: 'mcp-server', component: pick(guide.mcpServer, isZh) }, - { title: s.guideMenu.cli, route: 'cli', component: pick(guide.cli, isZh) }, + { title: s.guideMenu.mcpServer, route: 'mcp-server', component: pick(guide.mcpServer, isZh), tag: New }, + { title: s.guideMenu.cli, route: 'cli', component: pick(guide.cli, isZh), tag: New }, ], }, { @@ -254,6 +257,7 @@ export const getComponentMenu = (s: SiteLocale): RouterItem[] => { { title: 'Timeline', route: 'timeline', component: pick(c.timeline, z) }, { title: 'Tooltip', route: 'tooltip', component: pick(c.tooltip, z) }, { title: 'Tree', route: 'tree', component: pick(c.tree, z) }, + { title: 'Chart', route: 'chart', component: pick(c.chart, z), tag: New }, ], }, { diff --git a/apps/docs/vite.config.ts b/apps/docs/vite.config.ts index 0dbc1646..de6734d8 100644 --- a/apps/docs/vite.config.ts +++ b/apps/docs/vite.config.ts @@ -9,6 +9,7 @@ import { readFileSync } from 'fs'; const reactPkg = path.resolve(__dirname, '../../packages/react/package.json'); const reactSrc = path.resolve(__dirname, '../../packages/react/src'); const iconsSrc = path.resolve(__dirname, '../../packages/icons/src'); +const chartsSrc = path.resolve(__dirname, '../../packages/charts/src'); const tinyVersion = JSON.parse(readFileSync(reactPkg, 'utf-8')).version; @@ -31,6 +32,7 @@ export default defineConfig({ alias: { '@tiny-design/react': reactSrc, '@tiny-design/icons': iconsSrc, + '@tiny-design/charts': chartsSrc, }, dedupe: ['react', 'react-dom'], }, diff --git a/packages/charts/package.json b/packages/charts/package.json new file mode 100644 index 00000000..8332f92b --- /dev/null +++ b/packages/charts/package.json @@ -0,0 +1,58 @@ +{ + "name": "@tiny-design/charts", + "version": "1.6.1", + "description": "Theme-aware chart components for Tiny Design, built on Recharts", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/wangdicoder/tiny-design.git", + "directory": "packages/charts" + }, + "publishConfig": { + "access": "public" + }, + "main": "lib/index.js", + "module": "es/index.js", + "typings": "lib/index.d.ts", + "exports": { + ".": { + "types": "./lib/index.d.ts", + "import": "./es/index.js", + "require": "./lib/index.js" + }, + "./lib/*": "./lib/*", + "./es/*": "./es/*" + }, + "sideEffects": [ + "**/*.css", + "**/*.scss" + ], + "files": [ + "lib", + "es" + ], + "scripts": { + "build": "npm run clean && tsdown", + "clean": "rimraf lib es" + }, + "peerDependencies": { + "react": ">=18.0.0", + "react-dom": ">=18.0.0", + "recharts": ">=2.0.0" + }, + "dependencies": { + "@tiny-design/tokens": "workspace:*", + "classnames": "^2.3.1", + "tslib": "^2.3.1" + }, + "devDependencies": { + "@types/react": "^18.2.0", + "@types/react-dom": "^18.2.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "recharts": "^2.15.0", + "rimraf": "^3.0.2", + "tsdown": "^0.21.1", + "typescript": "^5.4.0" + } +} diff --git a/packages/charts/src/chart-container.tsx b/packages/charts/src/chart-container.tsx new file mode 100644 index 00000000..05dd038e --- /dev/null +++ b/packages/charts/src/chart-container.tsx @@ -0,0 +1,58 @@ +import React, { useId, useMemo } from 'react'; +import classNames from 'classnames'; +import { ResponsiveContainer } from 'recharts'; +import { ChartContextProvider } from './chart-context'; +import { ChartStyle } from './chart-style'; +import { ChartConfig } from './types'; + +const PREFIX = 'ty-chart'; + +export interface ChartContainerProps extends React.HTMLAttributes { + config: ChartConfig; + children: React.ReactElement; +} + +/** + * ChartContainer wraps Recharts charts with: + * 1. A ResponsiveContainer for auto-sizing + * 2. CSS custom properties (--color-KEY) injected via inline styles + * 3. ChartConfig provided via React context for tooltip/legend + */ +const ChartContainer = React.forwardRef( + ({ config, children, className, style, id, ...props }, ref) => { + const uniqueId = useId(); + const chartId = id || `chart-${uniqueId}`; + + // Build --color-KEY CSS custom properties from config + const colorVars = useMemo(() => { + const vars: Record = {}; + for (const [key, value] of Object.entries(config)) { + if (value.color) { + vars[`--color-${key}`] = value.color; + } + } + return vars; + }, [config]); + + return ( + + +
+ + {children} + +
+
+ ); + } +); + +ChartContainer.displayName = 'ChartContainer'; + +export default ChartContainer; diff --git a/packages/charts/src/chart-context.tsx b/packages/charts/src/chart-context.tsx new file mode 100644 index 00000000..b0d493f7 --- /dev/null +++ b/packages/charts/src/chart-context.tsx @@ -0,0 +1,28 @@ +import React, { createContext, useContext } from 'react'; +import { ChartConfig } from './types'; + +interface ChartContextValue { + config: ChartConfig; +} + +const ChartContext = createContext(null); + +export function useChart(): ChartContextValue { + const context = useContext(ChartContext); + if (!context) { + throw new Error('useChart must be used within a '); + } + return context; +} + +export function ChartContextProvider({ + config, + children, +}: { + config: ChartConfig; + children: React.ReactNode; +}) { + return ( + {children} + ); +} diff --git a/packages/charts/src/chart-legend.tsx b/packages/charts/src/chart-legend.tsx new file mode 100644 index 00000000..9cfb0046 --- /dev/null +++ b/packages/charts/src/chart-legend.tsx @@ -0,0 +1,89 @@ +import React from 'react'; +import classNames from 'classnames'; +import { Legend as RechartsLegend } from 'recharts'; +import { useChart } from './chart-context'; + +const PREFIX = 'ty-chart-legend'; + +export const ChartLegend = RechartsLegend; + +export interface ChartLegendContentProps + extends React.HTMLAttributes { + payload?: Array<{ + value?: string; + dataKey?: string; + color?: string; + type?: string; + }>; + nameKey?: string; + hideIcon?: boolean; + verticalAlign?: 'top' | 'bottom'; +} + +export const ChartLegendContent = React.forwardRef< + HTMLDivElement, + ChartLegendContentProps +>( + ( + { + payload, + nameKey, + hideIcon = false, + verticalAlign = 'bottom', + className, + ...props + }, + ref + ) => { + const { config } = useChart(); + + if (!payload?.length) { + return null; + } + + return ( +
+ {payload.map((entry) => { + const key = nameKey + ? (entry.dataKey || entry.value) + : entry.dataKey || entry.value; + const itemConfig = key ? config[key] : undefined; + const displayName = itemConfig?.label || entry.value || key; + const color = + entry.color || (itemConfig?.color ? `var(--color-${key})` : undefined); + + const IconComponent = itemConfig?.icon; + + return ( +
+ {!hideIcon && ( + IconComponent ? ( + + ) : ( + + ) + )} + {displayName} +
+ ); + })} +
+ ); + } +); + +ChartLegendContent.displayName = 'ChartLegendContent'; diff --git a/packages/charts/src/chart-style.tsx b/packages/charts/src/chart-style.tsx new file mode 100644 index 00000000..a9fcdaeb --- /dev/null +++ b/packages/charts/src/chart-style.tsx @@ -0,0 +1,48 @@ +import React, { useMemo } from 'react'; +import { ChartConfig } from './types'; + +const THEMES = { light: '', dark: 'html[data-tiny-theme=dark]' } as const; + +/** + * Generates an inline