Tauri
蓋在 Vite 建置之上的網頁視圖。Vite 的準備工作,以及一個不需要框架的主題開關。
Tauri 應用就是原生視窗裡的一個 Vite 應用,所以網頁的準備工作就是全部的準備工作:別名、副檔名順序和根,都是 Vite 那一頁上的。
建立專案
pnpm create tauri-app@latest my-app --template react-ts
cd my-app
pnpm add @hanzo/ui @hanzo/gui react-native-web設定 Vite
vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [react()],
clearScreen: false,
server: { port: 1420, strictPort: true },
resolve: {
alias: {
'react-native': 'react-native-web',
'@react-native/assets-registry/registry': 'react-native-web/dist/modules/AssetRegistry',
},
extensions: ['.web.js', '.web.jsx', '.web.ts', '.web.tsx', '.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
},
})server.port 和 clearScreen 是 Tauri 自己給開發視窗定的慣例;resolve 那一塊是函式庫的。
掛上根
src/main.tsx
import { createRoot } from 'react-dom/client'
import { Hanzo } from '@hanzo/ui'
import { App } from './App'
createRoot(document.getElementById('root')!).render(
<Hanzo>
<App />
</Hanzo>,
)不靠框架的主題開關
來自 @hanzo/ui/product 的 ThemeToggle 不依賴任何框架。受控時,它畫出按鈕並回報下一個主題;應用自己保存這個值,再交給根。
import { useState } from 'react'
import { Hanzo } from '@hanzo/ui'
import { ThemeToggle, type ThemeMode } from '@hanzo/ui/product'
export function App() {
const [theme, setTheme] = useState<ThemeMode>('dark')
return (
<Hanzo theme={theme}>
<ThemeToggle theme={theme} onToggle={setTheme} />
</Hanzo>
)
}同一個開關的 Next 繫結住在它自己的子路徑 @hanzo/ui/product/theme-toggle-next,正是為了讓 Tauri、Vite 或 Express 宿主在匯入產品層時,不會把 Next 拖進相依圖。