自動拡張テキストエリア
stimeo--textarea-autosize
内容に合わせて textarea の高さを拡張し、minRows〜maxRows でクランプする。
stimeo--textarea-autosize コントローラは内容に合わせて textarea の高さを拡張します。connect と各 input で要素を一旦つぶし(height:auto)、scrollHeight を読み、minRows〜maxRows(行高単位)でクランプした高さを px で設定します。maxRows に達すると内部スクロールへ切り替え data-at-max-rows を付与し、おおよその行数を --stimeo-textarea-rows カスタムプロパティで公開します。高さ変更のみでフォーカス/キャレットを保持します。ライブラリは挙動のみで、高さは要素自身のインラインスタイルへ反映し(CSS クラスは使わない)、connect で Turbo 遷移後に再計測、input リスナは disconnect で解除します。高さが変わると stimeo--textarea-autosize:resize を発火します。
<%# Textarea autosize demo: type and the field grows to fit, up to maxRows (then it
scrolls). The controller sets the element's inline height from scrollHeight and
toggles data-at-max-rows at the cap; it listens for input itself, so no consumer
JS is needed. This demo only sets the textarea's base look. %>
<div class="textarea-autosize-demo">
<label class="textarea-autosize-demo__label" for="ta-autosize">
<%= t("components.textarea_autosize.demo.label") %>
</label>
<textarea
id="ta-autosize"
class="textarea-autosize-demo__field"
rows="2"
placeholder="<%= t('components.textarea_autosize.demo.placeholder') %>"
data-controller="stimeo--textarea-autosize"
data-stimeo--textarea-autosize-max-rows-value="8"></textarea>
</div>
/*
* Presentation-only styles for the textarea autosize demo.
* The library owns the textarea's height (set inline from scrollHeight) and toggles
* data-at-max-rows; this CSS only provides the base look and the line metrics the
* controller measures against. resize:none because the library manages height.
*/
.textarea-autosize-demo {
display: flex;
flex-direction: column;
gap: 0.5rem;
max-width: 32rem;
}
.textarea-autosize-demo__label {
font-weight: 600;
color: var(--fg);
}
.textarea-autosize-demo__field {
width: 100%;
padding: 0.5rem;
border: 1px solid var(--border);
border-radius: 0.375rem;
font: inherit;
line-height: 1.5;
resize: none;
overflow: hidden;
}
このデモに固有の消費側 JS はありません(挙動はコントローラが担います)。
これらのスタイルは共通のデザイントークン(ライト/ダーク両対応)を使います。 共通スタイルも一緒にコピーし、ルート要素の data-theme を切り替えればダークになります。
このコンポーネントを動かすために HTML へ記述する data-* 属性です。ルート要素に下の data-controller を付け、その内側に各 target / value / action を配置します。
ルート要素に付与
data-controller="stimeo--textarea-autosize"
値(Values)
| 名前 | 説明 | 属性 |
|---|---|---|
minRows
|
最小行数(既定 1)。 | data-stimeo--textarea-autosize-min-rows-value |
maxRows
|
最大行数。0 は無制限(既定 0)。 | data-stimeo--textarea-autosize-max-rows-value |
アクション
| 名前 | アクション |
|---|---|
resize
|
stimeo--textarea-autosize#resize |
イベント
| 名前 | 説明 | イベント |
|---|---|---|
resize
|
高さが変化したときに発火。detail.height / detail.rows を伴う。 |
stimeo--textarea-autosize:resize |
状態フック
ライブラリが操作するのはこれらの ARIA / data 属性、カスタムプロパティだけです。見た目は利用側 CSS がこれらに反応して作ります([aria-selected] / [aria-expanded] / var(--stimeo--…) などのセレクタでフックします)。
| フック | 対象 | 意味 |
|---|---|---|
data-at-max-rows |
テキストエリア | 内容が maxRows に達すると付与(以降はスクロール)。 |
--stimeo-textarea-rows |
テキストエリア | 現在のおおよその行数。 |