dioxus-nox-select 0.13.2

Headless Select / Combobox / Multiselect primitives for Dioxus
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use dioxus::prelude::*;

use crate::context::{SelectContext, init_select_context};
use crate::types::*;

// ── ItemContext (inner) ─────────────────────────────────────────────────────

/// Inner context provided by [`Item`] to child components like [`ItemIndicator`].
#[derive(Clone)]
pub(crate) struct ItemContext {
    pub value: String,
}

// ── GroupContext (inner) ─────────────────────────────────────────────────────

/// Inner context provided by [`Group`] so child items can associate with a group.
#[derive(Clone)]
pub(crate) struct GroupContext {
    pub id: String,
}

// ── Root ────────────────────────────────────────────────────────────────────

/// Context provider for the select compound component.
///
/// Wraps a [`Trigger`] (or [`Input`]) and a [`Content`] popup. Ships **zero
/// visual styles** — all state is expressed through `data-*` attributes.
///
/// ## Variants
///
/// - **Select-only**: Compose `Trigger` + `Value` inside Root (no `Input`).
/// - **Combobox**: Compose `Input` inside Root (enables search/filter).
/// - **Multiselect**: Set `multiple: true`.
///
/// ## Data attributes
///
/// - `data-select-state="open|closed"`
/// - `data-select-disabled="true"` (when disabled)
#[component]
pub fn Root(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Initial value (uncontrolled, single-select).
    #[props(default)]
    default_value: Option<String>,
    /// Controlled single-select value.
    #[props(default)]
    value: Option<Signal<String>>,
    /// Fires when single-select value changes.
    #[props(default)]
    on_value_change: Option<EventHandler<String>>,
    /// Initial values (uncontrolled, multi-select).
    #[props(default)]
    default_values: Option<Vec<String>>,
    /// Controlled multi-select values.
    #[props(default)]
    values: Option<Signal<Vec<String>>>,
    /// Fires when multi-select values change.
    #[props(default)]
    on_values_change: Option<EventHandler<Vec<String>>>,
    /// Enable multi-select mode.
    #[props(default)]
    multiple: bool,
    /// Disable the entire select.
    #[props(default)]
    disabled: bool,
    /// Whether popup starts open.
    #[props(default)]
    default_open: bool,
    /// Controlled open state.
    #[props(default)]
    open: Option<Signal<bool>>,
    /// Fires when open state changes.
    #[props(default)]
    on_open_change: Option<EventHandler<bool>>,
    /// Autocomplete mode (only relevant when `Input` child is present).
    #[props(default)]
    autocomplete: AutoComplete,
    /// Auto-open dropdown when input receives focus (combobox variant).
    #[props(default = true)]
    open_on_focus: bool,
    /// Custom filter function. Overrides built-in nucleo fuzzy matching.
    #[props(default)]
    filter: Option<CustomFilter>,
    children: Element,
) -> Element {
    let ctx = init_select_context(
        default_value,
        value,
        on_value_change,
        default_values,
        values,
        on_values_change,
        multiple,
        disabled,
        default_open,
        open,
        on_open_change,
        autocomplete,
        open_on_focus,
        filter,
    );

    let state = if ctx.is_open() { "open" } else { "closed" };

    rsx! {
        div {
            "data-select-state": state,
            "data-select-disabled": disabled.then_some("true"),
            ..attributes,
            {children}
        }
    }
}

// ── Trigger ─────────────────────────────────────────────────────────────────

/// The button that opens/closes the select popup.
///
/// For the **select-only** variant (no `Input` child). Renders a `<button>`
/// with `role="combobox"` and full keyboard handling per the WAI-ARIA
/// select-only combobox pattern.
///
/// ## Data attributes
///
/// - `data-state="open|closed"`
/// - `data-disabled="true"` (when disabled)
#[component]
pub fn Trigger(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Disable the trigger.
    #[props(default)]
    disabled: bool,
    children: Element,
) -> Element {
    let ctx: SelectContext = use_context();

    let is_open = ctx.is_open();
    let state = if is_open { "open" } else { "closed" };
    let trigger_id = ctx.trigger_id();
    let listbox_id = ctx.listbox_id();
    let active_desc = ctx.active_descendant();
    let is_disabled = disabled || ctx.disabled;

    let onkeydown = move |event: KeyboardEvent| {
        if is_disabled {
            return;
        }
        let mut ctx: SelectContext = consume_context();
        let was_open = ctx.is_open();

        match event.key() {
            Key::Enter => {
                event.prevent_default();
                if was_open {
                    ctx.confirm_highlighted();
                } else {
                    ctx.set_open(true);
                    let current = ctx.current_value();
                    if !current.is_empty() {
                        ctx.highlighted.set(Some(current));
                    } else {
                        ctx.highlight_first();
                    }
                }
            }
            Key::Character(ref c) if c == " " => {
                event.prevent_default();
                if was_open {
                    ctx.confirm_highlighted();
                } else {
                    ctx.set_open(true);
                    let current = ctx.current_value();
                    if !current.is_empty() {
                        ctx.highlighted.set(Some(current));
                    } else {
                        ctx.highlight_first();
                    }
                }
            }
            Key::ArrowDown => {
                event.prevent_default();
                if !was_open {
                    ctx.set_open(true);
                    let current = ctx.current_value();
                    if !current.is_empty() {
                        ctx.highlighted.set(Some(current));
                    }
                    ctx.highlight_next();
                } else {
                    ctx.highlight_next();
                }
            }
            Key::ArrowUp => {
                event.prevent_default();
                if !was_open {
                    ctx.set_open(true);
                    let current = ctx.current_value();
                    if !current.is_empty() {
                        ctx.highlighted.set(Some(current));
                    }
                    ctx.highlight_prev();
                } else {
                    ctx.highlight_prev();
                }
            }
            Key::Home => {
                event.prevent_default();
                if !was_open {
                    ctx.set_open(true);
                }
                ctx.highlight_first();
            }
            Key::End => {
                event.prevent_default();
                if !was_open {
                    ctx.set_open(true);
                }
                ctx.highlight_last();
            }
            Key::Escape => {
                if was_open {
                    event.prevent_default();
                    ctx.set_open(false);
                }
            }
            Key::Tab => {
                // Close on Tab (do NOT prevent default — let focus move)
                if was_open {
                    ctx.confirm_highlighted();
                }
            }
            // Type-ahead for printable characters
            Key::Character(ref c) if c != " " => {
                event.prevent_default();
                if !was_open {
                    ctx.set_open(true);
                }
                ctx.type_ahead(c);
            }
            _ => {}
        }
    };

    let onclick = move |_: MouseEvent| {
        if !is_disabled {
            let mut ctx: SelectContext = consume_context();
            ctx.toggle_open();
            if ctx.is_open() {
                let current = ctx.current_value();
                if !current.is_empty() {
                    ctx.highlighted.set(Some(current));
                }
            }
        }
    };

    // Close on blur (click-outside).
    // Content uses `onmousedown: prevent_default()` which prevents the browser
    // from moving focus away when clicking inside the listbox. So blur only
    // fires when focus genuinely leaves the select — exactly when we want to close.
    let onblur = move |_: FocusEvent| {
        let mut ctx: SelectContext = consume_context();
        ctx.set_open(false);
    };

    rsx! {
        button {
            id: "{trigger_id}",
            role: "combobox",
            r#type: "button",
            aria_expanded: if is_open { "true" } else { "false" },
            aria_haspopup: "listbox",
            aria_controls: "{listbox_id}",
            aria_activedescendant: if !active_desc.is_empty() { "{active_desc}" },
            tabindex: "0",
            "data-state": state,
            "data-disabled": is_disabled.then_some("true"),
            disabled: is_disabled,
            onkeydown,
            onclick,
            onblur,
            ..attributes,
            {children}
        }
    }
}

// ── Value ───────────────────────────────────────────────────────────────────

/// Displays the current selected value text, or a placeholder.
///
/// In single-select mode, looks up the selected item's label.
/// In multi-select mode, consumers should provide their own rendering via
/// children (the component provides context access for selected values).
///
/// ## Data attributes
///
/// - `data-select-placeholder` — present when showing placeholder text
#[component]
pub fn Value(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Placeholder text shown when no value is selected.
    #[props(default)]
    placeholder: Option<String>,
    #[props(default)] children: Element,
) -> Element {
    let ctx: SelectContext = use_context();

    let has_children = children != VNode::empty();

    // Determine display text for single-select mode
    let (display_text, is_placeholder) = if !has_children {
        if ctx.multiple {
            let vals = ctx.current_values();
            if vals.is_empty() {
                (placeholder.clone().unwrap_or_default(), true)
            } else {
                // Show comma-separated labels
                let items = ctx.items.read();
                let labels: Vec<String> = vals
                    .iter()
                    .filter_map(|v| {
                        items
                            .iter()
                            .find(|e| &e.value == v)
                            .map(|e| e.label.clone())
                    })
                    .collect();
                if labels.is_empty() {
                    (vals.join(", "), false)
                } else {
                    (labels.join(", "), false)
                }
            }
        } else {
            let current = ctx.current_value();
            if current.is_empty() {
                (placeholder.clone().unwrap_or_default(), true)
            } else {
                // Look up label from registered items
                let items = ctx.items.read();
                let label = items
                    .iter()
                    .find(|e| e.value == current)
                    .map(|e| e.label.clone())
                    .unwrap_or(current);
                (label, false)
            }
        }
    } else {
        (String::new(), false)
    };

    rsx! {
        span {
            "data-select-placeholder": is_placeholder.then_some("true"),
            ..attributes,
            if has_children {
                {children}
            } else {
                "{display_text}"
            }
        }
    }
}

// ── Input ───────────────────────────────────────────────────────────────────

/// Search input for the combobox variant.
///
/// Its presence inside `Root` switches the component from select-only to
/// combobox mode. Renders an `<input>` with `role="combobox"` and full
/// ARIA attributes.
///
/// ## Data attributes
///
/// - `data-select-input` — always present
#[component]
pub fn Input(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Placeholder text for the input.
    #[props(default)]
    placeholder: Option<String>,
) -> Element {
    let mut ctx: SelectContext = use_context();

    // Mark that we have an input (switches to combobox mode)
    use_hook(|| {
        ctx.mark_has_input();
    });

    let is_open = ctx.is_open();
    let input_id = ctx.input_id();
    let listbox_id = ctx.listbox_id();
    let active_desc = ctx.active_descendant();
    let autocomplete_attr = ctx.autocomplete.as_aria_attr();
    let is_disabled = ctx.disabled;

    let oninput = move |evt: Event<FormData>| {
        let mut ctx: SelectContext = consume_context();
        ctx.search_query.set(evt.value());
        if !ctx.is_open() {
            ctx.set_open(true);
        }
        // Reset highlight to first match
        ctx.highlight_first();
    };

    let onkeydown = move |event: KeyboardEvent| {
        if is_disabled {
            return;
        }
        let mut ctx: SelectContext = consume_context();
        let was_open = ctx.is_open();

        match event.key() {
            Key::ArrowDown => {
                event.prevent_default();
                if event.modifiers().alt() {
                    // Alt+ArrowDown: open without highlighting
                    if !was_open {
                        ctx.set_open(true);
                    }
                } else if !was_open {
                    ctx.set_open(true);
                    ctx.highlight_first();
                } else {
                    ctx.highlight_next();
                }
            }
            Key::ArrowUp => {
                if was_open {
                    event.prevent_default();
                    ctx.highlight_prev();
                }
            }
            Key::Enter => {
                if was_open && ctx.highlighted.read().is_some() {
                    event.prevent_default();
                    ctx.confirm_highlighted();
                }
            }
            Key::Escape => {
                if was_open {
                    event.prevent_default();
                    ctx.set_open(false);
                }
            }
            // Home/End: let the input handle cursor movement (no preventDefault)
            Key::Tab => {
                if was_open {
                    ctx.set_open(false);
                }
            }
            _ => {}
        }
    };

    let onfocus = move |_: FocusEvent| {
        let mut ctx: SelectContext = consume_context();
        if ctx.open_on_focus && !ctx.disabled && !ctx.is_open() {
            ctx.set_open(true);
            ctx.highlight_first();
        }
    };

    let onblur = move |_: FocusEvent| {
        let mut ctx: SelectContext = consume_context();
        ctx.set_open(false);
    };

    rsx! {
        input {
            id: "{input_id}",
            r#type: "text",
            role: "combobox",
            aria_expanded: if is_open { "true" } else { "false" },
            aria_haspopup: "listbox",
            aria_controls: "{listbox_id}",
            aria_activedescendant: if !active_desc.is_empty() { "{active_desc}" },
            aria_autocomplete: "{autocomplete_attr}",
            disabled: is_disabled,
            placeholder: placeholder,
            value: "{ctx.search_query}",
            "data-select-input": "true",
            oninput,
            onkeydown,
            onfocus,
            onblur,
            ..attributes,
        }
    }
}

// ── ClearButton ─────────────────────────────────────────────────────────────

/// Button to clear the search query.
///
/// ## Data attributes
///
/// - `data-select-clear` — always present
#[component]
pub fn ClearButton(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let onclick = move |evt: MouseEvent| {
        evt.prevent_default();
        let mut ctx: SelectContext = consume_context();
        ctx.search_query.set(String::new());
        ctx.focus_combobox();
    };

    rsx! {
        button {
            r#type: "button",
            aria_label: "Clear",
            tabindex: "-1",
            "data-select-clear": "true",
            onclick,
            ..attributes,
            {children}
        }
    }
}

// ── Content ─────────────────────────────────────────────────────────────────

/// The popup containing the listbox of options.
///
/// Only renders when the select is open.
///
/// ## Data attributes
///
/// - `data-select-content` — always present
/// - `data-state="open|closed"`
#[component]
pub fn Content(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Optional accessible label for the listbox.
    #[props(default)]
    aria_label: Option<String>,
    children: Element,
) -> Element {
    let ctx: SelectContext = use_context();

    if !ctx.is_open() {
        return rsx! {};
    }

    let listbox_id = ctx.listbox_id();
    let multi = ctx.multiple;

    rsx! {
        div {
            id: "{listbox_id}",
            role: "listbox",
            aria_label: aria_label,
            aria_multiselectable: if multi { "true" } else { "false" },
            "data-select-content": "true",
            "data-state": "open",
            // Prevent focus leaving the combobox when clicking inside content
            onmousedown: |evt: MouseEvent| { evt.prevent_default(); },
            ..attributes,
            {children}
        }
    }
}

// ── Item ────────────────────────────────────────────────────────────────────

/// A single selectable option in the listbox.
///
/// Registers itself with the context on mount and deregisters on unmount.
/// Only renders if it passes the current filter.
///
/// ## Data attributes
///
/// - `data-state="checked|unchecked"`
/// - `data-highlighted` — present when this item has visual focus
/// - `data-disabled="true"` — when disabled
#[component]
pub fn Item(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Unique value identifying this option.
    value: String,
    /// Searchable text label. Falls back to `value` if not provided.
    #[props(default)]
    label: Option<String>,
    /// Additional keywords for fuzzy matching (space-separated).
    #[props(default)]
    keywords: Option<String>,
    /// Prevent selection and skip in keyboard navigation.
    #[props(default)]
    disabled: bool,
    children: Element,
) -> Element {
    let mut ctx: SelectContext = use_context();
    let display_label = label.clone().unwrap_or_else(|| value.clone());
    let val = value.clone();

    // Get group context if nested inside a Group
    let group_id = try_use_context::<GroupContext>().map(|g| g.id.clone());

    // Register on mount
    use_hook(|| {
        ctx.register_item(ItemEntry {
            value: val.clone(),
            label: display_label.clone(),
            keywords: keywords.clone().unwrap_or_default(),
            disabled,
            group_id: group_id.clone(),
        });
    });

    // Sync disabled prop changes to registered item (use_hook only runs on mount)
    {
        let val_sync = value.clone();
        use_effect(move || {
            let disabled = disabled; // subscribe to prop
            let mut ctx: SelectContext = consume_context();
            let mut items = ctx.items.write();
            if let Some(item) = items.iter_mut().find(|e| e.value == val_sync) {
                item.disabled = disabled;
            }
        });
    }

    let val_drop = value.clone();
    use_drop(move || {
        let mut ctx: SelectContext = consume_context();
        ctx.deregister_item(&val_drop);
    });

    // Check if this item is visible (passes filter)
    let visible = ctx.visible_values.read();
    if !visible.iter().any(|v| v == &value) {
        return rsx! {};
    }

    let is_selected = ctx.is_selected(&value);
    let is_highlighted = ctx.highlighted.read().as_deref() == Some(value.as_str());
    let item_id = ctx.item_id(&value);
    let state = if is_selected { "checked" } else { "unchecked" };

    // Provide inner context for ItemIndicator
    let item_ctx = ItemContext {
        value: value.clone(),
    };
    use_context_provider(|| item_ctx);

    let val_click = value.clone();
    let onmousedown = move |evt: MouseEvent| {
        evt.prevent_default();
        if !disabled {
            let mut ctx: SelectContext = consume_context();
            if ctx.multiple {
                ctx.toggle_value(&val_click);
            } else {
                ctx.select_single(&val_click);
            }
        }
    };

    let val_enter = value.clone();
    let onpointerenter = move |_| {
        let mut ctx: SelectContext = consume_context();
        ctx.highlighted.set(Some(val_enter.clone()));
    };

    rsx! {
        div {
            id: "{item_id}",
            role: "option",
            aria_selected: if is_selected { "true" } else { "false" },
            aria_disabled: disabled.then_some("true"),
            "data-state": state,
            "data-highlighted": is_highlighted.then_some("true"),
            "data-disabled": disabled.then_some("true"),
            onmousedown,
            onpointerenter,
            ..attributes,
            {children}
        }
    }
}

// ── ItemText ────────────────────────────────────────────────────────────────

/// The text content of an option item.
///
/// ## Data attributes
///
/// - `data-select-item-text` — always present
#[component]
pub fn ItemText(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    rsx! {
        span {
            "data-select-item-text": "true",
            ..attributes,
            {children}
        }
    }
}

// ── ItemIndicator ───────────────────────────────────────────────────────────

/// Renders its children only when the parent [`Item`] is selected.
///
/// Requires being nested inside an [`Item`] component.
///
/// ## Data attributes
///
/// - `data-select-item-indicator` — always present
#[component]
pub fn ItemIndicator(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let ctx: SelectContext = use_context();
    let item_ctx: ItemContext = use_context();

    if !ctx.is_selected(&item_ctx.value) {
        return rsx! {};
    }

    rsx! {
        span {
            "data-select-item-indicator": "true",
            ..attributes,
            {children}
        }
    }
}

// ── Group ───────────────────────────────────────────────────────────────────

/// Groups related options with an optional label.
///
/// ## Data attributes
///
/// - `data-select-group` — always present
#[component]
pub fn Group(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    /// Unique group identifier.
    id: String,
    /// Optional heading for the group (used for `aria-labelledby`).
    #[props(default)]
    label: Option<String>,
    children: Element,
) -> Element {
    let mut ctx: SelectContext = use_context();
    let group_id = id.clone();

    // Register on mount
    use_hook(|| {
        ctx.register_group(GroupEntry {
            id: group_id.clone(),
            label: label.clone(),
        });
    });
    let id_drop = id.clone();
    use_drop(move || {
        let mut ctx: SelectContext = consume_context();
        ctx.deregister_group(&id_drop);
    });

    // Provide group context for child items
    let group_ctx = GroupContext { id: id.clone() };
    use_context_provider(|| group_ctx);

    let label_id = if label.is_some() {
        Some(ctx.group_label_id(&id))
    } else {
        None
    };

    rsx! {
        div {
            role: "group",
            aria_labelledby: label_id,
            "data-select-group": "true",
            ..attributes,
            {children}
        }
    }
}

// ── Label ───────────────────────────────────────────────────────────────────

/// Heading label for a [`Group`].
///
/// ## Data attributes
///
/// - `data-select-label` — always present
#[component]
pub fn Label(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let ctx: SelectContext = use_context();
    let group_ctx: GroupContext = use_context();
    let label_id = ctx.group_label_id(&group_ctx.id);

    rsx! {
        div {
            id: "{label_id}",
            "data-select-label": "true",
            ..attributes,
            {children}
        }
    }
}

// ── Separator ───────────────────────────────────────────────────────────────

/// Visual separator between items or groups.
///
/// ## Data attributes
///
/// - `data-select-separator` — always present
#[component]
pub fn Separator(#[props(extends = GlobalAttributes)] attributes: Vec<Attribute>) -> Element {
    rsx! {
        div {
            role: "separator",
            aria_orientation: "horizontal",
            "data-select-separator": "true",
            ..attributes,
        }
    }
}

// ── Empty ───────────────────────────────────────────────────────────────────

/// Rendered when no items match the current filter query.
///
/// ## Data attributes
///
/// - `data-select-empty` — always present
#[component]
pub fn Empty(
    #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
    children: Element,
) -> Element {
    let ctx: SelectContext = use_context();

    if !ctx.visible_values.read().is_empty() {
        return rsx! {};
    }

    // Don't show when there's no query (all items are visible when unfiltered)
    if ctx.search_query.read().is_empty() {
        return rsx! {};
    }

    // Don't show if no items have registered yet — they may still be mounting
    if ctx.items.read().is_empty() {
        return rsx! {};
    }

    rsx! {
        div {
            role: "status",
            "data-select-empty": "true",
            ..attributes,
            {children}
        }
    }
}