-
Notifications
You must be signed in to change notification settings - Fork 151
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
Support bindings on non-AMP elements #895
base: ampstart-2.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = (config, amp) => { | ||
const bindAttrs = Array.from( | ||
new Set(amp.tags.map(tag => tag.bindAttrs.map(a => `${a.name}?: string`)).flat()).values() | ||
); | ||
return `${config.lincenseHeader} | ||
|
||
import * as React from 'react'; | ||
|
||
type AmpBindProps = { | ||
children: React.ReactElement; | ||
${bindAttrs.join(';\n')} | ||
}; | ||
export default AmpBindProps; | ||
`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright 2019 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import {AmpIncludeCustomElement} from '../../src-gen/'; | ||
import AmpBindProps from '../../src-gen/AmpBindProps'; | ||
|
||
/** | ||
* Renders an amp-state element, by either adding local state via `value` | ||
* or remote state via the `src` property. | ||
*/ | ||
const AmpBind: React.FunctionComponent<AmpBindProps> = ({children, ...rest}) => { | ||
const newProps = {}; | ||
Object.keys(rest).forEach(key => { | ||
if (/^bind[A-Z]/g.test(key)) { | ||
const newKey = `data-amp-bind-${key.substring(4).toLowerCase()}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to lower case the whole string or just the first character of the string? I know standard HTML elements won't have hyphenated names, but just felt a little risking assuming nothing else ever will. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The React toString serializer will automatically lowercase attribute names. So I think it's fine to lowercase by default. |
||
newProps[newKey] = rest[key]; | ||
} | ||
}); | ||
return ( | ||
<> | ||
<AmpIncludeCustomElement name='amp-bind' /> | ||
{React.cloneElement(children, newProps)} | ||
</> | ||
); | ||
}; | ||
|
||
export default AmpBind; |
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.
It is a pity extra markup is required find bind attributes, but if there is no alternative supported by Next.js then a wrapper element names sense.
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'm not happy about it either. I see two other options:
<AmpDiv bindText/>
This would have the added benefit of element specific auto completion. But it's probably more confusing than it helps.
bindX
types to the HTML elements and rename the attributes in AMP Optimizer. This would work best, but the downside is that it means we have to move Next.js specific features into AMP Optimizer.