Skip to content

DEV: Make outletArgs available as regular arguments in PluginOutlet #32742

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

Merged
merged 4 commits into from
May 19, 2025
Merged
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
38 changes: 35 additions & 3 deletions app/assets/javascripts/discourse/app/components/plugin-outlet.gjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Component from "@glimmer/component";
import { cached } from "@glimmer/tracking";
import ClassicComponent from "@ember/component";
import { concat } from "@ember/helper";
import { get } from "@ember/object";
import { getOwner } from "@ember/owner";
import { service } from "@ember/service";
import curryComponent from "ember-curry-component";
import { or } from "truth-helpers";
import PluginConnector from "discourse/components/plugin-connector";
import PluginOutlet from "discourse/components/plugin-outlet";
Expand Down Expand Up @@ -124,6 +126,29 @@ export default class PluginOutletComponent extends Component {
);
}

@bind
safeCurryComponent(component, args) {
if (component.prototype instanceof ClassicComponent) {
for (const arg of Object.keys(args)) {
if (component.prototype.hasOwnProperty(arg)) {
deprecated(
`Unable to set @${arg} on connector for ${this.args.name}, because a property on the component class clashes with the argument name. Resolve the clash, or convert to a glimmer component.`,
{
id: "discourse.plugin-outlet-classic-args-clash",
}
);

// Build a clone of `args`, without the offending key, while preserving getters
const descriptors = Object.getOwnPropertyDescriptors(args);
delete descriptors[arg];
args = Object.defineProperties({}, descriptors);
}
}
}

return curryComponent(component, args, getOwner(this));
}

<template>
{{#if (this.connectorsExist hasBlock=(has-block))~}}
{{~#if (has-block)~}}
Expand All @@ -135,9 +160,16 @@ export default class PluginOutletComponent extends Component {

{{~#each (this.getConnectors hasBlock=(has-block)) as |c|~}}
{{~#if c.componentClass~}}
<c.componentClass
@outletArgs={{this.outletArgsWithDeprecations}}
>{{yield}}</c.componentClass>
{{#let
(this.safeCurryComponent
c.componentClass this.outletArgsWithDeprecations
)
as |CurriedComponent|
}}
<CurriedComponent
@outletArgs={{this.outletArgsWithDeprecations}}
>{{yield}}</CurriedComponent>
{{/let}}
{{~else if @defaultGlimmer~}}
<c.templateOnly
@outletArgs={{this.outletArgsWithDeprecations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const CRITICAL_DEPRECATIONS = [
"discourse.mobile-view",
"discourse.mobile-templates",
"discourse.component-template-overrides",
"discourse.plugin-outlet-classic-args-clash",
];

if (DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class ArgsTable extends Component {
window[`arg${globalI}`] = value;
/* eslint-disable no-console */
console.log(
`[plugin outlet debug] \`@outletArgs.${key}\` saved to global \`arg${globalI}\`, and logged below:`
`[plugin outlet debug] \`@${key}\` saved to global \`arg${globalI}\`, and logged below:`
);
console.log(value);
/* eslint-enable no-console */
Expand All @@ -53,7 +53,7 @@ export default class ArgsTable extends Component {

<template>
{{#each this.renderArgs as |arg|}}
<div class="key"><span class="fw">{{arg.key}}</span>:</div>
<div class="key"><span class="fw">@{{arg.key}}</span>:</div>
<div class="value">
<span class="fw">{{arg.value}}</span>
<a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";
import ClassicComponent from "@ember/component";
import templateOnly from "@ember/component/template-only";
import { hash } from "@ember/helper";
import { getOwner } from "@ember/owner";
Expand Down Expand Up @@ -1056,3 +1057,80 @@ module(
});
}
);

module(
"Integration | Component | plugin-outlet | argument currying",
function (hooks) {
setupRenderingTest(hooks);

test("makes arguments available at top level", async function (assert) {
extraConnectorComponent(
"test-name",
<template>
<span class="glimmer-test">{{@arg1}} from glimmer</span>
</template>
);

await render(
<template>
<PluginOutlet
@name="test-name"
@outletArgs={{hash arg1="Hello world"}}
/>
</template>
);
assert.dom(".glimmer-test").hasText("Hello world from glimmer");
});

test("makes arguments available at top level in classic components", async function (assert) {
extraConnectorComponent(
"test-name",
class extends ClassicComponent {
<template>
<span class="classic-test">{{this.arg1}} from classic</span>
</template>
}
);

await render(
<template>
<PluginOutlet
@name="test-name"
@outletArgs={{hash arg1="Hello world"}}
/>
</template>
);
assert.dom(".classic-test").hasText("Hello world from classic");
});

test("guards against name clashes in classic components", async function (assert) {
extraConnectorComponent(
"test-name",
class extends ClassicComponent {
get arg1() {
return "overridden";
}

<template>
<span class="classic-test">{{this.arg1}} from classic</span>
</template>
}
);

await withSilencedDeprecationsAsync(
"discourse.plugin-outlet-classic-args-clash",
async () => {
await render(
<template>
<PluginOutlet
@name="test-name"
@outletArgs={{hash arg1="Hello world"}}
/>
</template>
);
}
);
assert.dom(".classic-test").hasText("overridden from classic");
});
}
);
Loading