[yew-devel] [PATCH yew-widget-toolkit 2/3] widget: rtl switcher: refactor setting rtl direction

Dominik Csapak d.csapak at proxmox.com
Fri Sep 12 10:32:14 CEST 2025


we want to use that from the language selector too, so refactor this.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/widget/rtl_switcher.rs | 55 +++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 16 deletions(-)

diff --git a/src/widget/rtl_switcher.rs b/src/widget/rtl_switcher.rs
index 2400dd7..8c18bbd 100644
--- a/src/widget/rtl_switcher.rs
+++ b/src/widget/rtl_switcher.rs
@@ -1,8 +1,13 @@
+use anyhow::format_err;
+
 use pwt_macros::widget;
 use yew::{Classes, Component, Properties};
 
 use super::form::Checkbox;
-use crate::props::{FieldBuilder, WidgetBuilder};
+use crate::{
+    props::{FieldBuilder, WidgetBuilder},
+    state::TextDirection,
+};
 
 /// A checkbox to switch between Left-to-Right and Right-to-Left layouts
 #[widget(pwt=crate, comp=PwtRtlSwitcher, @input, @element)]
@@ -51,25 +56,23 @@ impl Component for PwtRtlSwitcher {
     fn update(&mut self, _ctx: &yew::Context<Self>, msg: Self::Message) -> bool {
         match msg {
             Msg::ToggleRtl => {
-                let elements = gloo_utils::document().get_elements_by_tag_name("html");
-                if let Some(html) = elements.get_with_index(0) {
-                    if self.rtl {
-                        if let Err(err) = html.remove_attribute("dir") {
-                            log::error!("could not remove dir attribute: {:?}", err);
-                            return false;
-                        }
-                        self.rtl = false;
-                    } else {
-                        if let Err(err) = html.set_attribute("dir", "rtl") {
-                            log::error!("could not set dir attribute: {:?}", err);
-                            return false;
-                        }
-                        self.rtl = true;
+                let direction = if self.rtl {
+                    TextDirection::Ltr
+                } else {
+                    TextDirection::Rtl
+                };
+                match set_text_direction(direction) {
+                    Err(err) => {
+                        log::error!("{err}");
+                        false
+                    }
+                    Ok(()) => {
+                        self.rtl = !self.rtl;
+                        true
                     }
                 }
             }
         }
-        true
     }
 
     fn create(_ctx: &yew::Context<Self>) -> Self {
@@ -94,3 +97,23 @@ impl Component for PwtRtlSwitcher {
             .into()
     }
 }
+
+/// Sets the global text direction on the root HTML element (if possible).
+/// Otherwise returns an error.
+pub fn set_text_direction(direction: TextDirection) -> Result<(), anyhow::Error> {
+    let elements = gloo_utils::document().get_elements_by_tag_name("html");
+    let html = elements
+        .get_with_index(0)
+        .ok_or_else(|| format_err!("no html element found"))?;
+    match direction {
+        TextDirection::Ltr => {
+            html.remove_attribute("dir")
+                .map_err(|err| format_err!("could not remove dir attribute: {err:?}"))?;
+        }
+        TextDirection::Rtl => {
+            html.set_attribute("dir", "rtl")
+                .map_err(|err| format_err!("could not set dir attribute: {err:?}"))?;
+        }
+    }
+    Ok(())
+}
-- 
2.47.3





More information about the yew-devel mailing list