dwui 0.10.0

UI Component library built on the DWIND style crate!
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Browser tests for the styling layer: `dwkeyframes!` injection, pseudo-element
//! variants, and arbitrary declarations.
//!
//! These live in dwui rather than dwind because dwui is the crate already wired
//! for `wasm-pack test`, and because the last test here guards dwui's own
//! keyframe migration.
//!
//! Run with: `wasm-pack test --headless --firefox crates/dwui`

#![cfg(target_arch = "wasm32")]

use dominator::html;
use dwind::prelude::*;
use dwind_macros::{dwclass, dwkeyframes};
use wasm_bindgen_test::*;
use web_sys::js_sys;
use web_sys::wasm_bindgen::JsCast;

wasm_bindgen_test_configure!(run_in_browser);

dwkeyframes! {
    #![prefix = "dwuitest"]

    #[animation("1s linear infinite")]
    slide_probe {
        "from" => "transform: translateX(0);",
        "to" => "transform: translateX(10px);",
    }

    /// Declared but never used, to prove injection is lazy.
    unused_probe {
        "from" => "opacity: 0;",
        "to" => "opacity: 1;",
    }

    /// Only ever reached through a `hover:` variant.
    #[animation("1s linear infinite")]
    hover_probe {
        "from" => "opacity: 0.4;",
        "to" => "opacity: 1;",
    }

    /// Only ever reached through a `[&::before]:` variant.
    #[animation("1s linear infinite")]
    before_probe {
        "from" => "opacity: 0.4;",
        "to" => "opacity: 1;",
    }
}

struct TestContainer {
    element: web_sys::Element,
}

impl TestContainer {
    fn new() -> Self {
        let doc = web_sys::window().unwrap().document().unwrap();
        let el = doc.create_element("div").unwrap();
        el.set_attribute(
            "style",
            "position:absolute;left:0;top:0;width:800px;height:600px",
        )
        .unwrap();
        doc.body().unwrap().append_child(&el).unwrap();
        Self { element: el }
    }

    fn dom_element(&self) -> web_sys::HtmlElement {
        self.element.clone().dyn_into().unwrap()
    }
}

impl Drop for TestContainer {
    fn drop(&mut self) {
        self.element.remove();
    }
}

async fn wait_frame() {
    let promise = js_sys::Promise::new(&mut |resolve, _| {
        web_sys::window()
            .unwrap()
            .request_animation_frame(&resolve)
            .unwrap();
    });
    wasm_bindgen_futures::JsFuture::from(promise).await.unwrap();
}

/// Counts `@keyframes` rules with this name across every stylesheet in the
/// document. More than one means the registry failed to deduplicate.
fn count_keyframes(name: &str) -> usize {
    let doc = web_sys::window().unwrap().document().unwrap();
    let sheets = doc.style_sheets();
    let mut found = 0;

    for i in 0..sheets.length() {
        let Some(sheet) = sheets.item(i) else {
            continue;
        };
        let Ok(sheet) = sheet.dyn_into::<web_sys::CssStyleSheet>() else {
            continue;
        };
        // A cross-origin sheet throws on access; skip it.
        let Ok(rules) = sheet.css_rules() else {
            continue;
        };

        for r in 0..rules.length() {
            let Some(rule) = rules.item(r) else { continue };

            if let Ok(keyframes) = rule.dyn_into::<web_sys::CssKeyframesRule>() {
                if keyframes.name() == name {
                    found += 1;
                }
            }
        }
    }

    found
}

fn computed_pseudo(element: &web_sys::Element, pseudo: &str, property: &str) -> String {
    web_sys::window()
        .unwrap()
        .get_computed_style_with_pseudo_elt(element, pseudo)
        .unwrap()
        .unwrap()
        .get_property_value(property)
        .unwrap()
}

// ---------------------------------------------------------------------------
// dwkeyframes!
// ---------------------------------------------------------------------------

#[wasm_bindgen_test]
async fn keyframes_are_injected_lazily_and_only_once() {
    // Nothing has touched `unused_probe`, so its rule must not be in the
    // document. This is the property the old eager `&str` blob could not offer.
    assert_eq!(
        count_keyframes("dwuitest-unused-probe"),
        0,
        "an unused keyframe was injected"
    );

    let tc = TestContainer::new();

    // Instantiate the class twice: the registry must inject exactly one rule.
    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .dwclass!("animate-slide-probe")
            .child(html!("span", { .dwclass!("animate-slide-probe") }))
        }),
    );
    wait_frame().await;

    assert_eq!(
        count_keyframes("dwuitest-slide-probe"),
        1,
        "expected exactly one @keyframes rule after two instantiations"
    );
}

#[wasm_bindgen_test]
async fn keyframe_handles_register_when_formatted() {
    // `Display` registers, so a hand-composed shorthand cannot reference a rule
    // that was never injected.
    let shorthand = format!("{UNUSED_PROBE_KEYFRAMES} 1s linear");

    assert!(
        shorthand.starts_with("dwuitest-unused-probe"),
        "{shorthand}"
    );
    assert_eq!(count_keyframes("dwuitest-unused-probe"), 1);
}

#[wasm_bindgen_test]
async fn dwui_keyframes_survived_the_migration() {
    dwui::theme::apply_style_sheet(None);
    // Idempotent: calling twice must not duplicate the rules.
    dwui::theme::apply_style_sheet(None);

    for name in [
        "dwui-modal-in",
        "dwui-fade-in",
        "dwui-progress-indeterminate",
        "dwui-spin",
        "dwui-skeleton-pulse",
    ] {
        assert_eq!(count_keyframes(name), 1, "{name} should be injected once");
    }
}

#[wasm_bindgen_test]
async fn modified_animation_utilities_still_register_their_keyframes() {
    // A variant compiles the declaration text into a fresh class and never
    // touches the generated utility, so registration has to hang off the text.
    assert_eq!(count_keyframes("dwuitest-hover-probe"), 0);
    assert_eq!(count_keyframes("dwuitest-before-probe"), 0);

    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .dwclass!("hover:animate-hover-probe")
            .child(html!("span", { .dwclass!("relative [&::before]:animate-before-probe") }))
        }),
    );
    wait_frame().await;

    assert_eq!(
        count_keyframes("dwuitest-hover-probe"),
        1,
        "hover:animate-* did not inject its @keyframes"
    );
    assert_eq!(
        count_keyframes("dwuitest-before-probe"),
        1,
        "[&::before]:animate-* did not inject its @keyframes"
    );
}

// ---------------------------------------------------------------------------
// Pseudo-element variants
// ---------------------------------------------------------------------------

#[wasm_bindgen_test]
async fn pseudo_element_variants_materialise_without_a_content_utility() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-before")
            .dwclass!("relative [&::before]:absolute [&::before]:opacity-100")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-before").unwrap();

    // Without a `content`, a ::before never generates a box, so this is the
    // property that decides whether the utility is usable on its own.
    let content = computed_pseudo(&el, "::before", "content");
    assert!(
        content != "none",
        "::before had no generated content (got {content:?})"
    );
    assert_eq!(computed_pseudo(&el, "::before", "position"), "absolute");
}

#[wasm_bindgen_test]
async fn before_shorthand_matches_the_bracket_form() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-alias")
            .dwclass!("relative before:absolute")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-alias").unwrap();

    assert!(computed_pseudo(&el, "::before", "content") != "none");
    assert_eq!(computed_pseudo(&el, "::before", "position"), "absolute");
}

#[wasm_bindgen_test]
async fn explicit_pseudo_content_survives_composition() {
    // Each utility is its own class, so a literal `content: ""` from
    // `before:absolute` would be decided against `before:[content:'x']` by rule
    // order. Routing through --dw-content removes the ordering question.
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-compose")
            .dwclass!("relative before:[content:'x'] before:absolute before:opacity-100")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-compose").unwrap();

    let content = computed_pseudo(&el, "::before", "content");
    assert!(
        content.contains('x'),
        "composed pseudo-element lost its content (got {content:?})"
    );
    assert_eq!(computed_pseudo(&el, "::before", "position"), "absolute");
}

#[wasm_bindgen_test]
async fn arbitrary_values_may_be_non_ascii() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-unicode")
            .dwclass!("relative before:[content:'→'] before:m-r-2")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-unicode").unwrap();

    let content = computed_pseudo(&el, "::before", "content");
    assert!(content.contains(''), "got {content:?}");
    // The class after the Unicode one must survive too — an unparsed remainder
    // used to discard it silently.
    assert_eq!(computed_pseudo(&el, "::before", "margin-right"), "8px");
}

#[wasm_bindgen_test]
async fn quoted_values_may_contain_brackets() {
    // `[content:'[']` is valid CSS. The parser has to know that a bracket inside
    // a string is content rather than a delimiter.
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-bracket")
            .dwclass!("relative before:[content:'['] before:absolute")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-bracket").unwrap();

    let content = computed_pseudo(&el, "::before", "content");
    assert!(content.contains('['), "got {content:?}");
    assert_eq!(computed_pseudo(&el, "::before", "position"), "absolute");
}

#[wasm_bindgen_test]
async fn pseudo_classes_do_not_gain_content() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-hover")
            .dwclass!("hover:opacity-50")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-hover").unwrap();

    // The element itself is unaffected; only `::before`/`::after` get content.
    assert_eq!(computed_pseudo(&el, "::before", "content"), "none");
}

// ---------------------------------------------------------------------------
// Arbitrary declarations
// ---------------------------------------------------------------------------

#[wasm_bindgen_test]
async fn arbitrary_declarations_reach_the_element() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-arbitrary")
            .dwclass!("[mix-blend-mode:overlay] [--sx:42%] [letter-spacing:0.5px]")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-arbitrary").unwrap();
    let style = web_sys::window()
        .unwrap()
        .get_computed_style(&el)
        .unwrap()
        .unwrap();

    assert_eq!(
        style.get_property_value("mix-blend-mode").unwrap(),
        "overlay"
    );
    assert_eq!(style.get_property_value("letter-spacing").unwrap(), "0.5px");
    // Custom properties round-trip too, which is what the pointer-tracking
    // effects in the example app need.
    assert_eq!(style.get_property_value("--sx").unwrap().trim(), "42%");
}

#[wasm_bindgen_test]
async fn arbitrary_declarations_compose_with_pseudo_elements() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-arb-before")
            .dwclass!("relative [&::before]:[mix-blend-mode:screen]")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-arb-before").unwrap();

    assert!(computed_pseudo(&el, "::before", "content") != "none");
    assert_eq!(computed_pseudo(&el, "::before", "mix-blend-mode"), "screen");
}

// ---------------------------------------------------------------------------
// New utilities
// ---------------------------------------------------------------------------

#[wasm_bindgen_test]
async fn newly_added_utilities_apply() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-utils")
            .dwclass!("absolute inset-0 isolate no-underline whitespace-nowrap will-change-transform delay-150 tracking-tight")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-utils").unwrap();
    let style = web_sys::window()
        .unwrap()
        .get_computed_style(&el)
        .unwrap()
        .unwrap();

    assert_eq!(style.get_property_value("isolation").unwrap(), "isolate");
    assert_eq!(style.get_property_value("white-space").unwrap(), "nowrap");
    assert_eq!(
        style.get_property_value("transition-delay").unwrap(),
        "0.15s"
    );
    assert_eq!(style.get_property_value("top").unwrap(), "0px");
    assert_eq!(
        style.get_property_value("text-decoration-line").unwrap(),
        "none"
    );
}

#[wasm_bindgen_test]
async fn pseudo_content_composes_in_either_order() {
    // --dw-content is order-independent by construction, but that is the whole
    // claim, so measure it rather than reason about it.
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .child(html!("div", {
                .attr("id", "order-a")
                .dwclass!("relative before:[content:'a'] before:absolute")
            }))
            .child(html!("div", {
                .attr("id", "order-b")
                .dwclass!("relative before:absolute before:[content:'b']")
            }))
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();

    for (id, want) in [("order-a", 'a'), ("order-b", 'b')] {
        let el = doc.get_element_by_id(id).unwrap();
        let content = computed_pseudo(&el, "::before", "content");

        assert!(content.contains(want), "{id}: got {content:?}");
        assert_eq!(computed_pseudo(&el, "::before", "position"), "absolute");
    }
}

#[wasm_bindgen_test]
async fn content_utilities_reach_the_pseudo_element() {
    // `content-none` has to suppress the generated default, or a utility that
    // only wants to hide a pseudo-element cannot.
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-content-none")
            .dwclass!("relative before:absolute before:content-none")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-content-none").unwrap();

    assert_eq!(computed_pseudo(&el, "::before", "content"), "none");
}

// dwgenerate! must be able to alias any class — a plain utility, and one minted
// by dwkeyframes!. Aliasing a bare class never worked before (the generated
// static tried to hold a `&String` in a `String`), and the AnimationDecl change
// added a second error on the same path.
dwind_macros::dwgenerate!("aliased-flex", "flex");
dwind_macros::dwgenerate!("aliased-anim", "animate-slide-probe");

