Closed naiyerasif closed 1 year ago
As a workaround, I am explicitly importing the components as follows.
import icon from "./_components/icon.ts"
export const title = "Home"
export const renderOrder = 1
export default function () {
return `<h1>${title}</h1>${icon("moon", { slot: "dark" })}`
}
Mmm, ok. Interesting use case.
Components are designed to work with any template engine, so they need to receive all properties in the first argument in an object (this is how JSX works).
And they also needs access to the comp
object in order to create nested components, and other global data like search
, etc.
Both requirements are not compatible with the use of spread {...attribs}
to collect the remaining attributes. I have to think in a way to fix this. Maybe defining these properties as not enumerable. 🤔
Another workaround is using an attr
property to store all attributes:
You component:
export default function ({ name, title, attrs }) {
}
In your template:
const icon = comp.icon({ name: "moon", attrs: {slot: "dark"} })
I'm going to do some tests to see if I can fix it. Thanks.
I've tried to make the inherited properties not enumerable but this introduce some issues in nunjucks (that only detect enumerable properties).
So finally I've decided to make it optional. You can configure your components to inherit the context data (by default) or disable it, so the only data received is the directly passed to the component.
In your component, set the property inheritData
to false:
export const inheritData = false;
export default function ({name, title, ...attrs}) {
// code here
}
This is not released yet, but you can use it by upgrading Lume to the latest development version with deno task lume upgrade --dev
.
Let me know if this works to you. Thanks!
@oscarotero Thanks for taking a stab at this. I tried the dev build and I am still getting the same error. Here's what I changed: https://github.com/naiyerasif/lume-components-issue/commit/bdbb735693d001618d1c1607f2f7fde8fa5e2c98#diff-ca7f5073a339af20e76bf73a56628c71ae873d2fe29dce97e2677a03ad9dc8bc
Components need all properties in the first argument. You have to change the icon to:
export default function ({ name, title, ...attribs }: { name: string, title?: string; [key: string]: unknown }) {
}
(Sorry the example in my previous comment was wrong. I just changed it)
Yup, that worked. Thanks @oscarotero.
Lume 1.15.2 was released, including this change.
Version
1.15.1
Platform
Windows
What steps will reproduce the bug?
Currently, Lume injects the components in the templates so that they are easily accessible by the layouts and templates. For example, I have an
_components/icon.ts
as follows.Lume fails to render the component when I try to use it in a template through
comp
as described by the docs, for example, like this.The issue originates because of the
...attribs
specified in the component as I sometimes expect dynamic HTML attributes to be passed to the component (for example,slot
in this case). Unfortunately, Lume also passes the entireData
object to the component and theattributes
plugin fails to handle it.This also leads to unexpected issues. For example, sometimes, I may pass a
title
attribute to the component. But when I export atitle
in the template, that's also being implicitly passed to the component which is definitely not correct.How often does it reproduce? Is there a required condition?
Reproducing this does not require anything specific. Here's the repository with the reproduction: https://github.com/naiyerasif/lume-components-issue
Launch the application through
deno task serve
and you should encounter the issue described above.What is the expected behavior?
The components should receive only those arguments which are explicitly defined in their contract. The
Data
object should not be passed to them at all.What do you see instead?
I get the following error on console.
Additional information
No response