Skip to content

Commit e447729

Browse files
authored
Add bindings for ResizeObserver (#2701)
* Add bindings for `ResizeObserver` I had to add a `Constructor` attribute to `ResizeObserver`, since the new constructor syntax isn't supported yet (#1952). * Mark API as unstable * reset ci
1 parent d4b21e7 commit e447729

8 files changed

+334
-0
lines changed

crates/web-sys/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,11 @@ RequestInit = []
954954
RequestMediaKeySystemAccessNotification = []
955955
RequestMode = []
956956
RequestRedirect = []
957+
ResizeObserver = []
958+
ResizeObserverBoxOptions = []
959+
ResizeObserverEntry = []
960+
ResizeObserverOptions = []
961+
ResizeObserverSize = []
957962
Response = []
958963
ResponseInit = []
959964
ResponseType = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![allow(unused_imports)]
2+
use super::*;
3+
use wasm_bindgen::prelude::*;
4+
#[cfg(web_sys_unstable_apis)]
5+
#[wasm_bindgen]
6+
extern "C" {
7+
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = ResizeObserver , typescript_type = "ResizeObserver")]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[doc = "The `ResizeObserver` class."]
10+
#[doc = ""]
11+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)"]
12+
#[doc = ""]
13+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserver`*"]
14+
#[doc = ""]
15+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
16+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
17+
pub type ResizeObserver;
18+
#[cfg(web_sys_unstable_apis)]
19+
#[wasm_bindgen(catch, constructor, js_class = "ResizeObserver")]
20+
#[doc = "The `new ResizeObserver(..)` constructor, creating a new instance of `ResizeObserver`."]
21+
#[doc = ""]
22+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver)"]
23+
#[doc = ""]
24+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserver`*"]
25+
#[doc = ""]
26+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
27+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
28+
pub fn new(callback: &::js_sys::Function) -> Result<ResizeObserver, JsValue>;
29+
#[cfg(web_sys_unstable_apis)]
30+
# [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = disconnect)]
31+
#[doc = "The `disconnect()` method."]
32+
#[doc = ""]
33+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/disconnect)"]
34+
#[doc = ""]
35+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserver`*"]
36+
#[doc = ""]
37+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
38+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
39+
pub fn disconnect(this: &ResizeObserver);
40+
#[cfg(web_sys_unstable_apis)]
41+
#[cfg(feature = "Element")]
42+
# [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = observe)]
43+
#[doc = "The `observe()` method."]
44+
#[doc = ""]
45+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/observe)"]
46+
#[doc = ""]
47+
#[doc = "*This API requires the following crate features to be activated: `Element`, `ResizeObserver`*"]
48+
#[doc = ""]
49+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
50+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
51+
pub fn observe(this: &ResizeObserver, target: &Element);
52+
#[cfg(web_sys_unstable_apis)]
53+
#[cfg(all(feature = "Element", feature = "ResizeObserverOptions",))]
54+
# [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = observe)]
55+
#[doc = "The `observe()` method."]
56+
#[doc = ""]
57+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/observe)"]
58+
#[doc = ""]
59+
#[doc = "*This API requires the following crate features to be activated: `Element`, `ResizeObserver`, `ResizeObserverOptions`*"]
60+
#[doc = ""]
61+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
62+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
63+
pub fn observe_with_options(
64+
this: &ResizeObserver,
65+
target: &Element,
66+
options: &ResizeObserverOptions,
67+
);
68+
#[cfg(web_sys_unstable_apis)]
69+
#[cfg(feature = "Element")]
70+
# [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = unobserve)]
71+
#[doc = "The `unobserve()` method."]
72+
#[doc = ""]
73+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/unobserve)"]
74+
#[doc = ""]
75+
#[doc = "*This API requires the following crate features to be activated: `Element`, `ResizeObserver`*"]
76+
#[doc = ""]
77+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
78+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
79+
pub fn unobserve(this: &ResizeObserver, target: &Element);
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![allow(unused_imports)]
2+
use wasm_bindgen::prelude::*;
3+
#[cfg(web_sys_unstable_apis)]
4+
#[wasm_bindgen]
5+
#[doc = "The `ResizeObserverBoxOptions` enum."]
6+
#[doc = ""]
7+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverBoxOptions`*"]
8+
#[doc = ""]
9+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
10+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12+
pub enum ResizeObserverBoxOptions {
13+
BorderBox = "border-box",
14+
ContentBox = "content-box",
15+
DevicePixelContentBox = "device-pixel-content-box",
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#![allow(unused_imports)]
2+
use super::*;
3+
use wasm_bindgen::prelude::*;
4+
#[cfg(web_sys_unstable_apis)]
5+
#[wasm_bindgen]
6+
extern "C" {
7+
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = ResizeObserverEntry , typescript_type = "ResizeObserverEntry")]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[doc = "The `ResizeObserverEntry` class."]
10+
#[doc = ""]
11+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry)"]
12+
#[doc = ""]
13+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverEntry`*"]
14+
#[doc = ""]
15+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
16+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
17+
pub type ResizeObserverEntry;
18+
#[cfg(web_sys_unstable_apis)]
19+
#[cfg(feature = "Element")]
20+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverEntry" , js_name = target)]
21+
#[doc = "Getter for the `target` field of this object."]
22+
#[doc = ""]
23+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/target)"]
24+
#[doc = ""]
25+
#[doc = "*This API requires the following crate features to be activated: `Element`, `ResizeObserverEntry`*"]
26+
#[doc = ""]
27+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
28+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
29+
pub fn target(this: &ResizeObserverEntry) -> Element;
30+
#[cfg(web_sys_unstable_apis)]
31+
#[cfg(feature = "DomRectReadOnly")]
32+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverEntry" , js_name = contentRect)]
33+
#[doc = "Getter for the `contentRect` field of this object."]
34+
#[doc = ""]
35+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/contentRect)"]
36+
#[doc = ""]
37+
#[doc = "*This API requires the following crate features to be activated: `DomRectReadOnly`, `ResizeObserverEntry`*"]
38+
#[doc = ""]
39+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
40+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
41+
pub fn content_rect(this: &ResizeObserverEntry) -> DomRectReadOnly;
42+
#[cfg(web_sys_unstable_apis)]
43+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverEntry" , js_name = borderBoxSize)]
44+
#[doc = "Getter for the `borderBoxSize` field of this object."]
45+
#[doc = ""]
46+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/borderBoxSize)"]
47+
#[doc = ""]
48+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverEntry`*"]
49+
#[doc = ""]
50+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
51+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
52+
pub fn border_box_size(this: &ResizeObserverEntry) -> ::js_sys::Array;
53+
#[cfg(web_sys_unstable_apis)]
54+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverEntry" , js_name = contentBoxSize)]
55+
#[doc = "Getter for the `contentBoxSize` field of this object."]
56+
#[doc = ""]
57+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/contentBoxSize)"]
58+
#[doc = ""]
59+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverEntry`*"]
60+
#[doc = ""]
61+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
62+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
63+
pub fn content_box_size(this: &ResizeObserverEntry) -> ::js_sys::Array;
64+
#[cfg(web_sys_unstable_apis)]
65+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverEntry" , js_name = devicePixelContentBoxSize)]
66+
#[doc = "Getter for the `devicePixelContentBoxSize` field of this object."]
67+
#[doc = ""]
68+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/devicePixelContentBoxSize)"]
69+
#[doc = ""]
70+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverEntry`*"]
71+
#[doc = ""]
72+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
73+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
74+
pub fn device_pixel_content_box_size(this: &ResizeObserverEntry) -> ::js_sys::Array;
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![allow(unused_imports)]
2+
use super::*;
3+
use wasm_bindgen::prelude::*;
4+
#[cfg(web_sys_unstable_apis)]
5+
#[wasm_bindgen]
6+
extern "C" {
7+
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = ResizeObserverOptions)]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[doc = "The `ResizeObserverOptions` dictionary."]
10+
#[doc = ""]
11+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverOptions`*"]
12+
#[doc = ""]
13+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
14+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
15+
pub type ResizeObserverOptions;
16+
}
17+
#[cfg(web_sys_unstable_apis)]
18+
impl ResizeObserverOptions {
19+
#[doc = "Construct a new `ResizeObserverOptions`."]
20+
#[doc = ""]
21+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverOptions`*"]
22+
#[doc = ""]
23+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
24+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
25+
pub fn new() -> Self {
26+
#[allow(unused_mut)]
27+
let mut ret: Self = ::wasm_bindgen::JsCast::unchecked_into(::js_sys::Object::new());
28+
ret
29+
}
30+
#[cfg(web_sys_unstable_apis)]
31+
#[cfg(feature = "ResizeObserverBoxOptions")]
32+
#[doc = "Change the `box` field of this object."]
33+
#[doc = ""]
34+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverBoxOptions`, `ResizeObserverOptions`*"]
35+
#[doc = ""]
36+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
37+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
38+
pub fn box_(&mut self, val: ResizeObserverBoxOptions) -> &mut Self {
39+
use wasm_bindgen::JsValue;
40+
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("box"), &JsValue::from(val));
41+
debug_assert!(
42+
r.is_ok(),
43+
"setting properties should never fail on our dictionary objects"
44+
);
45+
let _ = r;
46+
self
47+
}
48+
}
49+
#[cfg(web_sys_unstable_apis)]
50+
impl Default for ResizeObserverOptions {
51+
fn default() -> Self {
52+
Self::new()
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![allow(unused_imports)]
2+
use super::*;
3+
use wasm_bindgen::prelude::*;
4+
#[cfg(web_sys_unstable_apis)]
5+
#[wasm_bindgen]
6+
extern "C" {
7+
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = ResizeObserverSize , typescript_type = "ResizeObserverSize")]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[doc = "The `ResizeObserverSize` class."]
10+
#[doc = ""]
11+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverSize)"]
12+
#[doc = ""]
13+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverSize`*"]
14+
#[doc = ""]
15+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
16+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
17+
pub type ResizeObserverSize;
18+
#[cfg(web_sys_unstable_apis)]
19+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverSize" , js_name = inlineSize)]
20+
#[doc = "Getter for the `inlineSize` field of this object."]
21+
#[doc = ""]
22+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverSize/inlineSize)"]
23+
#[doc = ""]
24+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverSize`*"]
25+
#[doc = ""]
26+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
27+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
28+
pub fn inline_size(this: &ResizeObserverSize) -> f64;
29+
#[cfg(web_sys_unstable_apis)]
30+
# [wasm_bindgen (structural , method , getter , js_class = "ResizeObserverSize" , js_name = blockSize)]
31+
#[doc = "Getter for the `blockSize` field of this object."]
32+
#[doc = ""]
33+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverSize/blockSize)"]
34+
#[doc = ""]
35+
#[doc = "*This API requires the following crate features to be activated: `ResizeObserverSize`*"]
36+
#[doc = ""]
37+
#[doc = "*This API is unstable and requires `--cfg=web_sys_unstable_apis` to be activated, as"]
38+
#[doc = "[described in the `wasm-bindgen` guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html)*"]
39+
pub fn block_size(this: &ResizeObserverSize) -> f64;
40+
}

crates/web-sys/src/features/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -5542,6 +5542,36 @@ mod gen_RequestRedirect;
55425542
#[cfg(feature = "RequestRedirect")]
55435543
pub use gen_RequestRedirect::*;
55445544

5545+
#[cfg(feature = "ResizeObserver")]
5546+
#[allow(non_snake_case)]
5547+
mod gen_ResizeObserver;
5548+
#[cfg(feature = "ResizeObserver")]
5549+
pub use gen_ResizeObserver::*;
5550+
5551+
#[cfg(feature = "ResizeObserverBoxOptions")]
5552+
#[allow(non_snake_case)]
5553+
mod gen_ResizeObserverBoxOptions;
5554+
#[cfg(feature = "ResizeObserverBoxOptions")]
5555+
pub use gen_ResizeObserverBoxOptions::*;
5556+
5557+
#[cfg(feature = "ResizeObserverEntry")]
5558+
#[allow(non_snake_case)]
5559+
mod gen_ResizeObserverEntry;
5560+
#[cfg(feature = "ResizeObserverEntry")]
5561+
pub use gen_ResizeObserverEntry::*;
5562+
5563+
#[cfg(feature = "ResizeObserverOptions")]
5564+
#[allow(non_snake_case)]
5565+
mod gen_ResizeObserverOptions;
5566+
#[cfg(feature = "ResizeObserverOptions")]
5567+
pub use gen_ResizeObserverOptions::*;
5568+
5569+
#[cfg(feature = "ResizeObserverSize")]
5570+
#[allow(non_snake_case)]
5571+
mod gen_ResizeObserverSize;
5572+
#[cfg(feature = "ResizeObserverSize")]
5573+
pub use gen_ResizeObserverSize::*;
5574+
55455575
#[cfg(feature = "Response")]
55465576
#[allow(non_snake_case)]
55475577
mod gen_Response;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
enum ResizeObserverBoxOptions {
2+
"border-box", "content-box", "device-pixel-content-box"
3+
};
4+
5+
dictionary ResizeObserverOptions {
6+
ResizeObserverBoxOptions box = "content-box";
7+
};
8+
9+
// TODO: remove this once the `constructor()` syntax is supported.
10+
[Constructor(ResizeObserverCallback callback),
11+
Exposed=(Window)]
12+
interface ResizeObserver {
13+
constructor(ResizeObserverCallback callback);
14+
undefined observe(Element target, optional ResizeObserverOptions options = {});
15+
undefined unobserve(Element target);
16+
undefined disconnect();
17+
};
18+
19+
callback ResizeObserverCallback = undefined (sequence<ResizeObserverEntry> entries, ResizeObserver observer);
20+
21+
[Exposed=Window]
22+
interface ResizeObserverEntry {
23+
readonly attribute Element target;
24+
readonly attribute DOMRectReadOnly contentRect;
25+
readonly attribute FrozenArray<ResizeObserverSize> borderBoxSize;
26+
readonly attribute FrozenArray<ResizeObserverSize> contentBoxSize;
27+
readonly attribute FrozenArray<ResizeObserverSize> devicePixelContentBoxSize;
28+
};
29+
30+
[Exposed=Window]
31+
interface ResizeObserverSize {
32+
readonly attribute unrestricted double inlineSize;
33+
readonly attribute unrestricted double blockSize;
34+
};

0 commit comments

Comments
 (0)