Skip to content

feat: add copied notice for copy button #877#893

Merged
mbostock merged 8 commits into
observablehq:mainfrom
imhalid:imhalid/copy-notice
Mar 2, 2024
Merged

feat: add copied notice for copy button #877#893
mbostock merged 8 commits into
observablehq:mainfrom
imhalid:imhalid/copy-notice

Conversation

@imhalid
Copy link
Copy Markdown
Contributor

@imhalid imhalid commented Feb 22, 2024

I made the notification for the copy button this way, if there is any problem I can fix it.

Dark Notice Light Notice
dark-notice light-notice

Copy link
Copy Markdown
Contributor

@Fil Fil 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 the suggestion! I have noticed a few details; also you can run tests locally (as you can see, prettier is complaining).

Comment thread src/client/pre.js Outdated
Comment thread src/client/pre.js Outdated
Comment thread src/client/pre.js Outdated
Comment thread src/client/pre.js Outdated
Comment thread src/style/layout.css Outdated
@imhalid
Copy link
Copy Markdown
Contributor Author

imhalid commented Feb 22, 2024

thanks for the suggestion! I have noticed a few details; also you can run tests locally (as you can see, prettier is complaining).

i fixed issues and run locally tests.

Comment thread src/client/pre.js
Comment thread src/client/pre.js Outdated
copyButton.innerHTML = '<button title="Copy code" class="observablehq-pre-copy"><span class="observablehq-data-copied">Copied</span><svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2"><path d="M2 6C2 5.44772 2.44772 5 3 5H10C10.5523 5 11 5.44772 11 6V13C11 13.5523 10.5523 14 10 14H3C2.44772 14 2 13.5523 2 13V6Z M4 2.00004L12 2.00001C13.1046 2 14 2.89544 14 4.00001V12"></path></svg></button>'; // prettier-ignore

enableCopyButtons();
const copiedNotice = document.querySelector(".observablehq-data-copied");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I expect copiedNotice is null here because copyButton hasn’t been inserted into the DOM yet. But I also expect you could fix this by saying:

Suggested change
const copiedNotice = document.querySelector(".observablehq-data-copied");
const copiedNotice = copyButton.querySelector(".observablehq-data-copied");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For some reason I don't understand, it gives error when I use copyButton instead of document.

I think using currentTarget.firstElementChild.animate instead of querySelector might be a better solution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, right, it’s a template element that we haven’t instantiated yet. You’ll need a slightly different fix in the event listener since you can’t capture a reference to the copied notice statically.

Fil
Fil previously requested changes Feb 28, 2024
Copy link
Copy Markdown
Contributor

@Fil Fil left a comment

Choose a reason for hiding this comment

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

This sets the word "Copied" with css, so it does not become part of the page's textContent. So you can ctrl-A ctrl-C the whole page and not see an old "Copied" text in your clipboard. It will also make it easier when time comes to translate in other languages (#865).

Comment thread src/client/pre.js Outdated
Comment thread src/style/layout.css Outdated
@mbostock mbostock dismissed Fil’s stale review March 2, 2024 15:46

resolved

@mbostock mbostock enabled auto-merge (squash) March 2, 2024 15:46
@mbostock mbostock merged commit 7d67523 into observablehq:main Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants