From d16d64de9a27337fa32871225c6101a0aa5af36c Mon Sep 17 00:00:00 2001 From: mc1098 Date: Fri, 24 Sep 2021 15:33:22 +0100 Subject: [PATCH 1/2] Remove `web_sys` re-export Removes re-exporting the `web_sys` dependency from the yew crate and removes some `web_sys` features that were only enabled for re-exporting. --- examples/boids/Cargo.toml | 7 +++++ examples/boids/src/slider.rs | 6 ++-- examples/crm/Cargo.toml | 7 +++++ examples/crm/src/add_client.rs | 2 +- examples/js_callback/Cargo.toml | 7 +++++ examples/js_callback/src/main.rs | 3 +- examples/keyed_list/Cargo.toml | 8 ++++++ examples/keyed_list/src/main.rs | 2 +- examples/nested_list/Cargo.toml | 6 ++++ examples/nested_list/src/header.rs | 1 + examples/nested_list/src/item.rs | 1 + examples/store/Cargo.toml | 7 +++++ examples/store/src/text_input.rs | 2 +- examples/todomvc/Cargo.toml | 8 ++++++ examples/todomvc/src/main.rs | 6 ++-- packages/website-test/Cargo.toml | 11 ++++++++ packages/yew-macro/Cargo.toml | 7 +++++ .../tests/html_macro/element-fail.stderr | 28 +++++++++---------- .../tests/html_macro/html-element-pass.rs | 9 +++--- packages/yew-router/src/components/link.rs | 1 + packages/yew/Cargo.toml | 6 ---- packages/yew/src/functional/hooks/use_ref.rs | 3 +- packages/yew/src/html/listener/mod.rs | 6 ++-- packages/yew/src/lib.rs | 8 ------ website/docs/concepts/components.md | 4 +-- website/docs/concepts/components/callbacks.md | 3 +- website/docs/concepts/components/refs.md | 3 +- .../function-components/pre-defined-hooks.md | 2 +- website/docs/concepts/html/elements.md | 4 +-- website/docs/concepts/html/events.md | 17 ++++++----- 30 files changed, 124 insertions(+), 61 deletions(-) diff --git a/examples/boids/Cargo.toml b/examples/boids/Cargo.toml index 52f9ffc895d..e94c59134fb 100644 --- a/examples/boids/Cargo.toml +++ b/examples/boids/Cargo.toml @@ -13,3 +13,10 @@ rand = "0.8" serde = { version = "1.0", features = ["derive"] } yew = { path = "../../packages/yew" } gloo = "0.3" + +[dependencies.web-sys] +version = "0.3" +features = [ + "HtmlInputElement", + "InputEvent", +] diff --git a/examples/boids/src/slider.rs b/examples/boids/src/slider.rs index 33fdfba94d3..d7342bf1146 100644 --- a/examples/boids/src/slider.rs +++ b/examples/boids/src/slider.rs @@ -1,8 +1,6 @@ use std::cell::Cell; -use yew::{ - html, web_sys::HtmlInputElement, Callback, Component, Context, Html, InputEvent, Properties, - TargetCast, -}; +use web_sys::{HtmlInputElement, InputEvent}; +use yew::{html, Callback, Component, Context, Html, Properties, TargetCast}; thread_local! { static SLIDER_ID: Cell = Cell::default(); diff --git a/examples/crm/Cargo.toml b/examples/crm/Cargo.toml index 01714683d09..588b74aba9b 100644 --- a/examples/crm/Cargo.toml +++ b/examples/crm/Cargo.toml @@ -11,3 +11,10 @@ serde_derive = "1" yew = { path = "../../packages/yew" } gloo = "0.3" +[dependencies.web-sys] +version = "0.3" +features = [ + "Event", + "HtmlInputElement", + "HtmlTextAreaElement", +] diff --git a/examples/crm/src/add_client.rs b/examples/crm/src/add_client.rs index 7d4d9d7fb60..23974d2f43a 100644 --- a/examples/crm/src/add_client.rs +++ b/examples/crm/src/add_client.rs @@ -1,5 +1,5 @@ use crate::Client; -use yew::web_sys::{Event, HtmlInputElement, HtmlTextAreaElement}; +use web_sys::{Event, HtmlInputElement, HtmlTextAreaElement}; use yew::{classes, html, Callback, Component, Context, Html, Properties, TargetCast}; #[derive(Debug)] diff --git a/examples/js_callback/Cargo.toml b/examples/js_callback/Cargo.toml index a1b99d76d59..1380af1d8e5 100644 --- a/examples/js_callback/Cargo.toml +++ b/examples/js_callback/Cargo.toml @@ -8,3 +8,10 @@ license = "MIT OR Apache-2.0" [dependencies] wasm-bindgen = "0.2" yew = { path = "../../packages/yew" } + +[dependencies.web-sys] +version = "0.3" +features = [ + "HtmlTextAreaElement", + "InputEvent", +] diff --git a/examples/js_callback/src/main.rs b/examples/js_callback/src/main.rs index 0377c618422..c2f0e8b19f4 100644 --- a/examples/js_callback/src/main.rs +++ b/examples/js_callback/src/main.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; -use yew::{prelude::*, web_sys::HtmlTextAreaElement}; +use web_sys::{HtmlTextAreaElement, InputEvent}; +use yew::prelude::*; mod bindings; diff --git a/examples/keyed_list/Cargo.toml b/examples/keyed_list/Cargo.toml index f438cf648db..400dfccffe1 100644 --- a/examples/keyed_list/Cargo.toml +++ b/examples/keyed_list/Cargo.toml @@ -13,3 +13,11 @@ log = "0.4" rand = "0.8" wasm-logger = "0.2" yew = { path = "../../packages/yew" } + +[dependencies.web-sys] +version = "0.3" +features = [ + "HtmlElement", + "HtmlInputElement", + "InputEvent", +] diff --git a/examples/keyed_list/src/main.rs b/examples/keyed_list/src/main.rs index 78ed21e6fc8..faa4e87e262 100644 --- a/examples/keyed_list/src/main.rs +++ b/examples/keyed_list/src/main.rs @@ -1,8 +1,8 @@ use instant::Instant; use person::PersonType; +use web_sys::{HtmlElement, HtmlInputElement, InputEvent}; use yew::html::Scope; use yew::prelude::*; -use yew::web_sys::{HtmlElement, HtmlInputElement}; mod person; mod random; diff --git a/examples/nested_list/Cargo.toml b/examples/nested_list/Cargo.toml index d1404d9ab45..239d682a757 100644 --- a/examples/nested_list/Cargo.toml +++ b/examples/nested_list/Cargo.toml @@ -9,3 +9,9 @@ license = "MIT OR Apache-2.0" log = "0.4" wasm-logger = "0.2" yew = { path = "../../packages/yew" } + +[dependencies.web-sys] +version = "0.3" +features = [ + "MouseEvent", +] diff --git a/examples/nested_list/src/header.rs b/examples/nested_list/src/header.rs index dbad68b6034..e6d96d00262 100644 --- a/examples/nested_list/src/header.rs +++ b/examples/nested_list/src/header.rs @@ -1,5 +1,6 @@ use super::list::{List, Msg as ListMsg}; use super::{Hovered, WeakComponentLink}; +use web_sys::MouseEvent; use yew::prelude::*; #[derive(Clone, PartialEq, Properties)] diff --git a/examples/nested_list/src/item.rs b/examples/nested_list/src/item.rs index fcbffd826c9..a47233bb40a 100644 --- a/examples/nested_list/src/item.rs +++ b/examples/nested_list/src/item.rs @@ -1,4 +1,5 @@ use crate::Hovered; +use web_sys::MouseEvent; use yew::prelude::*; #[derive(PartialEq, Clone, Properties)] diff --git a/examples/store/Cargo.toml b/examples/store/Cargo.toml index f65f0209df9..b4698eec566 100644 --- a/examples/store/Cargo.toml +++ b/examples/store/Cargo.toml @@ -10,3 +10,10 @@ yew = { path = "../../packages/yew" } yew-agent = { path = "../../packages/yew-agent" } wasm-bindgen = "0.2" gloo-console = "0.1" + +[dependencies.web-sys] +version = "0.3" +features = [ + "HtmlInputElement", + "KeyboardEvent", +] diff --git a/examples/store/src/text_input.rs b/examples/store/src/text_input.rs index 46bc6346838..0653ae01147 100644 --- a/examples/store/src/text_input.rs +++ b/examples/store/src/text_input.rs @@ -1,5 +1,5 @@ +use web_sys::{HtmlInputElement, KeyboardEvent}; use yew::prelude::*; -use yew::web_sys::HtmlInputElement; pub enum Msg { Submit(String), diff --git a/examples/todomvc/Cargo.toml b/examples/todomvc/Cargo.toml index 9ceb2b8cea7..6d12e778df1 100644 --- a/examples/todomvc/Cargo.toml +++ b/examples/todomvc/Cargo.toml @@ -12,3 +12,11 @@ serde = "1" serde_derive = "1" yew = { path = "../../packages/yew" } gloo = "0.3" + +[dependencies.web-sys] +version = "0.3" +features = [ + "FocusEvent", + "HtmlInputElement", + "KeyboardEvent", +] diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index ebf5d389521..1aeecae6682 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -1,10 +1,8 @@ use gloo::storage::{LocalStorage, Storage}; use state::{Entry, Filter, State}; use strum::IntoEnumIterator; -use yew::html::Scope; -use yew::web_sys::HtmlInputElement as InputElement; -use yew::{classes, html, Component, Context, FocusEvent, Html, NodeRef, TargetCast}; -use yew::{events::KeyboardEvent, Classes}; +use web_sys::{FocusEvent, HtmlInputElement as InputElement, KeyboardEvent}; +use yew::{classes, html, html::Scope, Classes, Component, Context, Html, NodeRef, TargetCast}; mod state; diff --git a/packages/website-test/Cargo.toml b/packages/website-test/Cargo.toml index a6755e6900b..2acc376153f 100644 --- a/packages/website-test/Cargo.toml +++ b/packages/website-test/Cargo.toml @@ -17,5 +17,16 @@ weblog = "0.3.0" yew = { path = "../../packages/yew/" } yew-router = { path = "../../packages/yew-router/" } +[dev-dependencies.web-sys] +version = "0.3" +features = [ + "Element", + "Event", + "EventTarget", + "HtmlElement", + "HtmlInputElement", + "KeyboardEvent", +] + [build-dependencies] glob = "0.3" diff --git a/packages/yew-macro/Cargo.toml b/packages/yew-macro/Cargo.toml index c01b817d8c9..2565d57c945 100644 --- a/packages/yew-macro/Cargo.toml +++ b/packages/yew-macro/Cargo.toml @@ -27,6 +27,13 @@ rustversion = "1" trybuild = "1" yew = { path = "../yew" } +[dev-dependencies.web-sys] +version = "0.3" +features = [ + "FocusEvent", + "MouseEvent", +] + [build-dependencies] [features] diff --git a/packages/yew-macro/tests/html_macro/element-fail.stderr b/packages/yew-macro/tests/html_macro/element-fail.stderr index 4ce990ed9e8..a348d61e0f6 100644 --- a/packages/yew-macro/tests/html_macro/element-fail.stderr +++ b/packages/yew-macro/tests/html_macro/element-fail.stderr @@ -302,11 +302,11 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue as IntoPropValue>>> = note: required by `into_prop_value` -error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}` +error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `{integer}` --> $DIR/element-fail.rs:51:28 | 51 | html! { }; - | ^ expected an `Fn<(MouseEvent,)>` closure, found `{integer}` + | ^ expected an `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `{integer}` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs | @@ -319,16 +319,16 @@ error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}` 197 | | } | |_- required by this bound in `yew::html::onclick::Wrapper::__macro_new` | - = help: the trait `Fn<(MouseEvent,)>` is not implemented for `{integer}` - = note: required because of the requirements on the impl of `IntoEventCallback` for `{integer}` + = help: the trait `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` is not implemented for `{integer}` + = note: required because of the requirements on the impl of `IntoEventCallback` for `{integer}` -error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback` +error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `yew::Callback` --> $DIR/element-fail.rs:52:29 | 52 | html! { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | expected an implementor of trait `IntoEventCallback` + | expected an implementor of trait `IntoEventCallback` | help: consider borrowing here: `&Callback::from(|a: String| ())` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs @@ -342,14 +342,14 @@ error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback: IntoEventCallback` is not satisfied - = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` + = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied + = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` -error[E0277]: the trait bound `Option<{integer}>: IntoEventCallback` is not satisfied +error[E0277]: the trait bound `Option<{integer}>: IntoEventCallback` is not satisfied --> $DIR/element-fail.rs:53:29 | 53 | html! { }; - | ^^^^^^^ the trait `IntoEventCallback` is not implemented for `Option<{integer}>` + | ^^^^^^^ the trait `IntoEventCallback` is not implemented for `Option<{integer}>` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs | @@ -386,13 +386,13 @@ error[E0277]: the trait bound `Option: IntoPropValue as IntoPropValue>>> = note: required by `into_prop_value` -error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback` +error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `yew::Callback` --> $DIR/element-fail.rs:58:29 | 58 | html! { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | expected an implementor of trait `IntoEventCallback` + | expected an implementor of trait `IntoEventCallback` | help: consider borrowing here: `&Callback::from(|a: String| ())` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs @@ -406,8 +406,8 @@ error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback: IntoEventCallback` is not satisfied - = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` + = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied + = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` error[E0277]: the trait bound `NotToString: IntoPropValue>>` is not satisfied --> $DIR/element-fail.rs:60:28 diff --git a/packages/yew-macro/tests/html_macro/html-element-pass.rs b/packages/yew-macro/tests/html_macro/html-element-pass.rs index 558281dbc95..bd9d50935a7 100644 --- a/packages/yew-macro/tests/html_macro/html-element-pass.rs +++ b/packages/yew-macro/tests/html_macro/html-element-pass.rs @@ -37,12 +37,13 @@ pub struct u8; pub struct usize; fn compile_pass() { - let onclick = <::yew::Callback<::yew::MouseEvent> as ::std::convert::From<_>>::from( - |_: ::yew::MouseEvent| (), + let onclick = <::yew::Callback<::web_sys::MouseEvent> as ::std::convert::From<_>>::from( + |_: ::web_sys::MouseEvent| (), ); let parent_ref = <::yew::NodeRef as ::std::default::Default>::default(); - let dyn_tag = || <::std::string::String as ::std::convert::From<&::std::primitive::str>>::from("test"); + let dyn_tag = + || <::std::string::String as ::std::convert::From<&::std::primitive::str>>::from("test"); let mut extra_tags_iter = ::std::iter::IntoIterator::into_iter(::std::vec!["a", "b"]); let cow_none: ::std::option::Option<::std::borrow::Cow<'static, ::std::primitive::str>> = @@ -103,7 +104,7 @@ fn compile_pass() { as ::std::convert::From<_>>::from(|_| ()))} + onblur={::std::option::Option::Some(<::yew::Callback<::web_sys::FocusEvent> as ::std::convert::From<_>>::from(|_| ()))} /> }; diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index da318b9cff5..a9125da0e29 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -1,5 +1,6 @@ use crate::{service, Routable}; use std::marker::PhantomData; +use web_sys::MouseEvent; use yew::prelude::*; /// Props for [`Link`] diff --git a/packages/yew/Cargo.toml b/packages/yew/Cargo.toml index f170b6b2cf0..1129976d585 100644 --- a/packages/yew/Cargo.toml +++ b/packages/yew/Cargo.toml @@ -32,7 +32,6 @@ scoped-tls-hkt = "0.1" version = "0.3" features = [ "AnimationEvent", - "console", "Document", "DragEvent", "Element", @@ -40,14 +39,9 @@ features = [ "Event", "EventInit", "EventTarget", - "File", - "FileList", - "FileReader", "FocusEvent", "HtmlElement", - "HtmlButtonElement", "HtmlInputElement", - "HtmlSelectElement", "HtmlTextAreaElement", "InputEvent", "InputEventInit", diff --git a/packages/yew/src/functional/hooks/use_ref.rs b/packages/yew/src/functional/hooks/use_ref.rs index c714cfceaa4..af9d42fd484 100644 --- a/packages/yew/src/functional/hooks/use_ref.rs +++ b/packages/yew/src/functional/hooks/use_ref.rs @@ -9,7 +9,8 @@ use std::{cell::RefCell, rc::Rc}; /// /// # Example /// ```rust -/// # use yew::{prelude::*, web_sys::{Event, HtmlInputElement}}; +/// # use yew::prelude::*; +/// # use web_sys::{Event, HtmlInputElement}; /// # use std::rc::Rc; /// # use std::cell::RefCell; /// # use std::ops::{Deref, DerefMut}; diff --git a/packages/yew/src/html/listener/mod.rs b/packages/yew/src/html/listener/mod.rs index 6c64dc18c92..df4d728e718 100644 --- a/packages/yew/src/html/listener/mod.rs +++ b/packages/yew/src/html/listener/mod.rs @@ -37,7 +37,8 @@ where /// # Example /// /// ``` - /// use yew::{prelude::*, web_sys::{Event, HtmlTextAreaElement}}; + /// use yew::prelude::*; + /// use web_sys::{Event, HtmlTextAreaElement}; /// # enum Msg { /// # Value(String), /// # } @@ -93,7 +94,8 @@ where /// # Example /// /// ``` - /// use yew::{prelude::*, web_sys::{Event, HtmlInputElement}}; + /// use yew::prelude::*; + /// use web_sys::{Event, HtmlInputElement}; /// # enum Msg { /// # Value(String), /// # } diff --git a/packages/yew/src/lib.rs b/packages/yew/src/lib.rs index ccdbd6be635..a3adb667f82 100644 --- a/packages/yew/src/lib.rs +++ b/packages/yew/src/lib.rs @@ -265,19 +265,11 @@ pub mod scheduler; pub mod utils; pub mod virtual_dom; -pub use web_sys; - /// The module that contains all events available in the framework. pub mod events { pub use crate::html::TargetCast; pub use crate::virtual_dom::listeners::set_event_bubbling; - - #[doc(no_inline)] - pub use web_sys::{ - AnimationEvent, DragEvent, ErrorEvent, Event, FocusEvent, InputEvent, KeyboardEvent, - MouseEvent, PointerEvent, ProgressEvent, TouchEvent, TransitionEvent, UiEvent, WheelEvent, - }; } pub use crate::app_handle::AppHandle; diff --git a/website/docs/concepts/components.md b/website/docs/concepts/components.md index 8c07afaceec..579751f2522 100644 --- a/website/docs/concepts/components.md +++ b/website/docs/concepts/components.md @@ -102,9 +102,9 @@ is also a parameter called `first_render` which can be used to determine whether being called on the first render, or instead a subsequent one. ```rust +use web_sys::HtmlInputElement; use yew::{ - Component, Context, html, Html, NodeRef, - web_sys::HtmlInputElement + Component, Context, html, Html, NodeRef, }; pub struct MyComponent { diff --git a/website/docs/concepts/components/callbacks.md b/website/docs/concepts/components/callbacks.md index 12aaa798929..b32b200b664 100644 --- a/website/docs/concepts/components/callbacks.md +++ b/website/docs/concepts/components/callbacks.md @@ -131,7 +131,8 @@ The function passed to `callback` must always take a parameter. For example, the If you need a callback that might not need to cause an update, use `batch_callback`. ```rust -use yew::{html, Component, Context, Html, KeyboardEvent}; +use web_sys::KeyboardEvent; +use yew::{html, Component, Context, Html}; enum Msg { Submit, diff --git a/website/docs/concepts/components/refs.md b/website/docs/concepts/components/refs.md index 2e6e9ae6d7d..a7ffa03a8d4 100644 --- a/website/docs/concepts/components/refs.md +++ b/website/docs/concepts/components/refs.md @@ -14,7 +14,8 @@ a canvas element after it has been rendered from `view`. The syntax is: ```rust -use yew::{html, web_sys::Element, Component, Context, Html, NodeRef}; +use web_sys::Element; +use yew::{html, Component, Context, Html, NodeRef}; struct Comp { node_ref: NodeRef, diff --git a/website/docs/concepts/function-components/pre-defined-hooks.md b/website/docs/concepts/function-components/pre-defined-hooks.md index 7c4a2a0ef22..5c2359fa2b4 100644 --- a/website/docs/concepts/function-components/pre-defined-hooks.md +++ b/website/docs/concepts/function-components/pre-defined-hooks.md @@ -48,9 +48,9 @@ If you need the component to be re-rendered on state change, consider using [`us ### Example ```rust +use web_sys::{Event, HtmlInputElement}; use yew::{ function_component, html, use_ref, use_state, - web_sys::{Event, HtmlInputElement}, Callback, TargetCast, }; diff --git a/website/docs/concepts/html/elements.md b/website/docs/concepts/html/elements.md index c6f89e3b348..d1f84dad5bc 100644 --- a/website/docs/concepts/html/elements.md +++ b/website/docs/concepts/html/elements.md @@ -12,9 +12,9 @@ Using `web-sys`, you can create DOM elements and convert them into a `Node` - wh used as a `Html` value using `VRef`: ```rust +use web_sys::{Element, Node}; use yew::{ - Component, Context, html, Html, utils::document, - web_sys::{Element, Node} + Component, Context, html, Html, utils::document, }; struct Comp; diff --git a/website/docs/concepts/html/events.md b/website/docs/concepts/html/events.md index 95e4415b5da..7b3ebbbce2b 100644 --- a/website/docs/concepts/html/events.md +++ b/website/docs/concepts/html/events.md @@ -188,9 +188,9 @@ wasm-bindgen = "0.2" ```rust //highlight-next-line use wasm_bindgen::JsCast; +use web_sys::{Event, EventTarget, HtmlInputElement}; use yew::{ html, - web_sys::{Event, EventTarget, HtmlInputElement}, Component, Context, Html, }; @@ -295,9 +295,9 @@ from events. but it works in a very similar way to `JsCast`. ```rust +use web_sys::{Event, HtmlInputElement}; use yew::{ html, - web_sys::{Event, HtmlInputElement}, // Need to import TargetCast //highlight-next-line Component, Context, Html, TargetCast, @@ -366,8 +366,10 @@ does the cast on the target of the event. `TargetCast::target_unchecked_into` is [`NodeRef`](../components/refs.md) can be used instead of querying the event given to a `Callback`. ```rust -//highlight-next-line -use yew::{html, web_sys::HtmlInputElement, Component, Context, Html, NodeRef}; +//highlight-start +use web_sys::HtmlInputElement; +use yew::{html, Component, Context, Html, NodeRef}; +//highlight-end pub struct Comp { //highlight-next-line @@ -425,7 +427,8 @@ You might also see by using `NodeRef` we don't have to send the `String` back in `Msg::InputValue` as we always have `my_input` in the component state - so we could do the following: ```rust -use yew::{html, web_sys::HtmlInputElement, Component, Context, Html, NodeRef}; +use web_sys::HtmlInputElement; +use yew::{html, Component, Context, Html, NodeRef}; pub struct Comp { my_input: NodeRef, @@ -512,9 +515,9 @@ yourself ([there is a more concise way thanks to `gloo`](#using-gloo-concise)). ```rust use wasm_bindgen::{prelude::Closure, JsCast}; +use web_sys::{Event, HtmlElement}; use yew::{ html, - web_sys::{Event, HtmlElement}, Component, Context, Html, NodeRef, }; @@ -617,9 +620,9 @@ gloo-events = "0.1" ``` ```rust +use web_sys::{Event, HtmlElement}; use yew::{ html, - web_sys::{Event, HtmlElement}, Component, Context, Html, NodeRef, }; From ac11278137bb0946803c6c8ec47fe937d46e8216 Mon Sep 17 00:00:00 2001 From: mc1098 Date: Sat, 25 Sep 2021 15:28:02 +0100 Subject: [PATCH 2/2] re-export events through yew::events --- examples/boids/Cargo.toml | 1 - examples/boids/src/slider.rs | 4 +-- examples/crm/Cargo.toml | 1 - examples/crm/src/add_client.rs | 6 ++-- examples/js_callback/Cargo.toml | 1 - examples/js_callback/src/main.rs | 2 +- examples/keyed_list/Cargo.toml | 1 - examples/keyed_list/src/main.rs | 2 +- examples/nested_list/Cargo.toml | 6 ---- examples/nested_list/src/header.rs | 1 - examples/nested_list/src/item.rs | 1 - examples/store/Cargo.toml | 1 - examples/store/src/text_input.rs | 2 +- examples/todomvc/Cargo.toml | 2 -- examples/todomvc/src/main.rs | 10 +++++-- packages/website-test/Cargo.toml | 2 -- packages/yew-macro/Cargo.toml | 7 ----- .../tests/html_macro/element-fail.stderr | 28 +++++++++---------- .../tests/html_macro/html-element-pass.rs | 6 ++-- packages/yew-router/src/components/link.rs | 1 - packages/yew/src/functional/hooks/use_ref.rs | 2 +- packages/yew/src/html/listener/mod.rs | 4 +-- packages/yew/src/lib.rs | 6 ++++ website/docs/concepts/components/callbacks.md | 3 +- .../function-components/pre-defined-hooks.md | 3 +- website/docs/concepts/html/events.md | 12 +++++--- 26 files changed, 54 insertions(+), 61 deletions(-) diff --git a/examples/boids/Cargo.toml b/examples/boids/Cargo.toml index e94c59134fb..770464c9623 100644 --- a/examples/boids/Cargo.toml +++ b/examples/boids/Cargo.toml @@ -18,5 +18,4 @@ gloo = "0.3" version = "0.3" features = [ "HtmlInputElement", - "InputEvent", ] diff --git a/examples/boids/src/slider.rs b/examples/boids/src/slider.rs index d7342bf1146..c5126352353 100644 --- a/examples/boids/src/slider.rs +++ b/examples/boids/src/slider.rs @@ -1,6 +1,6 @@ use std::cell::Cell; -use web_sys::{HtmlInputElement, InputEvent}; -use yew::{html, Callback, Component, Context, Html, Properties, TargetCast}; +use web_sys::HtmlInputElement; +use yew::{events::InputEvent, html, Callback, Component, Context, Html, Properties, TargetCast}; thread_local! { static SLIDER_ID: Cell = Cell::default(); diff --git a/examples/crm/Cargo.toml b/examples/crm/Cargo.toml index 588b74aba9b..cc30bc7be34 100644 --- a/examples/crm/Cargo.toml +++ b/examples/crm/Cargo.toml @@ -14,7 +14,6 @@ gloo = "0.3" [dependencies.web-sys] version = "0.3" features = [ - "Event", "HtmlInputElement", "HtmlTextAreaElement", ] diff --git a/examples/crm/src/add_client.rs b/examples/crm/src/add_client.rs index 23974d2f43a..1d6c0e12065 100644 --- a/examples/crm/src/add_client.rs +++ b/examples/crm/src/add_client.rs @@ -1,6 +1,8 @@ use crate::Client; -use web_sys::{Event, HtmlInputElement, HtmlTextAreaElement}; -use yew::{classes, html, Callback, Component, Context, Html, Properties, TargetCast}; +use web_sys::{HtmlInputElement, HtmlTextAreaElement}; +use yew::{ + classes, events::Event, html, Callback, Component, Context, Html, Properties, TargetCast, +}; #[derive(Debug)] pub enum Msg { diff --git a/examples/js_callback/Cargo.toml b/examples/js_callback/Cargo.toml index 1380af1d8e5..26bad873e77 100644 --- a/examples/js_callback/Cargo.toml +++ b/examples/js_callback/Cargo.toml @@ -13,5 +13,4 @@ yew = { path = "../../packages/yew" } version = "0.3" features = [ "HtmlTextAreaElement", - "InputEvent", ] diff --git a/examples/js_callback/src/main.rs b/examples/js_callback/src/main.rs index c2f0e8b19f4..b3351dd05c6 100644 --- a/examples/js_callback/src/main.rs +++ b/examples/js_callback/src/main.rs @@ -1,5 +1,5 @@ use wasm_bindgen::prelude::*; -use web_sys::{HtmlTextAreaElement, InputEvent}; +use web_sys::HtmlTextAreaElement; use yew::prelude::*; mod bindings; diff --git a/examples/keyed_list/Cargo.toml b/examples/keyed_list/Cargo.toml index 400dfccffe1..81aff6afa82 100644 --- a/examples/keyed_list/Cargo.toml +++ b/examples/keyed_list/Cargo.toml @@ -19,5 +19,4 @@ version = "0.3" features = [ "HtmlElement", "HtmlInputElement", - "InputEvent", ] diff --git a/examples/keyed_list/src/main.rs b/examples/keyed_list/src/main.rs index faa4e87e262..fc5014c0624 100644 --- a/examples/keyed_list/src/main.rs +++ b/examples/keyed_list/src/main.rs @@ -1,6 +1,6 @@ use instant::Instant; use person::PersonType; -use web_sys::{HtmlElement, HtmlInputElement, InputEvent}; +use web_sys::{HtmlElement, HtmlInputElement}; use yew::html::Scope; use yew::prelude::*; diff --git a/examples/nested_list/Cargo.toml b/examples/nested_list/Cargo.toml index 239d682a757..d1404d9ab45 100644 --- a/examples/nested_list/Cargo.toml +++ b/examples/nested_list/Cargo.toml @@ -9,9 +9,3 @@ license = "MIT OR Apache-2.0" log = "0.4" wasm-logger = "0.2" yew = { path = "../../packages/yew" } - -[dependencies.web-sys] -version = "0.3" -features = [ - "MouseEvent", -] diff --git a/examples/nested_list/src/header.rs b/examples/nested_list/src/header.rs index e6d96d00262..dbad68b6034 100644 --- a/examples/nested_list/src/header.rs +++ b/examples/nested_list/src/header.rs @@ -1,6 +1,5 @@ use super::list::{List, Msg as ListMsg}; use super::{Hovered, WeakComponentLink}; -use web_sys::MouseEvent; use yew::prelude::*; #[derive(Clone, PartialEq, Properties)] diff --git a/examples/nested_list/src/item.rs b/examples/nested_list/src/item.rs index a47233bb40a..fcbffd826c9 100644 --- a/examples/nested_list/src/item.rs +++ b/examples/nested_list/src/item.rs @@ -1,5 +1,4 @@ use crate::Hovered; -use web_sys::MouseEvent; use yew::prelude::*; #[derive(PartialEq, Clone, Properties)] diff --git a/examples/store/Cargo.toml b/examples/store/Cargo.toml index b4698eec566..ca699b8b723 100644 --- a/examples/store/Cargo.toml +++ b/examples/store/Cargo.toml @@ -15,5 +15,4 @@ gloo-console = "0.1" version = "0.3" features = [ "HtmlInputElement", - "KeyboardEvent", ] diff --git a/examples/store/src/text_input.rs b/examples/store/src/text_input.rs index 0653ae01147..7a8dca10a6d 100644 --- a/examples/store/src/text_input.rs +++ b/examples/store/src/text_input.rs @@ -1,4 +1,4 @@ -use web_sys::{HtmlInputElement, KeyboardEvent}; +use web_sys::HtmlInputElement; use yew::prelude::*; pub enum Msg { diff --git a/examples/todomvc/Cargo.toml b/examples/todomvc/Cargo.toml index 6d12e778df1..819e5840836 100644 --- a/examples/todomvc/Cargo.toml +++ b/examples/todomvc/Cargo.toml @@ -16,7 +16,5 @@ gloo = "0.3" [dependencies.web-sys] version = "0.3" features = [ - "FocusEvent", "HtmlInputElement", - "KeyboardEvent", ] diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index 1aeecae6682..4169c2df414 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -1,8 +1,14 @@ use gloo::storage::{LocalStorage, Storage}; use state::{Entry, Filter, State}; use strum::IntoEnumIterator; -use web_sys::{FocusEvent, HtmlInputElement as InputElement, KeyboardEvent}; -use yew::{classes, html, html::Scope, Classes, Component, Context, Html, NodeRef, TargetCast}; +use web_sys::HtmlInputElement as InputElement; +use yew::{ + classes, + events::{FocusEvent, KeyboardEvent}, + html, + html::Scope, + Classes, Component, Context, Html, NodeRef, TargetCast, +}; mod state; diff --git a/packages/website-test/Cargo.toml b/packages/website-test/Cargo.toml index 2acc376153f..df117754e52 100644 --- a/packages/website-test/Cargo.toml +++ b/packages/website-test/Cargo.toml @@ -21,11 +21,9 @@ yew-router = { path = "../../packages/yew-router/" } version = "0.3" features = [ "Element", - "Event", "EventTarget", "HtmlElement", "HtmlInputElement", - "KeyboardEvent", ] [build-dependencies] diff --git a/packages/yew-macro/Cargo.toml b/packages/yew-macro/Cargo.toml index 2565d57c945..c01b817d8c9 100644 --- a/packages/yew-macro/Cargo.toml +++ b/packages/yew-macro/Cargo.toml @@ -27,13 +27,6 @@ rustversion = "1" trybuild = "1" yew = { path = "../yew" } -[dev-dependencies.web-sys] -version = "0.3" -features = [ - "FocusEvent", - "MouseEvent", -] - [build-dependencies] [features] diff --git a/packages/yew-macro/tests/html_macro/element-fail.stderr b/packages/yew-macro/tests/html_macro/element-fail.stderr index a348d61e0f6..4ce990ed9e8 100644 --- a/packages/yew-macro/tests/html_macro/element-fail.stderr +++ b/packages/yew-macro/tests/html_macro/element-fail.stderr @@ -302,11 +302,11 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue as IntoPropValue>>> = note: required by `into_prop_value` -error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `{integer}` +error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}` --> $DIR/element-fail.rs:51:28 | 51 | html! { }; - | ^ expected an `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `{integer}` + | ^ expected an `Fn<(MouseEvent,)>` closure, found `{integer}` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs | @@ -319,16 +319,16 @@ error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` 197 | | } | |_- required by this bound in `yew::html::onclick::Wrapper::__macro_new` | - = help: the trait `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` is not implemented for `{integer}` - = note: required because of the requirements on the impl of `IntoEventCallback` for `{integer}` + = help: the trait `Fn<(MouseEvent,)>` is not implemented for `{integer}` + = note: required because of the requirements on the impl of `IntoEventCallback` for `{integer}` -error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `yew::Callback` +error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback` --> $DIR/element-fail.rs:52:29 | 52 | html! { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | expected an implementor of trait `IntoEventCallback` + | expected an implementor of trait `IntoEventCallback` | help: consider borrowing here: `&Callback::from(|a: String| ())` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs @@ -342,14 +342,14 @@ error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` 197 | | } | |_- required by this bound in `yew::html::onclick::Wrapper::__macro_new` | - = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied - = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` + = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied + = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` -error[E0277]: the trait bound `Option<{integer}>: IntoEventCallback` is not satisfied +error[E0277]: the trait bound `Option<{integer}>: IntoEventCallback` is not satisfied --> $DIR/element-fail.rs:53:29 | 53 | html! { }; - | ^^^^^^^ the trait `IntoEventCallback` is not implemented for `Option<{integer}>` + | ^^^^^^^ the trait `IntoEventCallback` is not implemented for `Option<{integer}>` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs | @@ -386,13 +386,13 @@ error[E0277]: the trait bound `Option: IntoPropValue as IntoPropValue>>> = note: required by `into_prop_value` -error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` closure, found `yew::Callback` +error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback` --> $DIR/element-fail.rs:58:29 | 58 | html! { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | expected an implementor of trait `IntoEventCallback` + | expected an implementor of trait `IntoEventCallback` | help: consider borrowing here: `&Callback::from(|a: String| ())` | ::: $WORKSPACE/packages/yew/src/html/listener/events.rs @@ -406,8 +406,8 @@ error[E0277]: expected a `Fn<(web_sys::features::gen_MouseEvent::MouseEvent,)>` 197 | | } | |_- required by this bound in `yew::html::onclick::Wrapper::__macro_new` | - = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied - = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` + = note: the trait bound `yew::Callback: IntoEventCallback` is not satisfied + = note: required because of the requirements on the impl of `IntoEventCallback` for `yew::Callback` error[E0277]: the trait bound `NotToString: IntoPropValue>>` is not satisfied --> $DIR/element-fail.rs:60:28 diff --git a/packages/yew-macro/tests/html_macro/html-element-pass.rs b/packages/yew-macro/tests/html_macro/html-element-pass.rs index bd9d50935a7..c5f4b3ff250 100644 --- a/packages/yew-macro/tests/html_macro/html-element-pass.rs +++ b/packages/yew-macro/tests/html_macro/html-element-pass.rs @@ -37,8 +37,8 @@ pub struct u8; pub struct usize; fn compile_pass() { - let onclick = <::yew::Callback<::web_sys::MouseEvent> as ::std::convert::From<_>>::from( - |_: ::web_sys::MouseEvent| (), + let onclick = <::yew::Callback<::yew::events::MouseEvent> as ::std::convert::From<_>>::from( + |_: ::yew::events::MouseEvent| (), ); let parent_ref = <::yew::NodeRef as ::std::default::Default>::default(); @@ -104,7 +104,7 @@ fn compile_pass() { as ::std::convert::From<_>>::from(|_| ()))} + onblur={::std::option::Option::Some(<::yew::Callback<::yew::events::FocusEvent> as ::std::convert::From<_>>::from(|_| ()))} /> }; diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index a9125da0e29..da318b9cff5 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -1,6 +1,5 @@ use crate::{service, Routable}; use std::marker::PhantomData; -use web_sys::MouseEvent; use yew::prelude::*; /// Props for [`Link`] diff --git a/packages/yew/src/functional/hooks/use_ref.rs b/packages/yew/src/functional/hooks/use_ref.rs index af9d42fd484..4ec1ee7cfff 100644 --- a/packages/yew/src/functional/hooks/use_ref.rs +++ b/packages/yew/src/functional/hooks/use_ref.rs @@ -10,7 +10,7 @@ use std::{cell::RefCell, rc::Rc}; /// # Example /// ```rust /// # use yew::prelude::*; -/// # use web_sys::{Event, HtmlInputElement}; +/// # use web_sys::HtmlInputElement; /// # use std::rc::Rc; /// # use std::cell::RefCell; /// # use std::ops::{Deref, DerefMut}; diff --git a/packages/yew/src/html/listener/mod.rs b/packages/yew/src/html/listener/mod.rs index df4d728e718..4882fa684b2 100644 --- a/packages/yew/src/html/listener/mod.rs +++ b/packages/yew/src/html/listener/mod.rs @@ -38,7 +38,7 @@ where /// /// ``` /// use yew::prelude::*; - /// use web_sys::{Event, HtmlTextAreaElement}; + /// use web_sys::HtmlTextAreaElement; /// # enum Msg { /// # Value(String), /// # } @@ -95,7 +95,7 @@ where /// /// ``` /// use yew::prelude::*; - /// use web_sys::{Event, HtmlInputElement}; + /// use web_sys::HtmlInputElement; /// # enum Msg { /// # Value(String), /// # } diff --git a/packages/yew/src/lib.rs b/packages/yew/src/lib.rs index a3adb667f82..33cec405859 100644 --- a/packages/yew/src/lib.rs +++ b/packages/yew/src/lib.rs @@ -270,6 +270,12 @@ pub mod events { pub use crate::html::TargetCast; pub use crate::virtual_dom::listeners::set_event_bubbling; + + #[doc(no_inline)] + pub use web_sys::{ + AnimationEvent, DragEvent, ErrorEvent, Event, FocusEvent, InputEvent, KeyboardEvent, + MouseEvent, PointerEvent, ProgressEvent, TouchEvent, TransitionEvent, UiEvent, WheelEvent, + }; } pub use crate::app_handle::AppHandle; diff --git a/website/docs/concepts/components/callbacks.md b/website/docs/concepts/components/callbacks.md index b32b200b664..9213be3bb3d 100644 --- a/website/docs/concepts/components/callbacks.md +++ b/website/docs/concepts/components/callbacks.md @@ -131,8 +131,7 @@ The function passed to `callback` must always take a parameter. For example, the If you need a callback that might not need to cause an update, use `batch_callback`. ```rust -use web_sys::KeyboardEvent; -use yew::{html, Component, Context, Html}; +use yew::{events::KeyboardEvent, html, Component, Context, Html}; enum Msg { Submit, diff --git a/website/docs/concepts/function-components/pre-defined-hooks.md b/website/docs/concepts/function-components/pre-defined-hooks.md index 5c2359fa2b4..c1d699c4c3a 100644 --- a/website/docs/concepts/function-components/pre-defined-hooks.md +++ b/website/docs/concepts/function-components/pre-defined-hooks.md @@ -48,8 +48,9 @@ If you need the component to be re-rendered on state change, consider using [`us ### Example ```rust -use web_sys::{Event, HtmlInputElement}; +use web_sys::HtmlInputElement; use yew::{ + events::Event, function_component, html, use_ref, use_state, Callback, TargetCast, }; diff --git a/website/docs/concepts/html/events.md b/website/docs/concepts/html/events.md index 7b3ebbbce2b..2d769802c47 100644 --- a/website/docs/concepts/html/events.md +++ b/website/docs/concepts/html/events.md @@ -188,8 +188,9 @@ wasm-bindgen = "0.2" ```rust //highlight-next-line use wasm_bindgen::JsCast; -use web_sys::{Event, EventTarget, HtmlInputElement}; +use web_sys::{EventTarget, HtmlInputElement}; use yew::{ + events::Event, html, Component, Context, Html, }; @@ -295,8 +296,9 @@ from events. but it works in a very similar way to `JsCast`. ```rust -use web_sys::{Event, HtmlInputElement}; +use web_sys::HtmlInputElement; use yew::{ + events::Event, html, // Need to import TargetCast //highlight-next-line @@ -515,8 +517,9 @@ yourself ([there is a more concise way thanks to `gloo`](#using-gloo-concise)). ```rust use wasm_bindgen::{prelude::Closure, JsCast}; -use web_sys::{Event, HtmlElement}; +use web_sys::HtmlElement; use yew::{ + events::Event, html, Component, Context, Html, NodeRef, }; @@ -620,8 +623,9 @@ gloo-events = "0.1" ``` ```rust -use web_sys::{Event, HtmlElement}; +use web_sys::HtmlElement; use yew::{ + events::Event, html, Component, Context, Html, NodeRef, };