|
| 1 | +library; |
| 2 | + |
| 3 | +import 'package:arcane_jaspr/arcane_jaspr.dart'; |
| 4 | +import 'package:arcane_jaspr/core/dom_value.dart'; |
| 5 | +import 'package:jaspr/dom.dart' as dom; |
| 6 | + |
| 7 | +import 'class_names.dart'; |
| 8 | +import 'hui_action_menu_dom.dart'; |
| 9 | + |
| 10 | +final class HuiActionMenuItem { |
| 11 | + const HuiActionMenuItem({ |
| 12 | + required this.label, |
| 13 | + required this.icon, |
| 14 | + required this.onSelect, |
| 15 | + this.hint, |
| 16 | + this.disabled = false, |
| 17 | + this.destructive = false, |
| 18 | + this.separatorBefore = false, |
| 19 | + }); |
| 20 | + |
| 21 | + final String label; |
| 22 | + final Widget icon; |
| 23 | + final void Function() onSelect; |
| 24 | + final String? hint; |
| 25 | + final bool disabled; |
| 26 | + final bool destructive; |
| 27 | + final bool separatorBefore; |
| 28 | +} |
| 29 | + |
| 30 | +final class HuiActionMenuPoint { |
| 31 | + const HuiActionMenuPoint(this.x, this.y); |
| 32 | + |
| 33 | + final double x; |
| 34 | + final double y; |
| 35 | +} |
| 36 | + |
| 37 | +HuiActionMenuPoint huiActionMenuEventPoint(Object? event) { |
| 38 | + final ({double x, double y}) point = huiActionMenuEventCoordinates(event); |
| 39 | + return HuiActionMenuPoint(point.x, point.y); |
| 40 | +} |
| 41 | + |
| 42 | +HuiActionMenuPoint huiActionMenuAnchor(String elementId) { |
| 43 | + final ({double x, double y}) point = huiActionMenuAnchorCoordinates( |
| 44 | + elementId, |
| 45 | + ); |
| 46 | + return HuiActionMenuPoint(point.x, point.y); |
| 47 | +} |
| 48 | + |
| 49 | +void focusHuiActionMenu(String elementId) => |
| 50 | + focusHuiActionMenuElement(elementId); |
| 51 | + |
| 52 | +class HuiActionMenu extends StatelessWidget { |
| 53 | + const HuiActionMenu({ |
| 54 | + required this.id, |
| 55 | + required this.label, |
| 56 | + required this.point, |
| 57 | + required this.items, |
| 58 | + required this.onClose, |
| 59 | + super.key, |
| 60 | + }); |
| 61 | + |
| 62 | + final String id; |
| 63 | + final String label; |
| 64 | + final HuiActionMenuPoint point; |
| 65 | + final List<HuiActionMenuItem> items; |
| 66 | + final void Function() onClose; |
| 67 | + |
| 68 | + @override |
| 69 | + Widget build(BuildContext context) => dom.div( |
| 70 | + classes: 'hui-action-menu-layer', |
| 71 | + <Widget>[ |
| 72 | + dom.div( |
| 73 | + classes: 'hui-action-menu-scrim', |
| 74 | + attributes: const <String, String>{'aria-hidden': 'true'}, |
| 75 | + events: <String, void Function(Object)>{ |
| 76 | + 'pointerdown': (Object _) => onClose(), |
| 77 | + 'contextmenu': (Object event) { |
| 78 | + domPreventDefault(event); |
| 79 | + onClose(); |
| 80 | + }, |
| 81 | + }, |
| 82 | + const <Widget>[], |
| 83 | + ), |
| 84 | + dom.div( |
| 85 | + id: id, |
| 86 | + classes: 'hui-action-menu', |
| 87 | + styles: dom.Styles( |
| 88 | + raw: <String, String>{ |
| 89 | + '--hui-menu-x': '${point.x.round()}px', |
| 90 | + '--hui-menu-y': '${point.y.round()}px', |
| 91 | + '--hui-menu-height': '${(items.length * 42 + 20).clamp(96, 440)}px', |
| 92 | + }, |
| 93 | + ), |
| 94 | + attributes: <String, String>{ |
| 95 | + 'role': 'menu', |
| 96 | + 'aria-label': label, |
| 97 | + 'tabindex': '-1', |
| 98 | + }, |
| 99 | + events: <String, void Function(Object)>{ |
| 100 | + 'keydown': (Object event) { |
| 101 | + final String key = domEventKey(event); |
| 102 | + if (key == 'Escape') { |
| 103 | + domStopPropagation(event); |
| 104 | + onClose(); |
| 105 | + return; |
| 106 | + } |
| 107 | + if (key != 'ArrowDown' && |
| 108 | + key != 'ArrowUp' && |
| 109 | + key != 'Home' && |
| 110 | + key != 'End') { |
| 111 | + return; |
| 112 | + } |
| 113 | + domPreventDefault(event); |
| 114 | + domStopPropagation(event); |
| 115 | + moveHuiActionMenuFocus(id, key); |
| 116 | + }, |
| 117 | + }, |
| 118 | + <Widget>[ |
| 119 | + for (final HuiActionMenuItem item in items) ...<Widget>[ |
| 120 | + if (item.separatorBefore) |
| 121 | + const dom.div( |
| 122 | + classes: 'hui-action-menu-separator', |
| 123 | + attributes: <String, String>{'role': 'separator'}, |
| 124 | + <Widget>[], |
| 125 | + ), |
| 126 | + dom.button( |
| 127 | + classes: classNames(<String?>[ |
| 128 | + 'hui-action-menu-item', |
| 129 | + item.destructive ? 'is-destructive' : null, |
| 130 | + ]), |
| 131 | + attributes: <String, String>{ |
| 132 | + 'type': 'button', |
| 133 | + 'role': 'menuitem', |
| 134 | + if (item.disabled) 'disabled': '', |
| 135 | + if (item.disabled) 'aria-disabled': 'true', |
| 136 | + }, |
| 137 | + events: <String, void Function(Object)>{ |
| 138 | + 'click': (Object _) { |
| 139 | + if (item.disabled) return; |
| 140 | + onClose(); |
| 141 | + item.onSelect(); |
| 142 | + }, |
| 143 | + }, |
| 144 | + <Widget>[ |
| 145 | + dom.span( |
| 146 | + classes: 'hui-action-menu-icon', |
| 147 | + attributes: const <String, String>{'aria-hidden': 'true'}, |
| 148 | + <Widget>[item.icon], |
| 149 | + ), |
| 150 | + dom.span(classes: 'hui-action-menu-label', <Widget>[ |
| 151 | + Text(item.label), |
| 152 | + ]), |
| 153 | + if (item.hint != null) |
| 154 | + dom.span(classes: 'hui-action-menu-hint', <Widget>[ |
| 155 | + Text(item.hint!), |
| 156 | + ]), |
| 157 | + ], |
| 158 | + ), |
| 159 | + ], |
| 160 | + ], |
| 161 | + ), |
| 162 | + ], |
| 163 | + ); |
| 164 | +} |
0 commit comments