Crates.io | yewlish-checkbox |
lib.rs | yewlish-checkbox |
version | 0.1.12 |
source | src |
created_at | 2024-09-01 20:43:07.581024 |
updated_at | 2024-12-02 10:23:40.214696 |
description | A Yew component that represents a checkbox. |
homepage | |
repository | https://github.com/patchwork-body/yewlish.git |
max_upload_size | |
id | 1359876 |
size | 52,107 |
Yewlish Checkbox is a customizable checkbox component for the Yew framework. It offers a flexible API to create accessible and interactive checkbox inputs, allowing developers to manage various states and behaviors seamlessly within their Yew applications.
checked
, unchecked
, and indeterminate
states.Add the crate to your Cargo.toml:
[dependencies]
yew = "^0.21"
yewlish-checkbox = "^0.1"
Create a simple checkbox with default props.
use yew::prelude::*;
use yewlish_checkbox::{Checkbox, CheckedState};
#[function_component(App)]
fn app() -> Html {
html! {
<Checkbox>
<CheckboxIndicator
shows_when={CheckboxState::Checked}
>
{"✔"}
</CheckboxIndicator>
</Checkbox>
}
}
fn main() {
yew::start_app::<App>();
}
Manage the checkbox state externally using the checked and on_checked_change props.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
let checked_state = use_state(|| CheckedState::Unchecked);
let on_checked_change = {
let checked_state = checked_state.clone();
Callback::from(move |new_state: CheckedState| {
checked_state.set(new_state);
})
};
html! {
<Checkbox
checked={(*checked_state).clone()}
on_checked_change={on_checked_change}
>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"✔"}
</CheckboxIndicator>
</Checkbox>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Use the indeterminate state for partially selected options.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
html! {
<Checkbox default_checked={CheckboxState::Indeterminate}>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"✔"}
</CheckboxIndicator>
<CheckboxIndicator show_when={CheckedState::Indeterminate}>
{"−"}
</CheckboxIndicator>
</Checkbox>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Customize how the checkbox is rendered using the render_as prop.
use yew::prelude::*;
use yewlish_checkbox::{Checkbox, CheckboxRenderAsProps, CheckedState};
#[function_component(App)]
fn app() -> Html {
let render_as = Callback::from(|props: CheckboxRenderAsProps| {
let is_checked = props.checked == CheckedState::Checked;
html! {
<label class={props.class.clone()}>
<input
type="checkbox"
checked={is_checked}
onclick={props.toggle.clone()}
disabled={props.disabled}
required={props.required}
name={props.name.clone()}
value={props.value.clone()}
/>
{ for props.children.iter() }
</label>
}
});
html! {
<Checkbox render_as={render_as}>
{ "Custom Checkbox" }
</Checkbox>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Apply custom styles using the class prop.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
html! {
<Checkbox class="custom-checkbox">
<CheckboxIndicator
show_when={CheckedState::Checked}
class="custom-indicator"
>
{"✔"}
</CheckboxIndicator>
</Checkbox>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Define styles in your CSS:
.custom-checkbox {
display: flex;
align-items: center;
}
.custom-indicator {
color: green;
}
Disable interaction using the disabled or readonly props.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
html! {
<>
<Checkbox disabled={true}>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"Disabled Checkbox"}
</CheckboxIndicator>
</Checkbox>
<Checkbox default_checked={CheckedState::Checked} readonly={true}>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"Read-Only Checkbox"}
</CheckboxIndicator>
</Checkbox>
</>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Integrate with forms using the name and value props.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
html! {
<form>
<label for={"terms_agreement"}>
{"I agree to the terms"}
</label>
<Checkbox id="terms_agreement" name="agree_terms" value="yes">
<CheckboxIndicator show_when={CheckedState::Checked}>
{"✔"}
</CheckboxIndicator>
</Checkbox>
<button type="submit">{"Submit"}</button>
</form>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Perform actions when the checkbox state changes.
use yew::prelude::*;
use yewlish_checkbox::*;
#[function_component(App)]
fn app() -> Html {
let on_checked_change = Callback::from(|new_state: CheckedState| {
if new_state == CheckedState::Checked {
log::debug!("Checkbox is checked");
}
});
html! {
// You don't need to provide a value,
// this checkbox will still be uncontrollable
<Checkbox on_checked_change={on_checked_change}>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"✔"}
</CheckboxIndicator>
</Checkbox>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
The yewlish-checkbox component is designed to satisfy the W3C ARIA accessibility pattern guide for checkboxes. It includes ARIA attributes to ensure it is accessible to all users. The role
attribute is set to checkbox
, and the aria-checked
attribute reflects the current state ("true", "false", or "mixed").
To ensure full accessibility:
CheckboxIndicator
to provide visual feedback.disabled
and required
props as needed.use yew::prelude::*;
use yewlish_checkbox::*;
use yewlish_attr_passer::*;
#[function_component(App)]
fn app() -> Html {
html! {
<AttrPasser name="checkbox" ..attributify {
"aria-label" => "Check me"
}>
<Checkbox on_checked_change={on_checked_change}>
<CheckboxIndicator show_when={CheckedState::Checked}>
{"✔"}
</CheckboxIndicator>
</Checkbox>
</AttrPasser>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Property | Type | Default | Description |
---|---|---|---|
children | ChildrenWithProps |
None | Children elements, typically one or more CheckboxIndicators. |
ref | NodeRef | None | Reference to the checkbox DOM node. |
id | Option |
None | The ID of the checkbox element. |
class | Option |
None | CSS class for styling the checkbox. |
default_checked | Option |
Unchecked | The default checked state. |
checked | Option |
None | Controlled checked state. |
disabled | bool | false | Whether the checkbox is disabled. |
on_checked_change | Callback |
None | Callback when the checked state changes. |
required | bool | false | Whether the checkbox is required. |
name | Option |
None | The name attribute of the checkbox. |
value | Option |
None | The value attribute of the checkbox. |
readonly | bool | false | Whether the checkbox is read-only. |
render_as | Option<Callback<CheckboxRenderAsProps, Html>> | None | Custom render function for the checkbox. |
Property | Type | Default | Description |
---|---|---|---|
ref | NodeRef | None | Reference to the indicator DOM node. |
class | Option |
None | CSS class for styling the indicator. |
children | Children | None | Children elements to display inside the indicator. |
show_when | CheckedState | Checked | The state when the indicator is visible. |
render_as | Option<Callback<CheckboxIndicatorRenderAsProps, Html>> | None | Custom render function for the indicator. |
The yewlish-checkbox crate includes a suite of tests to ensure reliability. Tests can be run using:
wasm-pack test --firefox --headless
Ensure you have wasm-pack and a compatible browser installed.
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.
Yewlish Checkbox is dual-licensed under the MIT and Apache 2.0 licenses. You may choose to use either license at your option.
This project is inspired by the Radix UI library, known for its robust and accessible UI components. Special thanks to the Radix community for their excellent work and contributions, which have greatly influenced the development of Yewlish Checkbox.
For any questions or inquiries, feel free to reach out to the author:
Kirill Korotkov Email: personal.gugfug@gmail.com