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

✨ Bento amp-copy #35221

Closed
wants to merge 19 commits into from
Closed

✨ Bento amp-copy #35221

wants to merge 19 commits into from

Conversation

mangesh
Copy link
Contributor

@mangesh mangesh commented Jul 14, 2021

The goal of the PR is to support copying to the clipboard functionality.

Facilitating clipboard copying is useful not only for AMP.dev but also for any site that has code samples.

Features

  • Copy any content to the clipboard
    • Support for copying value of input tags (including textarea)
    • Support for copying text content inside any HTML tag (e.g div, code, pre, p tags)
  • Button Customisation
    • Any HTML tag or plain text can be used inside the amp-copy button

In Progress

  • Styling
  • Test cases

Output/Demo

amp-copy

Fixes #4853

export function Copy({children, sourceId, text, ...rest}) {
const [status, setStaus] = useState(null);
let theme = useMemo(()=>{
console.log('theme');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these console logging lines for debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will get this removed.

const content = document.getElementById(sourceId);
let text;
if(content.value != undefined){
text = content.value.trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume trimming is the desired behavior here so the user doesn't get spaces from the beginning/end of the input?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

@mangesh mangesh marked this pull request as ready for review July 20, 2021 22:06
@amp-owners-bot amp-owners-bot bot requested a review from gmajoulet July 20, 2021 22:06
@amp-owners-bot
Copy link

amp-owners-bot bot commented Jul 20, 2021

Hey @ampproject/wg-caching! These files were changed:

extensions/amp-copy/1.0/test/validator-amp-copy.html
extensions/amp-copy/validator-amp-copy.protoascii

@Gregable Gregable self-requested a review July 20, 2021 22:17
@Gregable
Copy link
Member

As this is a new component, has this been through design review approval yet?

@mangesh
Copy link
Contributor Author

mangesh commented Jul 21, 2021

As this is a new component, has this been through design review approval yet?

It hasn’t been through the design review process yet. Will now follow the process given here and try to have this reviewed during the next meet. In the meantime, please let me know if there’s anything that needs to be updated/added for this component.

@caroqliu caroqliu self-requested a review July 21, 2021 20:36
Copy link
Contributor

@caroqliu caroqliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this contribution! I had a few questions and suggestions, and things are looking on the right track. In the mean time, it would be really helpful to have this go to design review with a concrete API (attributes, events, and actions, if any) so the wider community can discuss.

build-system/compile/bundles.config.extensions.json Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/amp-copy.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
extensions/amp-copy/1.0/base-element.js Show resolved Hide resolved
extensions/amp-copy/1.0/component.js Outdated Show resolved Hide resolved
text = content.textContent.trim();
}
if (copyTextToClipboard(window, text)) {
setStaus(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the first successful copy, subsequent successful copies won't cause an additional render. This is because when status === true, setStatus(true) => noop. Is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. It wasn't intended. We have just added the rendering support for the subsequent successful copies.

Remove unwanted if and else condition
Fix some typos
Optimise the code
It is not valid AMP until the feature is ready for wider use.
Files can be added back later when the AMP is ready
Text passed in text attribute will get copied,
if sorceId attribute is missing
@caroqliu
Copy link
Contributor

It looks like CircleCI wants to see a fresh pull from main in this PR to run tests: https://app.circleci.com/pipelines/github/ampproject/amphtml/13781/workflows/35ce9398-42a7-47ed-9cbc-bdcb8092f96f/jobs/226709

@Gregable
Copy link
Member

It looks like the validator rules have been removed from the PR for now. Once this has been design reviewed, please ping @wg-caching for validation review on whichever PR ultimately includes the validation rules.

@Gregable Gregable added the WG: Caching: Triaged Initial triage from wg-caching complete. Remove label if new input required. label Jul 28, 2021
@mangesh
Copy link
Contributor Author

mangesh commented Jul 29, 2021

As this is a new component, has this been through design review approval yet?

Quick update: I have created I2I issue/doc here for wider discussion. Please let me know if I have missed any details in that issue.

@@ -0,0 +1,15 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the copyright header from all new files now :)

if (!ref.current) {
return;
}
if (isCopyingToClipboardSupported(ref.current.ownerDocument)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would setting isCopySupported be better suited with a callback ref? https://reactjs.org/docs/refs-and-the-dom.html#callback-refs

ref,
() =>
/** @type {!CopyDef.CopyApi} */ ({
copyToClipboard: (selector = null, staticText = null) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be the behavior if both are provided? Should we emit an error message?

[ref, text]
);

const copyText = useCallback((textToCopy) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't copy written to handle both the copyText and copySelector case?

ref={ref}
className={objstr({
[classes.success]: status,
[classes.failed]: status === false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[classes.failed]: status === false,
[classes.failed]: !status,

* @param {{current: ?CopyDef.CopyApi}} ref
* @return {PreactDef.Renderable}
*/
export function CopyWithRef({children, sourceId, text, ...rest}, ref) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it may be worth aliasing this to textProp so that you can then use the variable name text in smaller scope locations instead of staticText etc.

* Remove this comment!
-->

<!--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this copyright header!

@mangesh
Copy link
Contributor Author

mangesh commented Aug 25, 2021

@samouri Thank you for the feedback. I am already working on this. Along with this, I am working on a similar AMP Global action, which is being discussed here.

@stale
Copy link

stale bot commented Sep 21, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale Inactive for one year or more label Sep 21, 2022
@stale stale bot closed this Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Inactive for one year or more WG: Caching: Triaged Initial triage from wg-caching complete. Remove label if new input required. WG: caching
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for copying to clipboard
6 participants