アスペクト比
stimeo--aspect-ratio
決めた縦横比を CSS へ渡します。箱の比率を保つのは利用側のスタイルです。
決めた縦横比を CSS から使える形で渡すだけの、小さなレイアウト補助です。読み上げに影響する役割も状態も持ちません。`16/9` のような書き方と、ただの数値のどちらでも受け取ります。読み取れない値のときは 1 対 1 に落とします。値を書き換えれば、その場で反映し直します。実際に箱をその比率にするのは利用側のスタイルなので、画像の切り取り方まで含めて自由に決められます。
実行中
16 / 9(ワイド)
1 / 1(正方形)
3 / 4(縦長)
16 / 9
1 / 1
3 / 4
キーボード操作
このコンポーネント自体はキーボード操作を持ちません。
<%# Markup for the aspect-ratio demo.
The library only supplies the ratio Value as the --stimeo--aspect-ratio custom
property; demo.css does the actual ratio rendering
(aspect-ratio: var(--stimeo--aspect-ratio)). %>
<div class="aspect-ratio-demo">
<figure class="aspect-ratio-demo__item">
<div class="ratio-box" data-controller="stimeo--aspect-ratio"
data-stimeo--aspect-ratio-ratio-value="16/9">
<div class="ratio-box__content">16 / 9</div>
</div>
<figcaption><%= t("components.aspect_ratio.demo.wide") %></figcaption>
</figure>
<figure class="aspect-ratio-demo__item">
<div class="ratio-box" data-controller="stimeo--aspect-ratio"
data-stimeo--aspect-ratio-ratio-value="1/1">
<div class="ratio-box__content">1 / 1</div>
</div>
<figcaption><%= t("components.aspect_ratio.demo.square") %></figcaption>
</figure>
<figure class="aspect-ratio-demo__item">
<div class="ratio-box" data-controller="stimeo--aspect-ratio"
data-stimeo--aspect-ratio-ratio-value="3/4">
<div class="ratio-box__content">3 / 4</div>
</div>
<figcaption><%= t("components.aspect_ratio.demo.portrait") %></figcaption>
</figure>
</div>
/* Presentation CSS for aspect-ratio. The library only supplies
--stimeo--aspect-ratio, so this CSS reads it with var() and draws the ratio. */
.aspect-ratio-demo {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
align-items: flex-start;
}
.aspect-ratio-demo__item {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-muted);
display: flex;
flex-direction: column;
gap: 0.5rem;
}
/* Feed the ratio the library supplies straight into CSS aspect-ratio. */
.ratio-box {
width: 200px;
aspect-ratio: var(--stimeo--aspect-ratio);
border: 2px dashed var(--border-interactive);
border-radius: 8px;
background: repeating-linear-gradient(
45deg,
var(--surface-subtle),
var(--surface-subtle) 10px,
var(--surface-subtle) 10px,
var(--surface-subtle) 20px
);
}
.ratio-box__content {
width: 100%;
height: 100%;
display: grid;
place-items: center;
font-weight: 600;
color: var(--color-text);
}
このデモに固有の消費側 JS はありません(挙動はコントローラが担います)。
これらのスタイルは共通のデザイントークン(ライト/ダーク両対応)を使います。共通スタイルも一緒にコピーし、ルート要素の data-theme を切り替えればダークになります。
このコンポーネントを動かすために HTML へ記述する data-* 属性です。ルート要素に下の data-controller を付け、その内側に各 target / value / action を配置します。
ルート要素に付与
data-controller="stimeo--aspect-ratio"
値(Values)
| 名前 | 説明 | 属性 |
|---|---|---|
ratio
|
適用するCSSの比率(16/9や数値文字列)。既定は1/1で、解釈不能な値はこれにフォールバックする。 |
data-stimeo--aspect-ratio-ratio-value |
状態フック
ライブラリが操作するのはこれらの ARIA / data 属性、カスタムプロパティだけです。見た目は利用側 CSS がこれらに反応して作ります([aria-selected] / [aria-expanded] / var(--stimeo--…) などのセレクタでフックします)。
| フック | 対象 | 意味 |
|---|---|---|
--stimeo--aspect-ratio |
ホスト要素 | 比率(例: 16 / 9)。利用側 CSS が参照する。 |