Macro impl_tabs

Source
macro_rules! impl_tabs {
    ($($element:ident),*) => { ... };
}
Expand description

UI macro: implements methods for an interface with tabs.

This macro implements a method hide_all_tab_panels with signature fn hide_all_tab_panels(&self) -> Result<(), JsValue> that sets, for each element given in the macro call, the corresponding element_panel to hidden by adding hidden to its class list, and the corresponding element_tab to deselected by seting its aria-selected attribute to false. For each element, it implements an element_tab_onclick method of signature fn element_tab_onclick(&self) -> Closure<dyn Fn()>. The closure returned by this method is a suitable onclick callback for the tab element. It calls the hide_all_tab_panels method and then selects the tab corresponding to element by removing the hidden class from the element_panel and setting the arial-selected attribute to true in the element_tab.

ยงExample

use maia_wasm::{impl_tabs, set_on, ui_elements};
use std::rc::Rc;
use wasm_bindgen::JsValue;
use web_sys::{Document, HtmlButtonElement, HtmlElement, Window};

#[derive(Clone)]
struct Ui {
    window: Rc<Window>,
    elements: Elements,
}

ui_elements! {
    a_tab: HtmlButtonElement => Rc<HtmlButtonElement>,
    a_panel: HtmlElement => Rc<HtmlElement>,
    b_tab: HtmlButtonElement => Rc<HtmlButtonElement>,
    b_panel: HtmlElement => Rc<HtmlElement>,
}

impl Ui {
    fn new(window: Rc<Window>, document: &Document) -> Result<Ui, JsValue> {
        let elements = Elements::new(document)?;
        let ui = Ui { window, elements };
        ui.set_callbacks();
        Ok(ui)
    }

    fn set_callbacks(&self) -> Result<(), JsValue> {
        set_on!(click, self, a_tab, b_tab);
        Ok(())
    }

    impl_tabs!(a, b);
}