| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
#![forbid(unsafe_code)] |
| 195 |
|
| 196 |
pub mod figure; |
| 197 |
pub mod form; |
| 198 |
pub mod list; |
| 199 |
pub mod meter; |
| 200 |
pub mod placeholder; |
| 201 |
|
| 202 |
use crate::list::part_class; |
| 203 |
use makeover_geometry::{Density, SizeClass}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
pub use makeover_geometry::{CSS_LAYER, in_css_layer}; |
| 210 |
use makeover_layout::{Bevel, Depth, Fill, Intent, RowPart, Selector, State, Token, Tone}; |
| 211 |
use makeover_touch::Affordance; |
| 212 |
use std::fmt::Write as _; |
| 213 |
|
| 214 |
|
| 215 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 216 |
pub struct Emit { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
pub border_width: &'static str, |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
pub focus_width: &'static str, |
| 236 |
|
| 237 |
pub class_prefix: &'static str, |
| 238 |
} |
| 239 |
|
| 240 |
impl Default for Emit { |
| 241 |
fn default() -> Self { |
| 242 |
Self { |
| 243 |
border_width: "1px", |
| 244 |
focus_width: "2px", |
| 245 |
class_prefix: "", |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
#[must_use] |
| 252 |
pub fn bevel_var(bevel: Bevel) -> &'static str { |
| 253 |
match bevel { |
| 254 |
Bevel::Raised => "--bevel-raised", |
| 255 |
Bevel::Inset => "--bevel-inset", |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#[must_use] |
| 265 |
pub fn fill_var(fill: Fill) -> String { |
| 266 |
match fill { |
| 267 |
Fill::Well => format!("var(--{}, var(--{}))", fill.token(), Fill::Page.token()), |
| 268 |
other => format!("var(--{})", other.token()), |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
#[must_use] |
| 279 |
pub fn bevel_shadow(bevel: Bevel, opts: &Emit) -> String { |
| 280 |
let (top_left, bottom_right) = bevel.edges(); |
| 281 |
let w = opts.border_width; |
| 282 |
format!( |
| 283 |
"inset {w} {w} 0 var(--{}), inset -{w} -{w} 0 var(--{})", |
| 284 |
top_left.token(), |
| 285 |
bottom_right.token() |
| 286 |
) |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#[must_use] |
| 295 |
pub fn bevel_properties(opts: &Emit) -> String { |
| 296 |
let mut css = String::new(); |
| 297 |
for bevel in [Bevel::Raised, Bevel::Inset] { |
| 298 |
let _ = writeln!( |
| 299 |
css, |
| 300 |
" {}: {};", |
| 301 |
bevel_var(bevel), |
| 302 |
bevel_shadow(bevel, opts) |
| 303 |
); |
| 304 |
} |
| 305 |
css.push_str(ELEVATION_PROPERTY); |
| 306 |
css |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
const ELEVATION_PROPERTY: &str = |
| 323 |
" --elevation-overlay: 0 2px 4px var(--elevation), 0 8px 24px var(--elevation);\n"; |
| 324 |
|
| 325 |
|
| 326 |
#[must_use] |
| 327 |
pub fn depth_class(depth: Depth, opts: &Emit) -> Option<String> { |
| 328 |
let name = match depth { |
| 329 |
Depth::Flat => return None, |
| 330 |
Depth::Raised => "raised", |
| 331 |
Depth::Well => "well", |
| 332 |
Depth::Sunken => "sunken", |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
_ => return None, |
| 338 |
}; |
| 339 |
Some(format!("{}{name}", opts.class_prefix)) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
fn class(name: &str, opts: &Emit) -> String { |
| 344 |
format!("{}{name}", opts.class_prefix) |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
#[must_use] |
| 360 |
pub fn depth_declarations(depth: Depth) -> String { |
| 361 |
let mut css = String::new(); |
| 362 |
if let Some(fill) = depth.fill() { |
| 363 |
let _ = writeln!(css, " background: {};", fill_var(fill)); |
| 364 |
} |
| 365 |
if let Some(bevel) = depth.bevel() { |
| 366 |
let _ = writeln!(css, " box-shadow: var({});", bevel_var(bevel)); |
| 367 |
} |
| 368 |
css |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
#[must_use] |
| 374 |
pub fn depth_rule(selector: &str, depth: Depth) -> String { |
| 375 |
let body = depth_declarations(depth); |
| 376 |
if body.is_empty() { |
| 377 |
return String::new(); |
| 378 |
} |
| 379 |
format!(".{selector} {{\n{body}}}\n") |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
fn hover_condition() -> Option<&'static str> { |
| 396 |
if Affordance::Hover.available(Density::Touch, SizeClass::Compact) { |
| 397 |
|
| 398 |
|
| 399 |
None |
| 400 |
} else { |
| 401 |
Some(Density::Pointer.media_condition()) |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
fn gated(condition: Option<&str>, rule: &str) -> String { |
| 407 |
let Some(condition) = condition else { |
| 408 |
return rule.to_string(); |
| 409 |
}; |
| 410 |
let mut css = format!("@media {condition} {{\n"); |
| 411 |
for line in rule.lines() { |
| 412 |
|
| 413 |
|
| 414 |
if line.is_empty() { |
| 415 |
css.push('\n'); |
| 416 |
} else { |
| 417 |
let _ = writeln!(css, " {line}"); |
| 418 |
} |
| 419 |
} |
| 420 |
css.push_str("}\n"); |
| 421 |
css |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
#[must_use] |
| 442 |
pub fn focus_rule(selector: &str, depth: Depth, opts: &Emit) -> String { |
| 443 |
let w = opts.focus_width; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
let offset = match depth.bevel() { |
| 449 |
|
| 450 |
Some(Bevel::Inset) => format!("calc(-1 * {w})"), |
| 451 |
|
| 452 |
_ => w.to_string(), |
| 453 |
}; |
| 454 |
format!( |
| 455 |
".{selector}:focus-visible {{\n outline: {w} solid var(--{});\n outline-offset: {offset};\n}}\n", |
| 456 |
State::Focus.token() |
| 457 |
) |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
#[must_use] |
| 476 |
pub fn disabled_rule(selector: &str, depth: Depth) -> String { |
| 477 |
format!( |
| 478 |
".{selector}:disabled,\n.{selector}[aria-disabled=\"true\"] {{\n{} color: var(--{});\n cursor: not-allowed;\n}}\n", |
| 479 |
depth_declarations(depth), |
| 480 |
State::Disabled.token() |
| 481 |
) |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
#[must_use] |
| 513 |
pub fn interactive_rules(selector: &str, depth: Depth, opts: &Emit) -> String { |
| 514 |
let mut css = gated( |
| 515 |
hover_condition(), |
| 516 |
&format!(".{selector}:hover {{\n background: var(--hover-surface);\n}}\n"), |
| 517 |
); |
| 518 |
css.push_str(&depth_rule( |
| 519 |
&format!("{selector}:active"), |
| 520 |
Depth::Raised.pressed(), |
| 521 |
)); |
| 522 |
css.push_str(&focus_rule(selector, depth, opts)); |
| 523 |
css.push_str(&disabled_rule(selector, depth)); |
| 524 |
css |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
#[must_use] |
| 543 |
pub fn depth_rules(opts: &Emit) -> String { |
| 544 |
let mut css = String::new(); |
| 545 |
for depth in [Depth::Raised, Depth::Well] { |
| 546 |
let Some(class) = depth_class(depth, opts) else { |
| 547 |
continue; |
| 548 |
}; |
| 549 |
css.push_str(&depth_rule(&class, depth)); |
| 550 |
} |
| 551 |
css |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
fn surface_rules(opts: &Emit) -> String { |
| 563 |
let mut css = String::new(); |
| 564 |
for name in ["button", "card"] { |
| 565 |
let c = class(name, opts); |
| 566 |
css.push_str(&depth_rule(&c, Depth::Raised)); |
| 567 |
css.push_str(&interactive_rules(&c, Depth::Raised, opts)); |
| 568 |
} |
| 569 |
|
| 570 |
let field = class("field", opts); |
| 571 |
css.push_str(&depth_rule(&field, Depth::Well)); |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
css.push_str(&focus_rule(&field, Depth::Well, opts)); |
| 579 |
css.push_str(&disabled_rule(&field, Depth::Well)); |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
let _ = writeln!( |
| 592 |
css, |
| 593 |
".{field}[aria-invalid=\"true\"] {{\n box-shadow: var({}), 0 0 0 {} var(--danger);\n}}", |
| 594 |
bevel_var(Bevel::Inset), |
| 595 |
opts.border_width |
| 596 |
); |
| 597 |
css |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
fn token_rules(opts: &Emit) -> String { |
| 612 |
let mut css = String::new(); |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
let badge = class("badge", opts); |
| 617 |
let _ = writeln!( |
| 618 |
css, |
| 619 |
".{badge} {{\n color: var(--{});\n}}", |
| 620 |
Tone::Neutral.token() |
| 621 |
); |
| 622 |
for tone in [Tone::Info, Tone::Success, Tone::Warning, Tone::Danger] { |
| 623 |
let _ = writeln!( |
| 624 |
css, |
| 625 |
".{badge}[data-tone=\"{0}\"] {{\n color: var(--{0});\n}}", |
| 626 |
tone.token() |
| 627 |
); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
let chip = class("chip", opts); |
| 634 |
let unlatched = Token::Chip { removable: false }; |
| 635 |
css.push_str(&depth_rule(&chip, unlatched.depth(false))); |
| 636 |
css.push_str(&interactive_rules(&chip, unlatched.depth(false), opts)); |
| 637 |
css.push_str(&depth_rule( |
| 638 |
&format!("{chip}.latched"), |
| 639 |
unlatched.depth(true), |
| 640 |
)); |
| 641 |
css |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
fn selector_rules(opts: &Emit) -> String { |
| 659 |
let mut css = String::new(); |
| 660 |
for (selector, name) in [ |
| 661 |
(Selector::Tabs, "tab"), |
| 662 |
(Selector::Segmented, "segment"), |
| 663 |
(Selector::Toggle, "toggle"), |
| 664 |
] { |
| 665 |
let c = class(name, opts); |
| 666 |
css.push_str(&depth_rule(&c, selector.unchosen())); |
| 667 |
css.push_str(&interactive_rules(&c, selector.unchosen(), opts)); |
| 668 |
css.push_str(&depth_rule(&format!("{c}.chosen"), selector.chosen())); |
| 669 |
} |
| 670 |
css |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
fn row_rules(opts: &Emit) -> String { |
| 681 |
let mut css = String::new(); |
| 682 |
let row = class("row", opts); |
| 683 |
for part in [ |
| 684 |
RowPart::Primary, |
| 685 |
RowPart::Secondary, |
| 686 |
RowPart::Meta, |
| 687 |
RowPart::Actions, |
| 688 |
RowPart::Tokens, |
| 689 |
RowPart::Proportion, |
| 690 |
] { |
| 691 |
let c = class(part_class(part), opts); |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if !matches!( |
| 700 |
part, |
| 701 |
RowPart::Actions | RowPart::Tokens | RowPart::Proportion |
| 702 |
) { |
| 703 |
let _ = writeln!(css, ".{c} {{\n color: var(--{});\n}}", part.intent()); |
| 704 |
} |
| 705 |
|
| 706 |
if part.revealed_on_hover() { |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
css.push_str(&gated( |
| 731 |
hover_condition(), |
| 732 |
&format!(".{c} {{\n opacity: 0;\n pointer-events: none;\n}}\n"), |
| 733 |
)); |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
let revealed = " opacity: 1;\n pointer-events: auto;\n"; |
| 746 |
css.push_str(&gated( |
| 747 |
hover_condition(), |
| 748 |
&format!(".{row}:hover .{c} {{\n{revealed}}}\n"), |
| 749 |
)); |
| 750 |
let _ = write!(css, ".{row}:focus-within .{c} {{\n{revealed}}}\n"); |
| 751 |
} |
| 752 |
} |
| 753 |
css |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
fn progress_rules(opts: &Emit) -> String { |
| 771 |
let progress = class("progress", opts); |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
let fill = class("progress-fill", opts); |
| 777 |
let mut css = depth_rule(&progress, Depth::Well); |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
let _ = writeln!( |
| 784 |
css, |
| 785 |
".{progress} > .{fill} {{\n background: var(--action);\n}}" |
| 786 |
); |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
for tone in [Tone::Info, Tone::Success, Tone::Warning, Tone::Danger] { |
| 793 |
let _ = writeln!( |
| 794 |
css, |
| 795 |
".{progress} > .{fill}[data-tone=\"{0}\"] {{\n background: var(--{0});\n}}", |
| 796 |
tone.token() |
| 797 |
); |
| 798 |
} |
| 799 |
css |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
fn figure_rules(opts: &Emit) -> String { |
| 816 |
let figure = class("figure", opts); |
| 817 |
let value = class("figure-value", opts); |
| 818 |
let caption = class("figure-caption", opts); |
| 819 |
let mut css = String::new(); |
| 820 |
|
| 821 |
let _ = writeln!( |
| 822 |
css, |
| 823 |
".{figure} > .{value} {{\n color: var(--{});\n}}", |
| 824 |
Tone::Neutral.token() |
| 825 |
); |
| 826 |
let _ = writeln!( |
| 827 |
css, |
| 828 |
".{figure} > .{caption} {{\n color: var(--content-muted);\n}}" |
| 829 |
); |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
for tone in [Tone::Info, Tone::Success, Tone::Warning, Tone::Danger] { |
| 834 |
let _ = writeln!( |
| 835 |
css, |
| 836 |
".{figure}[data-tone=\"{0}\"] > .{value} {{\n color: var(--{0});\n}}", |
| 837 |
tone.token() |
| 838 |
); |
| 839 |
} |
| 840 |
css |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
fn state_rules(opts: &Emit) -> String { |
| 857 |
let placeholder = class("placeholder", opts); |
| 858 |
let text = class("placeholder-text", opts); |
| 859 |
let heading = class("table-heading", opts); |
| 860 |
let mut css = String::new(); |
| 861 |
|
| 862 |
let _ = writeln!( |
| 863 |
css, |
| 864 |
".{placeholder} > .{text} {{\n color: var(--content-muted);\n}}" |
| 865 |
); |
| 866 |
|
| 867 |
|
| 868 |
let _ = writeln!( |
| 869 |
css, |
| 870 |
".{placeholder}[data-tone=\"{0}\"] > .{text} {{\n color: var(--{0});\n}}", |
| 871 |
Tone::Danger.token() |
| 872 |
); |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
let _ = writeln!( |
| 877 |
css, |
| 878 |
".{heading}[data-sortable] {{\n cursor: pointer;\n}}" |
| 879 |
); |
| 880 |
for (direction, caret) in [("ascending", "\\2191"), ("descending", "\\2193")] { |
| 881 |
let _ = writeln!( |
| 882 |
css, |
| 883 |
".{heading}[aria-sort=\"{direction}\"]::after {{\n content: \"{caret}\";\n}}" |
| 884 |
); |
| 885 |
} |
| 886 |
css |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
#[must_use] |
| 895 |
pub fn component_rules(opts: &Emit) -> String { |
| 896 |
let mut css = String::new(); |
| 897 |
css.push_str(&surface_rules(opts)); |
| 898 |
css.push_str(&token_rules(opts)); |
| 899 |
css.push_str(&selector_rules(opts)); |
| 900 |
css.push_str(&row_rules(opts)); |
| 901 |
css.push_str(&progress_rules(opts)); |
| 902 |
css.push_str(&figure_rules(opts)); |
| 903 |
css.push_str(&state_rules(opts)); |
| 904 |
css |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
#[must_use] |
| 914 |
pub fn stylesheet(opts: &Emit) -> String { |
| 915 |
format!( |
| 916 |
"/* Generated by makeover-webview from makeover-layout. Do not edit.\n \ |
| 917 |
Depth is a fill and an edge together; naming them apart is what let\n \ |
| 918 |
them disagree. See the crate's README and wiki note makeover-layout.\n\n \ |
| 919 |
Everything below is in the `{CSS_LAYER}` cascade layer. Declare the\n \ |
| 920 |
order once in your own stylesheet, or this layer's position is decided\n \ |
| 921 |
by whichever generated file the browser happens to see first:\n\n \ |
| 922 |
@layer {CSS_LAYER}, base, components, responsive; */\n{}", |
| 923 |
in_css_layer(&format!( |
| 924 |
":root {{\n{}}}\n\n{}\n{}", |
| 925 |
bevel_properties(opts), |
| 926 |
depth_rules(opts), |
| 927 |
component_rules(opts) |
| 928 |
)) |
| 929 |
) |
| 930 |
} |
| 931 |
|
| 932 |
#[cfg(test)] |
| 933 |
mod tests { |
| 934 |
use super::*; |
| 935 |
use makeover_layout::Edge; |
| 936 |
|
| 937 |
#[test] |
| 938 |
fn the_emitted_bevel_matches_what_the_apps_already_hand_write() { |
| 939 |
|
| 940 |
|
| 941 |
let opts = Emit::default(); |
| 942 |
assert_eq!( |
| 943 |
bevel_shadow(Bevel::Raised, &opts), |
| 944 |
"inset 1px 1px 0 var(--bevel-light), inset -1px -1px 0 var(--bevel-dark)" |
| 945 |
); |
| 946 |
assert_eq!( |
| 947 |
bevel_shadow(Bevel::Inset, &opts), |
| 948 |
"inset 1px 1px 0 var(--bevel-dark), inset -1px -1px 0 var(--bevel-light)" |
| 949 |
); |
| 950 |
} |
| 951 |
|
| 952 |
#[test] |
| 953 |
fn no_colour_ever_reaches_the_output() { |
| 954 |
let css = stylesheet(&Emit::default()); |
| 955 |
assert!(!css.contains('#'), "a hex literal escaped into the CSS"); |
| 956 |
assert!( |
| 957 |
!css.contains("rgb"), |
| 958 |
"a colour function escaped into the CSS" |
| 959 |
); |
| 960 |
|
| 961 |
assert!(css.contains("var(--surface-raised)")); |
| 962 |
assert!(css.contains("var(--bevel-light)")); |
| 963 |
} |
| 964 |
|
| 965 |
#[test] |
| 966 |
fn a_well_falls_back_through_css_rather_than_through_rust() { |
| 967 |
assert_eq!( |
| 968 |
fill_var(Fill::Well), |
| 969 |
"var(--surface-well, var(--surface-page))" |
| 970 |
); |
| 971 |
|
| 972 |
assert_eq!(fill_var(Fill::Raised), "var(--surface-raised)"); |
| 973 |
assert_eq!(fill_var(Fill::Page), "var(--surface-page)"); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn raised_and_well_do_not_collapse_onto_each_other() { |
| 978 |
let css = depth_rules(&Emit::default()); |
| 979 |
assert!(css.contains(".raised {")); |
| 980 |
assert!(css.contains(".well {")); |
| 981 |
assert!(css.contains("var(--bevel-raised)")); |
| 982 |
assert!(css.contains("var(--bevel-inset)")); |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
#[test] |
| 994 |
fn the_cast_shadow_is_a_root_property_not_a_depth() { |
| 995 |
let css = bevel_properties(&Emit::default()); |
| 996 |
assert!(css.contains("--elevation-overlay:")); |
| 997 |
assert!(css.contains("var(--elevation)")); |
| 998 |
assert!( |
| 999 |
!depth_rules(&Emit::default()).contains("elevation"), |
| 1000 |
"elevation is not a depth class" |
| 1001 |
); |
| 1002 |
} |
| 1003 |
|
| 1004 |
#[test] |
| 1005 |
fn the_cascade_carries_the_pressed_state() { |
| 1006 |
let css = surface_rules(&Emit::default()); |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
assert!(css.contains(".card:active {")); |
| 1011 |
assert!(css.contains(".button:active {")); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[test] |
| 1015 |
fn the_depth_class_is_a_surface_and_not_a_control() { |
| 1016 |
let css = depth_rules(&Emit::default()); |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
for state in [":hover", ":active", ":focus-visible", ":disabled"] { |
| 1021 |
assert!( |
| 1022 |
!css.contains(&format!(".raised{state}")), |
| 1023 |
"the depth class claimed {state}: {css}" |
| 1024 |
); |
| 1025 |
} |
| 1026 |
assert!(css.contains("var(--bevel-raised)"), "still raised: {css}"); |
| 1027 |
} |
| 1028 |
|
| 1029 |
#[test] |
| 1030 |
fn pressing_moves_the_fill_and_not_only_the_edge() { |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
let pressed = interactive_rules("button", Depth::Raised, &Emit::default()); |
| 1036 |
assert!(pressed.contains(".button:active {")); |
| 1037 |
assert!( |
| 1038 |
pressed.contains("background: var(--surface-well, var(--surface-page))"), |
| 1039 |
"pressed dropped its fill: {pressed}" |
| 1040 |
); |
| 1041 |
assert!(pressed.contains("box-shadow: var(--bevel-inset)")); |
| 1042 |
} |
| 1043 |
|
| 1044 |
#[test] |
| 1045 |
fn pressed_takes_its_fill_from_the_description_not_from_the_app() { |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
let css = stylesheet(&Emit::default()); |
| 1056 |
let mut checked = 0; |
| 1057 |
for rule in css.split("}\n") { |
| 1058 |
if !rule.contains(":active") { |
| 1059 |
continue; |
| 1060 |
} |
| 1061 |
checked += 1; |
| 1062 |
assert!( |
| 1063 |
!rule.contains("surface-sunken"), |
| 1064 |
"a pressed rule took the app's fill: {rule}" |
| 1065 |
); |
| 1066 |
} |
| 1067 |
assert!(checked > 0, "no pressed rules found to check"); |
| 1068 |
assert_eq!( |
| 1069 |
Depth::Raised.pressed().fill(), |
| 1070 |
Some(Fill::Well), |
| 1071 |
"the description changed under us" |
| 1072 |
); |
| 1073 |
} |
| 1074 |
|
| 1075 |
#[test] |
| 1076 |
fn the_whole_stylesheet_is_emitted_in_the_family_layer() { |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
let css = stylesheet(&Emit::default()); |
| 1081 |
assert!(css.contains(&format!("@layer {CSS_LAYER} {{"))); |
| 1082 |
|
| 1083 |
|
| 1084 |
assert_eq!(css.matches("@layer").count(), 2, "banner names it once"); |
| 1085 |
let opened = css.find("@layer makeover {").expect("layer opens"); |
| 1086 |
for (i, line) in css.lines().enumerate() { |
| 1087 |
let before_layer = css.lines().take(i).map(str::len).sum::<usize>() < opened; |
| 1088 |
if before_layer || line.is_empty() { |
| 1089 |
continue; |
| 1090 |
} |
| 1091 |
assert!( |
| 1092 |
line.starts_with(" ") || line == "}" || line.starts_with(" "), |
| 1093 |
"line outside the layer: {line:?}" |
| 1094 |
); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
#[test] |
| 1099 |
fn the_generated_sheet_carries_no_trailing_whitespace() { |
| 1100 |
|
| 1101 |
|
| 1102 |
let css = stylesheet(&Emit::default()); |
| 1103 |
for (i, line) in css.lines().enumerate() { |
| 1104 |
assert_eq!(line, line.trim_end(), "trailing whitespace on line {i}"); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
#[test] |
| 1109 |
fn the_banner_tells_an_app_how_to_order_the_layer() { |
| 1110 |
|
| 1111 |
|
| 1112 |
let css = stylesheet(&Emit::default()); |
| 1113 |
assert!(css.contains("@layer makeover, base, components, responsive;")); |
| 1114 |
|
| 1115 |
assert!(css.starts_with("/* Generated by makeover-webview")); |
| 1116 |
} |
| 1117 |
|
| 1118 |
#[test] |
| 1119 |
fn a_primitive_owns_every_state_it_implies() { |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
let css = stylesheet(&Emit::default()); |
| 1125 |
for selector in ["button", "card", "chip", "tab", "segment", "toggle"] { |
| 1126 |
assert!(css.contains(&format!(".{selector}:hover {{")), "{selector}"); |
| 1127 |
assert!( |
| 1128 |
css.contains(&format!(".{selector}:active {{")), |
| 1129 |
"{selector}" |
| 1130 |
); |
| 1131 |
assert!( |
| 1132 |
css.contains(&format!(".{selector}:focus-visible {{")), |
| 1133 |
"{selector} has no focus ring" |
| 1134 |
); |
| 1135 |
assert!( |
| 1136 |
css.contains(&format!(".{selector}:disabled,")), |
| 1137 |
"{selector} has no disabled state" |
| 1138 |
); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
#[test] |
| 1143 |
fn a_field_takes_focus_and_refuses_input_without_taking_a_hover() { |
| 1144 |
|
| 1145 |
|
| 1146 |
let css = stylesheet(&Emit::default()); |
| 1147 |
assert!(css.contains(".field:focus-visible {")); |
| 1148 |
assert!(css.contains(".field:disabled,")); |
| 1149 |
assert!(!css.contains(".field:hover {")); |
| 1150 |
assert!(!css.contains(".field:active {")); |
| 1151 |
} |
| 1152 |
|
| 1153 |
#[test] |
| 1154 |
fn disabled_is_emitted_after_hover_so_source_order_settles_it() { |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
let css = interactive_rules("button", Depth::Raised, &Emit::default()); |
| 1160 |
let hover = css.find(":hover").expect("hover"); |
| 1161 |
let active = css.find(":active").expect("active"); |
| 1162 |
let focus = css.find(":focus-visible").expect("focus"); |
| 1163 |
let disabled = css.find(":disabled").expect("disabled"); |
| 1164 |
assert!(hover < active && active < focus && focus < disabled); |
| 1165 |
|
| 1166 |
|
| 1167 |
let tail = &css[disabled..]; |
| 1168 |
assert!(tail.contains("background: var(--surface-raised)")); |
| 1169 |
} |
| 1170 |
|
| 1171 |
#[test] |
| 1172 |
fn a_disabled_state_reaches_things_that_cannot_be_disabled() { |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
let css = disabled_rule("chip", Depth::Raised); |
| 1177 |
assert!(css.contains(".chip:disabled,")); |
| 1178 |
assert!(css.contains(".chip[aria-disabled=\"true\"]")); |
| 1179 |
assert!(css.contains("cursor: not-allowed")); |
| 1180 |
} |
| 1181 |
|
| 1182 |
#[test] |
| 1183 |
fn the_focus_ring_does_not_disturb_the_bevel_it_lands_on() { |
| 1184 |
|
| 1185 |
|
| 1186 |
let opts = Emit::default(); |
| 1187 |
let css = focus_rule("button", Depth::Raised, &opts); |
| 1188 |
assert!(css.contains("outline: 2px solid var(--focus-ring)")); |
| 1189 |
assert!(!css.contains("box-shadow"), "the ring restated the bevel"); |
| 1190 |
} |
| 1191 |
|
| 1192 |
#[test] |
| 1193 |
fn a_well_takes_the_ring_inside_and_a_raised_surface_outside() { |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
let opts = Emit::default(); |
| 1198 |
assert!(focus_rule("field", Depth::Well, &opts).contains("outline-offset: calc(-1 * 2px)")); |
| 1199 |
assert!(focus_rule("button", Depth::Raised, &opts).contains("outline-offset: 2px")); |
| 1200 |
|
| 1201 |
assert!(focus_rule("badge", Depth::Sunken, &opts).contains("outline-offset: 2px")); |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
assert_ne!(opts.focus_width, opts.border_width); |
| 1206 |
} |
| 1207 |
|
| 1208 |
#[test] |
| 1209 |
fn hover_is_gated_on_capability_and_the_keyboard_path_is_not() { |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
let css = stylesheet(&Emit::default()); |
| 1214 |
let condition = format!("@media {}", Density::Pointer.media_condition()); |
| 1215 |
assert!(css.contains(&condition)); |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
let indent = |needle: &str| { |
| 1222 |
let line = css |
| 1223 |
.lines() |
| 1224 |
.find(|l| l.contains(needle)) |
| 1225 |
.unwrap_or_else(|| panic!("no line for {needle}")); |
| 1226 |
line.len() - line.trim_start().len() |
| 1227 |
}; |
| 1228 |
let hover = indent(".row:hover .row-actions"); |
| 1229 |
let keyboard = indent(".row:focus-within .row-actions"); |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
let hide = css |
| 1234 |
.lines() |
| 1235 |
.position(|l| l.trim() == ".row-actions {") |
| 1236 |
.expect("hide rule"); |
| 1237 |
let query = css |
| 1238 |
.lines() |
| 1239 |
.take(hide) |
| 1240 |
.enumerate() |
| 1241 |
.filter(|(_, l)| l.trim_start().starts_with("@media")) |
| 1242 |
.map(|(i, _)| i) |
| 1243 |
.last() |
| 1244 |
.expect("a query precedes it"); |
| 1245 |
assert!(hide - query < 3, "the hide is not inside the query"); |
| 1246 |
assert!( |
| 1247 |
hover > keyboard, |
| 1248 |
"hover reveal must be nested inside the capability query and the \ |
| 1249 |
keyboard reveal must not be: hover indent {hover}, keyboard {keyboard}" |
| 1250 |
); |
| 1251 |
} |
| 1252 |
|
| 1253 |
#[test] |
| 1254 |
fn the_capability_answer_is_asked_for_and_not_assumed() { |
| 1255 |
|
| 1256 |
|
| 1257 |
assert!(!Affordance::Hover.available(Density::Touch, SizeClass::Compact)); |
| 1258 |
assert!(Affordance::Hover.available(Density::Pointer, SizeClass::Compact)); |
| 1259 |
assert_eq!(hover_condition(), Some(Density::Pointer.media_condition())); |
| 1260 |
|
| 1261 |
|
| 1262 |
assert!(Affordance::Hover.reads_density()); |
| 1263 |
for size in [SizeClass::Compact, SizeClass::Medium, SizeClass::Expanded] { |
| 1264 |
assert!(!Affordance::Hover.available(Density::Touch, size)); |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
#[test] |
| 1269 |
fn hover_resolves_against_the_token_makeover_already_derives() { |
| 1270 |
let css = interactive_rules("card", Depth::Raised, &Emit::default()); |
| 1271 |
assert!(css.contains(".card:hover {")); |
| 1272 |
assert!(css.contains("background: var(--hover-surface)")); |
| 1273 |
|
| 1274 |
assert!(!css.contains("surface-overlay")); |
| 1275 |
} |
| 1276 |
|
| 1277 |
#[test] |
| 1278 |
fn a_badge_gets_no_edge_and_no_fill() { |
| 1279 |
|
| 1280 |
|
| 1281 |
let css = token_rules(&Emit::default()); |
| 1282 |
let badge = css |
| 1283 |
.lines() |
| 1284 |
.skip_while(|l| !l.starts_with(".badge {")) |
| 1285 |
.take_while(|l| !l.starts_with('}')) |
| 1286 |
.collect::<Vec<_>>() |
| 1287 |
.join("\n"); |
| 1288 |
assert!(!badge.contains("box-shadow"), "badge kept an edge: {badge}"); |
| 1289 |
assert!(!badge.contains("background"), "badge kept a fill: {badge}"); |
| 1290 |
assert_eq!(Token::Badge.depth(false), Depth::Flat); |
| 1291 |
assert_eq!(Token::Badge.depth(true), Depth::Flat); |
| 1292 |
} |
| 1293 |
|
| 1294 |
#[test] |
| 1295 |
fn a_badge_carries_a_tone_and_neutral_is_the_bare_class() { |
| 1296 |
let css = token_rules(&Emit::default()); |
| 1297 |
|
| 1298 |
assert!(css.contains(".badge {\n color: var(--content-muted);")); |
| 1299 |
assert!(!css.contains("data-tone=\"content-muted\"")); |
| 1300 |
for tone in ["info", "success", "warning", "danger"] { |
| 1301 |
assert!( |
| 1302 |
css.contains(&format!(".badge[data-tone=\"{tone}\"]")), |
| 1303 |
"missing tone {tone}" |
| 1304 |
); |
| 1305 |
assert!(css.contains(&format!("color: var(--{tone})"))); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
#[test] |
| 1310 |
fn a_chip_is_raised_and_latches_into_a_well() { |
| 1311 |
let css = token_rules(&Emit::default()); |
| 1312 |
assert!(css.contains(".chip {")); |
| 1313 |
assert!(css.contains(".chip.latched {")); |
| 1314 |
assert!(css.contains(".chip:active {")); |
| 1315 |
|
| 1316 |
assert!(Token::Chip { removable: false }.interactive()); |
| 1317 |
assert!(!Token::Badge.interactive()); |
| 1318 |
} |
| 1319 |
|
| 1320 |
#[test] |
| 1321 |
fn only_a_tab_comes_forward_when_chosen() { |
| 1322 |
|
| 1323 |
let css = selector_rules(&Emit::default()); |
| 1324 |
assert!(css.contains(".tab.chosen {")); |
| 1325 |
assert!(css.contains(".segment.chosen {")); |
| 1326 |
assert!(css.contains(".toggle.chosen {")); |
| 1327 |
assert_eq!(Selector::Tabs.chosen(), Depth::Raised); |
| 1328 |
assert_eq!(Selector::Segmented.chosen(), Depth::Well); |
| 1329 |
assert_eq!(Selector::Toggle.chosen(), Depth::Well); |
| 1330 |
|
| 1331 |
let tab = css |
| 1332 |
.lines() |
| 1333 |
.skip_while(|l| !l.starts_with(".tab.chosen {")) |
| 1334 |
.take_while(|l| !l.starts_with('}')) |
| 1335 |
.collect::<Vec<_>>() |
| 1336 |
.join("\n"); |
| 1337 |
assert!( |
| 1338 |
tab.contains("var(--bevel-raised)"), |
| 1339 |
"tab was held in: {tab}" |
| 1340 |
); |
| 1341 |
} |
| 1342 |
|
| 1343 |
#[test] |
| 1344 |
fn an_unchosen_tab_recedes_without_looking_picked() { |
| 1345 |
let css = selector_rules(&Emit::default()); |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
assert!( |
| 1350 |
css.contains(".tab {\n background: var(--surface-sunken);\n}"), |
| 1351 |
"unchosen tab is not recessed: {css}" |
| 1352 |
); |
| 1353 |
assert_eq!(Selector::Tabs.unchosen(), Depth::Sunken); |
| 1354 |
assert!(css.contains(".tab:hover {")); |
| 1355 |
} |
| 1356 |
|
| 1357 |
#[test] |
| 1358 |
fn a_segment_stands_up_so_the_chosen_one_can_be_held_in() { |
| 1359 |
|
| 1360 |
|
| 1361 |
let css = selector_rules(&Emit::default()); |
| 1362 |
assert!(css.contains(".segment {\n background: var(--surface-raised);")); |
| 1363 |
assert_eq!(Selector::Segmented.unchosen(), Depth::Raised); |
| 1364 |
assert_eq!(Selector::Segmented.chosen(), Depth::Well); |
| 1365 |
} |
| 1366 |
|
| 1367 |
#[test] |
| 1368 |
fn row_actions_are_revealed_without_moving_the_row() { |
| 1369 |
let css = row_rules(&Emit::default()); |
| 1370 |
assert!(css.contains(".row-actions {\n opacity: 0;")); |
| 1371 |
|
| 1372 |
assert!(!css.contains("display: none")); |
| 1373 |
|
| 1374 |
assert!(css.contains(".row:focus-within .row-actions")); |
| 1375 |
assert!(RowPart::Actions.revealed_on_hover()); |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[test] |
| 1379 |
fn a_hidden_row_action_is_still_focusable_and_not_tappable() { |
| 1380 |
let css = row_rules(&Emit::default()); |
| 1381 |
|
| 1382 |
|
| 1383 |
assert!(!css.contains("visibility:")); |
| 1384 |
|
| 1385 |
|
| 1386 |
assert!( |
| 1387 |
css.contains( |
| 1388 |
".row-actions {\n opacity: 0;\n pointer-events: none;\n }" |
| 1389 |
) |
| 1390 |
); |
| 1391 |
assert!(css.contains("opacity: 1;\n pointer-events: auto;")); |
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
let hide = css.find(".row-actions {").expect("hide rule"); |
| 1398 |
let query = css[..hide].rfind("@media").expect("a query precedes it"); |
| 1399 |
assert!( |
| 1400 |
css[query..hide].find('}').is_none(), |
| 1401 |
"the hide escaped the query" |
| 1402 |
); |
| 1403 |
} |
| 1404 |
|
| 1405 |
#[test] |
| 1406 |
fn the_three_text_parts_take_their_intents_and_actions_inherits() { |
| 1407 |
let css = row_rules(&Emit::default()); |
| 1408 |
assert!(css.contains(".row-primary {\n color: var(--content);")); |
| 1409 |
assert!(css.contains(".row-secondary {\n color: var(--content-secondary);")); |
| 1410 |
assert!(css.contains(".row-meta {\n color: var(--content-muted);")); |
| 1411 |
|
| 1412 |
|
| 1413 |
assert!(!css.contains(".row-actions {\n color:")); |
| 1414 |
} |
| 1415 |
|
| 1416 |
#[test] |
| 1417 |
fn the_token_strip_takes_no_colour_of_its_own() { |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
let css = row_rules(&Emit::default()); |
| 1422 |
assert!(!css.contains(".row-tokens {\n color:")); |
| 1423 |
} |
| 1424 |
|
| 1425 |
#[test] |
| 1426 |
fn an_unknown_row_part_renders_plainly_rather_than_failing_to_build() { |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
assert_eq!(part_class(RowPart::Tokens), "row-tokens"); |
| 1433 |
assert_eq!(part_class(RowPart::Meta), "row-meta"); |
| 1434 |
} |
| 1435 |
|
| 1436 |
#[test] |
| 1437 |
fn the_progress_trough_is_a_well() { |
| 1438 |
let css = progress_rules(&Emit::default()); |
| 1439 |
assert!(css.contains(".progress {")); |
| 1440 |
assert!(css.contains("box-shadow: var(--bevel-inset)")); |
| 1441 |
assert!(css.contains(".progress > .progress-fill {")); |
| 1442 |
assert!(css.contains("background: var(--action)")); |
| 1443 |
|
| 1444 |
|
| 1445 |
assert!(!css.contains("> .fill ")); |
| 1446 |
} |
| 1447 |
|
| 1448 |
#[test] |
| 1449 |
fn a_progress_bar_can_carry_a_tone_and_defaults_to_action() { |
| 1450 |
let css = progress_rules(&Emit::default()); |
| 1451 |
|
| 1452 |
|
| 1453 |
assert!(css.contains(".progress > .progress-fill {\n background: var(--action);")); |
| 1454 |
assert!(!css.contains("progress-fill {\n color: var(--content-muted)")); |
| 1455 |
for tone in ["info", "success", "warning", "danger"] { |
| 1456 |
assert!( |
| 1457 |
css.contains(&format!(".progress > .progress-fill[data-tone=\"{tone}\"]")), |
| 1458 |
"missing progress tone {tone}" |
| 1459 |
); |
| 1460 |
} |
| 1461 |
|
| 1462 |
assert!(css.contains("[data-tone=\"success\"] {\n background: var(--success);")); |
| 1463 |
assert!(css.contains("[data-tone=\"danger\"] {\n background: var(--danger);")); |
| 1464 |
} |
| 1465 |
|
| 1466 |
#[test] |
| 1467 |
fn no_scrollbar_track_is_emitted() { |
| 1468 |
|
| 1469 |
|
| 1470 |
let css = stylesheet(&Emit::default()); |
| 1471 |
assert!(!css.contains("scrollbar")); |
| 1472 |
assert!(!css.contains("::-webkit")); |
| 1473 |
} |
| 1474 |
|
| 1475 |
#[test] |
| 1476 |
fn an_invalid_field_is_ringed_without_being_lit() { |
| 1477 |
let css = surface_rules(&Emit::default()); |
| 1478 |
assert!(css.contains(".field {")); |
| 1479 |
|
| 1480 |
|
| 1481 |
assert!(css.contains(".field[aria-invalid=\"true\"] {")); |
| 1482 |
assert!(!css.contains(".field.invalid")); |
| 1483 |
|
| 1484 |
|
| 1485 |
assert!(css.contains("0 0 0 1px var(--danger)")); |
| 1486 |
} |
| 1487 |
|
| 1488 |
#[test] |
| 1489 |
fn an_invalid_field_keeps_the_well_underneath_it() { |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
let css = surface_rules(&Emit::default()); |
| 1494 |
let invalid = css |
| 1495 |
.lines() |
| 1496 |
.skip_while(|l| !l.starts_with(".field[aria-invalid")) |
| 1497 |
.take_while(|l| !l.starts_with('}')) |
| 1498 |
.collect::<Vec<_>>() |
| 1499 |
.join("\n"); |
| 1500 |
assert!( |
| 1501 |
invalid.contains("var(--bevel-inset)"), |
| 1502 |
"the well was dropped: {invalid}" |
| 1503 |
); |
| 1504 |
assert!(invalid.contains("var(--danger)")); |
| 1505 |
} |
| 1506 |
|
| 1507 |
#[test] |
| 1508 |
fn button_and_card_come_out_identical_by_construction() { |
| 1509 |
|
| 1510 |
|
| 1511 |
let opts = Emit::default(); |
| 1512 |
let css = surface_rules(&opts); |
| 1513 |
assert_eq!( |
| 1514 |
depth_declarations(Depth::Raised), |
| 1515 |
depth_declarations(Depth::Raised) |
| 1516 |
); |
| 1517 |
assert!(css.contains(".button {")); |
| 1518 |
assert!(css.contains(".card {")); |
| 1519 |
assert_eq!( |
| 1520 |
interactive_rules("button", Depth::Raised, &Emit::default()).replace("button", "card"), |
| 1521 |
interactive_rules("card", Depth::Raised, &Emit::default()) |
| 1522 |
); |
| 1523 |
} |
| 1524 |
|
| 1525 |
#[test] |
| 1526 |
fn a_prefix_reaches_the_component_classes_too() { |
| 1527 |
let opts = Emit { |
| 1528 |
class_prefix: "mo-", |
| 1529 |
..Emit::default() |
| 1530 |
}; |
| 1531 |
let css = stylesheet(&opts); |
| 1532 |
for name in [ |
| 1533 |
"mo-button", |
| 1534 |
"mo-card", |
| 1535 |
"mo-field", |
| 1536 |
"mo-badge", |
| 1537 |
"mo-chip", |
| 1538 |
"mo-tab", |
| 1539 |
"mo-row-primary", |
| 1540 |
"mo-progress", |
| 1541 |
"mo-progress-fill", |
| 1542 |
] { |
| 1543 |
assert!(css.contains(&format!(".{name}")), "unprefixed: {name}"); |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
assert!(!css.contains(".button {")); |
| 1548 |
assert!(!css.contains(".card {")); |
| 1549 |
assert!(!css.contains(".badge {")); |
| 1550 |
} |
| 1551 |
|
| 1552 |
#[test] |
| 1553 |
fn the_whole_sheet_still_names_every_colour() { |
| 1554 |
|
| 1555 |
|
| 1556 |
let css = stylesheet(&Emit::default()); |
| 1557 |
assert!(!css.contains('#')); |
| 1558 |
assert!(!css.contains("rgb")); |
| 1559 |
for line in css.lines() { |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
let trimmed = line.trim(); |
| 1566 |
if !trimmed.ends_with(';') { |
| 1567 |
continue; |
| 1568 |
} |
| 1569 |
let Some((_, value)) = trimmed.split_once(": ") else { |
| 1570 |
continue; |
| 1571 |
}; |
| 1572 |
if value.contains("var(--") { |
| 1573 |
continue; |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
let opts = Emit::default(); |
| 1583 |
assert!( |
| 1584 |
value.contains("inset") |
| 1585 |
|| value.contains(opts.border_width) |
| 1586 |
|| value.contains(opts.focus_width) |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|| value.starts_with("\"\\2") |
| 1593 |
|| matches!( |
| 1594 |
value.trim_end_matches(';'), |
| 1595 |
"0" | "1" | "none" | "auto" | "not-allowed" | "pointer" |
| 1596 |
), |
| 1597 |
"unrecognised literal value: {line}" |
| 1598 |
); |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
#[test] |
| 1603 |
fn flat_emits_nothing_at_all() { |
| 1604 |
assert_eq!(depth_class(Depth::Flat, &Emit::default()), None); |
| 1605 |
assert!(!depth_rules(&Emit::default()).contains("flat")); |
| 1606 |
} |
| 1607 |
|
| 1608 |
#[test] |
| 1609 |
fn a_prefix_namespaces_every_class() { |
| 1610 |
let opts = Emit { |
| 1611 |
class_prefix: "mo-", |
| 1612 |
..Emit::default() |
| 1613 |
}; |
| 1614 |
let css = depth_rules(&opts); |
| 1615 |
assert!(css.contains(".mo-raised {")); |
| 1616 |
assert!(css.contains(".mo-well {")); |
| 1617 |
assert!(!css.contains(".raised {")); |
| 1618 |
} |
| 1619 |
|
| 1620 |
#[test] |
| 1621 |
fn the_border_width_is_the_callers() { |
| 1622 |
let opts = Emit { |
| 1623 |
border_width: "2px", |
| 1624 |
..Emit::default() |
| 1625 |
}; |
| 1626 |
assert!(bevel_shadow(Bevel::Raised, &opts).contains("inset 2px 2px 0")); |
| 1627 |
} |
| 1628 |
|
| 1629 |
#[test] |
| 1630 |
fn edges_agree_with_the_description() { |
| 1631 |
|
| 1632 |
|
| 1633 |
let (tl, br) = Bevel::Raised.edges(); |
| 1634 |
assert_eq!(tl.token(), Edge::Light.token()); |
| 1635 |
assert_eq!(br.token(), Edge::Dark.token()); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|