Material-components-web-components: deregisterBodyClickListener not working - possible fix

Created on 28 Oct 2020  路  6Comments  路  Source: material-components/material-components-web-components

Screenshot 2020-10-28 at 09 48 59

activating - deactivating the menu's keeps registrering the click event listener on MDCMenuSurface.

Possible fix:
menu-surface component
component.js

MDCMenuSurface.prototype.initialSyncWithDOM
        this.deregisterBodyClickListener = function () {
//BEFORE: document.body.removeEventListener('click', _this.handleBodyClick);
//AFTER: add the , { capture: true } to the removeEventListener 
//to match the document.body.addEventListener('click', _this.handleBodyClick, { capture: true });
            document.body.removeEventListener('click', _this.handleBodyClick, { capture: true });
        };

FYI - I will post this suggestion on the material-components/material-components-web too

Menu Components Low Bug

All 6 comments

thanks for looking into this - my current "hack" is to do this (in lit-element updated):

this.temp_menu = new MDCMenu(div);
let origHandler = this.temp_menu.menuSurface_.handleBodyClick;
this.temp_menu.menuSurface_.deregisterBodyClickListener = () => {    
    document.body.removeEventListener('click', origHandler, { capture: true });
};

it's ugly - but seems to work.

@dfreedm - just out of curiosity - may I ask why these types of memory leak issues are considered "Severity: Low"? As I see it there are a couple of them in the "framework" and when using any of the components (menu and tab bar seems rather widespread) in a SPA the events are adding up and up and up.... when building a solid SPA where users might work for hours - it seems to slow down and refiring etc....?

I see it's been fixed in MDC, when is the next release with this fix expected? It's not just a memory leak, by the way, it messes up the logic when you subscribe to the 'closed' event of the menu surface--each undelegated handler emits an event resulting in an avalanche of events increasing the more times you open the menu.

@zandaqo You can use the @canary version of MWC to get the latest version, which will have the MDC fix included, until the next release.

The version 0.20.0 fixes this for me, but only when MDCMenuSurface#quick is set to true for some reason.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bennypowers picture bennypowers  路  4Comments

AndreasGalster picture AndreasGalster  路  5Comments

nicolasr75 picture nicolasr75  路  4Comments

ErikHellman picture ErikHellman  路  3Comments

MarcSkovMadsen picture MarcSkovMadsen  路  4Comments