Anchor
リンクを作成するアンカーコンポーネント。
インストール
import { Anchor } from "@ginga-ui/core";基本的な使い方
シンプルなリンクの使用例です。
anchor.tsx
import { Anchor } from "@ginga-ui/core";
export function BasicExample() {
return <Anchor href="https://example.com">リンクをクリック</Anchor>;
}外部リンク
外部サイトへのリンクを新しいタブで開きます。
anchor.tsx
import { Anchor } from "@ginga-ui/core";
export function ExternalExample() {
return (
<Anchor href="https://github.com/newt239/ginga-ui" target="_blank">
GitHubで開く(新しいタブ)
</Anchor>
);
}無効化
disabledプロップでリンクを無効化できます。
通常のリンク無効化されたリンク
anchor.tsx
anchor.module.css
import { Anchor } from "@ginga-ui/core";
import styles from "./anchor.module.css";
export function DisabledExample() {
return (
<div className={styles.linkStack}>
<Anchor href="https://example.com">通常のリンク</Anchor>
<Anchor href="https://example.com" disabled>
無効化されたリンク
</Anchor>
</div>
);
}ボタンスタイル
variant="button"でボタンのような見た目のリンクになります。buttonVariantとsizeでButtonと同じバリアントとサイズを指定できます。
anchor.tsx
anchor.module.css
import { Anchor } from "@ginga-ui/core";
import styles from "./anchor.module.css";
export function ButtonVariantExample() {
return (
<div className={styles.buttonRow}>
<Anchor href="https://example.com" variant="button">
Filled
</Anchor>
<Anchor href="https://example.com" variant="button" buttonVariant="light">
Light
</Anchor>
<Anchor
href="https://example.com"
variant="button"
buttonVariant="outline"
>
Outline
</Anchor>
<Anchor href="https://example.com" variant="button" buttonVariant="ghost">
Ghost
</Anchor>
</div>
);
}ルーターのリンク
render に関数を渡すと、next/link などの別のコンポーネントでリンクを描画できます。受け取った props は ref も含めて a 要素に渡してください。
anchor.tsx
import { Anchor } from "@ginga-ui/core";
import NextLink from "next/link";
export function RouterLinkExample() {
return (
<Anchor href="/ja/components" render={(props) => <NextLink {...props} />}>
コンポーネント一覧へ
</Anchor>
);
}Props
Anchor
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
variant | "default" | "button" | "default" | リンクの見た目。"button" を指定すると Button と同じ見た目になります。 |
buttonVariant | "filled" | "light" | "outline" | "ghost" | "reverse" | "filled" | variant が "button" のときのボタンのバリアント。 |
size | "sm" | "md" | "lg" | "md" | variant が "button" のときのボタンのサイズ。 |
disabled | boolean | false | リンクを無効にします。 |
startIcon | ReactNode | - | variant が "button" のときにラベルの前に表示するアイコン。 |
endIcon | ReactNode | - | variant が "button" のときにラベルの後に表示するアイコン。 |
render | (props, renderProps) => ReactElement | - | ルート要素を next/link などに差し替える関数。href があるときだけ使われます。 |
上記に加えて、React Aria の Link(新しいタブで開く) の props を受け取ります。