-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
✨ Added support for attribute prefixes in PreactBaseElement #30639
✨ Added support for attribute prefixes in PreactBaseElement #30639
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very close. Just a few more edge cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just two small nits.
if (matchesAttrPrefix(attrib.name, def.attrPrefix)) { | ||
currObj[ | ||
dashToCamelCase( | ||
attrib.name.substring(def.attrPrefix.length, attrib.name.length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we can save a few bytes: attrib.name.slice(def.attrPrefix.length)
] = attrib.value; | ||
} | ||
} | ||
if (Object.keys(currObj).length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This is better done by storing a separate boolean value, doing Object.keys
is expensive.
@@ -671,6 +675,20 @@ function usesShadowDom(Ctor) { | |||
); | |||
} | |||
|
|||
/** | |||
* @param {null|string} attributeName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this can ever be null? Was Closure complaining when it was just string
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this was mainly so Closure doesn't complain. I don't think it can be null either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MutationRecord
, I believe, defines attributeName
as nullable.
FMI, what was the motivating use-case for this feature? |
@samouri It's very common for a Preact component's prop to be a map. E.g. |
@dvoytenko: are there any specific examples that we want to pass through to the Preact components? Is it mainly dataset or are there others? |
@samouri For example, for |
…ct#30639) * Added support for data prefixes. Ex. data-param-* * Added testing of attribute prefixes * Addressed comments. Mutation check handled, tests more fleshed out. * Removed startsWith to be consistent with master * Addressed comments (handled def.attrPrefix === attrib.name) * Addressed nits. * Fixed gulp check-types error
I added support for attribute prefixes in PreactBaseElement.
Usage:
AmpYoutube['props'] = {'params': {attrPrefix: 'data-param-'}};
<amp-youtube data-param-test="hello" />
This passes the object
params = {'test': 'hello'}
to the underlying Preact element.Once this PR is merged, Bento documentation will be updated describing this attribute prefix usage.