| 1 | use topcoat::{ |
| 2 | Result, |
| 3 | context::Cx, |
| 4 | icon::{IconData, icon, iconify::iconify_icon}, |
| 5 | view::{Attributes, View, attributes, class, component, view}, |
| 6 | }; |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | const SELECT: &str = "h-9 w-full appearance-none items-center rounded-lg border border-border \ |
| 15 | bg-background pr-8 pl-3 text-left text-sm shadow-xs transition-colors outline-none \ |
| 16 | focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 \ |
| 17 | focus-visible:ring-offset-background disabled:pointer-events-none"; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | const PICKER: &str = "[&::picker(select)]:[appearance:base-select] \ |
| 31 | [&::picker(select)]:mt-1 [&::picker(select)]:rounded-lg \ |
| 32 | [&::picker(select)]:border [&::picker(select)]:border-border \ |
| 33 | [&::picker(select)]:bg-background [&::picker(select)]:p-1 \ |
| 34 | [&::picker(select)]:text-foreground [&::picker(select)]:shadow-sm \ |
| 35 | [&::picker-icon]:hidden \ |
| 36 | [&_option]:flex [&_option]:items-center [&_option]:gap-2 [&_option]:rounded-md \ |
| 37 | [&_option]:px-2 [&_option]:py-1.5 [&_option]:text-sm [&_option]:outline-none \ |
| 38 | [&_option:hover]:bg-foreground/5 [&_option:focus]:bg-foreground/5 \ |
| 39 | [&_option:checked]:font-medium \ |
| 40 | [&_option::checkmark]:order-1 [&_option::checkmark]:ml-auto \ |
| 41 | [&_option::checkmark]:size-4 [&_option::checkmark]:shrink-0 \ |
| 42 | [&_option::checkmark]:content-[''] [&_option::checkmark]:bg-muted-foreground \ |
| 43 | [&_option::checkmark]:[mask-size:100%_100%] \ |
| 44 | [&_option::checkmark]:[mask-image:var(--select-checkmark)]"; |
| 45 | |
| 46 | |
| 47 | const CHECKMARK: IconData = iconify_icon!("feather:check"); |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | fn checkmark_style(cx: &Cx) -> String { |
| 55 | let svg = format!( |
| 56 | r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{}">{}</svg>"#, |
| 57 | CHECKMARK.view_box(), |
| 58 | CHECKMARK.into_body().render(cx), |
| 59 | ); |
| 60 | let mut style = String::from(r#"--select-checkmark: url("data:image/svg+xml,"#); |
| 61 | |
| 62 | |
| 63 | for char in svg.chars() { |
| 64 | match char { |
| 65 | '%' => style.push_str("%25"), |
| 66 | '"' => style.push_str("%22"), |
| 67 | '#' => style.push_str("%23"), |
| 68 | _ => style.push(char), |
| 69 | } |
| 70 | } |
| 71 | style.push_str(r#"")"#); |
| 72 | style |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | #[component] |
| 98 | pub async fn select(cx: &Cx, #[default] mut attrs: Attributes, #[default] child: View) -> Result { |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | view! { |
| 105 | <span |
| 106 | class=(class!( |
| 107 | "relative block has-[:disabled]:opacity-50 \ |
| 108 | [&>select]:[appearance:base-select] \ |
| 109 | [&:has(select:open)>svg]:rotate-180", |
| 110 | attrs.remove("class"), |
| 111 | )) |
| 112 | style=(checkmark_style(cx)) |
| 113 | > |
| 114 | <select class=(class!(SELECT, PICKER)) (attrs)>(child)</select> |
| 115 | icon( |
| 116 | data: iconify_icon!("feather:chevron-down"), |
| 117 | attrs: attributes! { |
| 118 | class="pointer-events-none absolute top-1/2 right-3 size-4 \ |
| 119 | -translate-y-1/2 text-muted-foreground transition-transform" |
| 120 | } |
| 121 | ) |
| 122 | </span> |
| 123 | } |
| 124 | } |