@wcstack/resize 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +189 -0
- package/README.md +189 -0
- package/dist/auto.js +3 -0
- package/dist/auto.min.js +1 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.esm.js +592 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/package.json +71 -0
package/README.ja.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# @wcstack/resize
|
|
2
|
+
|
|
3
|
+
`@wcstack/resize` は wcstack エコシステム向けのヘッドレスな ResizeObserver コンポーネントです。
|
|
4
|
+
|
|
5
|
+
これは視覚的な UI ウィジェットではありません。
|
|
6
|
+
要素の*サイズ*をリアクティブな状態に変換する**非同期プリミティブノード**です。`@wcstack/fetch` がネットワークリクエストを、`@wcstack/intersection` が要素の可視状態をリアクティブな状態に変換するのと同じ位置づけです。
|
|
7
|
+
|
|
8
|
+
`@wcstack/state` と組み合わせると、`<wcs-resize>` はパス契約を通じて直接バインドできます。
|
|
9
|
+
|
|
10
|
+
- **入力 / コマンド面**: `target`、`box`、`round`、`once`、`manual`、`trigger`
|
|
11
|
+
- **出力状態面**: `entry`、`width`、`height`、`observing`
|
|
12
|
+
|
|
13
|
+
これにより、サイズ依存のロジック(canvas の再描画、仮想リスト、画像解像度の選択、幅しきい値でのレイアウトモード切替など)を、UI 層で `new ResizeObserver()`・`observe()`・`disconnect()`・後始末を書かずに、HTML 上で宣言的に表現できます。
|
|
14
|
+
|
|
15
|
+
`@wcstack/resize` は [CSBC](https://github.com/csbc-dev/arch/blob/main/README.md)(Core / Shell / Binding Contract)アーキテクチャに従います。
|
|
16
|
+
|
|
17
|
+
- **Core**(`ResizeCore`)はオブザーバ、entry の正規化、box 追従のサイズ導出、監視ライフサイクルを所有
|
|
18
|
+
- **Shell**(`<wcs-resize>`)は*何を*監視するかを DOM から解決し、display・ライフサイクル・宣言的コマンドを管理
|
|
19
|
+
- **Binding Contract**(`static wcBindable`)は監視可能な `properties`、書き込み可能な `inputs`、呼び出し可能な `commands` を宣言
|
|
20
|
+
|
|
21
|
+
## いつ使うか(そして使わないか)
|
|
22
|
+
|
|
23
|
+
コンテナのサイズに依存した**スタイリング**には、CSS の [`@container` クエリ](https://developer.mozilla.org/ja/docs/Web/CSS/CSS_containment/Container_queries)を使ってください。それがまさにそのために存在し、JavaScript を経由しません。
|
|
24
|
+
|
|
25
|
+
`@wcstack/resize` は CSS では表現できないケース、すなわち**サイズ依存のロジック**のためのものです。要素のピクセルサイズで `<canvas>` を再描画する、仮想リストの行数を計算する、取得する画像解像度を選ぶ、幅しきい値を越えた非スタイルの状態フラグを切り替える、など。要素のサイズ変化が、命令的なコールバック配線ではなく**状態遷移**になります。これは読み取り専用のプロデューサです。要素/レイアウトは状態に値を供給するだけで(`element/layout → state`)、戻る経路はありません。
|
|
26
|
+
|
|
27
|
+
## `target` 属性がすべてを決める
|
|
28
|
+
|
|
29
|
+
`target` は*何を*監視するかを選ぶ唯一のツマミであり、それに伴って `<wcs-resize>` の描画方法も決めます。明示的に要求しない限り layout box を注入しません。
|
|
30
|
+
|
|
31
|
+
| `target` | 監視対象 | `display` | 用途 |
|
|
32
|
+
|-------------------|---------------------|-------------|------------------------|
|
|
33
|
+
| *省略* | 最初の子要素 | `contents` | 包んだ子のサイズ計測 |
|
|
34
|
+
| `"#panel"` / セレクタ | マッチした要素 | `none` | 既存ノードのサイズ計測 |
|
|
35
|
+
| `"self"` | 自分自身 | `block` | コンテナ幅プローブ |
|
|
36
|
+
|
|
37
|
+
`display:contents` は包んだ子に自前の box を注入しません(`<wcs-resize><div></wcs-resize>` は flex/grid の親を乱しません)。`display:none` の要素は **box を生成しない**ため、セレクタポインタの `<wcs-resize>` は自分自身ではなく参照先ノード(box を持つ)を正しく監視します。
|
|
38
|
+
|
|
39
|
+
> **`target="self"` はコンテナプローブ。** `self` の `<wcs-resize>` は `display:block` のゼロ高要素として描画されるため、親の利用可能な inline サイズいっぱいに広がります。`width` をバインドすると*親コンテナの*幅を追跡します — CSS コンテナクエリの JS 版です。(`display:contents` / `display:none` の要素は box を持たず発火しないため、`self` は `block` box を取ります。)
|
|
40
|
+
|
|
41
|
+
> **最初の子要素。** `target` 省略時は*最初の子要素*を監視します。target は `observe()` のたびに再解決されるため(connect 時と監視対象属性の変更ごとに実行)、connect 後に最初の子を追加/削除すると次の再 observe で監視対象が切り替わります。解決時点で子要素がなければ自分自身の監視(`display:block`)にフォールバックします。複数対象の同時監視は意図的に対象外です — 対象ごとに `<wcs-resize>` で包んでください。
|
|
42
|
+
|
|
43
|
+
## インストール
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @wcstack/resize
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## クイックスタート
|
|
50
|
+
|
|
51
|
+
### 1. コンテナ幅プローブ(`self`)
|
|
52
|
+
|
|
53
|
+
`width` を状態値にバインドし、状態ロジック側で*親コンテナの*サイズに反応します。
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<script type="module" src="https://esm.run/@wcstack/state/auto"></script>
|
|
57
|
+
<script type="module" src="https://esm.run/@wcstack/resize/auto"></script>
|
|
58
|
+
|
|
59
|
+
<wcs-state>
|
|
60
|
+
<script type="module">
|
|
61
|
+
export default {
|
|
62
|
+
width: 0,
|
|
63
|
+
get layout() {
|
|
64
|
+
return this.width < 480 ? "stack" : "columns";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
</script>
|
|
68
|
+
</wcs-state>
|
|
69
|
+
|
|
70
|
+
<div class="panel">
|
|
71
|
+
<wcs-resize target="self" round data-wcs="width: width"></wcs-resize>
|
|
72
|
+
<!-- `layout` を class や属性などにバインド -->
|
|
73
|
+
</div>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`round` は `width` を整数に丸めるため、サブピクセルのレイアウト変化でバインド先の状態が揺れません。
|
|
77
|
+
|
|
78
|
+
### 2. 包んだ子のサイズ計測(canvas 再描画)
|
|
79
|
+
|
|
80
|
+
`target` を省略すると box を注入せず最初の子を監視します。`width` / `height` を再描画コマンドにバインドします。
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<wcs-resize round data-wcs="width: canvasWidth; height: canvasHeight">
|
|
84
|
+
<canvas data-wcs="..."></canvas>
|
|
85
|
+
</wcs-resize>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 3. 一度だけ計測(`once`)
|
|
89
|
+
|
|
90
|
+
`ResizeObserver` は監視開始時に必ず初期サイズを通知するため、`once` は `<wcs-resize>` を一度きりの計測にします。
|
|
91
|
+
|
|
92
|
+
```html
|
|
93
|
+
<wcs-resize target="#card" once data-wcs="width: cardWidth"></wcs-resize>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## リサイズループの回避
|
|
97
|
+
|
|
98
|
+
コールバックが監視対象のサイズを変える `ResizeObserver` は自分自身を駆動しえます(*サイズ → 状態 → DOM サイズ → サイズ*)。ブラウザには組み込みのループブレーカがあり(同一フレーム内の再通知を次フレームへ延期するのでハングしません)、`@wcstack/state` の同値ガードも一周で収束するバインディングを止めます。残る危険は**サブピクセルの振動**です(幅が `99.99` と `100.01` を永遠に往復)。
|
|
99
|
+
|
|
100
|
+
優先順に2つの防御策があります。
|
|
101
|
+
|
|
102
|
+
1. **サイズ出力をサイズ入力に配線しない。** `width` / `height` はレイアウトの駆動源ではなく、*ロジック*のための読み取り専用センサとして扱う。
|
|
103
|
+
2. **シグナルを量子化する。** `round` で整数にスナップするか、`@wcstack/debounce` の `<wcs-throttle>` と合成して状態更新をレート制限する。
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<!-- 連続するリサイズを 100ms ごとに 1 回の状態更新へまとめる -->
|
|
107
|
+
<wcs-resize target="self" data-wcs="width: rawWidth"></wcs-resize>
|
|
108
|
+
<wcs-throttle wait="100" data-wcs="source: rawWidth; value: settledWidth"></wcs-throttle>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 属性
|
|
112
|
+
|
|
113
|
+
| 属性 | 型 | 既定 | 説明 |
|
|
114
|
+
|-----------|---------|----------------|------|
|
|
115
|
+
| `target` | string | *(省略)* | 監視対象: 省略 → 最初の子、セレクタ → その要素、`self` → 自分自身。 |
|
|
116
|
+
| `box` | string | `content-box` | 報告する box: `content-box`、`border-box`、`device-pixel-content-box`。未知の値は `content-box` にフォールバック。 |
|
|
117
|
+
| `round` | boolean | `false` | `width` / `height` を整数に丸める(サブピクセルの揺れを吸収)。 |
|
|
118
|
+
| `once` | boolean | `false` | 初回計測後に切断(一度きり計測)。 |
|
|
119
|
+
| `manual` | boolean | `false` | connect 時に自動監視せず、コマンドで駆動する。 |
|
|
120
|
+
|
|
121
|
+
> **`trigger`** には*属性がありません* — `@wcstack/state` 配線専用の momentary なコマンドプロパティです。`false → true` の書き込みで `observe()` を再実行し、即 `false` に戻ります(一度きりの確認応答。実際の成否は `observing` を読む)。状態駆動の監視には、この boolean よりコマンドトークンプロトコル(`command.observe: …`)を推奨します。
|
|
122
|
+
|
|
123
|
+
## 出力状態
|
|
124
|
+
|
|
125
|
+
| プロパティ | 型 | 説明 |
|
|
126
|
+
|-------------|--------------------------|------|
|
|
127
|
+
| `entry` | `WcsResizeEntry \| null` | 最新 `ResizeObserverEntry` の plain スナップショット(`contentRect` と box-size 断片を plain な数値に正規化)と live な `target` ノード。 |
|
|
128
|
+
| `width` | `number` | 監視中の `box` から得た代表 width(`contentRect` にフォールバック)。`round` 指定時は丸める。 |
|
|
129
|
+
| `height` | `number` | 代表 height。規則は `width` と同じ。 |
|
|
130
|
+
| `observing` | `boolean` | 監視が現在アクティブか。 |
|
|
131
|
+
|
|
132
|
+
> **`width` は `box` に追従。** `box="border-box"` なら border-box 幅、`device-pixel-content-box` ならデバイスピクセル幅、それ以外は content-box 幅です。生の `inlineSize` / `blockSize` 断片を `width` / `height` にマップし(横書きで正しい)、丸めていない値も `entry` で参照できます。
|
|
133
|
+
|
|
134
|
+
## コマンド
|
|
135
|
+
|
|
136
|
+
| コマンド | 説明 |
|
|
137
|
+
|----------------|------|
|
|
138
|
+
| `observe()` | `target` を DOM から再解決して監視を(再)開始。 |
|
|
139
|
+
| `unobserve()` | 現在の対象の監視を停止。 |
|
|
140
|
+
| `disconnect()` | すべての監視を停止。 |
|
|
141
|
+
|
|
142
|
+
## Binding Contract(`wcBindable`)
|
|
143
|
+
|
|
144
|
+
Core と Shell の両方が [wc-bindable](https://github.com/csbc-dev) プロトコルを宣言します。
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
// ResizeCore(ヘッドレス)
|
|
148
|
+
ResizeCore.wcBindable = {
|
|
149
|
+
protocol: "wc-bindable",
|
|
150
|
+
version: 1,
|
|
151
|
+
properties: [
|
|
152
|
+
{ name: "entry", event: "wcs-resize:change" },
|
|
153
|
+
{ name: "width", event: "wcs-resize:change", getter: (e) => e.detail.width },
|
|
154
|
+
{ name: "height", event: "wcs-resize:change", getter: (e) => e.detail.height },
|
|
155
|
+
{ name: "observing", event: "wcs-resize:observing-changed" },
|
|
156
|
+
],
|
|
157
|
+
commands: [
|
|
158
|
+
{ name: "observe" }, { name: "unobserve" }, { name: "disconnect" },
|
|
159
|
+
],
|
|
160
|
+
};
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Shell(`<wcs-resize>`)は Core の `properties` / `commands` を継承し、momentary な `trigger` プロパティを追加し、DOM 駆動の `inputs`(`target`、`box`、`round`、`once`、`manual`、`trigger`)を宣言します。
|
|
164
|
+
|
|
165
|
+
## Core を単体で使う
|
|
166
|
+
|
|
167
|
+
`ResizeCore` はフレームワーク非依存で、カスタム要素なしでも使えます。監視する要素を渡します(Shell がこの解決を代行します)。
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
import { ResizeCore } from "@wcstack/resize";
|
|
171
|
+
|
|
172
|
+
const core = new ResizeCore();
|
|
173
|
+
core.addEventListener("wcs-resize:change", (e) => {
|
|
174
|
+
console.log(e.detail.width, e.detail.height);
|
|
175
|
+
});
|
|
176
|
+
core.observe(document.querySelector("#panel"), { box: "border-box", round: true });
|
|
177
|
+
// 後で
|
|
178
|
+
core.disconnect();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## 注意・制限
|
|
182
|
+
|
|
183
|
+
- **単一対象。** 各 `<wcs-resize>` は厳密に 1 要素を監視し、状態は単一の値面にマップされます。多数の対象には多数の要素を使ってください。
|
|
184
|
+
- **決して throw しない。** 非対応環境(`ResizeObserver` なし)は静かな no-op です。有効だが非対応の `box`(例: 対応していないエンジンでの `device-pixel-content-box`)は `content-box` で 1 回再試行し、それも失敗すれば throw せず `observing` を `false` のままにします。
|
|
185
|
+
- **スタイリングには CSS `@container` を優先。** 本コンポーネントはサイズ依存の*スタイル*ではなく、サイズ依存の*ロジック*のためのものです。
|
|
186
|
+
|
|
187
|
+
## ライセンス
|
|
188
|
+
|
|
189
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# @wcstack/resize
|
|
2
|
+
|
|
3
|
+
`@wcstack/resize` is a headless ResizeObserver component for the wcstack ecosystem.
|
|
4
|
+
|
|
5
|
+
It is not a visual UI widget.
|
|
6
|
+
It is an **async primitive node** that turns an element's *size* into reactive state — the same way `@wcstack/fetch` turns a network request into reactive state and `@wcstack/intersection` turns an element's visibility into reactive state.
|
|
7
|
+
|
|
8
|
+
With `@wcstack/state`, `<wcs-resize>` can be bound directly through path contracts:
|
|
9
|
+
|
|
10
|
+
- **input / command surface**: `target`, `box`, `round`, `once`, `manual`, `trigger`
|
|
11
|
+
- **output state surface**: `entry`, `width`, `height`, `observing`
|
|
12
|
+
|
|
13
|
+
This means size-aware logic — canvas redraws, virtual lists, picking an image size, switching a layout mode — can be expressed declaratively in HTML, without writing `new ResizeObserver()`, `observe()`, `disconnect()`, or teardown glue in your UI layer.
|
|
14
|
+
|
|
15
|
+
`@wcstack/resize` follows the [CSBC](https://github.com/csbc-dev/arch/blob/main/README.md) (Core / Shell / Binding Contract) architecture:
|
|
16
|
+
|
|
17
|
+
- **Core** (`ResizeCore`) owns the observer, entry normalization, box-following size derivation, and observation lifecycle
|
|
18
|
+
- **Shell** (`<wcs-resize>`) resolves *what* to observe from the DOM, manages display, lifecycle, and declarative commands
|
|
19
|
+
- **Binding Contract** (`static wcBindable`) declares observable `properties`, writable `inputs`, and callable `commands`
|
|
20
|
+
|
|
21
|
+
## When to reach for this (and when not to)
|
|
22
|
+
|
|
23
|
+
For **styling** that depends on a container's size, use CSS [`@container` queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_containment/Container_queries) — that is what they exist for, and they never round-trip through JavaScript.
|
|
24
|
+
|
|
25
|
+
`@wcstack/resize` is for the cases CSS can't express: **size-dependent logic**. Redrawing a `<canvas>` at the element's pixel size, computing how many virtual-list rows fit, choosing which image resolution to fetch, flipping a non-style state flag past a width threshold. An element changing size becomes a **state transition**, not imperative callback wiring. It is a read-only producer: the element/layout only produces values for the state (`element/layout → state`), with no path back.
|
|
26
|
+
|
|
27
|
+
## The `target` attribute decides everything
|
|
28
|
+
|
|
29
|
+
`target` is the single knob that selects *what* is observed — and, with it, how `<wcs-resize>` renders. It never injects a layout box unless you explicitly ask for one:
|
|
30
|
+
|
|
31
|
+
| `target` | observes | `display` | use case |
|
|
32
|
+
|-------------------|-----------------------|-------------|------------------------|
|
|
33
|
+
| *omitted* | first element child | `contents` | size a wrapped child |
|
|
34
|
+
| `"#panel"` / sel. | the matched element | `none` | size an existing node |
|
|
35
|
+
| `"self"` | the element itself | `block` | container-width probe |
|
|
36
|
+
|
|
37
|
+
`display:contents` means wrapping a child injects no box of its own — so `<wcs-resize><div></wcs-resize>` does not disturb a flex/grid parent. A `display:none` element generates **no box**, so a selector-pointer `<wcs-resize>` correctly observes the referenced node (which has a box), not itself.
|
|
38
|
+
|
|
39
|
+
> **`target="self"` is a container probe.** A `self` `<wcs-resize>` renders as a `display:block`, zero-height element, so it stretches to fill its parent's available inline size. Binding `width` then tracks the *parent container's* width — the JS counterpart to a CSS container query. (A `display:contents` / `display:none` element has no box and would never fire, which is why `self` takes a `block` box.)
|
|
40
|
+
|
|
41
|
+
> **First element child.** When `target` is omitted, the *first element child* is observed. The target is re-resolved on every `observe()` (which runs on connect and on each observed-attribute change), so adding or removing the first child after connect switches the observed element on the next re-observe. If there is no element child at resolution time, it falls back to observing itself (`display:block`). Observing multiple targets at once is intentionally out of scope — wrap each target in its own `<wcs-resize>`.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @wcstack/resize
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
### 1. Container-width probe (`self`)
|
|
52
|
+
|
|
53
|
+
Bind `width` to a state value and react to the *parent container's* size in your state logic.
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<script type="module" src="https://esm.run/@wcstack/state/auto"></script>
|
|
57
|
+
<script type="module" src="https://esm.run/@wcstack/resize/auto"></script>
|
|
58
|
+
|
|
59
|
+
<wcs-state>
|
|
60
|
+
<script type="module">
|
|
61
|
+
export default {
|
|
62
|
+
width: 0,
|
|
63
|
+
get layout() {
|
|
64
|
+
return this.width < 480 ? "stack" : "columns";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
</script>
|
|
68
|
+
</wcs-state>
|
|
69
|
+
|
|
70
|
+
<div class="panel">
|
|
71
|
+
<wcs-resize target="self" round data-wcs="width: width"></wcs-resize>
|
|
72
|
+
<!-- bind `layout` to a class, attribute, etc. -->
|
|
73
|
+
</div>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`round` rounds `width` to an integer so a sub-pixel layout change does not churn the bound state.
|
|
77
|
+
|
|
78
|
+
### 2. Size a wrapped child (canvas redraw)
|
|
79
|
+
|
|
80
|
+
Omit `target` to observe the first child without injecting a box. Bind `width` / `height` to drive a redraw command.
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<wcs-resize round data-wcs="width: canvasWidth; height: canvasHeight">
|
|
84
|
+
<canvas data-wcs="..."></canvas>
|
|
85
|
+
</wcs-resize>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 3. Measure once (`once`)
|
|
89
|
+
|
|
90
|
+
`ResizeObserver` always reports the initial size when observation starts, so `once` makes `<wcs-resize>` a one-shot measurement.
|
|
91
|
+
|
|
92
|
+
```html
|
|
93
|
+
<wcs-resize target="#card" once data-wcs="width: cardWidth"></wcs-resize>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Avoiding resize loops
|
|
97
|
+
|
|
98
|
+
A `ResizeObserver` whose callback changes the observed element's size can feed itself: *size → state → DOM size → size*. The browser has a built-in loop breaker (it defers same-frame re-notifications to the next frame, so you never hang), and `@wcstack/state`'s same-value guard stops a binding that converges in one pass. The remaining hazard is **sub-pixel oscillation** (a width flipping between `99.99` and `100.01` forever).
|
|
99
|
+
|
|
100
|
+
Two defenses, in order of preference:
|
|
101
|
+
|
|
102
|
+
1. **Don't wire a size output back into a size input.** Treat `width` / `height` as read-only sensors for *logic*, not as a layout driver.
|
|
103
|
+
2. **Quantize the signal.** Use `round` to snap to integers, or compose with `@wcstack/debounce`'s `<wcs-throttle>` to rate-limit the state update:
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<!-- coalesce rapid resizes into one state update every 100ms -->
|
|
107
|
+
<wcs-resize target="self" data-wcs="width: rawWidth"></wcs-resize>
|
|
108
|
+
<wcs-throttle wait="100" data-wcs="source: rawWidth; value: settledWidth"></wcs-throttle>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Attributes
|
|
112
|
+
|
|
113
|
+
| Attribute | Type | Default | Description |
|
|
114
|
+
|-----------|---------|---------------|-------------|
|
|
115
|
+
| `target` | string | *(omitted)* | What to observe: omitted → first child, a selector → that element, `self` → this element. |
|
|
116
|
+
| `box` | string | `content-box` | Which box to report: `content-box`, `border-box`, `device-pixel-content-box`. Unrecognized values fall back to `content-box`. |
|
|
117
|
+
| `round` | boolean | `false` | Round `width` / `height` to integers (absorbs sub-pixel jitter). |
|
|
118
|
+
| `once` | boolean | `false` | Disconnect after the first size observation (measure-once). |
|
|
119
|
+
| `manual` | boolean | `false` | Do not auto-observe on connect; drive it via commands instead. |
|
|
120
|
+
|
|
121
|
+
> **`trigger`** has *no attribute* — it is a momentary command-property meant for `@wcstack/state` wiring only. A `false → true` write re-runs `observe()` and the property auto-resets to `false` (a one-shot acknowledgement; read `observing` for the actual outcome). Prefer the command-token protocol (`command.observe: …`) over this boolean for state-driven observation.
|
|
122
|
+
|
|
123
|
+
## Output state
|
|
124
|
+
|
|
125
|
+
| Property | Type | Description |
|
|
126
|
+
|-------------|--------------------------|-------------|
|
|
127
|
+
| `entry` | `WcsResizeEntry \| null` | Plain snapshot of the latest `ResizeObserverEntry` (`contentRect` and the box-size fragments normalized to plain numbers), plus the live `target` node. |
|
|
128
|
+
| `width` | `number` | Headline width from the observed `box` (falls back to `contentRect`), rounded when `round` is set. |
|
|
129
|
+
| `height` | `number` | Headline height, same rules as `width`. |
|
|
130
|
+
| `observing` | `boolean` | Whether an observation is currently active. |
|
|
131
|
+
|
|
132
|
+
> **`width` follows `box`.** With `box="border-box"`, `width` is the border-box width; with `device-pixel-content-box`, the device-pixel width; otherwise the content-box width. The raw `inlineSize` / `blockSize` fragments are mapped to `width` / `height` (correct for horizontal writing modes) and are also available un-rounded on `entry`.
|
|
133
|
+
|
|
134
|
+
## Commands
|
|
135
|
+
|
|
136
|
+
| Command | Description |
|
|
137
|
+
|----------------|-------------|
|
|
138
|
+
| `observe()` | Re-resolve `target` from the DOM and (re)start observing. |
|
|
139
|
+
| `unobserve()` | Stop observing the current target. |
|
|
140
|
+
| `disconnect()` | Stop all observation. |
|
|
141
|
+
|
|
142
|
+
## Binding Contract (`wcBindable`)
|
|
143
|
+
|
|
144
|
+
Both the Core and the Shell declare the [wc-bindable](https://github.com/csbc-dev) protocol.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
// ResizeCore (headless)
|
|
148
|
+
ResizeCore.wcBindable = {
|
|
149
|
+
protocol: "wc-bindable",
|
|
150
|
+
version: 1,
|
|
151
|
+
properties: [
|
|
152
|
+
{ name: "entry", event: "wcs-resize:change" },
|
|
153
|
+
{ name: "width", event: "wcs-resize:change", getter: (e) => e.detail.width },
|
|
154
|
+
{ name: "height", event: "wcs-resize:change", getter: (e) => e.detail.height },
|
|
155
|
+
{ name: "observing", event: "wcs-resize:observing-changed" },
|
|
156
|
+
],
|
|
157
|
+
commands: [
|
|
158
|
+
{ name: "observe" }, { name: "unobserve" }, { name: "disconnect" },
|
|
159
|
+
],
|
|
160
|
+
};
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The Shell (`<wcs-resize>`) inherits the Core's `properties` / `commands`, adds the momentary `trigger` property, and declares the DOM-driven `inputs` (`target`, `box`, `round`, `once`, `manual`, `trigger`).
|
|
164
|
+
|
|
165
|
+
## Using the Core standalone
|
|
166
|
+
|
|
167
|
+
`ResizeCore` is framework-agnostic and can be used without the custom element. You hand it the element to observe (the Shell does this resolution for you):
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
import { ResizeCore } from "@wcstack/resize";
|
|
171
|
+
|
|
172
|
+
const core = new ResizeCore();
|
|
173
|
+
core.addEventListener("wcs-resize:change", (e) => {
|
|
174
|
+
console.log(e.detail.width, e.detail.height);
|
|
175
|
+
});
|
|
176
|
+
core.observe(document.querySelector("#panel"), { box: "border-box", round: true });
|
|
177
|
+
// later
|
|
178
|
+
core.disconnect();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Notes & limitations
|
|
182
|
+
|
|
183
|
+
- **Single target.** Each `<wcs-resize>` observes exactly one element so the state maps to a single value surface. For many targets, use many elements.
|
|
184
|
+
- **Never throws.** Unsupported environments (no `ResizeObserver`) are silent no-ops. A valid-but-unsupported `box` (e.g. `device-pixel-content-box` on engines that lack it) is retried once with `content-box`; if that also fails, `observing` stays `false` rather than throwing.
|
|
185
|
+
- **For styling, prefer CSS `@container`.** This component is for size-dependent *logic*, not size-dependent *styles*.
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
MIT
|
package/dist/auto.js
ADDED
package/dist/auto.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bootstrapResize as o}from"./index.esm.min.js";o();
|