|
| 1 | +import Component from '@glimmer/component'; |
| 2 | + |
| 3 | +import type { MenuDivider } from '../../helpers/menu-divider.ts'; |
| 4 | +import type { MenuItem } from '../../helpers/menu-item.ts'; |
| 5 | +import { DropdownArrowDown } from '../../icons.gts'; |
| 6 | +import BoxelButton from '../button/index.gts'; |
| 7 | +import BoxelDropdown from '../dropdown/index.gts'; |
| 8 | +import Menu from '../menu/index.gts'; |
| 9 | +import SelectionCheckmark from '../selection-checkmark/index.gts'; |
| 10 | + |
| 11 | +// SelectionMenu: a primary dropdown control for bulk selection. The trigger |
| 12 | +// shows a selection checkmark, the current count, and a caret that flips |
| 13 | +// while the menu is open; the menu body is whatever the caller supplies via |
| 14 | +// `@items`. |
| 15 | +// |
| 16 | +// It is deliberately content-agnostic — actions such as "Select All" / |
| 17 | +// "Deselect All" (and the inert count header) are app concerns the consumer |
| 18 | +// builds and passes in, so the design system owns only the trigger styling |
| 19 | +// and the dropdown shell, not the selection semantics. |
| 20 | +// |
| 21 | +// The caller decides when to render it (typically only once something is |
| 22 | +// selected) and what the items do. |
| 23 | +interface Signature { |
| 24 | + Args: { |
| 25 | + items: Array<MenuItem | MenuDivider>; |
| 26 | + // Accessible name for the trigger; defaults to the count. |
| 27 | + label?: string; |
| 28 | + selectedCount: number; |
| 29 | + }; |
| 30 | + Blocks: {}; |
| 31 | + Element: HTMLButtonElement; |
| 32 | +} |
| 33 | + |
| 34 | +export default class SelectionMenu extends Component<Signature> { |
| 35 | + private get triggerLabel(): string { |
| 36 | + return ( |
| 37 | + this.args.label ?? `Selection menu, ${this.args.selectedCount} selected` |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + <template> |
| 42 | + {{! Wrap so the trigger and ember-basic-dropdown's content-origin count |
| 43 | + as ONE flex item in the parent toolbar. BasicDropdown renders no |
| 44 | + wrapper of its own, so without this the origin becomes a sibling |
| 45 | + flex item when the menu opens — adding a parent gap and shifting the |
| 46 | + trigger sideways. }} |
| 47 | + <div class='selection-menu-root'> |
| 48 | + <BoxelDropdown |
| 49 | + @contentClass='selection-menu-content' |
| 50 | + @matchTriggerWidth={{false}} |
| 51 | + > |
| 52 | + <:trigger as |bindings|> |
| 53 | + {{! The trigger is a standard primary Button so it inherits the |
| 54 | + design system's highlight colors, hover, and focus-ring; the |
| 55 | + class only adds what Button doesn't: layout gap, the readable |
| 56 | + highlight foreground, the open-state deepening, and the caret |
| 57 | + flip. }} |
| 58 | + <BoxelButton |
| 59 | + @kind='primary' |
| 60 | + @rectangular={{true}} |
| 61 | + @class='selection-menu-trigger' |
| 62 | + aria-label={{this.triggerLabel}} |
| 63 | + {{bindings}} |
| 64 | + data-test-selection-dropdown-trigger |
| 65 | + ...attributes |
| 66 | + > |
| 67 | + <SelectionCheckmark class='selection-menu-icon' /> |
| 68 | + <span class='selection-menu-count'>{{@selectedCount}}</span> |
| 69 | + <DropdownArrowDown |
| 70 | + class='selection-menu-caret' |
| 71 | + width='13px' |
| 72 | + height='13px' |
| 73 | + /> |
| 74 | + </BoxelButton> |
| 75 | + </:trigger> |
| 76 | + <:content as |dd|> |
| 77 | + <Menu |
| 78 | + class='selection-menu-list' |
| 79 | + @items={{@items}} |
| 80 | + @closeMenu={{dd.close}} |
| 81 | + /> |
| 82 | + </:content> |
| 83 | + </BoxelDropdown> |
| 84 | + </div> |
| 85 | + <style scoped> |
| 86 | + /* Hold the trigger + basic-dropdown origin as a single flex item so |
| 87 | + opening the menu doesn't shift the trigger (see template note). */ |
| 88 | + .selection-menu-root { |
| 89 | + display: inline-flex; |
| 90 | + } |
| 91 | + /* The trigger is a primary BoxelButton; it supplies the highlight |
| 92 | + fill, hover, and disabled handling. These rules only add what |
| 93 | + Button's defaults don't fit here: a gap between the icon/count/ |
| 94 | + caret, the readable highlight foreground (Button's primary text |
| 95 | + defaults to --boxel-dark), a tighter radius, and compact sizing — |
| 96 | + the base size's wide --boxel-sp-xl padding + 5rem min-width make |
| 97 | + this count trigger far too wide, so collapse both to fit content. */ |
| 98 | + .selection-menu-trigger { |
| 99 | + gap: var(--boxel-sp-xxs); |
| 100 | + --boxel-button-text-color: var(--boxel-highlight-foreground); |
| 101 | + --boxel-button-border-radius: var(--boxel-border-radius-sm); |
| 102 | + --boxel-button-padding: var(--boxel-sp-5xs) var(--boxel-sp-xs); |
| 103 | + --boxel-button-min-width: 0; |
| 104 | + } |
| 105 | + /* Keyboard focus ring just outside the button. (Set explicitly so it |
| 106 | + renders regardless of the global button outline setup.) */ |
| 107 | + .selection-menu-trigger:focus-visible { |
| 108 | + outline: var(--boxel-outline-width) var(--boxel-outline-style) |
| 109 | + var(--boxel-highlight); |
| 110 | + outline-offset: 2px; |
| 111 | + } |
| 112 | + /* Deepen the fill while the menu is open, matching Button's hover. */ |
| 113 | + .selection-menu-trigger[aria-expanded='true'] { |
| 114 | + --boxel-button-color: var(--boxel-highlight-hover); |
| 115 | + } |
| 116 | + .selection-menu-icon { |
| 117 | + width: 0.875rem; |
| 118 | + height: 0.875rem; |
| 119 | + flex-shrink: 0; |
| 120 | + } |
| 121 | + .selection-menu-count { |
| 122 | + line-height: 1; |
| 123 | + white-space: nowrap; |
| 124 | + } |
| 125 | + .selection-menu-caret { |
| 126 | + flex-shrink: 0; |
| 127 | + transition: transform var(--boxel-transition); |
| 128 | + } |
| 129 | + /* Caret flips to point up while the menu is open, matching the standard |
| 130 | + dropdown affordance. */ |
| 131 | + .selection-menu-trigger[aria-expanded='true'] .selection-menu-caret { |
| 132 | + transform: rotate(180deg); |
| 133 | + } |
| 134 | + .selection-menu-list { |
| 135 | + --boxel-menu-item-content-padding: var(--boxel-sp-xs) var(--boxel-sp-sm); |
| 136 | + } |
| 137 | + </style> |
| 138 | + </template> |
| 139 | +} |
0 commit comments