コンテンツにスキップ

Collapsible

1 つの領域を開閉する UI。ARIA 属性の同期、無効状態、ページ内検索、JavaScript からの操作に対応します。

<pe-collapsible class="Collapsible">
<button type="button" class="Trigger" data-collapsible-trigger>
  Project details
</button>
<div class="Panel" data-collapsible-panel>
  <p class="Content">Plain Elements provides accessible, unstyled UI primitives.</p>
</div>
</pe-collapsible>

Collapsible は単独の開閉領域、Accordion は関連項目のグループに使います。トリガーにはネイティブボタンを使い、閉じた内容をページ内検索の対象にする場合は data-collapsible-hidden-until-found を指定します。

パーツ セレクター 役割
ホスト <pe-collapsible> ライフサイクルと開閉状態を管理します
トリガー [data-collapsible-trigger] パネルを開閉します。aria-expandedaria-controls が設定されます
パネル [data-collapsible-panel] 必要に応じて ID が生成され、表示状態とサイズ用の CSS 変数が設定されます

ホスト、トリガー、パネルには data-state="open|closed" が設定されます。無効な要素には data-disabled も付きます。

名前説明
data-collapsible-open現在の開閉状態を反映するブール型ホスト属性。外部から変更することもできます。
data-collapsible-disabledホストまたはトリガーに指定すると、フォーカス可能なまま利用者による開閉操作を無効にします。
data-collapsible-hidden-until-foundホストまたはパネルに指定すると、閉じている間は hidden="until-found" を使い、beforematch イベントで開きます。
名前説明
isOpenboolean (readonly)現在の開閉状態を読み取ります。open()、close()、toggle() で状態を設定します。
disabledbooleanルートの無効状態を取得または設定します。
open(trigger?) / close(trigger?) / toggle(trigger?)voidJavaScript から状態を変更します。操作元のトリガーを渡すこともできます。

CSS 変数とアニメーションフック

Section titled “CSS 変数とアニメーションフック”

開閉アニメーション中は、パネルの高さと幅を --collapsible-panel-height--collapsible-panel-width で参照できます。開くアニメーションが終わると両方とも auto に戻るため、その後に内容が増えても自然に広がります。表示開始時には data-starting-style、非表示開始時には data-ending-style が付きます。時間が有限のアニメーションについては、その終了を待ってから hidden を適用します。

.Collapsible.Animated .Panel {
height: var(--collapsible-panel-height);
overflow: hidden;
transition: height 150ms ease-out;
}
.Collapsible.Animated .Panel[hidden]:not([hidden="until-found"]) {
display: none;
}
.Collapsible.Animated .Panel[data-starting-style],
.Collapsible.Animated .Panel[data-ending-style] {
height: 0;
}

時間が有限の非表示アニメーション終了後にパネルを隠します。beforematch ではすぐにスクロールできるよう表示アニメーションを省略します。

名前説明
pe-collapsible:openパネルが開いたときに発火します。イベントはバブリングし、Shadow DOM の境界も越えます。
pe-collapsible:closeパネルが閉じたときに発火します。イベントはバブリングし、Shadow DOM の境界も越えます。

イベントの detail には、openpanel、任意の triggerreason が含まれます。reasontriggerprogrammaticattributebeforematch のいずれかです。