#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
Dialog,
Combobox,
Listbox,
Option,
Menu,
MenuItem,
Checkbox,
Radio,
Switch,
Button,
Textbox,
Grid,
Row,
Cell,
Tree,
TreeItem,
TabPanel,
Tab,
TabList,
Application,
Region,
Complementary,
Navigation,
Toolbar,
Tooltip,
Status,
Alert,
Progressbar,
Slider,
Scrollbar,
Group,
Presentation,
}
impl Role {
pub fn as_str(&self) -> &'static str {
match self {
Role::Dialog => "dialog",
Role::Combobox => "combobox",
Role::Listbox => "listbox",
Role::Option => "option",
Role::Menu => "menu",
Role::MenuItem => "menuitem",
Role::Checkbox => "checkbox",
Role::Radio => "radio",
Role::Switch => "switch",
Role::Button => "button",
Role::Textbox => "textbox",
Role::Grid => "grid",
Role::Row => "row",
Role::Cell => "cell",
Role::Tree => "tree",
Role::TreeItem => "treeitem",
Role::TabPanel => "tabpanel",
Role::Tab => "tab",
Role::TabList => "tablist",
Role::Application => "application",
Role::Region => "region",
Role::Complementary => "complementary",
Role::Navigation => "navigation",
Role::Toolbar => "toolbar",
Role::Tooltip => "tooltip",
Role::Status => "status",
Role::Alert => "alert",
Role::Progressbar => "progressbar",
Role::Slider => "slider",
Role::Scrollbar => "scrollbar",
Role::Group => "group",
Role::Presentation => "presentation",
}
}
}
pub struct AriaAttrs {
role: Option<Role>,
aria_label: Option<String>,
aria_labelledby: Option<String>,
aria_describedby: Option<String>,
aria_hidden: Option<bool>,
aria_disabled: Option<bool>,
aria_readonly: Option<bool>,
aria_required: Option<bool>,
aria_expanded: Option<bool>,
aria_checked: Option<bool>,
aria_selected: Option<bool>,
aria_activedescendant: Option<String>,
aria_autocomplete: Option<String>,
aria_controls: Option<String>,
aria_owns: Option<String>,
aria_haspopup: Option<String>,
aria_modal: Option<bool>,
aria_orientation: Option<String>,
aria_valuemin: Option<f64>,
aria_valuemax: Option<f64>,
aria_valuenow: Option<f64>,
aria_valuetext: Option<String>,
aria_live: Option<String>,
aria_level: Option<i32>,
tab_index: Option<i32>,
}
impl AriaAttrs {
pub fn new() -> Self {
Self {
role: None,
aria_label: None,
aria_labelledby: None,
aria_describedby: None,
aria_hidden: None,
aria_disabled: None,
aria_readonly: None,
aria_required: None,
aria_expanded: None,
aria_checked: None,
aria_selected: None,
aria_activedescendant: None,
aria_autocomplete: None,
aria_controls: None,
aria_owns: None,
aria_haspopup: None,
aria_modal: None,
aria_orientation: None,
aria_valuemin: None,
aria_valuemax: None,
aria_valuenow: None,
aria_valuetext: None,
aria_live: None,
aria_level: None,
tab_index: None,
}
}
pub fn role(mut self, role: Role) -> Self {
self.role = Some(role);
self
}
pub fn aria_label(mut self, label: impl Into<String>) -> Self {
self.aria_label = Some(label.into());
self
}
pub fn aria_labelledby(mut self, id: impl Into<String>) -> Self {
self.aria_labelledby = Some(id.into());
self
}
pub fn aria_describedby(mut self, id: impl Into<String>) -> Self {
self.aria_describedby = Some(id.into());
self
}
pub fn aria_hidden(mut self, hidden: bool) -> Self {
self.aria_hidden = Some(hidden);
self
}
pub fn aria_disabled(mut self, disabled: bool) -> Self {
self.aria_disabled = Some(disabled);
self
}
pub fn aria_readonly(mut self, readonly: bool) -> Self {
self.aria_readonly = Some(readonly);
self
}
pub fn aria_required(mut self, required: bool) -> Self {
self.aria_required = Some(required);
self
}
pub fn aria_expanded(mut self, expanded: bool) -> Self {
self.aria_expanded = Some(expanded);
self
}
pub fn aria_checked(mut self, checked: bool) -> Self {
self.aria_checked = Some(checked);
self
}
pub fn aria_selected(mut self, selected: bool) -> Self {
self.aria_selected = Some(selected);
self
}
pub fn aria_activedescendant(mut self, id: impl Into<String>) -> Self {
self.aria_activedescendant = Some(id.into());
self
}
pub fn aria_autocomplete(mut self, value: &str) -> Self {
self.aria_autocomplete = Some(value.to_string());
self
}
pub fn aria_controls(mut self, id: impl Into<String>) -> Self {
self.aria_controls = Some(id.into());
self
}
pub fn aria_owns(mut self, id: impl Into<String>) -> Self {
self.aria_owns = Some(id.into());
self
}
pub fn aria_haspopup(mut self, value: &str) -> Self {
self.aria_haspopup = Some(value.to_string());
self
}
pub fn aria_modal(mut self, modal: bool) -> Self {
self.aria_modal = Some(modal);
self
}
pub fn aria_orientation(mut self, orientation: &str) -> Self {
self.aria_orientation = Some(orientation.to_string());
self
}
pub fn aria_valuemin(mut self, value: f64) -> Self {
self.aria_valuemin = Some(value);
self
}
pub fn aria_valuemax(mut self, value: f64) -> Self {
self.aria_valuemax = Some(value);
self
}
pub fn aria_valuenow(mut self, value: f64) -> Self {
self.aria_valuenow = Some(value);
self
}
pub fn aria_valuetext(mut self, text: impl Into<String>) -> Self {
self.aria_valuetext = Some(text.into());
self
}
pub fn aria_live(mut self, value: &str) -> Self {
self.aria_live = Some(value.to_string());
self
}
pub fn aria_level(mut self, level: i32) -> Self {
self.aria_level = Some(level);
self
}
pub fn tab_index(mut self, index: i32) -> Self {
self.tab_index = Some(index);
self
}
pub fn build(self) -> Vec<(String, String)> {
let mut attrs = Vec::new();
if let Some(role) = self.role {
attrs.push(("role".to_string(), role.as_str().to_string()));
}
if let Some(label) = self.aria_label {
attrs.push(("aria-label".to_string(), label));
}
if let Some(id) = self.aria_labelledby {
attrs.push(("aria-labelledby".to_string(), id));
}
if let Some(id) = self.aria_describedby {
attrs.push(("aria-describedby".to_string(), id));
}
if let Some(hidden) = self.aria_hidden {
attrs.push(("aria-hidden".to_string(), hidden.to_string()));
}
if let Some(disabled) = self.aria_disabled {
attrs.push(("aria-disabled".to_string(), disabled.to_string()));
}
if let Some(readonly) = self.aria_readonly {
attrs.push(("aria-readonly".to_string(), readonly.to_string()));
}
if let Some(required) = self.aria_required {
attrs.push(("aria-required".to_string(), required.to_string()));
}
if let Some(expanded) = self.aria_expanded {
attrs.push(("aria-expanded".to_string(), expanded.to_string()));
}
if let Some(checked) = self.aria_checked {
attrs.push(("aria-checked".to_string(), checked.to_string()));
}
if let Some(selected) = self.aria_selected {
attrs.push(("aria-selected".to_string(), selected.to_string()));
}
if let Some(id) = self.aria_activedescendant {
attrs.push(("aria-activedescendant".to_string(), id));
}
if let Some(value) = self.aria_autocomplete {
attrs.push(("aria-autocomplete".to_string(), value));
}
if let Some(id) = self.aria_controls {
attrs.push(("aria-controls".to_string(), id));
}
if let Some(id) = self.aria_owns {
attrs.push(("aria-owns".to_string(), id));
}
if let Some(value) = self.aria_haspopup {
attrs.push(("aria-haspopup".to_string(), value));
}
if let Some(modal) = self.aria_modal {
attrs.push(("aria-modal".to_string(), modal.to_string()));
}
if let Some(orientation) = self.aria_orientation {
attrs.push(("aria-orientation".to_string(), orientation));
}
if let Some(value) = self.aria_valuemin {
attrs.push(("aria-valuemin".to_string(), value.to_string()));
}
if let Some(value) = self.aria_valuemax {
attrs.push(("aria-valuemax".to_string(), value.to_string()));
}
if let Some(value) = self.aria_valuenow {
attrs.push(("aria-valuenow".to_string(), value.to_string()));
}
if let Some(text) = self.aria_valuetext {
attrs.push(("aria-valuetext".to_string(), text));
}
if let Some(value) = self.aria_live {
attrs.push(("aria-live".to_string(), value));
}
if let Some(level) = self.aria_level {
attrs.push(("aria-level".to_string(), level.to_string()));
}
if let Some(index) = self.tab_index {
attrs.push(("tabindex".to_string(), index.to_string()));
}
attrs
}
}
impl Default for AriaAttrs {
fn default() -> Self {
Self::new()
}
}
pub fn aria() -> AriaAttrs {
AriaAttrs::new()
}
pub mod dialog {
pub fn attrs(labelledby: Option<&str>, describedby: Option<&str>) -> Vec<(String, String)> {
let mut attrs = vec![
("role".to_string(), "dialog".to_string()),
("aria-modal".to_string(), "true".to_string()),
];
if let Some(id) = labelledby {
attrs.push(("aria-labelledby".to_string(), id.to_string()));
}
if let Some(id) = describedby {
attrs.push(("aria-describedby".to_string(), id.to_string()));
}
attrs
}
}
pub mod combobox {
pub fn attrs(
expanded: bool,
activedescendant: Option<&str>,
controls: Option<&str>,
) -> Vec<(String, String)> {
let mut attrs = vec![
("role".to_string(), "combobox".to_string()),
("aria-expanded".to_string(), expanded.to_string()),
("aria-haspopup".to_string(), "listbox".to_string()),
("aria-autocomplete".to_string(), "none".to_string()),
];
if let Some(id) = activedescendant {
attrs.push(("aria-activedescendant".to_string(), id.to_string()));
}
if let Some(id) = controls {
attrs.push(("aria-controls".to_string(), id.to_string()));
}
attrs
}
}
pub mod option {
pub fn attrs(selected: bool, disabled: bool) -> Vec<(String, String)> {
vec![
("role".to_string(), "option".to_string()),
("aria-selected".to_string(), selected.to_string()),
("aria-disabled".to_string(), disabled.to_string()),
]
}
}
pub mod menuitem {
pub fn attrs(disabled: bool) -> Vec<(String, String)> {
vec![
("role".to_string(), "menuitem".to_string()),
("aria-disabled".to_string(), disabled.to_string()),
]
}
}
pub trait RoleExt {
fn role(self, _role: Role) -> Self
where
Self: Sized,
{
self
}
}
impl<E> RoleExt for E {}