Skip to content

Commit

Permalink
NEXT-37708 - update composition api extension syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jleifeld committed Oct 9, 2024
1 parent 5907932 commit 45c9b66
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions guides/plugins/plugins/administration/customizing-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,18 @@ Shopware 6 is introducing a new way to extend components using the Composition A
- In future versions, components will gradually migrate from Options API to Composition API.
- Plugin developers are encouraged to familiarize themselves with the new system, but should continue using the current Component factory extension system for components written with the Options API.
- For components written with the Composition API, the new extension system should be used.
- In the long term, the Composition API extension system will become the standard way to extend components. The Options API extension system will be deprecated and eventually removed when all components are migrated to the Composition API.
- In the long term, the Composition API extension system will become the standard way to override components. The Options API extension system will be deprecated and eventually removed when all components are migrated to the Composition API.

### How it works

The new extension system introduces two main functions:

1. `Shopware.Component.createExtendableSetup`: Used within components to make them extendable. This will mainly be used by the core team to make components extendable.

Check warning on line 243 in guides/plugins/plugins/administration/customizing-components.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/customizing-components.md#L243

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Component` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/customizing-components.md:243:13: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Component`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
2. `Shopware.Component.overrideComponentSetup`: Used by plugins to extend components.
2. `Shopware.Component.overrideComponentSetup`: Used by plugins to override components.

Check warning on line 244 in guides/plugins/plugins/administration/customizing-components.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/customizing-components.md#L244

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Component` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/customizing-components.md:244:13: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Component`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY

### Using overrideComponentSetup

The `overrideComponentSetup` function is a key part of the new Composition API extension system. It allows plugin developers to modify or extend the behavior of existing components without directly altering their source code.
The `overrideComponentSetup` function is a key part of the new Composition API extension system. It allows plugin developers to override or extend the behavior of existing components without directly altering their source code.

### Basic usage

Expand All @@ -260,7 +260,7 @@ Shopware.Component.overrideComponentSetup()('componentName', (previousState, pro

#### Parameters

1. `componentName`: A string identifying the component you want to extend.
1. `componentName`: A string identifying the component you want to override.
2. Callback function: This function receives three arguments:
1. `previousState`: The current state of the component, including all its reactive properties and methods.
2. `props`: The props passed to the component.
Expand Down Expand Up @@ -348,7 +348,7 @@ Shopware.Component.overrideComponentSetup()('sw-customer-list', (previousState,

### Example real world usage

Here is an example of how to create an extendable component and how to extend it:
Here is an example of how to create an extendable component and how to override it:

```javascript
import { defineComponent, reactive } from 'vue';
Expand Down Expand Up @@ -377,32 +377,33 @@ const originalComponent = defineComponent({
default: false,
},
},
setup(props, context) {
const publicApi = Shopware.Component.createExtendableSetup({
props,
context,
name: 'originalComponent',
}, () => {
const count = ref(0);
const message = 'Hello from Shopware!';
const countMessage = computed(() => `The current count is: ${count.value}`);

const increment = () => {
count.value++;
};

return {
setup: (props, context) => Shopware.Component.createExtendableSetup({

Check warning on line 380 in guides/plugins/plugins/administration/customizing-components.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/customizing-components.md#L380

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Component` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/customizing-components.md:380:40: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Component`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
props,
context,
name: 'originalComponent',
}, () => {
const count = ref(0);
const message = 'Hello from Shopware!';
const countMessage = computed(() => `The current count is: ${count.value}`);

const increment = () => {
count.value++;

Check warning on line 390 in guides/plugins/plugins/administration/customizing-components.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/customizing-components.md#L390

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/customizing-components.md:390:18: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
};

const privateExample = ref('This is a private property');

return {
public: {
count,
message,
countMessage,
increment,
};
});

return {
...publicApi,
};
},
},
private: {
privateExample,
}
};
}),
});

// Overriding the component with a plugin
Expand Down Expand Up @@ -435,14 +436,14 @@ In this example, `createExtendableSetup` is used to make the `originalComponent`

- Uses Composition API syntax and reactive primitives of Vue 3 instead of Vue 2 options API.
- Extensions are applied using function composition rather than option merging.
- Provides more granular control over what parts of a component can be extended.
- Provides more granular control over what parts of a component can be overridden.
- Only overrides are possible. Extending a component is not supported anymore. This can be done natively with the Composition API.

### Using TypeScript

To take full advantage of the Composition API extension system, it is recommended to use TypeScript. This will provide better type safety and autocompletion in your IDE and prevent common errors.

For adding type safety to props you need to import the type of the component you want to override and use it in the `overrideComponentSetup` function as a generic type: `<typeof _InternalTestComponent>`. The types for the `previousState` are automatically inferred from the component you are extending by using the correct component name.
For adding type safety to props you need to import the type of the component you want to override and use it in the `overrideComponentSetup` function as a generic type: `<typeof _InternalTestComponent>`. The types for the `previousState` are automatically inferred from the component you are overriding by using the correct component name.

```typescript
import _InternalTestComponent from 'src/the/path/to/the/exported/component';
Expand All @@ -464,6 +465,19 @@ Shopware.Component.overrideComponentSetup<typeof _InternalTestComponent>()('_int
});
```

### Accessing private properties

In some cases, you may need to access private properties of a component. This is not recommended, as it can lead to unexpected behavior and breakages when the component is updated. However, if you need to access private properties for debugging or testing purposes, you can do so using the `_private` property of the `previousState` object where all private properties are stored.

Note: overriding and accessing private properties has no TS support and is not recommended for production use.

```javascript
Shopware.Component.overrideComponentSetup()('sw-customer-list', (previousState, props, context) => {

Check warning on line 475 in guides/plugins/plugins/administration/customizing-components.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/customizing-components.md#L475

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Component` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/customizing-components.md:475:9: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Component`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
// Access the private properties
console.log(previousState._private.thePrivateProperty);
});
```

## More interesting topics

* [Customizing templates](writing-templates)
Expand Down

0 comments on commit 45c9b66

Please sign in to comment.