Skip to content

Commit

Permalink
Docs: Add documentation for Widget Optimization [ED-16419] (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
rami-elementor authored Dec 16, 2024
1 parent 4ccb3f1 commit bc2a1e9
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/.vuepress/sidebars/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ module.exports = [
'rendering-repeaters',
'rendering-html-attribute',
'rendering-inline-editing',
]
},
'widget-optimization',
{
collapsable: false,
sidebarDepth: -1,
children: [
'widget-inner-wrapper',
'widget-output-caching',
]
},
Expand Down
26 changes: 26 additions & 0 deletions src/controls/complex-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ class Elementor_Test_Widget extends \Elementor\Widget_Base {
return 'https://developers.elementor.com/docs/widgets/';
}

/**
* Whether the widget requires inner wrapper.
*
* Determine whether to optimize the DOM size.
*
* @since 1.0.0
* @access protected
* @return bool Whether to optimize the DOM size.
*/
public function has_widget_inner_wrapper(): bool {
return false;
}

/**
* Whether the element returns dynamic content.
*
* Determine whether to cache the element output or not.
*
* @since 1.0.0
* @access protected
* @return bool Whether to cache the element output.
*/
protected function is_dynamic_content(): bool {
return false;
}

/**
* Register test widget controls.
*
Expand Down
26 changes: 26 additions & 0 deletions src/controls/simple-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ class Elementor_Currency_Widget extends \Elementor\Widget_Base {
return 'https://developers.elementor.com/docs/widgets/';
}

/**
* Whether the widget requires inner wrapper.
*
* Determine whether to optimize the DOM size.
*
* @since 1.0.0
* @access protected
* @return bool Whether to optimize the DOM size.
*/
public function has_widget_inner_wrapper(): bool {
return false;
}

/**
* Whether the element returns dynamic content.
*
* Determine whether to cache the element output or not.
*
* @since 1.0.0
* @access protected
* @return bool Whether to cache the element output.
*/
protected function is_dynamic_content(): bool {
return false;
}

/**
* Register currency widget controls.
*
Expand Down
26 changes: 26 additions & 0 deletions src/widgets/advanced-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ class Elementor_List_Widget extends \Elementor\Widget_Base {
];
}

/**
* Whether the widget requires inner wrapper.
*
* Determine whether to optimize the DOM size.
*
* @since 1.0.0
* @access protected
* @return bool Whether to optimize the DOM size.
*/
public function has_widget_inner_wrapper(): bool {
return false;
}

/**
* Whether the element returns dynamic content.
*
* Determine whether to cache the element output or not.
*
* @since 1.0.0
* @access protected
* @return bool Whether to cache the element output.
*/
protected function is_dynamic_content(): bool {
return false;
}

/**
* Register list widget controls.
*
Expand Down
26 changes: 26 additions & 0 deletions src/widgets/simple-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ class Elementor_oEmbed_Widget extends \Elementor\Widget_Base {
return 'https://developers.elementor.com/docs/widgets/';
}

/**
* Whether the widget requires inner wrapper.
*
* Determine whether to optimize the DOM size.
*
* @since 1.0.0
* @access protected
* @return bool Whether to optimize the DOM size.
*/
public function has_widget_inner_wrapper(): bool {
return false;
}

/**
* Whether the element returns dynamic content.
*
* Determine whether to cache the element output or not.
*
* @since 1.0.0
* @access protected
* @return bool Whether to cache the element output.
*/
protected function is_dynamic_content(): bool {
return false;
}

/**
* Register oEmbed widget controls.
*
Expand Down
94 changes: 94 additions & 0 deletions src/widgets/widget-inner-wrapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Widget DOM Optimization

<Badge type="tip" vertical="top" text="Elementor Core" /> <Badge type="warning" vertical="top" text="Intermediate" />

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.

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.

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.

## Widget Markup

The current, unoptimized widget markup, includes two wrapping elements:

```html
<div class="elementor-widget elementor-widget-{widget-name}">
<div class="elementor-widget-container">
...
</div>
</div>
```

The optimized markup has only one wrapping element:

```html
<div class="elementor-widget elementor-widget-{widget-name}">
...
</div>
```

By default, Elementor uses the unoptimized markup for backwards compatibility.

## Examples

### Optimized Widget DOM

To reduce the DOM size, developers can use the `has_widget_inner_wrapper()` method in the widget class, as shown below:

```php
<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

public function has_widget_inner_wrapper(): bool {
return false;
}

}
```

This implementation instructs Elementor to render the widget with a single `<div>` wrapper.

### Retaining Unoptimized Widget DOM (for BC)

Legacy widgets that rely on the `.elementor-widget-container` class can continue using the unoptimized DOM by setting the method to return `true`:

```php{4-6,16}
<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {
public function has_widget_inner_wrapper(): bool {
return true;
}
protected function register_controls(): void {
$this->add_control(
'color',
[
'label' => esc_html__( 'Color', 'textdomain' ),
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} > .elementor-widget-container h3' => 'color: {{VALUE}};',
],
]
);
}
protected function render(): void {
?>
<h3>
...
</h3>
<?php
}
}
```

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.

## Notes

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.

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.
23 changes: 23 additions & 0 deletions src/widgets/widget-optimization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Widget Optimization

<Badge type="tip" vertical="top" text="Elementor Core" /> <Badge type="warning" vertical="top" text="Intermediate" />

Elementor offers several methods and best practices to optimize widgets and improve performance. The optimizations are not implemented for all widgets in order to maintain backwards compatibility. In addition, each widget has its own characteristics. The widget developer needs to add some extra methods to the widget class to make sure Elementor knows how to process it.

## Performance Optimization Methods

Elementor widgets has two performance optimization methods:

```php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

public function has_widget_inner_wrapper(): bool {}

protected function is_dynamic_content(): bool {}

}
```

* **DOM Optimization** - The `has_widget_inner_wrapper()` method lets you determine whether the widget uses optimized DOM structure or not.

* **Element Output Caching** - The `is_dynamic_content()` method lets you determine whether the widget returns dynamic content, to cache the element HTML output or not.
2 changes: 2 additions & 0 deletions src/widgets/widget-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Elementor_Test_Widget extends \Elementor\Widget_Base {

public function get_style_depends(): array {}

public function has_widget_inner_wrapper(): bool {}

protected function is_dynamic_content(): bool {}

protected function register_controls(): void {}
Expand Down

0 comments on commit bc2a1e9

Please sign in to comment.