React
React 19+ 原生支援自訂元素。React 18 及更早版本僅傳遞字串屬性 — 請使用以下類型化輔助模式以確保跨版本安全。
@wink/elements npm package 安裝並載入帶有 TypeScript 型別的 CDN 套件。
npm install @wink/elements在應用程式根目錄載入一次
Section titled “在應用程式根目錄載入一次”在頂層元件中呼叫 load()。此函式為冪等操作 — 可安全多次呼叫。
import { useEffect } from 'react';import { load } from '@wink/elements';
export default function App() { useEffect(() => { load({ clientId: import.meta.env.VITE_WINK_CLIENT_ID }); }, []);
return <YourRoutes />;}在 JSX 中使用元件
Section titled “在 JSX 中使用元件”export default function HotelPage() { return ( <main> <wink-content-loader layout="HOTEL" id="YOUR_LAYOUT_ID" /> <wink-lookup /> </main> );}TypeScript — 宣告 JSX 內建元素
Section titled “TypeScript — 宣告 JSX 內建元素”React 預設不認識自訂元素名稱。新增宣告檔以消除型別錯誤:
import type { WinkContentLoaderAttributes, WinkLookupAttributes, WinkSearchButtonAttributes, WinkAccountButtonAttributes, WinkItineraryButtonAttributes, WinkShoppingCartButtonAttributes, WinkAppLoaderAttributes,} from '@wink/elements';
declare module 'react' { namespace JSX { interface IntrinsicElements { 'wink-content-loader': WinkContentLoaderAttributes & React.HTMLAttributes<HTMLElement>; 'wink-lookup': WinkLookupAttributes & React.HTMLAttributes<HTMLElement>; 'wink-search-button': WinkSearchButtonAttributes & React.HTMLAttributes<HTMLElement>; 'wink-account-button': WinkAccountButtonAttributes & React.HTMLAttributes<HTMLElement>; 'wink-itinerary-button': WinkItineraryButtonAttributes & React.HTMLAttributes<HTMLElement>; 'wink-shopping-cart-button': WinkShoppingCartButtonAttributes & React.HTMLAttributes<HTMLElement>; 'wink-app-loader': WinkAppLoaderAttributes & React.HTMLAttributes<HTMLElement>; } }}傳遞類型化屬性(React 18 安全)
Section titled “傳遞類型化屬性(React 18 安全)”import type { WinkContentLoaderAttributes } from '@wink/elements';
const attrs: WinkContentLoaderAttributes = { layout: 'HOTEL', id: 'abc123', sort: 'POPULARITY',};
// React 19 可使用展開語法;React 18 所有值本來就是字串export default function HotelCard() { return <wink-content-loader layout={attrs.layout} id={attrs.id} sort={attrs.sort} />;}