React
React 19+ はカスタムエレメントをネイティブにサポートしています。React 18 以前は文字列属性のみを渡すため、以下の型付きヘルパーパターンを使うことでバージョン間の安全性を保てます。
@wink/elements npm package TypeScript 型定義付きの CDN バンドルをインストールして読み込みます。
インストール
Section titled “インストール”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} />;}