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

Add support for dynamic <slot> names #4207

Open
nolanlawson opened this issue May 10, 2024 · 3 comments
Open

Add support for dynamic <slot> names #4207

nolanlawson opened this issue May 10, 2024 · 3 comments

Comments

@nolanlawson
Copy link
Contributor

Developers would like to be able to have dynamic <slot> names:

<!-- x-slottable -->
<template>
  <slot name={dynamic}></slot>
</template>
<!-- x-slotter -->
<template>
  <x-slottable>
    <div slot={dynamic}></slot>
  </x-slottable>
</template>

This is currently disallowed by our compiler (Error: LWC1076: Name attribute on slot tag can't be an expression.). But there's nothing in native shadow DOM that inherently disallows this; we could trivially require it there.

In fact, you can already achieve this in native shadow DOM by doing manual DOM manipulation, or cheating by using lwc:spread.

One question is whether we'd want to support this in synthetic shadow DOM (probably not) or light DOM (maybe).

@cardoso
Copy link

cardoso commented May 30, 2024

I tried implementing this and to my surprise it seems to work by simply removing the check:

if (ast.isExpression(nameAttribute.value)) {
    ctx.throwOnNode(ParserDiagnostics.NAME_ON_SLOT_CANNOT_BE_EXPRESSION, nameAttribute);
}

Am I missing something as I suspect? Should I submit a PR?

@nolanlawson
Copy link
Contributor Author

Does it work if you run yarn && yarn test && yarn test:karma && DISABLE_SYNTHETIC=1 yarn test:karma? Curious whether it actually works across both synthetic and native shadow.

@cardoso
Copy link

cardoso commented May 30, 2024

@nolanlawson yes, it only fails the test for the NAME_ON_SLOT_CANNOT_BE_EXPRESSION error, which I believe would be updated / removed.

packages/@lwc/template-compiler/src/tests/fixtures/slots/error-definition-dynamic-name/expected.js

- Expected
+ Received

+ import { registerTemplate } from "lwc";
+ const stc0 = [];
+ function tmpl($api, $cmp, $slotset, $ctx) {
+   const { s: api_slot } = $api;
+   return [
+     api_slot(
+       "",
+       {
+         attrs: {
+           name: $cmp.slotName,
+         },
+         key: 0,
+       },
+       stc0,
+       $slotset
+     ),
+   ];
+   /*LWC compiler vX.X.X*/
+ }
+ export default registerTemplate(tmpl);
+ tmpl.slots = [""];
+ tmpl.stylesheets = [];

I'm guessing we would need a test for the actual feature and perhaps that would fail for non-native dom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants