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

Bug: can not pass some special custom attribute such as size #20159

Open
mh-alahdadian opened this issue Nov 4, 2020 · 4 comments
Open

Bug: can not pass some special custom attribute such as size #20159

mh-alahdadian opened this issue Nov 4, 2020 · 4 comments
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Discussion

Comments

@mh-alahdadian
Copy link

As we know after react 16 we can use any custom attributes in dom elements
but there is a bug which some special attributes (such as size) are not forwarding to dom element

screenshot

React version: 16.13 & 17.0

Steps To Reproduce

  1. render a dom element with some custom attributes including size
  2. inspect output and you will see your custom attributes but size

Link to code example:
https://codesandbox.io/s/setting-size-as-custom-attr-hdom2?file=/src/App.js

The current behavior

size attribute will not show in output

The expected behavior

size should be in output

@mh-alahdadian mh-alahdadian added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Nov 4, 2020
@ssslambda
Copy link

+1 (React 17.0.1), size is passed to DOM, not component props

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 5, 2020

Thanks for the report.

React 16.13.1 has the same behavior though. It needs to be a number in <select /> and <input /> and therefore React expects this type for every DOM component

  1. <h1 size="1" /> is working
  2. <select size="1" /> is working
  3. <select size="size" /> is not working and issues no warning

I can see two issues here:

  1. React silences errors when setting size
    It might be useful to add a dev warning if the size attribute has an invalid value where the DOM component implements it. For example, setting someInputElement.size = "size" throws a DOMException. React might be able to leverage that.
  2. React does not treat size as an attribute on elements other than <input /> and <select />

Looks like #12694 discussed this regarding custom components.

I could make some implementation arguments about why <h1 size="size" /> should be allowed:

  1. h1.setAttribute('size', 'size') works
  2. h1.size = 'size' works

Though at a higher level it might be confusing if you overload the size attribute by meaning different things depending on the DOM component. Note that React always treats specified HTML attributes the same on every element. <h1 open="whatever" /> ignores "whatever", <h1 span="span" /> and <h1 size="size" /> all do not work because these attributes would have the incorrect type on the elements where they make sense.

Maybe a warning like

The size attribute value of "size" has the wrong type. The size attribute is a well-known HTML attribute. You should be using data-size="size" instead.

@ghost
Copy link

ghost commented Nov 7, 2020

I can work on adding some warning here!

@mh-alahdadian
Copy link
Author

I could make some implementation arguments about why <h1 size="size" /> should be allowed:

  1. h1.setAttribute('size', 'size') works
  2. h1.size = 'size' works

Though at a higher level it might be confusing if you overload the size attribute by meaning different things depending on the DOM component. Note that React always treats specified HTML attributes the same on every element. <h1 open="whatever" /> ignores "whatever", <h1 span="span" /> and <h1 size="size" /> all do not work because these attributes would have the incorrect type on the elements where they make sense.

Maybe a warning like

The size attribute value of "size" has the wrong type. The size attribute is a well-known HTML attribute. You should be using data-size="size" instead.

I think that react should ignore props doesn't know about them
we could use size="size" out of input and select component because they have no meaning in html and react should not prevent using them

and also maybe there was a size attribute with string value in custom html elements and 3rd-party libraries using Web Components which must be worked without problem as mentioned in docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Discussion
Projects
None yet
Development

No branches or pull requests

3 participants