diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 4fba383bbb7..9a527bfcd37 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -4233,6 +4233,12 @@ class action_menu implements renderable, templatable { */ public $menutrigger = ''; + /** + * An array of attributes added to the trigger element of the secondary menu. + * @var array + */ + public $triggerattributes = []; + /** * Any extra classes for toggling to the secondary menu. * @var string @@ -4520,6 +4526,22 @@ class action_menu implements renderable, templatable { $this->attributessecondary['data-constraint'] = $ancestorselector; } + /** + * Set the overflow constraint boundary of the dropdown menu. + * @see https://getbootstrap.com/docs/4.6/components/dropdowns/#options The 'boundary' option in the Bootstrap documentation + * + * @param string $boundary Accepts the values of 'viewport', 'window', or 'scrollParent'. + * @throws coding_exception + */ + public function set_boundary(string $boundary) { + if (!in_array($boundary, ['viewport', 'window', 'scrollParent'])) { + throw new coding_exception("HTMLElement reference boundaries are not supported." . + "Accepted boundaries are 'viewport', 'window', or 'scrollParent'.", DEBUG_DEVELOPER); + } + + $this->triggerattributes['data-boundary'] = $boundary; + } + /** * If you call this method the action menu will be displayed but will not be enhanced. * @@ -4617,6 +4639,9 @@ class action_menu implements renderable, templatable { $primary->attributes = array_map(function($key, $value) { return [ 'name' => $key, 'value' => $value ]; }, array_keys($attributesprimary), $attributesprimary); + $primary->triggerattributes = array_map(function($key, $value) { + return [ 'name' => $key, 'value' => $value ]; + }, array_keys($this->triggerattributes), $this->triggerattributes); $actionicon = $this->actionicon; if (!empty($this->menutrigger)) { diff --git a/lib/templates/action_menu_trigger.mustache b/lib/templates/action_menu_trigger.mustache index 367e790dbe8..af6819ca224 100644 --- a/lib/templates/action_menu_trigger.mustache +++ b/lib/templates/action_menu_trigger.mustache @@ -21,8 +21,12 @@ Example context (json): { - "text": "Example link text", + "actiontext": "Example link text", "title": "Example link title", + "triggerrole": "button", + "triggerattributes": [ + {"name": "data-boundary", "value": "window" } + ], "url": "http://example.com/link", "classes": "icon menu-action", "instance": "1", @@ -52,7 +56,7 @@ "component": null, "title": "Dashboard" }, - "classes": "icon menu-action", + "classes": "menu-action", "attributes": [ {"name": "role", "value": "menuitem"}, {"name": "data-title", "value": "mymoodle,admin"} @@ -79,7 +83,21 @@ } }}