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

sidebar items rely on attribute gridstacknode, it should be data-gridstacknode instead for react+TS compatiblity #2900

Closed
BrannJoly opened this issue Dec 11, 2024 · 7 comments · Fixed by #2904 or #2906
Labels

Comments

@BrannJoly
Copy link

BrannJoly commented Dec 11, 2024

in a React + Typescript environment, to use sidebar items, one would write this:

<div className={`sidebar`}>
 <div className="sidebar-item" gridstacknode='{"w":2, "h":2, "content":"..."}'> // ts error : property 'gridstacknode' does not exist on type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>
         ...
  </div>
</div>

and then, following the docs example

  const myClone = (el: HTMLElement): HTMLElement => {
    const clone = el.cloneNode(true) as HTMLElement;
    const content = el.getAttribute('gridstacknode') || 'Dropped Item';
    clone.innerHTML = content;
    return clone;
  };

Unfortunatley, this code doesnt compile :

Type '{ children: string; className: string; gridstacknode: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'.
Property 'gridstacknode' does not exist on type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'.ts(2322)

The normal fix would be to prefix the unknown attribute by data (ie data-gridstacknode), so I tried that in both the div and the myclone function, but it doesnt work. I dug around, and it turns out 'gridstacknode' is actually hardcoded in gridstack.ts

In order to retain backwards compatiblity, maybe it would make sense to first check 'gridstacknode' and then, if it is not found, to fall back on data-gridstacknode ?

@BrannJoly
Copy link
Author

I fixed the issue in my codebase by doing this, but this should be handled in gridstack IMHO!

declare module 'react' {
  interface HTMLAttributes<T> {
    gridstacknode?: string;
  }
}

@adumesny
Copy link
Member

adumesny commented Dec 28, 2024

@BrannJoly thank you for the find. are you saying using

<div className="sidebar-item" data-gridstack-node='{"w":2, "h":2, "content":"..."}'>

gives no error ? honestly I don't tend to use DOM for attr (all done in json instead) so this didn't cross my mind. easy enough switch...

Also assume you meant

const content = el.getAttribute('gridstacknode')?.content || 'Dropped Item';

@adumesny
Copy link
Member

adumesny commented Dec 28, 2024

and wouldn't that be the same issues with attr gs- we've been using for years (before TS was used) ? yeah I could fix to use data-gridstack-node but I assuming you also need the type definition above as well or is that not needed ?
if so I probably should just have gs-node actually to match the rest...

adumesny added a commit to adumesny/gridstack.js that referenced this issue Dec 28, 2024
* fix gridstack#2900
* use attr `data-gs-widget` instead of `gridstacknode` (supported as well for backward compatibility)
@adumesny adumesny mentioned this issue Dec 28, 2024
3 tasks
@BrannJoly
Copy link
Author

and wouldn't that be the same issues with attr gs- we've been using for years (before TS was used) ? yeah I could fix to use data-gridstack-node but I assuming you also need the type definition above as well or is that not needed ? if so I probably should just have gs-node actually to match the rest...

the type definiton above is a ugly hack (I'm modifying the react lib !) needed to use custom attributes not prefixed by data-

Attributes starting by data- are considered valid, you could add data-foo='bar' to any div and react/ts wouldn't complain about anything

@adumesny
Copy link
Member

adumesny commented Dec 28, 2024

Right, but I don't really want to change all existing attr to data-gs-xyz for gs-x, gs-y, gs-w, etc... html and even angular template are not typed, so this is likely React specific since your templates are in your .tsx files.

So the Q is, can gridstack export some types that would spell out those attr (which is great of TS) instead of you doing it since I know all valid attr, and not use data- since I don't want to break all existing code and would rather spell custom attr instead of blind data-* which would not be checked for typing errors anyway.

@BrannJoly
Copy link
Author

That's a good point. I think exporting the types would work.
I'm not sure it's a good practice to mess with other libs type (eg react), but this would probably solve the issue...

And in the meantime, adding this somewhere in the documentation would probably help :)

Tahnk you for your work on this very useful library!

@adumesny
Copy link
Member

| I think exporting the types would work.

what woulkd this look like on the GS side ? any chance you you also post a React example that uses gs-* attr (like the one above) so I could have something to test against ? the TS React example uses JSON (better way)

adumesny added a commit to adumesny/gridstack.js that referenced this issue Dec 29, 2024
@adumesny adumesny mentioned this issue Dec 29, 2024
3 tasks
adumesny added a commit to adumesny/gridstack.js that referenced this issue Dec 29, 2024
adumesny added a commit to adumesny/gridstack.js that referenced this issue Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants