コンテンツにスキップ
Page

Input OTP

スロットに分かれたワンタイムコードの入力です。

Code: —
import { InputOTP, InputOTPSlot } from "@pelagornis/page/components";

上のプレビューはこのファイルを描画したものです。src/components/ にコピーして使い始めてください。

src/components/InputOtpExample.tsx
import { useState } from "react";
import { InputOTP, InputOTPSlot, Stack, Text } from "@pelagornis/page/components";
export default function InputOTPDemo() {
const [code, setCode] = useState("");
return (
<Stack gap="sizeSmall" align="center">
<InputOTP maxLength={6} value={code} onValueChange={setCode} pattern={/\d/} aria-label="Verification code">
{Array.from({ length: 6 }, (_, index) => (
<InputOTPSlot key={index} index={index} />
))}
</InputOTP>
<Text variant="captionMd">Code: {code || "—"}</Text>
</Stack>
);
}

Input OTP は React の状態を共有する複数のパーツで構成されます。MDX に書いた子要素はそれぞれ別のアイランドとして描画されるため、パーツ同士がつながりません。上の例のように .tsx ファイルで組み立て、そのファイルをインポートしてください。

src/content/docs/example.mdx
---
title: Example
---
import InputOtpExample from "../../components/InputOtpExample";
<InputOtpExample client:load />

client:load はページの読み込み時にアイランドをハイドレートします。ページの下の方にあるコンテンツには client:visible を使ってください。

Markdoc のタグは Astro コンポーネントを描画しますが、このコンポーネントにはクライアントの状態が必要です。例をハイドレートする .astro ファイルで包み、そのファイルをタグとして登録してください。

src/components/markdoc/InputOtpExample.astro
---
import InputOtpExample from "../InputOtpExample";
---
<InputOtpExample client:load />
markdoc.config.mjs
import { component, defineMarkdocConfig } from "@astrojs/markdoc/config";
import starlightMarkdoc from "@astrojs/starlight-markdoc";
import pageMarkdoc from "@pelagornis/page/markdoc";
export default defineMarkdocConfig({
extends: [starlightMarkdoc(), pageMarkdoc()],
tags: {
"input-otp-example": {
render: component("./src/components/markdoc/InputOtpExample.astro"),
selfClosing: true,
},
},
});
src/content/docs/example.mdoc
{% input-otp-example /%}

各 variant prop が受け付ける値です。

Prop値
sizesm md lg

コンポーネントを構成するパーツと、それぞれが描画する data-refineui のロールです。

パーツロール
rootinput-otp
slotinput-otp-slot

フォーカスがあるときにコンポーネントが処理するキーです。

キー動作
ArrowLeftprevious-trigger
ArrowRightnext-trigger