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

CopyButton enhancements #346

Merged
merged 2 commits into from
May 10, 2024
Merged

CopyButton enhancements #346

merged 2 commits into from
May 10, 2024

Conversation

dimfeld
Copy link
Contributor

@dimfeld dimfeld commented May 10, 2024

  • Allow value to be a function that returns a string
  • "Copied!" message customization
  • Emit a click event when the button is clicked

Copy link

changeset-bot bot commented May 10, 2024

🦋 Changeset detected

Latest commit: 5686823

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte-ux Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented May 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-ux ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 10, 2024 1:05am

@dimfeld dimfeld changed the title Allow CopyButton to take a function that returns a string CopyButton enhancements May 10, 2024
Copy link
Owner

@techniq techniq left a comment

Choose a reason for hiding this comment

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

Nice enhancements. Suggestion to simplify forwarding the on:click event

showMessage = true;
let copyValue = typeof value === 'function' ? value() : value;
navigator.clipboard.writeText(copyValue);
dispatch('click');
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of using dispatch maybe just add another on:click to forward the event. It's a pattern I've used elsewhere (NavItem, ToggleButton)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I go back and forth on that. Ended up doing it this way to be more explicit about the event firing after the clipboard has been copied, instead of relying on the undocumented behavior that the event handlers fire in the order they are listed in the template. (Not that it's likely to change at this point)

Also this is very slightly closer to how it will be in Svelte 5. :) Anyway I can change it if you still would like me to.

Copy link
Owner

Choose a reason for hiding this comment

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

Nope, makes sense to me to have the explicit ordering, especially to align with how things will be going forward with Svelte 5, which I believe becomes...

<script>
  const { onclick } = $props;
</script>

<Button
  onclick={() => {
    // ...
    onclick();
  }}
/>

(or something like that. just wrote that in a github comment without checking too much).

Copy link
Owner

@techniq techniq left a comment

Choose a reason for hiding this comment

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

Thanks @dimfeld

@techniq techniq merged commit 725fdcf into techniq:main May 10, 2024
4 checks passed
@github-actions github-actions bot mentioned this pull request May 9, 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.

2 participants