Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add attribute passing for angular and stencil #1665

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .changeset/good-books-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
'@builder.io/mitosis': patch
---

[angular, stencil]: Add `attributePassing` to enable passing attributes like `data-*`, `aria-*` or `class` to correct child.

There is a wired behaviour for Angular and Stencil (without shadow DOM), where attributes are rendered on parent elements like this:

**Input**

```angular2html
<!-- Angular -->
<my-component class="cool" data-nice="true" aria-label="wow"></my-component>
```

**Output**

```html
<!-- DOM -->
<my-component class="cool" data-nice="true" aria-label="wow">
<button class="my-component">My Component</button>
</my-component>
```

In general, we want to pass those attributes down to the rendered child, like this:

```html
<!-- DOM -->
<my-component>
<button class="my-component cool" data-nice="true" aria-label="wow">My Component</button>
</my-component>
```

We provide 2 ways to enable this attribute passing:

**Metadata**

```tsx
// my-component.lite.tsx
useMetadata({
attributePassing: {
enabled: true,
// customRef: "_myRef";
},
});
```

**Config**

```js
// mitosis.config.cjs
module.exports = {
// ... other settings
attributePassing: {
enabled: true,
// customRef: "_myRef";
},
};
```

If you enable the ``attributePassing`` we add a new `ref` to the root element named `_root` to interact with the DOM element. If you wish to control the name of the ref, because you already have a `useRef` on your root element, you can use the `customRef` property to select it.
58 changes: 58 additions & 0 deletions packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,35 @@ exports[`Alpine.js > jsx > Javascript Test > basicOnUpdateReturn 1`] = `
"
`;

exports[`Alpine.js > jsx > Javascript Test > basicRefAttributePassing 1`] = `
"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true}} */

<button x-data=\\"basicRefAttributePassingComponent()\\" x-ref=\\"buttonRef\\">
Attribute Passing
</button>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"basicRefAttributePassingComponent\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > basicRefAttributePassingCustomRef 1`] = `
"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true,\\"customRef\\":\\"buttonRef\\"}}
*/

<div x-data=\\"basicRefAttributePassingCustomRefComponent()\\">
<button x-ref=\\"buttonRef\\">Attribute Passing</button>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"basicRefAttributePassingCustomRefComponent\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > class + ClassName + css 1`] = `
"<style>
.div {
Expand Down Expand Up @@ -4807,6 +4836,35 @@ exports[`Alpine.js > jsx > Typescript Test > basicOnUpdateReturn 1`] = `
"
`;

exports[`Alpine.js > jsx > Typescript Test > basicRefAttributePassing 1`] = `
"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true}} */

<button x-data=\\"basicRefAttributePassingComponent()\\" x-ref=\\"buttonRef\\">
Attribute Passing
</button>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"basicRefAttributePassingComponent\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > basicRefAttributePassingCustomRef 1`] = `
"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true,\\"customRef\\":\\"buttonRef\\"}}
*/

<div x-data=\\"basicRefAttributePassingCustomRefComponent()\\">
<button x-ref=\\"buttonRef\\">Attribute Passing</button>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"basicRefAttributePassingCustomRefComponent\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > class + ClassName + css 1`] = `
"<style>
.div {
Expand Down
Loading
Loading