|
| 1 | +# Widget DOM Optimization |
| 2 | + |
| 3 | +<Badge type="tip" vertical="top" text="Elementor Core" /> <Badge type="warning" vertical="top" text="Intermediate" /> |
| 4 | + |
| 5 | +Elementor widgets define their own markup in the `render()` method. However, Elementor wraps each widget in two `<div>` elements; the outer `<div class="elementor-widget">` element, and the inner `<div class="elementor-widget-container">` element. These additional wrappers allow Elementor to add additional styles like background, margins, borders, motion effects, etc. |
| 6 | + |
| 7 | +Two wrappers for each widget increases the overall DOM size, reducing page performance. To overcome this, developers can use the `has_widget_inner_wrapper()` method to control the number of wrapper elements the widget has. |
| 8 | + |
| 9 | +By switching to a single wrapper, a widget can reduce the DOM size and optimize its footprint on the page. However, existing widgets that rely on the inner `.elementor-widget-container` wrapping element to style widgets, can maintain backwards compatibility. |
| 10 | + |
| 11 | +## Widget Markup |
| 12 | + |
| 13 | +The current, unoptimized widget markup, includes two wrapping elements: |
| 14 | + |
| 15 | +```html |
| 16 | +<div class="elementor-widget elementor-widget-{widget-name}"> |
| 17 | + <div class="elementor-widget-container"> |
| 18 | + ... |
| 19 | + </div> |
| 20 | +</div> |
| 21 | +``` |
| 22 | + |
| 23 | +The optimized markup has only one wrapping element: |
| 24 | + |
| 25 | +```html |
| 26 | +<div class="elementor-widget elementor-widget-{widget-name}"> |
| 27 | + ... |
| 28 | +</div> |
| 29 | +``` |
| 30 | + |
| 31 | +By default, Elementor uses the unoptimized markup for backwards compatibility. |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +### Optimized Widget DOM |
| 36 | + |
| 37 | +To reduce the DOM size, developers can use the `has_widget_inner_wrapper()` method in the widget class, as shown below: |
| 38 | + |
| 39 | +```php |
| 40 | +<?php |
| 41 | +class Elementor_Test_Widget extends \Elementor\Widget_Base { |
| 42 | + |
| 43 | + public function has_widget_inner_wrapper(): bool { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +This implementation instructs Elementor to render the widget with a single `<div>` wrapper. |
| 51 | + |
| 52 | +### Retaining Unoptimized Widget DOM (for BC) |
| 53 | + |
| 54 | +Legacy widgets that rely on the `.elementor-widget-container` class can continue using the unoptimized DOM by setting the method to return `true`: |
| 55 | + |
| 56 | +```php{4-6,16} |
| 57 | +<?php |
| 58 | +class Elementor_Test_Widget extends \Elementor\Widget_Base { |
| 59 | +
|
| 60 | + public function has_widget_inner_wrapper(): bool { |
| 61 | + return true; |
| 62 | + } |
| 63 | +
|
| 64 | + protected function register_controls(): void { |
| 65 | +
|
| 66 | + $this->add_control( |
| 67 | + 'color', |
| 68 | + [ |
| 69 | + 'label' => esc_html__( 'Color', 'textdomain' ), |
| 70 | + 'type' => \Elementor\Controls_Manager::COLOR, |
| 71 | + 'selectors' => [ |
| 72 | + '{{WRAPPER}} > .elementor-widget-container h3' => 'color: {{VALUE}};', |
| 73 | + ], |
| 74 | + ] |
| 75 | + ); |
| 76 | + } |
| 77 | +
|
| 78 | + protected function render(): void { |
| 79 | + ?> |
| 80 | + <h3> |
| 81 | + ... |
| 82 | + </h3> |
| 83 | + <?php |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +This widget can't use the optimized DOM capability as it uses the inner `.elementor-widget-container` CSS class to style the widget. Therefore, setting `has_widget_inner_wrapper()` to `true` will make sure that Elementor doesn't remove the inner wrapper for this widget. |
| 89 | + |
| 90 | +## Notes |
| 91 | + |
| 92 | +Please note that retaining unoptimized DOM is a temporary solution that allows addon developers to maintain compatibility while updating their code. The ultimate goal is to transition all widgets to use the optimized single-wrapper structure. |
| 93 | + |
| 94 | +Optimized DOM for widget wrappers is not only setting `has_widget_inner_wrapper()` to `false`, it requires removal of `.elementor-widget-container` from all files, including PHP, CSS and JS. |
0 commit comments