
# プラグイン

TailwindPHPには公式TailwindCSSプラグインのPHP移植版が含まれ、ネイティブなPHPプラグインAPIを公開しています。プラグインはユーティリティ、コンポーネント、バリアントを登録します。入力CSS内で`@plugin`ディレクティブを使って読み込みます。組み込みプラグインは、公式版のJavaScriptにあたる1:1移植版で、同じセレクターと出力を提供し、Nodeを介して実行する代わりにPHPで実装されています。

## `@plugin`の使用

`@plugin`でプラグイン名を指定します。組み込みプラグインには、それぞれのnpmパッケージ名を使います。ディレクティブと`@import "tailwindcss/utilities.css";`を組み合わせると、プラグインのクラスがutilitiesレイヤーに出力されます。

```php
use TailwindPHP\tw;

$css = tw::generate([
    'content' => '<article class="prose prose-lg"><h1>Hello</h1><p>Content here</p></article>',
    'css' => '
        @plugin "@tailwindcss/typography";
        @import "tailwindcss/utilities.css";
    ',
]);
```

複数のプラグインをまとめて読み込めます。順序は問いません。

```css
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
@import "tailwindcss/utilities.css";
```

`@plugin`を`@media`などの別のat-ruleの中にネストすることはできません。また、未登録のプラグイン名を指定するとエラーになります。

## プラグインのオプション

プラグイン名の後にCSSブロック構文を使うと、オプションを渡せます。キーはプラグインのオプション配列の要素に対応します。

```css
@plugin "@tailwindcss/forms" {
    strategy: "class";
}

@plugin "@tailwindcss/typography" {
    className: "markdown";
}
```

`@tailwindcss/forms`プラグインは`strategy`（`base`または`class`）を受け取ります。`@tailwindcss/typography`では`className`を指定して、生成される`prose`クラス名を変更できます。すべてのオプションは各プラグインのページを参照してください。

## 組み込みプラグイン

| プラグイン | 説明 |
|--------|-------------|
| `@tailwindcss/typography` | `prose`クラス。リッチテキスト向けのタイポグラフィ既定スタイルです。[Typography](https://tailwindphp.inline0.dev/ja/docs/plugins/typography/)を参照してください。 |
| `@tailwindcss/forms` | リセット可能でスタイリングしやすいフォームコントロールです。[Forms](https://tailwindphp.inline0.dev/ja/docs/plugins/forms/)を参照してください。 |

## カスタムプラグイン

`PluginInterface`を実装し、`PluginAPI`のメソッド（`addUtilities`、`matchUtilities`、`addComponents`、`addVariant`、`theme`など）を使って独自のプラグインを作成します。`registerPlugin(new MyPlugin())`で登録すると、`getName()`が返す名前を`@plugin "my-plugin"`ディレクティブで解決できます。

```php
use function TailwindPHP\registerPlugin;

registerPlugin(new MyCustomPlugin());
```

インターフェース、完全な例、`PluginAPI`メソッドの全リファレンスについては、[カスタムプラグイン](https://tailwindphp.inline0.dev/ja/docs/plugins/custom/)を参照してください。
