-
Notifications
You must be signed in to change notification settings - Fork 585
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: Mitosis does not camelCase HTML attributes in React #526
Comments
Technical information: We already have a list of all HTML elements:
We want to go to the mitosis/packages/core/src/generators/react.ts Lines 164 to 167 in b8d083c
mitosis/packages/core/src/generators/react.ts Lines 184 to 186 in b8d083c
In both of these for-loops, we want to add a check that looks like this: const isHtmlElement = VALID_HTML_TAGS.includes(json.name);
const checkKeyShouldBeCamelCased = (key: string) => {
// ... TO-DO:
}
const camelCaseNativeHTMLAttribute = (attrName: string) => {
// ... TO-DO:
}
if (isHtmlElement && checkKeyShouldBeCamelCased(key)) {
str += ` ${camelCaseNativeHTMLAttribute(key)=${value}`
} else if // the rest of the code |
I will like to take up this issue |
oh I'm not sure if lists are the wistest approach here, HTML spec constantly evolves. The intent behind our JSX is that we match React JSX, so In the early days I tried to do transformations like this (and we explored it with Qwik for a minute), but it wasn't wise what's the concern with just writing React's logic is similarly intentionally simple, there is no trying to replicate DOM tags and attrs in the library, their logic (which the intent behind Mitosis is)
|
looking a little deeper, I feel a bit more reason to copy react the convention. with all of this we need to account for webcomponents too. what I've observed react's full logic is:
but in general we should stick to if Mitosis uses JSX style (camelCase properties) or HTML style. for familiarity, my belief was JSX style is the right way, with one exception for |
I hadn't realized we wanted to match React more closely than the other ones. Thanks for the context
No concern, we could do it one way or the other! My thinking was that since every non-React framework I looked at sticks with the original syntax (Solid supports both
Worth noting that either way, we'll have to manually add every new HTML tag & attribute to our JSX type, so we are constantly maintaining a mirror of the HTML spec regardless. I'd rather stick with the list that's already hardcoded for this issue, and I can go in and replace it with a New Approach@shubham-y , hold off on working on this temporarily, I'll add a comment later with details about the new approach we want to take to address this. |
super interesting that solidjs supports both. I personally like that approach if it's not a logistical headache on the implementation side my main suggesting is just avoiding hardcoded lists of element tags/properties however possible. all frameworks need to do that for JSX types, but I've never seen it be built into the frameworks itself and affecting logic in these types of ways. I think the main reason why is it's ok if a type is wrong or incomplete (you can override it locally or use so types can be seen as a nice to have (that you can override/augment), but framework logic you can't override and needs to support more than just what is in the HTML spec today (including custom elements, custom attributes, etc) in a predictable way |
@shubham-y Sorry for the delay here! Wanted to chime in with an overall idea for the new approach: Instead of making the initial HTML casing our default (e.g.
The function I'm describing will look roughly like what we've described in this conversation: /*
* This function does side-effects on `json`
*/
const mapCamelCasedHtmlAttributes = (json: MitosisNode) => {
const isBuiltInHtmlElement = isFirstLetterLowerCase(json.name) && !json.name.includes('-');
if (!isBuiltInHtmlElement) {
return;
}
for (key in json.bindings) {
const isBuiltInHtmlAttrName = !key.include('-')
if (isBuiltInHtmlAttrName) {
const newKey = key.toLowerCase();
// if json[newKey] already exists: show a warning and don't override
json[newKey] = json[key];
delete json[key];
}
}
// repeat the above loop for json.properties too
} And then you'll want to add this logic to the start of every generator, like here: mitosis/packages/core/src/generators/svelte.ts Lines 311 to 334 in dae644a
A good place would be right after the I think this should work: the snapshot tests will help you (you might have to tweak a snapshot test or add one that shows this transformation). Let me know if you need any additional help! 🙏🏽 |
Scope
Describe the bug
Attributes like
srcset
are camelCased in React (possibly other frameworks? Not sure). However, Mitosis does not account for thatTo Reproduce
A link to a https://mitosis.builder.io/ fiddle containing the bug: link
Expected behavior
scrset
should becomesrcSet
in the React output. Same goes for all HTML attributes that are multi-word: https://reactjs.org/docs/dom-elements.htmlAdditional context
The text was updated successfully, but these errors were encountered: