|
| 1 | +<span class="hljs-keyword">import</span> { |
| 2 | + <span class="hljs-title class_">Combobox</span>, |
| 3 | + <span class="hljs-title class_">ComboboxInput</span>, |
| 4 | + <span class="hljs-title class_">ComboboxPopup</span>, |
| 5 | + <span class="hljs-title class_">ComboboxPopupContainer</span>, |
| 6 | +} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/combobox'</span>; |
| 7 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">Listbox</span>, <span class="hljs-title class_">Option</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/listbox'</span>; |
| 8 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">ToolbarWidget</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/toolbar'</span>; |
| 9 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">Dir</span>, <span class="hljs-title class_">Directionality</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/cdk/bidi'</span>; |
| 10 | +<span class="hljs-keyword">import</span> { |
| 11 | + afterRenderEffect, |
| 12 | + <span class="hljs-title class_">Component</span>, |
| 13 | + <span class="hljs-title class_">Directive</span>, |
| 14 | + <span class="hljs-title class_">ElementRef</span>, |
| 15 | + inject, |
| 16 | + signal, |
| 17 | + viewChild, |
| 18 | +} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>; |
| 19 | + |
| 20 | +<span class="hljs-meta">@Directive</span>({ |
| 21 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'button[toolbar-button]'</span>, |
| 22 | + <span class="hljs-attr">hostDirectives</span>: [{<span class="hljs-attr">directive</span>: <span class="hljs-title class_">ToolbarWidget</span>, <span class="hljs-attr">inputs</span>: [<span class="hljs-string">'value'</span>, <span class="hljs-string">'disabled'</span>]}], |
| 23 | + <span class="hljs-attr">host</span>: { |
| 24 | + <span class="hljs-attr">type</span>: <span class="hljs-string">'button'</span>, |
| 25 | + <span class="hljs-attr">class</span>: <span class="hljs-string">'example-button material-symbols-outlined'</span>, |
| 26 | + <span class="hljs-string">'[aria-label]'</span>: <span class="hljs-string">'widget.value()'</span>, |
| 27 | + }, |
| 28 | +}) |
| 29 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SimpleToolbarButton</span> { |
| 30 | + widget = <span class="hljs-title function_">inject</span>(<span class="hljs-title class_">ToolbarWidget</span>); |
| 31 | +} |
| 32 | + |
| 33 | +<span class="hljs-meta">@Directive</span>({ |
| 34 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'button[toolbar-toggle-button]'</span>, |
| 35 | + <span class="hljs-attr">hostDirectives</span>: [{<span class="hljs-attr">directive</span>: <span class="hljs-title class_">ToolbarWidget</span>, <span class="hljs-attr">inputs</span>: [<span class="hljs-string">'value'</span>]}], |
| 36 | + <span class="hljs-attr">host</span>: { |
| 37 | + <span class="hljs-attr">type</span>: <span class="hljs-string">'button'</span>, |
| 38 | + <span class="hljs-attr">class</span>: <span class="hljs-string">'example-button material-symbols-outlined'</span>, |
| 39 | + <span class="hljs-string">'[aria-pressed]'</span>: <span class="hljs-string">'widget.selected()'</span>, |
| 40 | + <span class="hljs-string">'[aria-label]'</span>: <span class="hljs-string">'widget.value()'</span>, |
| 41 | + }, |
| 42 | +}) |
| 43 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SimpleToolbarToggleButton</span> { |
| 44 | + widget = <span class="hljs-title function_">inject</span>(<span class="hljs-title class_">ToolbarWidget</span>); |
| 45 | +} |
| 46 | + |
| 47 | +<span class="hljs-meta">@Directive</span>({ |
| 48 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'button[toolbar-radio-button]'</span>, |
| 49 | + <span class="hljs-attr">hostDirectives</span>: [{<span class="hljs-attr">directive</span>: <span class="hljs-title class_">ToolbarWidget</span>, <span class="hljs-attr">inputs</span>: [<span class="hljs-string">'value'</span>, <span class="hljs-string">'disabled'</span>]}], |
| 50 | + <span class="hljs-attr">host</span>: { |
| 51 | + <span class="hljs-attr">role</span>: <span class="hljs-string">'radio'</span>, |
| 52 | + <span class="hljs-attr">type</span>: <span class="hljs-string">'button'</span>, |
| 53 | + <span class="hljs-attr">class</span>: <span class="hljs-string">'example-button material-symbols-outlined'</span>, |
| 54 | + <span class="hljs-string">'[aria-checked]'</span>: <span class="hljs-string">'widget.selected()'</span>, |
| 55 | + <span class="hljs-string">'[aria-label]'</span>: <span class="hljs-string">'widget.value()'</span>, |
| 56 | + }, |
| 57 | +}) |
| 58 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SimpleToolbarRadioButton</span> { |
| 59 | + widget = <span class="hljs-title function_">inject</span>(<span class="hljs-title class_">ToolbarWidget</span>); |
| 60 | +} |
| 61 | + |
| 62 | +<span class="hljs-meta">@Component</span>({ |
| 63 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'combobox'</span>, |
| 64 | + <span class="hljs-attr">imports</span>: [ |
| 65 | + <span class="hljs-title class_">Dir</span>, |
| 66 | + <span class="hljs-title class_">Combobox</span>, |
| 67 | + <span class="hljs-title class_">ComboboxInput</span>, |
| 68 | + <span class="hljs-title class_">ComboboxPopup</span>, |
| 69 | + <span class="hljs-title class_">ComboboxPopupContainer</span>, |
| 70 | + <span class="hljs-title class_">Listbox</span>, |
| 71 | + <span class="hljs-title class_">Option</span>, |
| 72 | + <span class="hljs-title class_">ToolbarWidget</span>, |
| 73 | + ], |
| 74 | + <span class="hljs-attr">styleUrl</span>: <span class="hljs-string">'toolbar-common.css'</span>, |
| 75 | + <span class="hljs-attr">host</span>: {<span class="hljs-attr">class</span>: <span class="hljs-string">'example-combobox-container'</span>}, |
| 76 | + <span class="hljs-attr">template</span>: <span class="hljs-string">` |
| 77 | + <div ngCombobox [dir]="dir()" #combobox="ngCombobox" class="example-combobox" [readonly]="true"> |
| 78 | + <div class="example-combobox-input-container"> |
| 79 | + <input |
| 80 | + ngComboboxInput |
| 81 | + ngToolbarWidget |
| 82 | + [(value)]="value" |
| 83 | + class="example-combobox-input" |
| 84 | + aria-label="Select a text style" |
| 85 | + /> |
| 86 | + <span class="material-symbols-outlined example-icon example-arrow-icon" |
| 87 | + >arrow_drop_down</span |
| 88 | + > |
| 89 | + </div> |
| 90 | + |
| 91 | + <div popover="manual" #popover class="example-popover"> |
| 92 | + <ng-template ngComboboxPopupContainer> |
| 93 | + <div ngListbox [value]="[value()]" class="example-listbox"> |
| 94 | + @for (option of options; track option) { |
| 95 | + <div ngOption [value]="option" [label]="option" class="example-option"> |
| 96 | + <span>{{option}}</span> |
| 97 | + <span aria-hidden="true" class="material-symbols-outlined example-option-icon" |
| 98 | + >check</span |
| 99 | + > |
| 100 | + </div> |
| 101 | + } |
| 102 | + </div> |
| 103 | + </ng-template> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + `</span>, |
| 107 | +}) |
| 108 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SimpleCombobox</span> { |
| 109 | + dir = <span class="hljs-title function_">inject</span>(<span class="hljs-title class_">Directionality</span>).<span class="hljs-property">valueSignal</span>; |
| 110 | + popover = viewChild<<span class="hljs-title class_">ElementRef</span>>(<span class="hljs-string">'popover'</span>); |
| 111 | + listbox = viewChild<<span class="hljs-title class_">Listbox</span><<span class="hljs-built_in">any</span>>>(<span class="hljs-title class_">Listbox</span>); |
| 112 | + combobox = viewChild<<span class="hljs-title class_">Combobox</span><<span class="hljs-built_in">any</span>>>(<span class="hljs-title class_">Combobox</span>); |
| 113 | + |
| 114 | + value = <span class="hljs-title function_">signal</span>(<span class="hljs-string">'Normal text'</span>); |
| 115 | + options = [<span class="hljs-string">'Normal text'</span>, <span class="hljs-string">'Title'</span>, <span class="hljs-string">'Subtitle'</span>, <span class="hljs-string">'Heading 1'</span>, <span class="hljs-string">'Heading 2'</span>, <span class="hljs-string">'Heading 3'</span>]; |
| 116 | + |
| 117 | + <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { |
| 118 | + <span class="hljs-title function_">afterRenderEffect</span>(<span class="hljs-function">() =></span> { |
| 119 | + <span class="hljs-keyword">const</span> popover = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">popover</span>()!; |
| 120 | + <span class="hljs-keyword">const</span> combobox = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">combobox</span>()!; |
| 121 | + combobox.<span class="hljs-title function_">expanded</span>() ? <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">showPopover</span>() : popover.<span class="hljs-property">nativeElement</span>.<span class="hljs-title function_">hidePopover</span>(); |
| 122 | + |
| 123 | + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listbox</span>()?.<span class="hljs-title function_">scrollActiveItemIntoView</span>(); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + <span class="hljs-title function_">showPopover</span>(<span class="hljs-params"></span>) { |
| 128 | + <span class="hljs-keyword">const</span> popover = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">popover</span>()!; |
| 129 | + <span class="hljs-keyword">const</span> combobox = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">combobox</span>()!; |
| 130 | + |
| 131 | + <span class="hljs-keyword">const</span> comboboxRect = combobox.<span class="hljs-title function_">inputElement</span>()?.<span class="hljs-title function_">getBoundingClientRect</span>(); |
| 132 | + <span class="hljs-keyword">const</span> popoverEl = popover.<span class="hljs-property">nativeElement</span>; |
| 133 | + |
| 134 | + <span class="hljs-keyword">if</span> (comboboxRect) { |
| 135 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.width}</span>px`</span>; |
| 136 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.bottom + <span class="hljs-number">4</span>}</span>px`</span>; |
| 137 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">left</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.left - <span class="hljs-number">1</span>}</span>px`</span>; |
| 138 | + } |
| 139 | + |
| 140 | + popover.<span class="hljs-property">nativeElement</span>.<span class="hljs-title function_">showPopover</span>(); |
| 141 | + } |
| 142 | +} |
0 commit comments