Skip to main content

rustolio_utils/web_sys/
event.rs

1//
2// SPDX-License-Identifier: MPL-2.0
3//
4// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9//
10
11use wasm_bindgen::prelude::*;
12
13use crate::error::FromCustomError;
14
15pub trait EventExt {
16    fn target_<T: JsCast>(&self) -> crate::Result<T>;
17    fn current_target_<T: JsCast>(&self) -> crate::Result<T>;
18}
19
20impl EventExt for web_sys::Event {
21    #[track_caller]
22    fn current_target_<T: JsCast>(&self) -> crate::Result<T> {
23        Ok(self
24            .current_target()
25            .context("Event does not have a `current_target`")?
26            .unchecked_into::<T>())
27    }
28    #[track_caller]
29    fn target_<T: JsCast>(&self) -> crate::Result<T> {
30        Ok(self
31            .target()
32            .context("Event does not have a `target`")?
33            .unchecked_into::<T>())
34    }
35}