#[wasm_bindgen_test]
async fn dwgenerate_can_alias_a_plain_class_and_an_animation() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-alias")
            .dwclass!("aliased-flex aliased-anim")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-alias").unwrap();
    let style = web_sys::window()
        .unwrap()
        .get_computed_style(&el)
        .unwrap()
        .unwrap();

    assert_eq!(style.get_property_value("display").unwrap(), "flex");
    // Aliasing the animation must still register its keyframes.
    assert!(
        style
            .get_property_value("animation-name")
            .unwrap()
            .contains("slide-probe"),
        "{:?}",
        style.get_property_value("animation-name")
    );
    assert_eq!(count_keyframes("dwuitest-slide-probe"), 1);
}

// ---------------------------------------------------------------------------
// Selector forms
// ---------------------------------------------------------------------------

// A selector the engine cannot parse is not ignored: `insertRule` throws and
// dominator panics ("selectors are incorrect"), taking the whole app down at
// load. A raw stylesheet would have dropped the rule silently, so moving a
// selector into `dwclass!` makes an unsupported one fatal.
//
// These pin the shapes the example webpage actually applies. CI runs both
// Firefox and Chrome, so an engine that rejects any of them fails the build
// rather than the page.

#[wasm_bindgen_test]
async fn pointer_spotlight_selectors_insert() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .dwclass!("relative isolate [transition:transform 400ms cubic-bezier(0.16, 1, 0.3, 1), border-color 300ms ease]")
            .dwclass!("[&::before]:absolute [&::before]:inset-0 [&::before]:[z-index:-1] \
                [&::before]:[border-radius:inherit] [&::before]:opacity-0 \
                [&::before]:[transition:opacity 320ms ease] [&.hot::before]:opacity-100 \
                [&::before]:[background:radial-gradient(22rem circle at var(--sx, 50%) var(--sy, 50%), rgba(213, 182, 95, 0.13), transparent 62%)]")
            .dwclass!("[&::after]:absolute [&::after]:inset-0 [&::after]:[z-index:-1] \
                [&::after]:[border-radius:inherit] [&::after]:[padding:1px] \
                [&::after]:opacity-0 [&::after]:[transition:opacity 320ms ease] [&.hot::after]:opacity-100 \
                [&::after]:[-webkit-mask:linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)] \
                [&::after]:[-webkit-mask-composite:xor] \
                [&::after]:[mask:linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)] \
                [&::after]:[mask-composite:exclude]")
        }),
    );
    wait_frame().await;
}

#[wasm_bindgen_test]
async fn child_and_state_variant_selectors_insert() {
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            // The scroll-reveal cascade: parent-state plus :nth-child stagger.
            .dwclass!("[& > *]:opacity-0 [& > *]:[transform:translateY(26px)] \
                [& > *]:[transition:opacity 650ms cubic-bezier(0.16, 1, 0.3, 1), transform 650ms cubic-bezier(0.16, 1, 0.3, 1)]")
            .dwclass!("[& > *:nth-child(2)]:delay-75 [& > *:nth-child(5)]:[transition-delay:280ms]")
            .dwclass!("[&.reveal-in > *]:opacity-100")
            // Hover the parent, animate the child — the marquee pause.
            .dwclass!("[&:hover > *]:[animation-play-state:paused]")
        }),
    );
    wait_frame().await;
}

#[wasm_bindgen_test]
async fn scrollbar_styling_uses_standard_properties_only() {
    // `[&::-webkit-scrollbar-thumb:hover]:` panics in Firefox: the engine
    // tolerates the unknown pseudo-element on its own but rejects it with a
    // pseudo-class appended. The standard properties cover both engines.
    let tc = TestContainer::new();

    dominator::append_dom(
        &tc.dom_element(),
        html!("div", {
            .attr("id", "probe-scrollbar")
            .dwclass!("[scrollbar-width:thin] [scrollbar-color:#26262C transparent]")
        }),
    );
    wait_frame().await;

    let doc = web_sys::window().unwrap().document().unwrap();
    let el = doc.get_element_by_id("probe-scrollbar").unwrap();
    let style = web_sys::window()
        .unwrap()
        .get_computed_style(&el)
        .unwrap()
        .unwrap();

    assert_eq!(style.get_property_value("scrollbar-width").unwrap(), "thin");
}