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

fix: context improvements #921

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
36 changes: 36 additions & 0 deletions docs/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,39 @@ export default function ComponentWithContext(props: { content: string }) {
);
}
```

More examples:

```tsx
import { setContext, useState } from '@builder.io/mitosis';

export default function MyComponent(props) {
const [disabled, setDisabled] = useState(false);

setContext('disabled', { disabled });

return <h1>Hello World</h1>;
}
```

```tsx
import { setContext, useState } from '@builder.io/mitosis';

export default function MyComponent(props) {
const [disabled, setDisabled] = useState(false);

setContext('disabled', disabled);

return <h1>Hello World</h1>;
}
```

```tsx
import { setContext, useState } from '@builder.io/mitosis';

export default function MyComponent(props) {
setContext('hello', props.world);

return <h1>Hello World</h1>;
}
```
48 changes: 48 additions & 0 deletions packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,30 @@ exports[`Alpine.js > jsx > Javascript Test > self-referencing component with chi
"
`;

exports[`Alpine.js > jsx > Javascript Test > setContextObject 1`] = `
"<h1 x-data=\\"myComponent()\\">Hello World</h1>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"myComponent\\", () => ({
disabled: false,
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > setContextVar 1`] = `
"<h1 x-data=\\"myComponent()\\">Hello World</h1>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"myComponent\\", () => ({
disabled: false,
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > showWithFor 1`] = `
"<template x-if=\\"conditionA\\">
<template x-for=\\"item in items\\">
Expand Down Expand Up @@ -4125,6 +4149,30 @@ exports[`Alpine.js > jsx > Typescript Test > self-referencing component with chi
"
`;

exports[`Alpine.js > jsx > Typescript Test > setContextObject 1`] = `
"<h1 x-data=\\"myComponent()\\">Hello World</h1>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"myComponent\\", () => ({
disabled: false,
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > setContextVar 1`] = `
"<h1 x-data=\\"myComponent()\\">Hello World</h1>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"myComponent\\", () => ({
disabled: false,
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > showWithFor 1`] = `
"<template x-if=\\"conditionA\\">
<template x-for=\\"item in items\\">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3536,6 +3536,56 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > setContextObject 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > setContextVar 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > showWithFor 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";
Expand Down Expand Up @@ -7362,6 +7412,56 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > setContextObject 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > setContextVar 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > showWithFor 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3609,6 +3609,58 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > setContextObject 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > setContextVar 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > showWithFor 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";
Expand Down Expand Up @@ -7518,6 +7570,58 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > setContextObject 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > setContextVar 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<h1>Hello World</h1>
\`,
})
export class MyComponent {
disabled = false;
}

@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > showWithFor 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { BrowserModule } from \\"@angular/platform-browser\\";
Expand Down
Loading