Tested this in Magento 2.4.4.
Copy this file to your theme:
vendor/magento/module-theme/view/frontend/templates/messages.phtml
into this folder of your theme:
Magento_Theme/templates/messages.phtml
Also, copy the messages.js
from this path to your theme:
vendor/magento/module-theme/view/frontend/web/js/view/messages.js
Into this folder of your theme:
Magento_Theme/web/js/messages.js`
Now adapt the PHTML in your theme like so.
...
<div data-bind="attr: {
class: 'message-' + message.type + ' ' + message.type + ' message',
'data-ui-id': 'message-' + message.type
}">
...
to:
...
<div data-bind="attr: {
class: 'message-' + message.type + ' ' + message.type + ' message',
'data-ui-id': 'message-' + message.type
}, click: $parent.removeAll()">
...
Notice how we've added click: $parent.removeAll()
to the data-bind
attribute. This binds an event on the click on the message.
Now, inside messages.js
, add the removeAll
logic below the prepareMessageForHtml
method, and don't forget about the comma (,
after the function):
removeAll: function() {
customerData.set('messages', {});
}
Now, if you click the message, the message should disappear. This affects all visible messages, and can be styles with jQuery (which is a dependency for this JS file).