Performance: Modal JavaScript API
This affects interaction responsiveness and visual stability when showing or hiding modal dialogs.
const modalElement = document.getElementById('myModal'); const modal = new bootstrap.Modal(modalElement); modal.show(); // Bootstrap handles focus trap and backdrop modal.hide();
const modal = document.querySelector('#myModal'); modal.style.display = 'block'; // No focus management or event handling modal.style.display = 'none';
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct style manipulation (display block/none) | Minimal DOM nodes, but style changes on existing nodes | 2 reflows per show/hide | High paint cost due to abrupt changes | [X] Bad |
| Bootstrap Modal API usage | Adds backdrop and modal nodes with managed CSS classes | 1 reflow with CSS transitions | Lower paint cost with smooth animations | [OK] Good |
modal.show() is called?const modal = new bootstrap.Modal(document.getElementById('exampleModal'));
modal.show();const modal = bootstrap.Modal(document.getElementById('myModal'));
modal.show();Pass the test and this pattern moves unknown