From 321033ab525d139e6723d5ad77bbae93e30706fe Mon Sep 17 00:00:00 2001 From: Nicolas Merget Date: Thu, 16 Jan 2025 08:14:10 +0100 Subject: [PATCH 1/5] feat: add attribute passing for angular and stencil --- .changeset/good-books-move.md | 61 ++ .../__snapshots__/alpine.test.ts.snap | 60 +- .../__snapshots__/angular.import.test.ts.snap | 346 ++++++- .../__snapshots__/angular.mapper.test.ts.snap | 350 ++++++- .../__snapshots__/angular.state.test.ts.snap | 346 ++++++- .../__snapshots__/angular.styles.test.ts.snap | 318 ++++++- .../__snapshots__/angular.test.ts.snap | 664 +++++++++++++- .../__tests__/__snapshots__/html.test.ts.snap | 48 +- .../__snapshots__/liquid.test.ts.snap | 30 + .../__tests__/__snapshots__/lit.test.ts.snap | 120 ++- .../__snapshots__/marko.test.ts.snap | 73 +- .../__snapshots__/mitosis.test.ts.snap | 242 ++++- .../__snapshots__/parse-jsx.test.ts.snap | 268 ++++++ .../__snapshots__/preact.test.ts.snap | 94 +- .../__tests__/__snapshots__/qwik.test.ts.snap | 94 +- .../__snapshots__/react-native.test.ts.snap | 154 +++- .../react-state-builder.test.ts.snap | 98 +- .../react-state-mobx.test.ts.snap | 106 ++- .../react-state-solid.test.ts.snap | 94 +- .../react-state-valtio.test.ts.snap | 94 +- .../react-state-variables.test.ts.snap | 94 +- .../__snapshots__/react.test.ts.snap | 94 +- .../__tests__/__snapshots__/rsc.test.ts.snap | 94 +- .../__snapshots__/solid.test.ts.snap | 196 +++- .../__snapshots__/stencil.test.ts.snap | 868 +++++++++++++++++- .../__snapshots__/svelte.test.ts.snap | 172 +++- .../__tests__/__snapshots__/taro.test.ts.snap | 98 +- .../vue-composition.test.ts.snap | 64 +- .../__tests__/__snapshots__/vue.test.ts.snap | 72 +- .../__snapshots__/webcomponent.test.ts.snap | 445 ++++++++- packages/core/src/__tests__/angular.test.ts | 15 + .../basic-forwardRef-metadata.raw.tsx | 0 .../data/{ => ref}/basic-forwardRef.raw.tsx | 0 .../{ => ref}/basic-ref-assignment.raw.tsx | 0 ...c-ref-attribute-passing-custom-ref.raw.tsx | 18 + .../ref/basic-ref-attribute-passing.raw.tsx | 13 + .../{ => ref}/basic-ref-usePrevious.raw.tsx | 0 .../data/{ => ref}/basic-ref.raw.tsx | 0 packages/core/src/__tests__/test-generator.ts | 16 +- .../core/src/generators/angular/blocks.ts | 489 ++++++++++ .../src/generators/angular/helpers/hooks.ts | 57 ++ .../angular/{helpers.ts => helpers/index.ts} | 40 +- packages/core/src/generators/angular/index.ts | 517 +---------- .../core/src/generators/stencil/blocks.ts | 44 +- .../core/src/generators/stencil/component.ts | 45 +- .../web-components/attribute-passing.ts | 70 ++ .../jsx/__snapshots__/signals.test.ts.snap | 12 +- packages/core/src/types/metadata.ts | 5 +- packages/core/src/types/transpiler.ts | 7 + 49 files changed, 6540 insertions(+), 665 deletions(-) create mode 100644 .changeset/good-books-move.md rename packages/core/src/__tests__/data/{ => ref}/basic-forwardRef-metadata.raw.tsx (100%) rename packages/core/src/__tests__/data/{ => ref}/basic-forwardRef.raw.tsx (100%) rename packages/core/src/__tests__/data/{ => ref}/basic-ref-assignment.raw.tsx (100%) create mode 100644 packages/core/src/__tests__/data/ref/basic-ref-attribute-passing-custom-ref.raw.tsx create mode 100644 packages/core/src/__tests__/data/ref/basic-ref-attribute-passing.raw.tsx rename packages/core/src/__tests__/data/{ => ref}/basic-ref-usePrevious.raw.tsx (100%) rename packages/core/src/__tests__/data/{ => ref}/basic-ref.raw.tsx (100%) create mode 100644 packages/core/src/generators/angular/blocks.ts create mode 100644 packages/core/src/generators/angular/helpers/hooks.ts rename packages/core/src/generators/angular/{helpers.ts => helpers/index.ts} (69%) create mode 100644 packages/core/src/helpers/web-components/attribute-passing.ts diff --git a/.changeset/good-books-move.md b/.changeset/good-books-move.md new file mode 100644 index 0000000000..f390730459 --- /dev/null +++ b/.changeset/good-books-move.md @@ -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 + + +``` + +**Output** + +```html + + + + +``` + +In general, we want to pass those attributes down to the rendered child, like this: + +```html + + + + +``` + +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. diff --git a/packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap b/packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap index 09c985ebb8..824b41cb7d 100644 --- a/packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap +++ b/packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap @@ -1692,6 +1692,35 @@ exports[`Alpine.js > jsx > Javascript Test > basicOnUpdateReturn 1`] = ` " `; +exports[`Alpine.js > jsx > Javascript Test > basicRefAttributePassing 1`] = ` +"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true}} */ + + + +" +`; + +exports[`Alpine.js > jsx > Javascript Test > basicRefAttributePassingCustomRef 1`] = ` +"/** useMetadata: {\\"attributePassing\\":{\\"enabled\\":true,\\"customRef\\":\\"buttonRef\\"}} +*/ + +
+ +
+ +" +`; + exports[`Alpine.js > jsx > Javascript Test > class + ClassName + css 1`] = ` "
- +
+ +" +`; + +exports[`Svelte > jsx > Javascript Test > basicRefAttributePassingCustomRef 1`] = ` +"/** + useMetadata: + {\\"attributePassing\\":{\\"enabled\\":true,\\"customRef\\":\\"buttonRef\\"}} + */ + + + +
" +`; + exports[`Svelte > jsx > Javascript Test > class + ClassName + css 1`] = ` " + +" +`; + +exports[`Svelte > jsx > Typescript Test > basicRefAttributePassingCustomRef 1`] = ` +"/** + useMetadata: + {\\"attributePassing\\":{\\"enabled\\":true,\\"customRef\\":\\"buttonRef\\"}} + */ + + + +
" +`; + exports[`Svelte > jsx > Typescript Test > class + ClassName + css 1`] = ` " -
{id}{$foo.bar.baz}
+
{id}{foo.value.bar.baz}
- +
-
{id}{foo.value.bar.baz}
+
{id}{$foo.bar.baz}